1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/stl_heap.h>
66 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
67 #include <cstdlib> // for rand
68 #include <debug/debug.h>
70 // See concept_check.h for the __glibcxx_*_requires macros.
72 _GLIBCXX_BEGIN_NAMESPACE(std
)
75 * @brief Find the median of three values.
79 * @return One of @p a, @p b or @p c.
81 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
82 * then the value returned will be @c m.
83 * This is an SGI extension.
84 * @ingroup SGIextensions
86 template<typename _Tp
>
88 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
90 // concept requirements
91 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
108 * @brief Find the median of three values using a predicate for comparison.
112 * @param comp A binary predicate.
113 * @return One of @p a, @p b or @p c.
115 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
116 * and @p comp(m,n) are both true then the value returned will be @c m.
117 * This is an SGI extension.
118 * @ingroup SGIextensions
120 template<typename _Tp
, typename _Compare
>
122 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
124 // concept requirements
125 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
,bool,_Tp
,_Tp
>)
126 if (__comp(__a
, __b
))
127 if (__comp(__b
, __c
))
129 else if (__comp(__a
, __c
))
133 else if (__comp(__a
, __c
))
135 else if (__comp(__b
, __c
))
142 * @brief Apply a function to every element of a sequence.
143 * @param first An input iterator.
144 * @param last An input iterator.
145 * @param f A unary function object.
148 * Applies the function object @p f to each element in the range
149 * @p [first,last). @p f must not modify the order of the sequence.
150 * If @p f has a return value it is ignored.
152 template<typename _InputIterator
, typename _Function
>
154 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
156 // concept requirements
157 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
158 __glibcxx_requires_valid_range(__first
, __last
);
159 for (; __first
!= __last
; ++__first
)
166 * This is an overload used by find() for the Input Iterator case.
169 template<typename _InputIterator
, typename _Tp
>
170 inline _InputIterator
171 __find(_InputIterator __first
, _InputIterator __last
,
172 const _Tp
& __val
, input_iterator_tag
)
174 while (__first
!= __last
&& !(*__first
== __val
))
181 * This is an overload used by find_if() for the Input Iterator case.
184 template<typename _InputIterator
, typename _Predicate
>
185 inline _InputIterator
186 __find_if(_InputIterator __first
, _InputIterator __last
,
187 _Predicate __pred
, input_iterator_tag
)
189 while (__first
!= __last
&& !bool(__pred(*__first
)))
196 * This is an overload used by find() for the RAI case.
199 template<typename _RandomAccessIterator
, typename _Tp
>
200 _RandomAccessIterator
201 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
202 const _Tp
& __val
, random_access_iterator_tag
)
204 typename iterator_traits
<_RandomAccessIterator
>::difference_type
205 __trip_count
= (__last
- __first
) >> 2;
207 for (; __trip_count
> 0; --__trip_count
)
209 if (*__first
== __val
)
213 if (*__first
== __val
)
217 if (*__first
== __val
)
221 if (*__first
== __val
)
226 switch (__last
- __first
)
229 if (*__first
== __val
)
233 if (*__first
== __val
)
237 if (*__first
== __val
)
248 * This is an overload used by find_if() for the RAI case.
251 template<typename _RandomAccessIterator
, typename _Predicate
>
252 _RandomAccessIterator
253 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
254 _Predicate __pred
, random_access_iterator_tag
)
256 typename iterator_traits
<_RandomAccessIterator
>::difference_type
257 __trip_count
= (__last
- __first
) >> 2;
259 for (; __trip_count
> 0; --__trip_count
)
261 if (__pred(*__first
))
265 if (__pred(*__first
))
269 if (__pred(*__first
))
273 if (__pred(*__first
))
278 switch (__last
- __first
)
281 if (__pred(*__first
))
285 if (__pred(*__first
))
289 if (__pred(*__first
))
299 * @brief Find the first occurrence of a value in a sequence.
300 * @param first An input iterator.
301 * @param last An input iterator.
302 * @param val The value to find.
303 * @return The first iterator @c i in the range @p [first,last)
304 * such that @c *i == @p val, or @p last if no such iterator exists.
306 template<typename _InputIterator
, typename _Tp
>
307 inline _InputIterator
308 find(_InputIterator __first
, _InputIterator __last
,
311 // concept requirements
312 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
313 __glibcxx_function_requires(_EqualOpConcept
<
314 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
315 __glibcxx_requires_valid_range(__first
, __last
);
316 return std::__find(__first
, __last
, __val
,
317 std::__iterator_category(__first
));
321 * @brief Find the first element in a sequence for which a predicate is true.
322 * @param first An input iterator.
323 * @param last An input iterator.
324 * @param pred A predicate.
325 * @return The first iterator @c i in the range @p [first,last)
326 * such that @p pred(*i) is true, or @p last if no such iterator exists.
328 template<typename _InputIterator
, typename _Predicate
>
329 inline _InputIterator
330 find_if(_InputIterator __first
, _InputIterator __last
,
333 // concept requirements
334 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
335 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
336 typename iterator_traits
<_InputIterator
>::value_type
>)
337 __glibcxx_requires_valid_range(__first
, __last
);
338 return std::__find_if(__first
, __last
, __pred
,
339 std::__iterator_category(__first
));
343 * @brief Find element from a set in a sequence.
344 * @param first1 Start of range to search.
345 * @param last1 End of range to search.
346 * @param first2 Start of match candidates.
347 * @param last2 End of match candidates.
348 * @return The first iterator @c i in the range
349 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
350 * interator in [first2,last2), or @p last1 if no such iterator exists.
352 * Searches the range @p [first1,last1) for an element that is equal to
353 * some element in the range [first2,last2). If found, returns an iterator
354 * in the range [first1,last1), otherwise returns @p last1.
356 template<typename _InputIterator
, typename _ForwardIterator
>
358 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
359 _ForwardIterator __first2
, _ForwardIterator __last2
)
361 // concept requirements
362 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
363 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
364 __glibcxx_function_requires(_EqualOpConcept
<
365 typename iterator_traits
<_InputIterator
>::value_type
,
366 typename iterator_traits
<_ForwardIterator
>::value_type
>)
367 __glibcxx_requires_valid_range(__first1
, __last1
);
368 __glibcxx_requires_valid_range(__first2
, __last2
);
370 for (; __first1
!= __last1
; ++__first1
)
371 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
372 if (*__first1
== *__iter
)
378 * @brief Find element from a set in a sequence using a predicate.
379 * @param first1 Start of range to search.
380 * @param last1 End of range to search.
381 * @param first2 Start of match candidates.
382 * @param last2 End of match candidates.
383 * @param comp Predicate to use.
384 * @return The first iterator @c i in the range
385 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
386 * interator in [first2,last2), or @p last1 if no such iterator exists.
388 * Searches the range @p [first1,last1) for an element that is equal to
389 * some element in the range [first2,last2). If found, returns an iterator in
390 * the range [first1,last1), otherwise returns @p last1.
392 template<typename _InputIterator
, typename _ForwardIterator
,
393 typename _BinaryPredicate
>
395 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
396 _ForwardIterator __first2
, _ForwardIterator __last2
,
397 _BinaryPredicate __comp
)
399 // concept requirements
400 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
401 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
402 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
403 typename iterator_traits
<_InputIterator
>::value_type
,
404 typename iterator_traits
<_ForwardIterator
>::value_type
>)
405 __glibcxx_requires_valid_range(__first1
, __last1
);
406 __glibcxx_requires_valid_range(__first2
, __last2
);
408 for (; __first1
!= __last1
; ++__first1
)
409 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
410 if (__comp(*__first1
, *__iter
))
416 * @brief Find two adjacent values in a sequence that are equal.
417 * @param first A forward iterator.
418 * @param last A forward iterator.
419 * @return The first iterator @c i such that @c i and @c i+1 are both
420 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
421 * or @p last if no such iterator exists.
423 template<typename _ForwardIterator
>
425 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
427 // concept requirements
428 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
429 __glibcxx_function_requires(_EqualityComparableConcept
<
430 typename iterator_traits
<_ForwardIterator
>::value_type
>)
431 __glibcxx_requires_valid_range(__first
, __last
);
432 if (__first
== __last
)
434 _ForwardIterator __next
= __first
;
435 while(++__next
!= __last
)
437 if (*__first
== *__next
)
445 * @brief Find two adjacent values in a sequence using a predicate.
446 * @param first A forward iterator.
447 * @param last A forward iterator.
448 * @param binary_pred A binary predicate.
449 * @return The first iterator @c i such that @c i and @c i+1 are both
450 * valid iterators in @p [first,last) and such that
451 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
454 template<typename _ForwardIterator
, typename _BinaryPredicate
>
456 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
457 _BinaryPredicate __binary_pred
)
459 // concept requirements
460 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
461 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
462 typename iterator_traits
<_ForwardIterator
>::value_type
,
463 typename iterator_traits
<_ForwardIterator
>::value_type
>)
464 __glibcxx_requires_valid_range(__first
, __last
);
465 if (__first
== __last
)
467 _ForwardIterator __next
= __first
;
468 while(++__next
!= __last
)
470 if (__binary_pred(*__first
, *__next
))
478 * @brief Count the number of copies of a value in a sequence.
479 * @param first An input iterator.
480 * @param last An input iterator.
481 * @param value The value to be counted.
482 * @return The number of iterators @c i in the range @p [first,last)
483 * for which @c *i == @p value
485 template<typename _InputIterator
, typename _Tp
>
486 typename iterator_traits
<_InputIterator
>::difference_type
487 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
489 // concept requirements
490 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
491 __glibcxx_function_requires(_EqualOpConcept
<
492 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
493 __glibcxx_requires_valid_range(__first
, __last
);
494 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
495 for (; __first
!= __last
; ++__first
)
496 if (*__first
== __value
)
502 * @brief Count the elements of a sequence for which a predicate is true.
503 * @param first An input iterator.
504 * @param last An input iterator.
505 * @param pred A predicate.
506 * @return The number of iterators @c i in the range @p [first,last)
507 * for which @p pred(*i) is true.
509 template<typename _InputIterator
, typename _Predicate
>
510 typename iterator_traits
<_InputIterator
>::difference_type
511 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
513 // concept requirements
514 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
515 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
516 typename iterator_traits
<_InputIterator
>::value_type
>)
517 __glibcxx_requires_valid_range(__first
, __last
);
518 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
519 for (; __first
!= __last
; ++__first
)
520 if (__pred(*__first
))
526 * @brief Finds the places in ranges which don't match.
527 * @param first1 An input iterator.
528 * @param last1 An input iterator.
529 * @param first2 An input iterator.
530 * @return A pair of iterators pointing to the first mismatch.
532 * This compares the elements of two ranges using @c == and returns a pair
533 * of iterators. The first iterator points into the first range, the
534 * second iterator points into the second range, and the elements pointed
535 * to by the iterators are not equal.
537 template<typename _InputIterator1
, typename _InputIterator2
>
538 pair
<_InputIterator1
, _InputIterator2
>
539 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
540 _InputIterator2 __first2
)
542 // concept requirements
543 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
544 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
545 __glibcxx_function_requires(_EqualOpConcept
<
546 typename iterator_traits
<_InputIterator1
>::value_type
,
547 typename iterator_traits
<_InputIterator2
>::value_type
>)
548 __glibcxx_requires_valid_range(__first1
, __last1
);
550 while (__first1
!= __last1
&& *__first1
== *__first2
)
555 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
559 * @brief Finds the places in ranges which don't match.
560 * @param first1 An input iterator.
561 * @param last1 An input iterator.
562 * @param first2 An input iterator.
563 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
564 * @return A pair of iterators pointing to the first mismatch.
566 * This compares the elements of two ranges using the binary_pred
567 * parameter, and returns a pair
568 * of iterators. The first iterator points into the first range, the
569 * second iterator points into the second range, and the elements pointed
570 * to by the iterators are not equal.
572 template<typename _InputIterator1
, typename _InputIterator2
,
573 typename _BinaryPredicate
>
574 pair
<_InputIterator1
, _InputIterator2
>
575 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
576 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
578 // concept requirements
579 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
580 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
581 __glibcxx_requires_valid_range(__first1
, __last1
);
583 while (__first1
!= __last1
&& bool(__binary_pred(*__first1
, *__first2
)))
588 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
592 * @brief Search a sequence for a matching sub-sequence.
593 * @param first1 A forward iterator.
594 * @param last1 A forward iterator.
595 * @param first2 A forward iterator.
596 * @param last2 A forward iterator.
597 * @return The first iterator @c i in the range
598 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
599 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
600 * such iterator exists.
602 * Searches the range @p [first1,last1) for a sub-sequence that compares
603 * equal value-by-value with the sequence given by @p [first2,last2) and
604 * returns an iterator to the first element of the sub-sequence, or
605 * @p last1 if the sub-sequence is not found.
607 * Because the sub-sequence must lie completely within the range
608 * @p [first1,last1) it must start at a position less than
609 * @p last1-(last2-first2) where @p last2-first2 is the length of the
611 * This means that the returned iterator @c i will be in the range
612 * @p [first1,last1-(last2-first2))
614 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
616 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
617 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
619 // concept requirements
620 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
621 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
622 __glibcxx_function_requires(_EqualOpConcept
<
623 typename iterator_traits
<_ForwardIterator1
>::value_type
,
624 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
625 __glibcxx_requires_valid_range(__first1
, __last1
);
626 __glibcxx_requires_valid_range(__first2
, __last2
);
627 // Test for empty ranges
628 if (__first1
== __last1
|| __first2
== __last2
)
631 // Test for a pattern of length 1.
632 _ForwardIterator2
__tmp(__first2
);
634 if (__tmp
== __last2
)
635 return std::find(__first1
, __last1
, *__first2
);
638 _ForwardIterator2 __p1
, __p
;
639 __p1
= __first2
; ++__p1
;
640 _ForwardIterator1 __current
= __first1
;
642 while (__first1
!= __last1
)
644 __first1
= std::find(__first1
, __last1
, *__first2
);
645 if (__first1
== __last1
)
649 __current
= __first1
;
650 if (++__current
== __last1
)
653 while (*__current
== *__p
)
655 if (++__p
== __last2
)
657 if (++__current
== __last1
)
666 * @brief Search a sequence for a matching sub-sequence using a predicate.
667 * @param first1 A forward iterator.
668 * @param last1 A forward iterator.
669 * @param first2 A forward iterator.
670 * @param last2 A forward iterator.
671 * @param predicate A binary predicate.
672 * @return The first iterator @c i in the range
673 * @p [first1,last1-(last2-first2)) such that
674 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
675 * @p [0,last2-first2), or @p last1 if no such iterator exists.
677 * Searches the range @p [first1,last1) for a sub-sequence that compares
678 * equal value-by-value with the sequence given by @p [first2,last2),
679 * using @p predicate to determine equality, and returns an iterator
680 * to the first element of the sub-sequence, or @p last1 if no such
683 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
685 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
686 typename _BinaryPredicate
>
688 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
689 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
690 _BinaryPredicate __predicate
)
692 // concept requirements
693 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
694 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
695 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
696 typename iterator_traits
<_ForwardIterator1
>::value_type
,
697 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
698 __glibcxx_requires_valid_range(__first1
, __last1
);
699 __glibcxx_requires_valid_range(__first2
, __last2
);
701 // Test for empty ranges
702 if (__first1
== __last1
|| __first2
== __last2
)
705 // Test for a pattern of length 1.
706 _ForwardIterator2
__tmp(__first2
);
708 if (__tmp
== __last2
)
710 while (__first1
!= __last1
711 && !bool(__predicate(*__first1
, *__first2
)))
717 _ForwardIterator2 __p1
, __p
;
718 __p1
= __first2
; ++__p1
;
719 _ForwardIterator1 __current
= __first1
;
721 while (__first1
!= __last1
)
723 while (__first1
!= __last1
)
725 if (__predicate(*__first1
, *__first2
))
729 while (__first1
!= __last1
&&
730 !bool(__predicate(*__first1
, *__first2
)))
732 if (__first1
== __last1
)
736 __current
= __first1
;
737 if (++__current
== __last1
)
740 while (__predicate(*__current
, *__p
))
742 if (++__p
== __last2
)
744 if (++__current
== __last1
)
754 * This is an uglified
755 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
756 * overloaded for forward iterators.
759 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
761 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
762 _Integer __count
, const _Tp
& __val
,
763 std::forward_iterator_tag
)
765 __first
= std::find(__first
, __last
, __val
);
766 while (__first
!= __last
)
768 typename iterator_traits
<_ForwardIterator
>::difference_type
770 _ForwardIterator __i
= __first
;
772 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
781 __first
= std::find(++__i
, __last
, __val
);
788 * This is an uglified
789 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
790 * overloaded for random access iterators.
793 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
795 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
796 _Integer __count
, const _Tp
& __val
,
797 std::random_access_iterator_tag
)
800 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
803 _DistanceType __tailSize
= __last
- __first
;
804 const _DistanceType __pattSize
= __count
;
806 if (__tailSize
< __pattSize
)
809 const _DistanceType __skipOffset
= __pattSize
- 1;
810 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
811 __tailSize
-= __pattSize
;
813 while (1) // the main loop...
815 // __lookAhead here is always pointing to the last element of next
817 while (!(*__lookAhead
== __val
)) // the skip loop...
819 if (__tailSize
< __pattSize
)
820 return __last
; // Failure
821 __lookAhead
+= __pattSize
;
822 __tailSize
-= __pattSize
;
824 _DistanceType __remainder
= __skipOffset
;
825 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
826 *__backTrack
== __val
; --__backTrack
)
828 if (--__remainder
== 0)
829 return (__lookAhead
- __skipOffset
); // Success
831 if (__remainder
> __tailSize
)
832 return __last
; // Failure
833 __lookAhead
+= __remainder
;
834 __tailSize
-= __remainder
;
839 * @brief Search a sequence for a number of consecutive values.
840 * @param first A forward iterator.
841 * @param last A forward iterator.
842 * @param count The number of consecutive values.
843 * @param val The value to find.
844 * @return The first iterator @c i in the range @p [first,last-count)
845 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
846 * or @p last if no such iterator exists.
848 * Searches the range @p [first,last) for @p count consecutive elements
851 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
853 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
854 _Integer __count
, const _Tp
& __val
)
856 // concept requirements
857 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
858 __glibcxx_function_requires(_EqualOpConcept
<
859 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
860 __glibcxx_requires_valid_range(__first
, __last
);
865 return std::find(__first
, __last
, __val
);
866 return std::__search_n(__first
, __last
, __count
, __val
,
867 std::__iterator_category(__first
));
872 * This is an uglified
873 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
875 * overloaded for forward iterators.
878 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
879 typename _BinaryPredicate
>
881 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
882 _Integer __count
, const _Tp
& __val
,
883 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
885 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
888 while (__first
!= __last
)
890 typename iterator_traits
<_ForwardIterator
>::difference_type
892 _ForwardIterator __i
= __first
;
894 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
904 while (__first
!= __last
905 && !bool(__binary_pred(*__first
, __val
)))
913 * This is an uglified
914 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
916 * overloaded for random access iterators.
919 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
920 typename _BinaryPredicate
>
922 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
923 _Integer __count
, const _Tp
& __val
,
924 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
927 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
930 _DistanceType __tailSize
= __last
- __first
;
931 const _DistanceType __pattSize
= __count
;
933 if (__tailSize
< __pattSize
)
936 const _DistanceType __skipOffset
= __pattSize
- 1;
937 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
938 __tailSize
-= __pattSize
;
940 while (1) // the main loop...
942 // __lookAhead here is always pointing to the last element of next
944 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
946 if (__tailSize
< __pattSize
)
947 return __last
; // Failure
948 __lookAhead
+= __pattSize
;
949 __tailSize
-= __pattSize
;
951 _DistanceType __remainder
= __skipOffset
;
952 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
953 __binary_pred(*__backTrack
, __val
); --__backTrack
)
955 if (--__remainder
== 0)
956 return (__lookAhead
- __skipOffset
); // Success
958 if (__remainder
> __tailSize
)
959 return __last
; // Failure
960 __lookAhead
+= __remainder
;
961 __tailSize
-= __remainder
;
966 * @brief Search a sequence for a number of consecutive values using a
968 * @param first A forward iterator.
969 * @param last A forward iterator.
970 * @param count The number of consecutive values.
971 * @param val The value to find.
972 * @param binary_pred A binary predicate.
973 * @return The first iterator @c i in the range @p [first,last-count)
974 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
975 * range @p [0,count), or @p last if no such iterator exists.
977 * Searches the range @p [first,last) for @p count consecutive elements
978 * for which the predicate returns true.
980 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
981 typename _BinaryPredicate
>
983 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
984 _Integer __count
, const _Tp
& __val
,
985 _BinaryPredicate __binary_pred
)
987 // concept requirements
988 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
989 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
990 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
991 __glibcxx_requires_valid_range(__first
, __last
);
997 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
1001 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
1002 std::__iterator_category(__first
));
1005 // find_end for forward iterators.
1006 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
1008 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
1009 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
1010 forward_iterator_tag
, forward_iterator_tag
)
1012 if (__first2
== __last2
)
1016 _ForwardIterator1 __result
= __last1
;
1019 _ForwardIterator1 __new_result
1020 = std::search(__first1
, __last1
, __first2
, __last2
);
1021 if (__new_result
== __last1
)
1025 __result
= __new_result
;
1026 __first1
= __new_result
;
1033 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
1034 typename _BinaryPredicate
>
1036 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
1037 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
1038 forward_iterator_tag
, forward_iterator_tag
,
1039 _BinaryPredicate __comp
)
1041 if (__first2
== __last2
)
1045 _ForwardIterator1 __result
= __last1
;
1048 _ForwardIterator1 __new_result
1049 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
1050 if (__new_result
== __last1
)
1054 __result
= __new_result
;
1055 __first1
= __new_result
;
1062 // find_end for bidirectional iterators (much faster).
1063 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
1064 _BidirectionalIterator1
1065 __find_end(_BidirectionalIterator1 __first1
,
1066 _BidirectionalIterator1 __last1
,
1067 _BidirectionalIterator2 __first2
,
1068 _BidirectionalIterator2 __last2
,
1069 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
1071 // concept requirements
1072 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1073 _BidirectionalIterator1
>)
1074 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1075 _BidirectionalIterator2
>)
1077 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
1078 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
1080 _RevIterator1
__rlast1(__first1
);
1081 _RevIterator2
__rlast2(__first2
);
1082 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
1083 _RevIterator2(__last2
), __rlast2
);
1085 if (__rresult
== __rlast1
)
1089 _BidirectionalIterator1 __result
= __rresult
.base();
1090 std::advance(__result
, -std::distance(__first2
, __last2
));
1095 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
1096 typename _BinaryPredicate
>
1097 _BidirectionalIterator1
1098 __find_end(_BidirectionalIterator1 __first1
,
1099 _BidirectionalIterator1 __last1
,
1100 _BidirectionalIterator2 __first2
,
1101 _BidirectionalIterator2 __last2
,
1102 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
1103 _BinaryPredicate __comp
)
1105 // concept requirements
1106 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1107 _BidirectionalIterator1
>)
1108 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1109 _BidirectionalIterator2
>)
1111 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
1112 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
1114 _RevIterator1
__rlast1(__first1
);
1115 _RevIterator2
__rlast2(__first2
);
1116 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
1117 _RevIterator2(__last2
), __rlast2
,
1120 if (__rresult
== __rlast1
)
1124 _BidirectionalIterator1 __result
= __rresult
.base();
1125 std::advance(__result
, -std::distance(__first2
, __last2
));
1131 * @brief Find last matching subsequence in a sequence.
1132 * @param first1 Start of range to search.
1133 * @param last1 End of range to search.
1134 * @param first2 Start of sequence to match.
1135 * @param last2 End of sequence to match.
1136 * @return The last iterator @c i in the range
1137 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
1138 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
1139 * such iterator exists.
1141 * Searches the range @p [first1,last1) for a sub-sequence that compares
1142 * equal value-by-value with the sequence given by @p [first2,last2) and
1143 * returns an iterator to the first element of the sub-sequence, or
1144 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
1145 * last such subsequence contained in [first,last1).
1147 * Because the sub-sequence must lie completely within the range
1148 * @p [first1,last1) it must start at a position less than
1149 * @p last1-(last2-first2) where @p last2-first2 is the length of the
1151 * This means that the returned iterator @c i will be in the range
1152 * @p [first1,last1-(last2-first2))
1154 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
1155 inline _ForwardIterator1
1156 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
1157 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
1159 // concept requirements
1160 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
1161 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
1162 __glibcxx_function_requires(_EqualOpConcept
<
1163 typename iterator_traits
<_ForwardIterator1
>::value_type
,
1164 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
1165 __glibcxx_requires_valid_range(__first1
, __last1
);
1166 __glibcxx_requires_valid_range(__first2
, __last2
);
1168 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
1169 std::__iterator_category(__first1
),
1170 std::__iterator_category(__first2
));
1174 * @brief Find last matching subsequence in a sequence using a predicate.
1175 * @param first1 Start of range to search.
1176 * @param last1 End of range to search.
1177 * @param first2 Start of sequence to match.
1178 * @param last2 End of sequence to match.
1179 * @param comp The predicate to use.
1180 * @return The last iterator @c i in the range
1181 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
1182 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
1183 * @p last1 if no such iterator exists.
1185 * Searches the range @p [first1,last1) for a sub-sequence that compares
1186 * equal value-by-value with the sequence given by @p [first2,last2) using
1187 * comp as a predicate and returns an iterator to the first element of the
1188 * sub-sequence, or @p last1 if the sub-sequence is not found. The
1189 * sub-sequence will be the last such subsequence contained in
1192 * Because the sub-sequence must lie completely within the range
1193 * @p [first1,last1) it must start at a position less than
1194 * @p last1-(last2-first2) where @p last2-first2 is the length of the
1196 * This means that the returned iterator @c i will be in the range
1197 * @p [first1,last1-(last2-first2))
1199 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
1200 typename _BinaryPredicate
>
1201 inline _ForwardIterator1
1202 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
1203 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
1204 _BinaryPredicate __comp
)
1206 // concept requirements
1207 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
1208 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
1209 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1210 typename iterator_traits
<_ForwardIterator1
>::value_type
,
1211 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
1212 __glibcxx_requires_valid_range(__first1
, __last1
);
1213 __glibcxx_requires_valid_range(__first2
, __last2
);
1215 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
1216 std::__iterator_category(__first1
),
1217 std::__iterator_category(__first2
),
1222 * @brief Perform an operation on a sequence.
1223 * @param first An input iterator.
1224 * @param last An input iterator.
1225 * @param result An output iterator.
1226 * @param unary_op A unary operator.
1227 * @return An output iterator equal to @p result+(last-first).
1229 * Applies the operator to each element in the input range and assigns
1230 * the results to successive elements of the output sequence.
1231 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
1232 * range @p [0,last-first).
1234 * @p unary_op must not alter its argument.
1236 template<typename _InputIterator
, typename _OutputIterator
,
1237 typename _UnaryOperation
>
1239 transform(_InputIterator __first
, _InputIterator __last
,
1240 _OutputIterator __result
, _UnaryOperation __unary_op
)
1242 // concept requirements
1243 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1244 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1245 // "the type returned by a _UnaryOperation"
1246 __typeof__(__unary_op(*__first
))>)
1247 __glibcxx_requires_valid_range(__first
, __last
);
1249 for (; __first
!= __last
; ++__first
, ++__result
)
1250 *__result
= __unary_op(*__first
);
1255 * @brief Perform an operation on corresponding elements of two sequences.
1256 * @param first1 An input iterator.
1257 * @param last1 An input iterator.
1258 * @param first2 An input iterator.
1259 * @param result An output iterator.
1260 * @param binary_op A binary operator.
1261 * @return An output iterator equal to @p result+(last-first).
1263 * Applies the operator to the corresponding elements in the two
1264 * input ranges and assigns the results to successive elements of the
1266 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
1267 * @c N in the range @p [0,last1-first1).
1269 * @p binary_op must not alter either of its arguments.
1271 template<typename _InputIterator1
, typename _InputIterator2
,
1272 typename _OutputIterator
, typename _BinaryOperation
>
1274 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
1275 _InputIterator2 __first2
, _OutputIterator __result
,
1276 _BinaryOperation __binary_op
)
1278 // concept requirements
1279 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1280 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1281 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1282 // "the type returned by a _BinaryOperation"
1283 __typeof__(__binary_op(*__first1
,*__first2
))>)
1284 __glibcxx_requires_valid_range(__first1
, __last1
);
1286 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
1287 *__result
= __binary_op(*__first1
, *__first2
);
1292 * @brief Replace each occurrence of one value in a sequence with another
1294 * @param first A forward iterator.
1295 * @param last A forward iterator.
1296 * @param old_value The value to be replaced.
1297 * @param new_value The replacement value.
1298 * @return replace() returns no value.
1300 * For each iterator @c i in the range @p [first,last) if @c *i ==
1301 * @p old_value then the assignment @c *i = @p new_value is performed.
1303 template<typename _ForwardIterator
, typename _Tp
>
1305 replace(_ForwardIterator __first
, _ForwardIterator __last
,
1306 const _Tp
& __old_value
, const _Tp
& __new_value
)
1308 // concept requirements
1309 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1311 __glibcxx_function_requires(_EqualOpConcept
<
1312 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1313 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
1314 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1315 __glibcxx_requires_valid_range(__first
, __last
);
1317 for (; __first
!= __last
; ++__first
)
1318 if (*__first
== __old_value
)
1319 *__first
= __new_value
;
1323 * @brief Replace each value in a sequence for which a predicate returns
1324 * true with another value.
1325 * @param first A forward iterator.
1326 * @param last A forward iterator.
1327 * @param pred A predicate.
1328 * @param new_value The replacement value.
1329 * @return replace_if() returns no value.
1331 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
1332 * is true then the assignment @c *i = @p new_value is performed.
1334 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
1336 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
1337 _Predicate __pred
, const _Tp
& __new_value
)
1339 // concept requirements
1340 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1342 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
1343 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1344 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1345 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1346 __glibcxx_requires_valid_range(__first
, __last
);
1348 for (; __first
!= __last
; ++__first
)
1349 if (__pred(*__first
))
1350 *__first
= __new_value
;
1354 * @brief Copy a sequence, replacing each element of one value with another
1356 * @param first An input iterator.
1357 * @param last An input iterator.
1358 * @param result An output iterator.
1359 * @param old_value The value to be replaced.
1360 * @param new_value The replacement value.
1361 * @return The end of the output sequence, @p result+(last-first).
1363 * Copies each element in the input range @p [first,last) to the
1364 * output range @p [result,result+(last-first)) replacing elements
1365 * equal to @p old_value with @p new_value.
1367 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1369 replace_copy(_InputIterator __first
, _InputIterator __last
,
1370 _OutputIterator __result
,
1371 const _Tp
& __old_value
, const _Tp
& __new_value
)
1373 // concept requirements
1374 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1375 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1376 typename iterator_traits
<_InputIterator
>::value_type
>)
1377 __glibcxx_function_requires(_EqualOpConcept
<
1378 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1379 __glibcxx_requires_valid_range(__first
, __last
);
1381 for (; __first
!= __last
; ++__first
, ++__result
)
1382 if (*__first
== __old_value
)
1383 *__result
= __new_value
;
1385 *__result
= *__first
;
1390 * @brief Copy a sequence, replacing each value for which a predicate
1391 * returns true with another value.
1392 * @param first An input iterator.
1393 * @param last An input iterator.
1394 * @param result An output iterator.
1395 * @param pred A predicate.
1396 * @param new_value The replacement value.
1397 * @return The end of the output sequence, @p result+(last-first).
1399 * Copies each element in the range @p [first,last) to the range
1400 * @p [result,result+(last-first)) replacing elements for which
1401 * @p pred returns true with @p new_value.
1403 template<typename _InputIterator
, typename _OutputIterator
,
1404 typename _Predicate
, typename _Tp
>
1406 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
1407 _OutputIterator __result
,
1408 _Predicate __pred
, const _Tp
& __new_value
)
1410 // concept requirements
1411 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1412 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1413 typename iterator_traits
<_InputIterator
>::value_type
>)
1414 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1415 typename iterator_traits
<_InputIterator
>::value_type
>)
1416 __glibcxx_requires_valid_range(__first
, __last
);
1418 for (; __first
!= __last
; ++__first
, ++__result
)
1419 if (__pred(*__first
))
1420 *__result
= __new_value
;
1422 *__result
= *__first
;
1427 * @brief Assign the result of a function object to each value in a
1429 * @param first A forward iterator.
1430 * @param last A forward iterator.
1431 * @param gen A function object taking no arguments.
1432 * @return generate() returns no value.
1434 * Performs the assignment @c *i = @p gen() for each @c i in the range
1437 template<typename _ForwardIterator
, typename _Generator
>
1439 generate(_ForwardIterator __first
, _ForwardIterator __last
,
1442 // concept requirements
1443 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1444 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
1445 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1446 __glibcxx_requires_valid_range(__first
, __last
);
1448 for (; __first
!= __last
; ++__first
)
1453 * @brief Assign the result of a function object to each value in a
1455 * @param first A forward iterator.
1456 * @param n The length of the sequence.
1457 * @param gen A function object taking no arguments.
1458 * @return The end of the sequence, @p first+n
1460 * Performs the assignment @c *i = @p gen() for each @c i in the range
1461 * @p [first,first+n).
1463 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
1465 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
1467 // concept requirements
1468 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1469 // "the type returned by a _Generator"
1470 __typeof__(__gen())>)
1472 for (; __n
> 0; --__n
, ++__first
)
1478 * @brief Copy a sequence, removing elements of a given value.
1479 * @param first An input iterator.
1480 * @param last An input iterator.
1481 * @param result An output iterator.
1482 * @param value The value to be removed.
1483 * @return An iterator designating the end of the resulting sequence.
1485 * Copies each element in the range @p [first,last) not equal to @p value
1486 * to the range beginning at @p result.
1487 * remove_copy() is stable, so the relative order of elements that are
1488 * copied is unchanged.
1490 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1492 remove_copy(_InputIterator __first
, _InputIterator __last
,
1493 _OutputIterator __result
, const _Tp
& __value
)
1495 // concept requirements
1496 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1497 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1498 typename iterator_traits
<_InputIterator
>::value_type
>)
1499 __glibcxx_function_requires(_EqualOpConcept
<
1500 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1501 __glibcxx_requires_valid_range(__first
, __last
);
1503 for (; __first
!= __last
; ++__first
)
1504 if (!(*__first
== __value
))
1506 *__result
= *__first
;
1513 * @brief Copy a sequence, removing elements for which a predicate is true.
1514 * @param first An input iterator.
1515 * @param last An input iterator.
1516 * @param result An output iterator.
1517 * @param pred A predicate.
1518 * @return An iterator designating the end of the resulting sequence.
1520 * Copies each element in the range @p [first,last) for which
1521 * @p pred returns true to the range beginning at @p result.
1523 * remove_copy_if() is stable, so the relative order of elements that are
1524 * copied is unchanged.
1526 template<typename _InputIterator
, typename _OutputIterator
,
1527 typename _Predicate
>
1529 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
1530 _OutputIterator __result
, _Predicate __pred
)
1532 // concept requirements
1533 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1534 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1535 typename iterator_traits
<_InputIterator
>::value_type
>)
1536 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1537 typename iterator_traits
<_InputIterator
>::value_type
>)
1538 __glibcxx_requires_valid_range(__first
, __last
);
1540 for (; __first
!= __last
; ++__first
)
1541 if (!bool(__pred(*__first
)))
1543 *__result
= *__first
;
1550 * @brief Remove elements from a sequence.
1551 * @param first An input iterator.
1552 * @param last An input iterator.
1553 * @param value The value to be removed.
1554 * @return An iterator designating the end of the resulting sequence.
1556 * All elements equal to @p value are removed from the range
1559 * remove() is stable, so the relative order of elements that are
1560 * not removed is unchanged.
1562 * Elements between the end of the resulting sequence and @p last
1563 * are still present, but their value is unspecified.
1565 template<typename _ForwardIterator
, typename _Tp
>
1567 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1570 // concept requirements
1571 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1573 __glibcxx_function_requires(_EqualOpConcept
<
1574 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1575 __glibcxx_requires_valid_range(__first
, __last
);
1577 __first
= std::find(__first
, __last
, __value
);
1578 _ForwardIterator __i
= __first
;
1579 return __first
== __last
? __first
1580 : std::remove_copy(++__i
, __last
,
1585 * @brief Remove elements from a sequence using a predicate.
1586 * @param first A forward iterator.
1587 * @param last A forward iterator.
1588 * @param pred A predicate.
1589 * @return An iterator designating the end of the resulting sequence.
1591 * All elements for which @p pred returns true are removed from the range
1594 * remove_if() is stable, so the relative order of elements that are
1595 * not removed is unchanged.
1597 * Elements between the end of the resulting sequence and @p last
1598 * are still present, but their value is unspecified.
1600 template<typename _ForwardIterator
, typename _Predicate
>
1602 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1605 // concept requirements
1606 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1608 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1609 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1610 __glibcxx_requires_valid_range(__first
, __last
);
1612 __first
= std::find_if(__first
, __last
, __pred
);
1613 _ForwardIterator __i
= __first
;
1614 return __first
== __last
? __first
1615 : std::remove_copy_if(++__i
, __last
,
1620 * @brief Remove consecutive duplicate values from a sequence.
1621 * @param first A forward iterator.
1622 * @param last A forward iterator.
1623 * @return An iterator designating the end of the resulting sequence.
1625 * Removes all but the first element from each group of consecutive
1626 * values that compare equal.
1627 * unique() is stable, so the relative order of elements that are
1628 * not removed is unchanged.
1629 * Elements between the end of the resulting sequence and @p last
1630 * are still present, but their value is unspecified.
1632 template<typename _ForwardIterator
>
1634 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1636 // concept requirements
1637 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1639 __glibcxx_function_requires(_EqualityComparableConcept
<
1640 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1641 __glibcxx_requires_valid_range(__first
, __last
);
1643 // Skip the beginning, if already unique.
1644 __first
= std::adjacent_find(__first
, __last
);
1645 if (__first
== __last
)
1648 // Do the real copy work.
1649 _ForwardIterator __dest
= __first
;
1651 while (++__first
!= __last
)
1652 if (!(*__dest
== *__first
))
1653 *++__dest
= *__first
;
1658 * @brief Remove consecutive values from a sequence using a predicate.
1659 * @param first A forward iterator.
1660 * @param last A forward iterator.
1661 * @param binary_pred A binary predicate.
1662 * @return An iterator designating the end of the resulting sequence.
1664 * Removes all but the first element from each group of consecutive
1665 * values for which @p binary_pred returns true.
1666 * unique() is stable, so the relative order of elements that are
1667 * not removed is unchanged.
1668 * Elements between the end of the resulting sequence and @p last
1669 * are still present, but their value is unspecified.
1671 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1673 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1674 _BinaryPredicate __binary_pred
)
1676 // concept requirements
1677 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1679 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1680 typename iterator_traits
<_ForwardIterator
>::value_type
,
1681 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1682 __glibcxx_requires_valid_range(__first
, __last
);
1684 // Skip the beginning, if already unique.
1685 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1686 if (__first
== __last
)
1689 // Do the real copy work.
1690 _ForwardIterator __dest
= __first
;
1692 while (++__first
!= __last
)
1693 if (!bool(__binary_pred(*__dest
, *__first
)))
1694 *++__dest
= *__first
;
1700 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1702 * overloaded for forward iterators and output iterator as result.
1705 template<typename _ForwardIterator
, typename _OutputIterator
>
1707 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1708 _OutputIterator __result
,
1709 forward_iterator_tag
, output_iterator_tag
)
1711 // concept requirements -- taken care of in dispatching function
1712 _ForwardIterator __next
= __first
;
1713 *__result
= *__first
;
1714 while (++__next
!= __last
)
1715 if (!(*__first
== *__next
))
1718 *++__result
= *__first
;
1725 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1727 * overloaded for input iterators and output iterator as result.
1730 template<typename _InputIterator
, typename _OutputIterator
>
1732 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1733 _OutputIterator __result
,
1734 input_iterator_tag
, output_iterator_tag
)
1736 // concept requirements -- taken care of in dispatching function
1737 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1738 *__result
= __value
;
1739 while (++__first
!= __last
)
1740 if (!(__value
== *__first
))
1743 *++__result
= __value
;
1750 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1752 * overloaded for input iterators and forward iterator as result.
1755 template<typename _InputIterator
, typename _ForwardIterator
>
1757 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1758 _ForwardIterator __result
,
1759 input_iterator_tag
, forward_iterator_tag
)
1761 // concept requirements -- taken care of in dispatching function
1762 *__result
= *__first
;
1763 while (++__first
!= __last
)
1764 if (!(*__result
== *__first
))
1765 *++__result
= *__first
;
1771 * This is an uglified
1772 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1774 * overloaded for forward iterators and output iterator as result.
1777 template<typename _ForwardIterator
, typename _OutputIterator
,
1778 typename _BinaryPredicate
>
1780 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1781 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1782 forward_iterator_tag
, output_iterator_tag
)
1784 // concept requirements -- iterators already checked
1785 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1786 typename iterator_traits
<_ForwardIterator
>::value_type
,
1787 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1789 _ForwardIterator __next
= __first
;
1790 *__result
= *__first
;
1791 while (++__next
!= __last
)
1792 if (!bool(__binary_pred(*__first
, *__next
)))
1795 *++__result
= *__first
;
1802 * This is an uglified
1803 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1805 * overloaded for input iterators and output iterator as result.
1808 template<typename _InputIterator
, typename _OutputIterator
,
1809 typename _BinaryPredicate
>
1811 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1812 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1813 input_iterator_tag
, output_iterator_tag
)
1815 // concept requirements -- iterators already checked
1816 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1817 typename iterator_traits
<_InputIterator
>::value_type
,
1818 typename iterator_traits
<_InputIterator
>::value_type
>)
1820 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1821 *__result
= __value
;
1822 while (++__first
!= __last
)
1823 if (!bool(__binary_pred(__value
, *__first
)))
1826 *++__result
= __value
;
1833 * This is an uglified
1834 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1836 * overloaded for input iterators and forward iterator as result.
1839 template<typename _InputIterator
, typename _ForwardIterator
,
1840 typename _BinaryPredicate
>
1842 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1843 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1844 input_iterator_tag
, forward_iterator_tag
)
1846 // concept requirements -- iterators already checked
1847 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1848 typename iterator_traits
<_ForwardIterator
>::value_type
,
1849 typename iterator_traits
<_InputIterator
>::value_type
>)
1851 *__result
= *__first
;
1852 while (++__first
!= __last
)
1853 if (!bool(__binary_pred(*__result
, *__first
)))
1854 *++__result
= *__first
;
1859 * @brief Copy a sequence, removing consecutive duplicate values.
1860 * @param first An input iterator.
1861 * @param last An input iterator.
1862 * @param result An output iterator.
1863 * @return An iterator designating the end of the resulting sequence.
1865 * Copies each element in the range @p [first,last) to the range
1866 * beginning at @p result, except that only the first element is copied
1867 * from groups of consecutive elements that compare equal.
1868 * unique_copy() is stable, so the relative order of elements that are
1869 * copied is unchanged.
1872 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1873 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1875 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1876 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
1880 template<typename _InputIterator
, typename _OutputIterator
>
1881 inline _OutputIterator
1882 unique_copy(_InputIterator __first
, _InputIterator __last
,
1883 _OutputIterator __result
)
1885 // concept requirements
1886 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1887 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1888 typename iterator_traits
<_InputIterator
>::value_type
>)
1889 __glibcxx_function_requires(_EqualityComparableConcept
<
1890 typename iterator_traits
<_InputIterator
>::value_type
>)
1891 __glibcxx_requires_valid_range(__first
, __last
);
1893 if (__first
== __last
)
1895 return std::__unique_copy(__first
, __last
, __result
,
1896 std::__iterator_category(__first
),
1897 std::__iterator_category(__result
));
1901 * @brief Copy a sequence, removing consecutive values using a predicate.
1902 * @param first An input iterator.
1903 * @param last An input iterator.
1904 * @param result An output iterator.
1905 * @param binary_pred A binary predicate.
1906 * @return An iterator designating the end of the resulting sequence.
1908 * Copies each element in the range @p [first,last) to the range
1909 * beginning at @p result, except that only the first element is copied
1910 * from groups of consecutive elements for which @p binary_pred returns
1912 * unique_copy() is stable, so the relative order of elements that are
1913 * copied is unchanged.
1916 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1917 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1920 template<typename _InputIterator
, typename _OutputIterator
,
1921 typename _BinaryPredicate
>
1922 inline _OutputIterator
1923 unique_copy(_InputIterator __first
, _InputIterator __last
,
1924 _OutputIterator __result
,
1925 _BinaryPredicate __binary_pred
)
1927 // concept requirements -- predicates checked later
1928 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1929 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1930 typename iterator_traits
<_InputIterator
>::value_type
>)
1931 __glibcxx_requires_valid_range(__first
, __last
);
1933 if (__first
== __last
)
1935 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
1936 std::__iterator_category(__first
),
1937 std::__iterator_category(__result
));
1942 * This is an uglified reverse(_BidirectionalIterator,
1943 * _BidirectionalIterator)
1944 * overloaded for bidirectional iterators.
1947 template<typename _BidirectionalIterator
>
1949 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1950 bidirectional_iterator_tag
)
1953 if (__first
== __last
|| __first
== --__last
)
1957 std::iter_swap(__first
, __last
);
1964 * This is an uglified reverse(_BidirectionalIterator,
1965 * _BidirectionalIterator)
1966 * overloaded for random access iterators.
1969 template<typename _RandomAccessIterator
>
1971 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1972 random_access_iterator_tag
)
1974 if (__first
== __last
)
1977 while (__first
< __last
)
1979 std::iter_swap(__first
, __last
);
1986 * @brief Reverse a sequence.
1987 * @param first A bidirectional iterator.
1988 * @param last A bidirectional iterator.
1989 * @return reverse() returns no value.
1991 * Reverses the order of the elements in the range @p [first,last),
1992 * so that the first element becomes the last etc.
1993 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1994 * swaps @p *(first+i) and @p *(last-(i+1))
1996 template<typename _BidirectionalIterator
>
1998 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
2000 // concept requirements
2001 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2002 _BidirectionalIterator
>)
2003 __glibcxx_requires_valid_range(__first
, __last
);
2004 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
2008 * @brief Copy a sequence, reversing its elements.
2009 * @param first A bidirectional iterator.
2010 * @param last A bidirectional iterator.
2011 * @param result An output iterator.
2012 * @return An iterator designating the end of the resulting sequence.
2014 * Copies the elements in the range @p [first,last) to the range
2015 * @p [result,result+(last-first)) such that the order of the
2016 * elements is reversed.
2017 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
2018 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
2019 * The ranges @p [first,last) and @p [result,result+(last-first))
2022 template<typename _BidirectionalIterator
, typename _OutputIterator
>
2024 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
2025 _OutputIterator __result
)
2027 // concept requirements
2028 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2029 _BidirectionalIterator
>)
2030 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2031 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2032 __glibcxx_requires_valid_range(__first
, __last
);
2034 while (__first
!= __last
)
2037 *__result
= *__last
;
2045 * This is a helper function for the rotate algorithm specialized on RAIs.
2046 * It returns the greatest common divisor of two integer values.
2049 template<typename _EuclideanRingElement
>
2050 _EuclideanRingElement
2051 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
2055 _EuclideanRingElement __t
= __m
% __n
;
2064 * This is a helper function for the rotate algorithm.
2067 template<typename _ForwardIterator
>
2069 __rotate(_ForwardIterator __first
,
2070 _ForwardIterator __middle
,
2071 _ForwardIterator __last
,
2072 forward_iterator_tag
)
2074 if (__first
== __middle
|| __last
== __middle
)
2077 _ForwardIterator __first2
= __middle
;
2080 swap(*__first
, *__first2
);
2083 if (__first
== __middle
)
2084 __middle
= __first2
;
2086 while (__first2
!= __last
);
2088 __first2
= __middle
;
2090 while (__first2
!= __last
)
2092 swap(*__first
, *__first2
);
2095 if (__first
== __middle
)
2096 __middle
= __first2
;
2097 else if (__first2
== __last
)
2098 __first2
= __middle
;
2104 * This is a helper function for the rotate algorithm.
2107 template<typename _BidirectionalIterator
>
2109 __rotate(_BidirectionalIterator __first
,
2110 _BidirectionalIterator __middle
,
2111 _BidirectionalIterator __last
,
2112 bidirectional_iterator_tag
)
2114 // concept requirements
2115 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2116 _BidirectionalIterator
>)
2118 if (__first
== __middle
|| __last
== __middle
)
2121 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
2122 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
2124 while (__first
!= __middle
&& __middle
!= __last
)
2126 swap(*__first
, *--__last
);
2130 if (__first
== __middle
)
2131 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
2133 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
2138 * This is a helper function for the rotate algorithm.
2141 template<typename _RandomAccessIterator
>
2143 __rotate(_RandomAccessIterator __first
,
2144 _RandomAccessIterator __middle
,
2145 _RandomAccessIterator __last
,
2146 random_access_iterator_tag
)
2148 // concept requirements
2149 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2150 _RandomAccessIterator
>)
2152 if (__first
== __middle
|| __last
== __middle
)
2155 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2157 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2160 const _Distance __n
= __last
- __first
;
2161 const _Distance __k
= __middle
- __first
;
2162 const _Distance __l
= __n
- __k
;
2166 std::swap_ranges(__first
, __middle
, __middle
);
2170 const _Distance __d
= std::__gcd(__n
, __k
);
2172 for (_Distance __i
= 0; __i
< __d
; __i
++)
2174 _ValueType __tmp
= *__first
;
2175 _RandomAccessIterator __p
= __first
;
2179 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
2181 if (__p
> __first
+ __l
)
2183 *__p
= *(__p
- __l
);
2187 *__p
= *(__p
+ __k
);
2193 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
2195 if (__p
< __last
- __k
)
2197 *__p
= *(__p
+ __k
);
2200 *__p
= * (__p
- __l
);
2211 * @brief Rotate the elements of a sequence.
2212 * @param first A forward iterator.
2213 * @param middle A forward iterator.
2214 * @param last A forward iterator.
2217 * Rotates the elements of the range @p [first,last) by @p (middle-first)
2218 * positions so that the element at @p middle is moved to @p first, the
2219 * element at @p middle+1 is moved to @first+1 and so on for each element
2220 * in the range @p [first,last).
2222 * This effectively swaps the ranges @p [first,middle) and
2225 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
2226 * each @p n in the range @p [0,last-first).
2228 template<typename _ForwardIterator
>
2230 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
2231 _ForwardIterator __last
)
2233 // concept requirements
2234 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2236 __glibcxx_requires_valid_range(__first
, __middle
);
2237 __glibcxx_requires_valid_range(__middle
, __last
);
2239 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
2241 std::__rotate(__first
, __middle
, __last
, _IterType());
2245 * @brief Copy a sequence, rotating its elements.
2246 * @param first A forward iterator.
2247 * @param middle A forward iterator.
2248 * @param last A forward iterator.
2249 * @param result An output iterator.
2250 * @return An iterator designating the end of the resulting sequence.
2252 * Copies the elements of the range @p [first,last) to the range
2253 * beginning at @result, rotating the copied elements by @p (middle-first)
2254 * positions so that the element at @p middle is moved to @p result, the
2255 * element at @p middle+1 is moved to @result+1 and so on for each element
2256 * in the range @p [first,last).
2258 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
2259 * each @p n in the range @p [0,last-first).
2261 template<typename _ForwardIterator
, typename _OutputIterator
>
2263 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
2264 _ForwardIterator __last
, _OutputIterator __result
)
2266 // concept requirements
2267 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2268 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2269 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2270 __glibcxx_requires_valid_range(__first
, __middle
);
2271 __glibcxx_requires_valid_range(__middle
, __last
);
2273 return std::copy(__first
, __middle
,
2274 std::copy(__middle
, __last
, __result
));
2278 * @brief Randomly shuffle the elements of a sequence.
2279 * @param first A forward iterator.
2280 * @param last A forward iterator.
2283 * Reorder the elements in the range @p [first,last) using a random
2284 * distribution, so that every possible ordering of the sequence is
2287 template<typename _RandomAccessIterator
>
2289 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2291 // concept requirements
2292 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2293 _RandomAccessIterator
>)
2294 __glibcxx_requires_valid_range(__first
, __last
);
2296 if (__first
!= __last
)
2297 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2298 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
2302 * @brief Shuffle the elements of a sequence using a random number
2304 * @param first A forward iterator.
2305 * @param last A forward iterator.
2306 * @param rand The RNG functor or function.
2309 * Reorders the elements in the range @p [first,last) using @p rand to
2310 * provide a random distribution. Calling @p rand(N) for a positive
2311 * integer @p N should return a randomly chosen integer from the
2314 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
2316 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2317 _RandomNumberGenerator
& __rand
)
2319 // concept requirements
2320 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2321 _RandomAccessIterator
>)
2322 __glibcxx_requires_valid_range(__first
, __last
);
2324 if (__first
== __last
)
2326 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2327 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
2333 * This is a helper function...
2336 template<typename _ForwardIterator
, typename _Predicate
>
2338 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
2340 forward_iterator_tag
)
2342 if (__first
== __last
)
2345 while (__pred(*__first
))
2346 if (++__first
== __last
)
2349 _ForwardIterator __next
= __first
;
2351 while (++__next
!= __last
)
2352 if (__pred(*__next
))
2354 swap(*__first
, *__next
);
2363 * This is a helper function...
2366 template<typename _BidirectionalIterator
, typename _Predicate
>
2367 _BidirectionalIterator
2368 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
2370 bidirectional_iterator_tag
)
2375 if (__first
== __last
)
2377 else if (__pred(*__first
))
2383 if (__first
== __last
)
2385 else if (!bool(__pred(*__last
)))
2389 std::iter_swap(__first
, __last
);
2395 * @brief Move elements for which a predicate is true to the beginning
2397 * @param first A forward iterator.
2398 * @param last A forward iterator.
2399 * @param pred A predicate functor.
2400 * @return An iterator @p middle such that @p pred(i) is true for each
2401 * iterator @p i in the range @p [first,middle) and false for each @p i
2402 * in the range @p [middle,last).
2404 * @p pred must not modify its operand. @p partition() does not preserve
2405 * the relative ordering of elements in each group, use
2406 * @p stable_partition() if this is needed.
2408 template<typename _ForwardIterator
, typename _Predicate
>
2409 inline _ForwardIterator
2410 partition(_ForwardIterator __first
, _ForwardIterator __last
,
2413 // concept requirements
2414 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2416 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
2417 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2418 __glibcxx_requires_valid_range(__first
, __last
);
2420 return std::__partition(__first
, __last
, __pred
,
2421 std::__iterator_category(__first
));
2427 * This is a helper function...
2430 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
2432 __inplace_stable_partition(_ForwardIterator __first
,
2433 _ForwardIterator __last
,
2434 _Predicate __pred
, _Distance __len
)
2437 return __pred(*__first
) ? __last
: __first
;
2438 _ForwardIterator __middle
= __first
;
2439 std::advance(__middle
, __len
/ 2);
2440 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
2444 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
2448 std::rotate(__begin
, __middle
, __end
);
2449 std::advance(__begin
, std::distance(__middle
, __end
));
2455 * This is a helper function...
2458 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
2461 __stable_partition_adaptive(_ForwardIterator __first
,
2462 _ForwardIterator __last
,
2463 _Predicate __pred
, _Distance __len
,
2465 _Distance __buffer_size
)
2467 if (__len
<= __buffer_size
)
2469 _ForwardIterator __result1
= __first
;
2470 _Pointer __result2
= __buffer
;
2471 for (; __first
!= __last
; ++__first
)
2472 if (__pred(*__first
))
2474 *__result1
= *__first
;
2479 *__result2
= *__first
;
2482 std::copy(__buffer
, __result2
, __result1
);
2487 _ForwardIterator __middle
= __first
;
2488 std::advance(__middle
, __len
/ 2);
2489 _ForwardIterator __begin
=
2490 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
2491 __len
/ 2, __buffer
,
2493 _ForwardIterator __end
=
2494 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
2496 __buffer
, __buffer_size
);
2497 std::rotate(__begin
, __middle
, __end
);
2498 std::advance(__begin
, std::distance(__middle
, __end
));
2504 * @brief Move elements for which a predicate is true to the beginning
2505 * of a sequence, preserving relative ordering.
2506 * @param first A forward iterator.
2507 * @param last A forward iterator.
2508 * @param pred A predicate functor.
2509 * @return An iterator @p middle such that @p pred(i) is true for each
2510 * iterator @p i in the range @p [first,middle) and false for each @p i
2511 * in the range @p [middle,last).
2513 * Performs the same function as @p partition() with the additional
2514 * guarantee that the relative ordering of elements in each group is
2515 * preserved, so any two elements @p x and @p y in the range
2516 * @p [first,last) such that @p pred(x)==pred(y) will have the same
2517 * relative ordering after calling @p stable_partition().
2519 template<typename _ForwardIterator
, typename _Predicate
>
2521 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
2524 // concept requirements
2525 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2527 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
2528 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2529 __glibcxx_requires_valid_range(__first
, __last
);
2531 if (__first
== __last
)
2535 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2537 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2540 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
2542 if (__buf
.size() > 0)
2544 std::__stable_partition_adaptive(__first
, __last
, __pred
,
2545 _DistanceType(__buf
.requested_size()),
2547 _DistanceType(__buf
.size()));
2550 std::__inplace_stable_partition(__first
, __last
, __pred
,
2551 _DistanceType(__buf
.requested_size()));
2557 * This is a helper function for the sort routines.
2560 template<typename _RandomAccessIterator
>
2562 __heap_select(_RandomAccessIterator __first
,
2563 _RandomAccessIterator __middle
,
2564 _RandomAccessIterator __last
)
2566 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2569 std::make_heap(__first
, __middle
);
2570 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2571 if (*__i
< *__first
)
2572 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2577 * This is a helper function for the sort routines.
2580 template<typename _RandomAccessIterator
, typename _Compare
>
2582 __heap_select(_RandomAccessIterator __first
,
2583 _RandomAccessIterator __middle
,
2584 _RandomAccessIterator __last
, _Compare __comp
)
2586 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2589 std::make_heap(__first
, __middle
, __comp
);
2590 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2591 if (__comp(*__i
, *__first
))
2592 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2596 * @brief Sort the smallest elements of a sequence.
2597 * @param first An iterator.
2598 * @param middle Another iterator.
2599 * @param last Another iterator.
2602 * Sorts the smallest @p (middle-first) elements in the range
2603 * @p [first,last) and moves them to the range @p [first,middle). The
2604 * order of the remaining elements in the range @p [middle,last) is
2606 * After the sort if @p i and @j are iterators in the range
2607 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2608 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2610 template<typename _RandomAccessIterator
>
2612 partial_sort(_RandomAccessIterator __first
,
2613 _RandomAccessIterator __middle
,
2614 _RandomAccessIterator __last
)
2616 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2619 // concept requirements
2620 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2621 _RandomAccessIterator
>)
2622 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2623 __glibcxx_requires_valid_range(__first
, __middle
);
2624 __glibcxx_requires_valid_range(__middle
, __last
);
2626 std::__heap_select(__first
, __middle
, __last
);
2627 std::sort_heap(__first
, __middle
);
2631 * @brief Sort the smallest elements of a sequence using a predicate
2633 * @param first An iterator.
2634 * @param middle Another iterator.
2635 * @param last Another iterator.
2636 * @param comp A comparison functor.
2639 * Sorts the smallest @p (middle-first) elements in the range
2640 * @p [first,last) and moves them to the range @p [first,middle). The
2641 * order of the remaining elements in the range @p [middle,last) is
2643 * After the sort if @p i and @j are iterators in the range
2644 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2645 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2648 template<typename _RandomAccessIterator
, typename _Compare
>
2650 partial_sort(_RandomAccessIterator __first
,
2651 _RandomAccessIterator __middle
,
2652 _RandomAccessIterator __last
,
2655 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2658 // concept requirements
2659 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2660 _RandomAccessIterator
>)
2661 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2662 _ValueType
, _ValueType
>)
2663 __glibcxx_requires_valid_range(__first
, __middle
);
2664 __glibcxx_requires_valid_range(__middle
, __last
);
2666 std::__heap_select(__first
, __middle
, __last
, __comp
);
2667 std::sort_heap(__first
, __middle
, __comp
);
2671 * @brief Copy the smallest elements of a sequence.
2672 * @param first An iterator.
2673 * @param last Another iterator.
2674 * @param result_first A random-access iterator.
2675 * @param result_last Another random-access iterator.
2676 * @return An iterator indicating the end of the resulting sequence.
2678 * Copies and sorts the smallest N values from the range @p [first,last)
2679 * to the range beginning at @p result_first, where the number of
2680 * elements to be copied, @p N, is the smaller of @p (last-first) and
2681 * @p (result_last-result_first).
2682 * After the sort if @p i and @j are iterators in the range
2683 * @p [result_first,result_first+N) such that @i precedes @j then
2684 * @p *j<*i is false.
2685 * The value returned is @p result_first+N.
2687 template<typename _InputIterator
, typename _RandomAccessIterator
>
2688 _RandomAccessIterator
2689 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2690 _RandomAccessIterator __result_first
,
2691 _RandomAccessIterator __result_last
)
2693 typedef typename iterator_traits
<_InputIterator
>::value_type
2695 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2697 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2700 // concept requirements
2701 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2702 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2704 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
2706 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2707 __glibcxx_requires_valid_range(__first
, __last
);
2708 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2710 if (__result_first
== __result_last
)
2711 return __result_last
;
2712 _RandomAccessIterator __result_real_last
= __result_first
;
2713 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2715 *__result_real_last
= *__first
;
2716 ++__result_real_last
;
2719 std::make_heap(__result_first
, __result_real_last
);
2720 while (__first
!= __last
)
2722 if (*__first
< *__result_first
)
2723 std::__adjust_heap(__result_first
, _DistanceType(0),
2724 _DistanceType(__result_real_last
2726 _InputValueType(*__first
));
2729 std::sort_heap(__result_first
, __result_real_last
);
2730 return __result_real_last
;
2734 * @brief Copy the smallest elements of a sequence using a predicate for
2736 * @param first An input iterator.
2737 * @param last Another input iterator.
2738 * @param result_first A random-access iterator.
2739 * @param result_last Another random-access iterator.
2740 * @param comp A comparison functor.
2741 * @return An iterator indicating the end of the resulting sequence.
2743 * Copies and sorts the smallest N values from the range @p [first,last)
2744 * to the range beginning at @p result_first, where the number of
2745 * elements to be copied, @p N, is the smaller of @p (last-first) and
2746 * @p (result_last-result_first).
2747 * After the sort if @p i and @j are iterators in the range
2748 * @p [result_first,result_first+N) such that @i precedes @j then
2749 * @p comp(*j,*i) is false.
2750 * The value returned is @p result_first+N.
2752 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2753 _RandomAccessIterator
2754 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2755 _RandomAccessIterator __result_first
,
2756 _RandomAccessIterator __result_last
,
2759 typedef typename iterator_traits
<_InputIterator
>::value_type
2761 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2763 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2766 // concept requirements
2767 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2768 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2769 _RandomAccessIterator
>)
2770 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2772 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2773 _InputValueType
, _OutputValueType
>)
2774 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2775 _OutputValueType
, _OutputValueType
>)
2776 __glibcxx_requires_valid_range(__first
, __last
);
2777 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2779 if (__result_first
== __result_last
)
2780 return __result_last
;
2781 _RandomAccessIterator __result_real_last
= __result_first
;
2782 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2784 *__result_real_last
= *__first
;
2785 ++__result_real_last
;
2788 std::make_heap(__result_first
, __result_real_last
, __comp
);
2789 while (__first
!= __last
)
2791 if (__comp(*__first
, *__result_first
))
2792 std::__adjust_heap(__result_first
, _DistanceType(0),
2793 _DistanceType(__result_real_last
2795 _InputValueType(*__first
),
2799 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2800 return __result_real_last
;
2805 * This is a helper function for the sort routine.
2808 template<typename _RandomAccessIterator
, typename _Tp
>
2810 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2812 _RandomAccessIterator __next
= __last
;
2814 while (__val
< *__next
)
2825 * This is a helper function for the sort routine.
2828 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2830 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2833 _RandomAccessIterator __next
= __last
;
2835 while (__comp(__val
, *__next
))
2846 * This is a helper function for the sort routine.
2849 template<typename _RandomAccessIterator
>
2851 __insertion_sort(_RandomAccessIterator __first
,
2852 _RandomAccessIterator __last
)
2854 if (__first
== __last
)
2857 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2859 typename iterator_traits
<_RandomAccessIterator
>::value_type
2861 if (__val
< *__first
)
2863 std::copy_backward(__first
, __i
, __i
+ 1);
2867 std::__unguarded_linear_insert(__i
, __val
);
2873 * This is a helper function for the sort routine.
2876 template<typename _RandomAccessIterator
, typename _Compare
>
2878 __insertion_sort(_RandomAccessIterator __first
,
2879 _RandomAccessIterator __last
, _Compare __comp
)
2881 if (__first
== __last
) return;
2883 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2885 typename iterator_traits
<_RandomAccessIterator
>::value_type
2887 if (__comp(__val
, *__first
))
2889 std::copy_backward(__first
, __i
, __i
+ 1);
2893 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2899 * This is a helper function for the sort routine.
2902 template<typename _RandomAccessIterator
>
2904 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2905 _RandomAccessIterator __last
)
2907 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2910 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2911 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2916 * This is a helper function for the sort routine.
2919 template<typename _RandomAccessIterator
, typename _Compare
>
2921 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2922 _RandomAccessIterator __last
, _Compare __comp
)
2924 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2927 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2928 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2934 * This controls some aspect of the sort routines.
2937 enum { _S_threshold
= 16 };
2941 * This is a helper function for the sort routine.
2944 template<typename _RandomAccessIterator
>
2946 __final_insertion_sort(_RandomAccessIterator __first
,
2947 _RandomAccessIterator __last
)
2949 if (__last
- __first
> int(_S_threshold
))
2951 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2952 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2955 std::__insertion_sort(__first
, __last
);
2960 * This is a helper function for the sort routine.
2963 template<typename _RandomAccessIterator
, typename _Compare
>
2965 __final_insertion_sort(_RandomAccessIterator __first
,
2966 _RandomAccessIterator __last
, _Compare __comp
)
2968 if (__last
- __first
> int(_S_threshold
))
2970 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2971 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2975 std::__insertion_sort(__first
, __last
, __comp
);
2980 * This is a helper function...
2983 template<typename _RandomAccessIterator
, typename _Tp
>
2984 _RandomAccessIterator
2985 __unguarded_partition(_RandomAccessIterator __first
,
2986 _RandomAccessIterator __last
, _Tp __pivot
)
2990 while (*__first
< __pivot
)
2993 while (__pivot
< *__last
)
2995 if (!(__first
< __last
))
2997 std::iter_swap(__first
, __last
);
3004 * This is a helper function...
3007 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
3008 _RandomAccessIterator
3009 __unguarded_partition(_RandomAccessIterator __first
,
3010 _RandomAccessIterator __last
,
3011 _Tp __pivot
, _Compare __comp
)
3015 while (__comp(*__first
, __pivot
))
3018 while (__comp(__pivot
, *__last
))
3020 if (!(__first
< __last
))
3022 std::iter_swap(__first
, __last
);
3029 * This is a helper function for the sort routine.
3032 template<typename _RandomAccessIterator
, typename _Size
>
3034 __introsort_loop(_RandomAccessIterator __first
,
3035 _RandomAccessIterator __last
,
3036 _Size __depth_limit
)
3038 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3041 while (__last
- __first
> int(_S_threshold
))
3043 if (__depth_limit
== 0)
3045 std::partial_sort(__first
, __last
, __last
);
3049 _RandomAccessIterator __cut
=
3050 std::__unguarded_partition(__first
, __last
,
3051 _ValueType(std::__median(*__first
,
3058 std::__introsort_loop(__cut
, __last
, __depth_limit
);
3065 * This is a helper function for the sort routine.
3068 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
3070 __introsort_loop(_RandomAccessIterator __first
,
3071 _RandomAccessIterator __last
,
3072 _Size __depth_limit
, _Compare __comp
)
3074 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3077 while (__last
- __first
> int(_S_threshold
))
3079 if (__depth_limit
== 0)
3081 std::partial_sort(__first
, __last
, __last
, __comp
);
3085 _RandomAccessIterator __cut
=
3086 std::__unguarded_partition(__first
, __last
,
3087 _ValueType(std::__median(*__first
,
3095 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
3102 * This is a helper function for the sort routines.
3105 template<typename _Size
>
3110 for (__k
= 0; __n
!= 1; __n
>>= 1)
3116 * @brief Sort the elements of a sequence.
3117 * @param first An iterator.
3118 * @param last Another iterator.
3121 * Sorts the elements in the range @p [first,last) in ascending order,
3122 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3123 * @p [first,last-1).
3125 * The relative ordering of equivalent elements is not preserved, use
3126 * @p stable_sort() if this is needed.
3128 template<typename _RandomAccessIterator
>
3130 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3132 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3135 // concept requirements
3136 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3137 _RandomAccessIterator
>)
3138 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3139 __glibcxx_requires_valid_range(__first
, __last
);
3141 if (__first
!= __last
)
3143 std::__introsort_loop(__first
, __last
,
3144 std::__lg(__last
- __first
) * 2);
3145 std::__final_insertion_sort(__first
, __last
);
3150 * @brief Sort the elements of a sequence using a predicate for comparison.
3151 * @param first An iterator.
3152 * @param last Another iterator.
3153 * @param comp A comparison functor.
3156 * Sorts the elements in the range @p [first,last) in ascending order,
3157 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
3158 * range @p [first,last-1).
3160 * The relative ordering of equivalent elements is not preserved, use
3161 * @p stable_sort() if this is needed.
3163 template<typename _RandomAccessIterator
, typename _Compare
>
3165 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3168 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3171 // concept requirements
3172 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3173 _RandomAccessIterator
>)
3174 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
3176 __glibcxx_requires_valid_range(__first
, __last
);
3178 if (__first
!= __last
)
3180 std::__introsort_loop(__first
, __last
,
3181 std::__lg(__last
- __first
) * 2, __comp
);
3182 std::__final_insertion_sort(__first
, __last
, __comp
);
3186 template<typename _RandomAccessIterator
, typename _Size
>
3188 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3189 _RandomAccessIterator __last
, _Size __depth_limit
)
3191 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3194 while (__last
- __first
> 3)
3196 if (__depth_limit
== 0)
3198 std::__heap_select(__first
, __nth
+ 1, __last
);
3199 // Place the nth largest element in its final position.
3200 std::iter_swap(__first
, __nth
);
3204 _RandomAccessIterator __cut
=
3205 std::__unguarded_partition(__first
, __last
,
3206 _ValueType(std::__median(*__first
,
3218 std::__insertion_sort(__first
, __last
);
3221 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
3223 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3224 _RandomAccessIterator __last
, _Size __depth_limit
,
3227 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3230 while (__last
- __first
> 3)
3232 if (__depth_limit
== 0)
3234 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
3235 // Place the nth largest element in its final position.
3236 std::iter_swap(__first
, __nth
);
3240 _RandomAccessIterator __cut
=
3241 std::__unguarded_partition(__first
, __last
,
3242 _ValueType(std::__median(*__first
,
3255 std::__insertion_sort(__first
, __last
, __comp
);
3259 * @brief Sort a sequence just enough to find a particular position.
3260 * @param first An iterator.
3261 * @param nth Another iterator.
3262 * @param last Another iterator.
3265 * Rearranges the elements in the range @p [first,last) so that @p *nth
3266 * is the same element that would have been in that position had the
3267 * whole sequence been sorted.
3268 * whole sequence been sorted. The elements either side of @p *nth are
3269 * not completely sorted, but for any iterator @i in the range
3270 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3271 * holds that @p *j<*i is false.
3273 template<typename _RandomAccessIterator
>
3275 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3276 _RandomAccessIterator __last
)
3278 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3281 // concept requirements
3282 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3283 _RandomAccessIterator
>)
3284 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3285 __glibcxx_requires_valid_range(__first
, __nth
);
3286 __glibcxx_requires_valid_range(__nth
, __last
);
3288 if (__first
== __last
|| __nth
== __last
)
3291 std::__introselect(__first
, __nth
, __last
,
3292 std::__lg(__last
- __first
) * 2);
3296 * @brief Sort a sequence just enough to find a particular position
3297 * using a predicate for comparison.
3298 * @param first An iterator.
3299 * @param nth Another iterator.
3300 * @param last Another iterator.
3301 * @param comp A comparison functor.
3304 * Rearranges the elements in the range @p [first,last) so that @p *nth
3305 * is the same element that would have been in that position had the
3306 * whole sequence been sorted. The elements either side of @p *nth are
3307 * not completely sorted, but for any iterator @i in the range
3308 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3309 * holds that @p comp(*j,*i) is false.
3311 template<typename _RandomAccessIterator
, typename _Compare
>
3313 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3314 _RandomAccessIterator __last
, _Compare __comp
)
3316 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3319 // concept requirements
3320 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3321 _RandomAccessIterator
>)
3322 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3323 _ValueType
, _ValueType
>)
3324 __glibcxx_requires_valid_range(__first
, __nth
);
3325 __glibcxx_requires_valid_range(__nth
, __last
);
3327 if (__first
== __last
|| __nth
== __last
)
3330 std::__introselect(__first
, __nth
, __last
,
3331 std::__lg(__last
- __first
) * 2, __comp
);
3335 * @brief Finds the first position in which @a val could be inserted
3336 * without changing the ordering.
3337 * @param first An iterator.
3338 * @param last Another iterator.
3339 * @param val The search term.
3340 * @return An iterator pointing to the first element "not less than" @a val,
3341 * or end() if every element is less than @a val.
3342 * @ingroup binarysearch
3344 template<typename _ForwardIterator
, typename _Tp
>
3346 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
3349 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3351 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3354 // concept requirements
3355 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3356 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
3357 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3359 _DistanceType __len
= std::distance(__first
, __last
);
3360 _DistanceType __half
;
3361 _ForwardIterator __middle
;
3365 __half
= __len
>> 1;
3367 std::advance(__middle
, __half
);
3368 if (*__middle
< __val
)
3372 __len
= __len
- __half
- 1;
3381 * @brief Finds the first position in which @a val could be inserted
3382 * without changing the ordering.
3383 * @param first An iterator.
3384 * @param last Another iterator.
3385 * @param val The search term.
3386 * @param comp A functor to use for comparisons.
3387 * @return An iterator pointing to the first element "not less than" @a val,
3388 * or end() if every element is less than @a val.
3389 * @ingroup binarysearch
3391 * The comparison function should have the same effects on ordering as
3392 * the function used for the initial sort.
3394 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3396 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
3397 const _Tp
& __val
, _Compare __comp
)
3399 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3401 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3404 // concept requirements
3405 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3406 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3408 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3410 _DistanceType __len
= std::distance(__first
, __last
);
3411 _DistanceType __half
;
3412 _ForwardIterator __middle
;
3416 __half
= __len
>> 1;
3418 std::advance(__middle
, __half
);
3419 if (__comp(*__middle
, __val
))
3423 __len
= __len
- __half
- 1;
3432 * @brief Finds the last position in which @a val could be inserted
3433 * without changing the ordering.
3434 * @param first An iterator.
3435 * @param last Another iterator.
3436 * @param val The search term.
3437 * @return An iterator pointing to the first element greater than @a val,
3438 * or end() if no elements are greater than @a val.
3439 * @ingroup binarysearch
3441 template<typename _ForwardIterator
, typename _Tp
>
3443 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
3446 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3448 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3451 // concept requirements
3452 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3453 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
3454 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3456 _DistanceType __len
= std::distance(__first
, __last
);
3457 _DistanceType __half
;
3458 _ForwardIterator __middle
;
3462 __half
= __len
>> 1;
3464 std::advance(__middle
, __half
);
3465 if (__val
< *__middle
)
3471 __len
= __len
- __half
- 1;
3478 * @brief Finds the last position in which @a val could be inserted
3479 * without changing the ordering.
3480 * @param first An iterator.
3481 * @param last Another iterator.
3482 * @param val The search term.
3483 * @param comp A functor to use for comparisons.
3484 * @return An iterator pointing to the first element greater than @a val,
3485 * or end() if no elements are greater than @a val.
3486 * @ingroup binarysearch
3488 * The comparison function should have the same effects on ordering as
3489 * the function used for the initial sort.
3491 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3493 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
3494 const _Tp
& __val
, _Compare __comp
)
3496 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3498 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3501 // concept requirements
3502 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3503 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3505 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3507 _DistanceType __len
= std::distance(__first
, __last
);
3508 _DistanceType __half
;
3509 _ForwardIterator __middle
;
3513 __half
= __len
>> 1;
3515 std::advance(__middle
, __half
);
3516 if (__comp(__val
, *__middle
))
3522 __len
= __len
- __half
- 1;
3529 * @brief Finds the largest subrange in which @a val could be inserted
3530 * at any place in it without changing the ordering.
3531 * @param first An iterator.
3532 * @param last Another iterator.
3533 * @param val The search term.
3534 * @return An pair of iterators defining the subrange.
3535 * @ingroup binarysearch
3537 * This is equivalent to
3539 * std::make_pair(lower_bound(first, last, val),
3540 * upper_bound(first, last, val))
3542 * but does not actually call those functions.
3544 template<typename _ForwardIterator
, typename _Tp
>
3545 pair
<_ForwardIterator
, _ForwardIterator
>
3546 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3549 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3551 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3554 // concept requirements
3555 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3556 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
3557 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
3558 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3560 _DistanceType __len
= std::distance(__first
, __last
);
3561 _DistanceType __half
;
3562 _ForwardIterator __middle
, __left
, __right
;
3566 __half
= __len
>> 1;
3568 std::advance(__middle
, __half
);
3569 if (*__middle
< __val
)
3573 __len
= __len
- __half
- 1;
3575 else if (__val
< *__middle
)
3579 __left
= std::lower_bound(__first
, __middle
, __val
);
3580 std::advance(__first
, __len
);
3581 __right
= std::upper_bound(++__middle
, __first
, __val
);
3582 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3585 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3589 * @brief Finds the largest subrange in which @a val could be inserted
3590 * at any place in it without changing the ordering.
3591 * @param first An iterator.
3592 * @param last Another iterator.
3593 * @param val The search term.
3594 * @param comp A functor to use for comparisons.
3595 * @return An pair of iterators defining the subrange.
3596 * @ingroup binarysearch
3598 * This is equivalent to
3600 * std::make_pair(lower_bound(first, last, val, comp),
3601 * upper_bound(first, last, val, comp))
3603 * but does not actually call those functions.
3605 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3606 pair
<_ForwardIterator
, _ForwardIterator
>
3607 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3611 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3613 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3616 // concept requirements
3617 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3618 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3620 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3622 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3624 _DistanceType __len
= std::distance(__first
, __last
);
3625 _DistanceType __half
;
3626 _ForwardIterator __middle
, __left
, __right
;
3630 __half
= __len
>> 1;
3632 std::advance(__middle
, __half
);
3633 if (__comp(*__middle
, __val
))
3637 __len
= __len
- __half
- 1;
3639 else if (__comp(__val
, *__middle
))
3643 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
3644 std::advance(__first
, __len
);
3645 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
3646 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3649 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3653 * @brief Determines whether an element exists in a range.
3654 * @param first An iterator.
3655 * @param last Another iterator.
3656 * @param val The search term.
3657 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3658 * @ingroup binarysearch
3660 * Note that this does not actually return an iterator to @a val. For
3661 * that, use std::find or a container's specialized find member functions.
3663 template<typename _ForwardIterator
, typename _Tp
>
3665 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3668 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3671 // concept requirements
3672 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3673 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
3674 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3676 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
3677 return __i
!= __last
&& !(__val
< *__i
);
3681 * @brief Determines whether an element exists in a range.
3682 * @param first An iterator.
3683 * @param last Another iterator.
3684 * @param val The search term.
3685 * @param comp A functor to use for comparisons.
3686 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3687 * @ingroup binarysearch
3689 * Note that this does not actually return an iterator to @a val. For
3690 * that, use std::find or a container's specialized find member functions.
3692 * The comparison function should have the same effects on ordering as
3693 * the function used for the initial sort.
3695 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3697 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3698 const _Tp
& __val
, _Compare __comp
)
3700 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3703 // concept requirements
3704 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3705 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3707 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3709 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
3710 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
3714 * @brief Merges two sorted ranges.
3715 * @param first1 An iterator.
3716 * @param first2 Another iterator.
3717 * @param last1 Another iterator.
3718 * @param last2 Another iterator.
3719 * @param result An iterator pointing to the end of the merged range.
3720 * @return An iterator pointing to the first element "not less than" @a val.
3722 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3723 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3724 * must be sorted, and the output range must not overlap with either of
3725 * the input ranges. The sort is @e stable, that is, for equivalent
3726 * elements in the two ranges, elements from the first range will always
3727 * come before elements from the second.
3729 template<typename _InputIterator1
, typename _InputIterator2
,
3730 typename _OutputIterator
>
3732 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3733 _InputIterator2 __first2
, _InputIterator2 __last2
,
3734 _OutputIterator __result
)
3736 typedef typename iterator_traits
<_InputIterator1
>::value_type
3738 typedef typename iterator_traits
<_InputIterator2
>::value_type
3741 // concept requirements
3742 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3743 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3744 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3746 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3748 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3749 __glibcxx_requires_sorted(__first1
, __last1
);
3750 __glibcxx_requires_sorted(__first2
, __last2
);
3752 while (__first1
!= __last1
&& __first2
!= __last2
)
3754 if (*__first2
< *__first1
)
3756 *__result
= *__first2
;
3761 *__result
= *__first1
;
3766 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3771 * @brief Merges two sorted ranges.
3772 * @param first1 An iterator.
3773 * @param first2 Another iterator.
3774 * @param last1 Another iterator.
3775 * @param last2 Another iterator.
3776 * @param result An iterator pointing to the end of the merged range.
3777 * @param comp A functor to use for comparisons.
3778 * @return An iterator pointing to the first element "not less than" @a val.
3780 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3781 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3782 * must be sorted, and the output range must not overlap with either of
3783 * the input ranges. The sort is @e stable, that is, for equivalent
3784 * elements in the two ranges, elements from the first range will always
3785 * come before elements from the second.
3787 * The comparison function should have the same effects on ordering as
3788 * the function used for the initial sort.
3790 template<typename _InputIterator1
, typename _InputIterator2
,
3791 typename _OutputIterator
, typename _Compare
>
3793 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3794 _InputIterator2 __first2
, _InputIterator2 __last2
,
3795 _OutputIterator __result
, _Compare __comp
)
3797 typedef typename iterator_traits
<_InputIterator1
>::value_type
3799 typedef typename iterator_traits
<_InputIterator2
>::value_type
3802 // concept requirements
3803 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3804 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3805 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3807 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3809 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3810 _ValueType2
, _ValueType1
>)
3811 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3812 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3814 while (__first1
!= __last1
&& __first2
!= __last2
)
3816 if (__comp(*__first2
, *__first1
))
3818 *__result
= *__first2
;
3823 *__result
= *__first1
;
3828 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3834 * This is a helper function for the merge routines.
3837 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3838 typename _BidirectionalIterator3
>
3839 _BidirectionalIterator3
3840 __merge_backward(_BidirectionalIterator1 __first1
,
3841 _BidirectionalIterator1 __last1
,
3842 _BidirectionalIterator2 __first2
,
3843 _BidirectionalIterator2 __last2
,
3844 _BidirectionalIterator3 __result
)
3846 if (__first1
== __last1
)
3847 return std::copy_backward(__first2
, __last2
, __result
);
3848 if (__first2
== __last2
)
3849 return std::copy_backward(__first1
, __last1
, __result
);
3854 if (*__last2
< *__last1
)
3856 *--__result
= *__last1
;
3857 if (__first1
== __last1
)
3858 return std::copy_backward(__first2
, ++__last2
, __result
);
3863 *--__result
= *__last2
;
3864 if (__first2
== __last2
)
3865 return std::copy_backward(__first1
, ++__last1
, __result
);
3873 * This is a helper function for the merge routines.
3876 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3877 typename _BidirectionalIterator3
, typename _Compare
>
3878 _BidirectionalIterator3
3879 __merge_backward(_BidirectionalIterator1 __first1
,
3880 _BidirectionalIterator1 __last1
,
3881 _BidirectionalIterator2 __first2
,
3882 _BidirectionalIterator2 __last2
,
3883 _BidirectionalIterator3 __result
,
3886 if (__first1
== __last1
)
3887 return std::copy_backward(__first2
, __last2
, __result
);
3888 if (__first2
== __last2
)
3889 return std::copy_backward(__first1
, __last1
, __result
);
3894 if (__comp(*__last2
, *__last1
))
3896 *--__result
= *__last1
;
3897 if (__first1
== __last1
)
3898 return std::copy_backward(__first2
, ++__last2
, __result
);
3903 *--__result
= *__last2
;
3904 if (__first2
== __last2
)
3905 return std::copy_backward(__first1
, ++__last1
, __result
);
3913 * This is a helper function for the merge routines.
3916 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3918 _BidirectionalIterator1
3919 __rotate_adaptive(_BidirectionalIterator1 __first
,
3920 _BidirectionalIterator1 __middle
,
3921 _BidirectionalIterator1 __last
,
3922 _Distance __len1
, _Distance __len2
,
3923 _BidirectionalIterator2 __buffer
,
3924 _Distance __buffer_size
)
3926 _BidirectionalIterator2 __buffer_end
;
3927 if (__len1
> __len2
&& __len2
<= __buffer_size
)
3929 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3930 std::copy_backward(__first
, __middle
, __last
);
3931 return std::copy(__buffer
, __buffer_end
, __first
);
3933 else if (__len1
<= __buffer_size
)
3935 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3936 std::copy(__middle
, __last
, __first
);
3937 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3941 std::rotate(__first
, __middle
, __last
);
3942 std::advance(__first
, std::distance(__middle
, __last
));
3949 * This is a helper function for the merge routines.
3952 template<typename _BidirectionalIterator
, typename _Distance
,
3955 __merge_adaptive(_BidirectionalIterator __first
,
3956 _BidirectionalIterator __middle
,
3957 _BidirectionalIterator __last
,
3958 _Distance __len1
, _Distance __len2
,
3959 _Pointer __buffer
, _Distance __buffer_size
)
3961 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3963 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3964 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3966 else if (__len2
<= __buffer_size
)
3968 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3969 std::__merge_backward(__first
, __middle
, __buffer
,
3970 __buffer_end
, __last
);
3974 _BidirectionalIterator __first_cut
= __first
;
3975 _BidirectionalIterator __second_cut
= __middle
;
3976 _Distance __len11
= 0;
3977 _Distance __len22
= 0;
3978 if (__len1
> __len2
)
3980 __len11
= __len1
/ 2;
3981 std::advance(__first_cut
, __len11
);
3982 __second_cut
= std::lower_bound(__middle
, __last
,
3984 __len22
= std::distance(__middle
, __second_cut
);
3988 __len22
= __len2
/ 2;
3989 std::advance(__second_cut
, __len22
);
3990 __first_cut
= std::upper_bound(__first
, __middle
,
3992 __len11
= std::distance(__first
, __first_cut
);
3994 _BidirectionalIterator __new_middle
=
3995 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3996 __len1
- __len11
, __len22
, __buffer
,
3998 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3999 __len22
, __buffer
, __buffer_size
);
4000 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
4002 __len2
- __len22
, __buffer
, __buffer_size
);
4008 * This is a helper function for the merge routines.
4011 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
4014 __merge_adaptive(_BidirectionalIterator __first
,
4015 _BidirectionalIterator __middle
,
4016 _BidirectionalIterator __last
,
4017 _Distance __len1
, _Distance __len2
,
4018 _Pointer __buffer
, _Distance __buffer_size
,
4021 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
4023 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
4024 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
4026 else if (__len2
<= __buffer_size
)
4028 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
4029 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
4034 _BidirectionalIterator __first_cut
= __first
;
4035 _BidirectionalIterator __second_cut
= __middle
;
4036 _Distance __len11
= 0;
4037 _Distance __len22
= 0;
4038 if (__len1
> __len2
)
4040 __len11
= __len1
/ 2;
4041 std::advance(__first_cut
, __len11
);
4042 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
4044 __len22
= std::distance(__middle
, __second_cut
);
4048 __len22
= __len2
/ 2;
4049 std::advance(__second_cut
, __len22
);
4050 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
4052 __len11
= std::distance(__first
, __first_cut
);
4054 _BidirectionalIterator __new_middle
=
4055 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
4056 __len1
- __len11
, __len22
, __buffer
,
4058 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
4059 __len22
, __buffer
, __buffer_size
, __comp
);
4060 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
4062 __len2
- __len22
, __buffer
,
4063 __buffer_size
, __comp
);
4069 * This is a helper function for the merge routines.
4072 template<typename _BidirectionalIterator
, typename _Distance
>
4074 __merge_without_buffer(_BidirectionalIterator __first
,
4075 _BidirectionalIterator __middle
,
4076 _BidirectionalIterator __last
,
4077 _Distance __len1
, _Distance __len2
)
4079 if (__len1
== 0 || __len2
== 0)
4081 if (__len1
+ __len2
== 2)
4083 if (*__middle
< *__first
)
4084 std::iter_swap(__first
, __middle
);
4087 _BidirectionalIterator __first_cut
= __first
;
4088 _BidirectionalIterator __second_cut
= __middle
;
4089 _Distance __len11
= 0;
4090 _Distance __len22
= 0;
4091 if (__len1
> __len2
)
4093 __len11
= __len1
/ 2;
4094 std::advance(__first_cut
, __len11
);
4095 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
4096 __len22
= std::distance(__middle
, __second_cut
);
4100 __len22
= __len2
/ 2;
4101 std::advance(__second_cut
, __len22
);
4102 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
4103 __len11
= std::distance(__first
, __first_cut
);
4105 std::rotate(__first_cut
, __middle
, __second_cut
);
4106 _BidirectionalIterator __new_middle
= __first_cut
;
4107 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
4108 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
4110 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
4111 __len1
- __len11
, __len2
- __len22
);
4116 * This is a helper function for the merge routines.
4119 template<typename _BidirectionalIterator
, typename _Distance
,
4122 __merge_without_buffer(_BidirectionalIterator __first
,
4123 _BidirectionalIterator __middle
,
4124 _BidirectionalIterator __last
,
4125 _Distance __len1
, _Distance __len2
,
4128 if (__len1
== 0 || __len2
== 0)
4130 if (__len1
+ __len2
== 2)
4132 if (__comp(*__middle
, *__first
))
4133 std::iter_swap(__first
, __middle
);
4136 _BidirectionalIterator __first_cut
= __first
;
4137 _BidirectionalIterator __second_cut
= __middle
;
4138 _Distance __len11
= 0;
4139 _Distance __len22
= 0;
4140 if (__len1
> __len2
)
4142 __len11
= __len1
/ 2;
4143 std::advance(__first_cut
, __len11
);
4144 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
4146 __len22
= std::distance(__middle
, __second_cut
);
4150 __len22
= __len2
/ 2;
4151 std::advance(__second_cut
, __len22
);
4152 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
4154 __len11
= std::distance(__first
, __first_cut
);
4156 std::rotate(__first_cut
, __middle
, __second_cut
);
4157 _BidirectionalIterator __new_middle
= __first_cut
;
4158 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
4159 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
4160 __len11
, __len22
, __comp
);
4161 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
4162 __len1
- __len11
, __len2
- __len22
, __comp
);
4166 * @brief Merges two sorted ranges in place.
4167 * @param first An iterator.
4168 * @param middle Another iterator.
4169 * @param last Another iterator.
4172 * Merges two sorted and consecutive ranges, [first,middle) and
4173 * [middle,last), and puts the result in [first,last). The output will
4174 * be sorted. The sort is @e stable, that is, for equivalent
4175 * elements in the two ranges, elements from the first range will always
4176 * come before elements from the second.
4178 * If enough additional memory is available, this takes (last-first)-1
4179 * comparisons. Otherwise an NlogN algorithm is used, where N is
4180 * distance(first,last).
4182 template<typename _BidirectionalIterator
>
4184 inplace_merge(_BidirectionalIterator __first
,
4185 _BidirectionalIterator __middle
,
4186 _BidirectionalIterator __last
)
4188 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
4190 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
4193 // concept requirements
4194 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
4195 _BidirectionalIterator
>)
4196 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4197 __glibcxx_requires_sorted(__first
, __middle
);
4198 __glibcxx_requires_sorted(__middle
, __last
);
4200 if (__first
== __middle
|| __middle
== __last
)
4203 _DistanceType __len1
= std::distance(__first
, __middle
);
4204 _DistanceType __len2
= std::distance(__middle
, __last
);
4206 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
4208 if (__buf
.begin() == 0)
4209 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
4211 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
4212 __buf
.begin(), _DistanceType(__buf
.size()));
4216 * @brief Merges two sorted ranges in place.
4217 * @param first An iterator.
4218 * @param middle Another iterator.
4219 * @param last Another iterator.
4220 * @param comp A functor to use for comparisons.
4223 * Merges two sorted and consecutive ranges, [first,middle) and
4224 * [middle,last), and puts the result in [first,last). The output will
4225 * be sorted. The sort is @e stable, that is, for equivalent
4226 * elements in the two ranges, elements from the first range will always
4227 * come before elements from the second.
4229 * If enough additional memory is available, this takes (last-first)-1
4230 * comparisons. Otherwise an NlogN algorithm is used, where N is
4231 * distance(first,last).
4233 * The comparison function should have the same effects on ordering as
4234 * the function used for the initial sort.
4236 template<typename _BidirectionalIterator
, typename _Compare
>
4238 inplace_merge(_BidirectionalIterator __first
,
4239 _BidirectionalIterator __middle
,
4240 _BidirectionalIterator __last
,
4243 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
4245 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
4248 // concept requirements
4249 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
4250 _BidirectionalIterator
>)
4251 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4252 _ValueType
, _ValueType
>)
4253 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
4254 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
4256 if (__first
== __middle
|| __middle
== __last
)
4259 const _DistanceType __len1
= std::distance(__first
, __middle
);
4260 const _DistanceType __len2
= std::distance(__middle
, __last
);
4262 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
4264 if (__buf
.begin() == 0)
4265 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
4268 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
4269 __buf
.begin(), _DistanceType(__buf
.size()),
4273 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
4276 __merge_sort_loop(_RandomAccessIterator1 __first
,
4277 _RandomAccessIterator1 __last
,
4278 _RandomAccessIterator2 __result
,
4279 _Distance __step_size
)
4281 const _Distance __two_step
= 2 * __step_size
;
4283 while (__last
- __first
>= __two_step
)
4285 __result
= std::merge(__first
, __first
+ __step_size
,
4286 __first
+ __step_size
, __first
+ __two_step
,
4288 __first
+= __two_step
;
4291 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
4292 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
4296 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
4297 typename _Distance
, typename _Compare
>
4299 __merge_sort_loop(_RandomAccessIterator1 __first
,
4300 _RandomAccessIterator1 __last
,
4301 _RandomAccessIterator2 __result
, _Distance __step_size
,
4304 const _Distance __two_step
= 2 * __step_size
;
4306 while (__last
- __first
>= __two_step
)
4308 __result
= std::merge(__first
, __first
+ __step_size
,
4309 __first
+ __step_size
, __first
+ __two_step
,
4312 __first
+= __two_step
;
4314 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
4316 std::merge(__first
, __first
+ __step_size
,
4317 __first
+ __step_size
, __last
,
4322 template<typename _RandomAccessIterator
, typename _Distance
>
4324 __chunk_insertion_sort(_RandomAccessIterator __first
,
4325 _RandomAccessIterator __last
,
4326 _Distance __chunk_size
)
4328 while (__last
- __first
>= __chunk_size
)
4330 std::__insertion_sort(__first
, __first
+ __chunk_size
);
4331 __first
+= __chunk_size
;
4333 std::__insertion_sort(__first
, __last
);
4336 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
4338 __chunk_insertion_sort(_RandomAccessIterator __first
,
4339 _RandomAccessIterator __last
,
4340 _Distance __chunk_size
, _Compare __comp
)
4342 while (__last
- __first
>= __chunk_size
)
4344 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
4345 __first
+= __chunk_size
;
4347 std::__insertion_sort(__first
, __last
, __comp
);
4350 enum { _S_chunk_size
= 7 };
4352 template<typename _RandomAccessIterator
, typename _Pointer
>
4354 __merge_sort_with_buffer(_RandomAccessIterator __first
,
4355 _RandomAccessIterator __last
,
4358 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4361 const _Distance __len
= __last
- __first
;
4362 const _Pointer __buffer_last
= __buffer
+ __len
;
4364 _Distance __step_size
= _S_chunk_size
;
4365 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
4367 while (__step_size
< __len
)
4369 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
4371 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
4376 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
4378 __merge_sort_with_buffer(_RandomAccessIterator __first
,
4379 _RandomAccessIterator __last
,
4380 _Pointer __buffer
, _Compare __comp
)
4382 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4385 const _Distance __len
= __last
- __first
;
4386 const _Pointer __buffer_last
= __buffer
+ __len
;
4388 _Distance __step_size
= _S_chunk_size
;
4389 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
4391 while (__step_size
< __len
)
4393 std::__merge_sort_loop(__first
, __last
, __buffer
,
4394 __step_size
, __comp
);
4396 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
4397 __step_size
, __comp
);
4402 template<typename _RandomAccessIterator
, typename _Pointer
,
4405 __stable_sort_adaptive(_RandomAccessIterator __first
,
4406 _RandomAccessIterator __last
,
4407 _Pointer __buffer
, _Distance __buffer_size
)
4409 const _Distance __len
= (__last
- __first
+ 1) / 2;
4410 const _RandomAccessIterator __middle
= __first
+ __len
;
4411 if (__len
> __buffer_size
)
4413 std::__stable_sort_adaptive(__first
, __middle
,
4414 __buffer
, __buffer_size
);
4415 std::__stable_sort_adaptive(__middle
, __last
,
4416 __buffer
, __buffer_size
);
4420 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
4421 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
4423 std::__merge_adaptive(__first
, __middle
, __last
,
4424 _Distance(__middle
- __first
),
4425 _Distance(__last
- __middle
),
4426 __buffer
, __buffer_size
);
4429 template<typename _RandomAccessIterator
, typename _Pointer
,
4430 typename _Distance
, typename _Compare
>
4432 __stable_sort_adaptive(_RandomAccessIterator __first
,
4433 _RandomAccessIterator __last
,
4434 _Pointer __buffer
, _Distance __buffer_size
,
4437 const _Distance __len
= (__last
- __first
+ 1) / 2;
4438 const _RandomAccessIterator __middle
= __first
+ __len
;
4439 if (__len
> __buffer_size
)
4441 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
4442 __buffer_size
, __comp
);
4443 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
4444 __buffer_size
, __comp
);
4448 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
4449 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
4451 std::__merge_adaptive(__first
, __middle
, __last
,
4452 _Distance(__middle
- __first
),
4453 _Distance(__last
- __middle
),
4454 __buffer
, __buffer_size
,
4460 * This is a helper function for the stable sorting routines.
4463 template<typename _RandomAccessIterator
>
4465 __inplace_stable_sort(_RandomAccessIterator __first
,
4466 _RandomAccessIterator __last
)
4468 if (__last
- __first
< 15)
4470 std::__insertion_sort(__first
, __last
);
4473 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
4474 std::__inplace_stable_sort(__first
, __middle
);
4475 std::__inplace_stable_sort(__middle
, __last
);
4476 std::__merge_without_buffer(__first
, __middle
, __last
,
4483 * This is a helper function for the stable sorting routines.
4486 template<typename _RandomAccessIterator
, typename _Compare
>
4488 __inplace_stable_sort(_RandomAccessIterator __first
,
4489 _RandomAccessIterator __last
, _Compare __comp
)
4491 if (__last
- __first
< 15)
4493 std::__insertion_sort(__first
, __last
, __comp
);
4496 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
4497 std::__inplace_stable_sort(__first
, __middle
, __comp
);
4498 std::__inplace_stable_sort(__middle
, __last
, __comp
);
4499 std::__merge_without_buffer(__first
, __middle
, __last
,
4506 * @brief Sort the elements of a sequence, preserving the relative order
4507 * of equivalent elements.
4508 * @param first An iterator.
4509 * @param last Another iterator.
4512 * Sorts the elements in the range @p [first,last) in ascending order,
4513 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4514 * @p [first,last-1).
4516 * The relative ordering of equivalent elements is preserved, so any two
4517 * elements @p x and @p y in the range @p [first,last) such that
4518 * @p x<y is false and @p y<x is false will have the same relative
4519 * ordering after calling @p stable_sort().
4521 template<typename _RandomAccessIterator
>
4523 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4525 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4527 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4530 // concept requirements
4531 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4532 _RandomAccessIterator
>)
4533 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4534 __glibcxx_requires_valid_range(__first
, __last
);
4536 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
4538 if (__buf
.begin() == 0)
4539 std::__inplace_stable_sort(__first
, __last
);
4541 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4542 _DistanceType(__buf
.size()));
4546 * @brief Sort the elements of a sequence using a predicate for comparison,
4547 * preserving the relative order of equivalent elements.
4548 * @param first An iterator.
4549 * @param last Another iterator.
4550 * @param comp A comparison functor.
4553 * Sorts the elements in the range @p [first,last) in ascending order,
4554 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
4555 * range @p [first,last-1).
4557 * The relative ordering of equivalent elements is preserved, so any two
4558 * elements @p x and @p y in the range @p [first,last) such that
4559 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
4560 * relative ordering after calling @p stable_sort().
4562 template<typename _RandomAccessIterator
, typename _Compare
>
4564 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4567 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4569 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4572 // concept requirements
4573 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4574 _RandomAccessIterator
>)
4575 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4578 __glibcxx_requires_valid_range(__first
, __last
);
4580 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
4582 if (__buf
.begin() == 0)
4583 std::__inplace_stable_sort(__first
, __last
, __comp
);
4585 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4586 _DistanceType(__buf
.size()), __comp
);
4589 // Set algorithms: includes, set_union, set_intersection, set_difference,
4590 // set_symmetric_difference. All of these algorithms have the precondition
4591 // that their input ranges are sorted and the postcondition that their output
4592 // ranges are sorted.
4595 * @brief Determines whether all elements of a sequence exists in a range.
4596 * @param first1 Start of search range.
4597 * @param last1 End of search range.
4598 * @param first2 Start of sequence
4599 * @param last2 End of sequence.
4600 * @return True if each element in [first2,last2) is contained in order
4601 * within [first1,last1). False otherwise.
4602 * @ingroup setoperations
4604 * This operation expects both [first1,last1) and [first2,last2) to be
4605 * sorted. Searches for the presence of each element in [first2,last2)
4606 * within [first1,last1). The iterators over each range only move forward,
4607 * so this is a linear algorithm. If an element in [first2,last2) is not
4608 * found before the search iterator reaches @a last2, false is returned.
4610 template<typename _InputIterator1
, typename _InputIterator2
>
4612 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4613 _InputIterator2 __first2
, _InputIterator2 __last2
)
4615 typedef typename iterator_traits
<_InputIterator1
>::value_type
4617 typedef typename iterator_traits
<_InputIterator2
>::value_type
4620 // concept requirements
4621 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4622 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4623 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4624 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4625 __glibcxx_requires_sorted(__first1
, __last1
);
4626 __glibcxx_requires_sorted(__first2
, __last2
);
4628 while (__first1
!= __last1
&& __first2
!= __last2
)
4629 if (*__first2
< *__first1
)
4631 else if(*__first1
< *__first2
)
4634 ++__first1
, ++__first2
;
4636 return __first2
== __last2
;
4640 * @brief Determines whether all elements of a sequence exists in a range
4642 * @param first1 Start of search range.
4643 * @param last1 End of search range.
4644 * @param first2 Start of sequence
4645 * @param last2 End of sequence.
4646 * @param comp Comparison function to use.
4647 * @return True if each element in [first2,last2) is contained in order
4648 * within [first1,last1) according to comp. False otherwise.
4649 * @ingroup setoperations
4651 * This operation expects both [first1,last1) and [first2,last2) to be
4652 * sorted. Searches for the presence of each element in [first2,last2)
4653 * within [first1,last1), using comp to decide. The iterators over each
4654 * range only move forward, so this is a linear algorithm. If an element
4655 * in [first2,last2) is not found before the search iterator reaches @a
4656 * last2, false is returned.
4658 template<typename _InputIterator1
, typename _InputIterator2
,
4661 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4662 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
4664 typedef typename iterator_traits
<_InputIterator1
>::value_type
4666 typedef typename iterator_traits
<_InputIterator2
>::value_type
4669 // concept requirements
4670 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4671 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4672 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4673 _ValueType1
, _ValueType2
>)
4674 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4675 _ValueType2
, _ValueType1
>)
4676 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4677 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4679 while (__first1
!= __last1
&& __first2
!= __last2
)
4680 if (__comp(*__first2
, *__first1
))
4682 else if(__comp(*__first1
, *__first2
))
4685 ++__first1
, ++__first2
;
4687 return __first2
== __last2
;
4691 * @brief Return the union of two sorted ranges.
4692 * @param first1 Start of first range.
4693 * @param last1 End of first range.
4694 * @param first2 Start of second range.
4695 * @param last2 End of second range.
4696 * @return End of the output range.
4697 * @ingroup setoperations
4699 * This operation iterates over both ranges, copying elements present in
4700 * each range in order to the output range. Iterators increment for each
4701 * range. When the current element of one range is less than the other,
4702 * that element is copied and the iterator advanced. If an element is
4703 * contained in both ranges, the element from the first range is copied and
4704 * both ranges advance. The output range may not overlap either input
4707 template<typename _InputIterator1
, typename _InputIterator2
,
4708 typename _OutputIterator
>
4710 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4711 _InputIterator2 __first2
, _InputIterator2 __last2
,
4712 _OutputIterator __result
)
4714 typedef typename iterator_traits
<_InputIterator1
>::value_type
4716 typedef typename iterator_traits
<_InputIterator2
>::value_type
4719 // concept requirements
4720 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4721 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4722 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4724 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4726 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4727 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4728 __glibcxx_requires_sorted(__first1
, __last1
);
4729 __glibcxx_requires_sorted(__first2
, __last2
);
4731 while (__first1
!= __last1
&& __first2
!= __last2
)
4733 if (*__first1
< *__first2
)
4735 *__result
= *__first1
;
4738 else if (*__first2
< *__first1
)
4740 *__result
= *__first2
;
4745 *__result
= *__first1
;
4751 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4756 * @brief Return the union of two sorted ranges using a comparison functor.
4757 * @param first1 Start of first range.
4758 * @param last1 End of first range.
4759 * @param first2 Start of second range.
4760 * @param last2 End of second range.
4761 * @param comp The comparison functor.
4762 * @return End of the output range.
4763 * @ingroup setoperations
4765 * This operation iterates over both ranges, copying elements present in
4766 * each range in order to the output range. Iterators increment for each
4767 * range. When the current element of one range is less than the other
4768 * according to @a comp, that element is copied and the iterator advanced.
4769 * If an equivalent element according to @a comp is contained in both
4770 * ranges, the element from the first range is copied and both ranges
4771 * advance. The output range may not overlap either input range.
4773 template<typename _InputIterator1
, typename _InputIterator2
,
4774 typename _OutputIterator
, typename _Compare
>
4776 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4777 _InputIterator2 __first2
, _InputIterator2 __last2
,
4778 _OutputIterator __result
, _Compare __comp
)
4780 typedef typename iterator_traits
<_InputIterator1
>::value_type
4782 typedef typename iterator_traits
<_InputIterator2
>::value_type
4785 // concept requirements
4786 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4787 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4788 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4790 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4792 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4793 _ValueType1
, _ValueType2
>)
4794 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4795 _ValueType2
, _ValueType1
>)
4796 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4797 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4799 while (__first1
!= __last1
&& __first2
!= __last2
)
4801 if (__comp(*__first1
, *__first2
))
4803 *__result
= *__first1
;
4806 else if (__comp(*__first2
, *__first1
))
4808 *__result
= *__first2
;
4813 *__result
= *__first1
;
4819 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4824 * @brief Return the intersection of two sorted ranges.
4825 * @param first1 Start of first range.
4826 * @param last1 End of first range.
4827 * @param first2 Start of second range.
4828 * @param last2 End of second range.
4829 * @return End of the output range.
4830 * @ingroup setoperations
4832 * This operation iterates over both ranges, copying elements present in
4833 * both ranges in order to the output range. Iterators increment for each
4834 * range. When the current element of one range is less than the other,
4835 * that iterator advances. If an element is contained in both ranges, the
4836 * element from the first range is copied and both ranges advance. The
4837 * output range may not overlap either input range.
4839 template<typename _InputIterator1
, typename _InputIterator2
,
4840 typename _OutputIterator
>
4842 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4843 _InputIterator2 __first2
, _InputIterator2 __last2
,
4844 _OutputIterator __result
)
4846 typedef typename iterator_traits
<_InputIterator1
>::value_type
4848 typedef typename iterator_traits
<_InputIterator2
>::value_type
4851 // concept requirements
4852 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4853 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4854 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4856 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4857 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4858 __glibcxx_requires_sorted(__first1
, __last1
);
4859 __glibcxx_requires_sorted(__first2
, __last2
);
4861 while (__first1
!= __last1
&& __first2
!= __last2
)
4862 if (*__first1
< *__first2
)
4864 else if (*__first2
< *__first1
)
4868 *__result
= *__first1
;
4877 * @brief Return the intersection of two sorted ranges using comparison
4879 * @param first1 Start of first range.
4880 * @param last1 End of first range.
4881 * @param first2 Start of second range.
4882 * @param last2 End of second range.
4883 * @param comp The comparison functor.
4884 * @return End of the output range.
4885 * @ingroup setoperations
4887 * This operation iterates over both ranges, copying elements present in
4888 * both ranges in order to the output range. Iterators increment for each
4889 * range. When the current element of one range is less than the other
4890 * according to @a comp, that iterator advances. If an element is
4891 * contained in both ranges according to @a comp, the element from the
4892 * first range is copied and both ranges advance. The output range may not
4893 * overlap either input range.
4895 template<typename _InputIterator1
, typename _InputIterator2
,
4896 typename _OutputIterator
, typename _Compare
>
4898 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4899 _InputIterator2 __first2
, _InputIterator2 __last2
,
4900 _OutputIterator __result
, _Compare __comp
)
4902 typedef typename iterator_traits
<_InputIterator1
>::value_type
4904 typedef typename iterator_traits
<_InputIterator2
>::value_type
4907 // concept requirements
4908 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4909 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4910 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4912 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4913 _ValueType1
, _ValueType2
>)
4914 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4915 _ValueType2
, _ValueType1
>)
4916 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4917 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4919 while (__first1
!= __last1
&& __first2
!= __last2
)
4920 if (__comp(*__first1
, *__first2
))
4922 else if (__comp(*__first2
, *__first1
))
4926 *__result
= *__first1
;
4935 * @brief Return the difference of two sorted ranges.
4936 * @param first1 Start of first range.
4937 * @param last1 End of first range.
4938 * @param first2 Start of second range.
4939 * @param last2 End of second range.
4940 * @return End of the output range.
4941 * @ingroup setoperations
4943 * This operation iterates over both ranges, copying elements present in
4944 * the first range but not the second in order to the output range.
4945 * Iterators increment for each range. When the current element of the
4946 * first range is less than the second, that element is copied and the
4947 * iterator advances. If the current element of the second range is less,
4948 * the iterator advances, but no element is copied. If an element is
4949 * contained in both ranges, no elements are copied and both ranges
4950 * advance. The output range may not overlap either input range.
4952 template<typename _InputIterator1
, typename _InputIterator2
,
4953 typename _OutputIterator
>
4955 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4956 _InputIterator2 __first2
, _InputIterator2 __last2
,
4957 _OutputIterator __result
)
4959 typedef typename iterator_traits
<_InputIterator1
>::value_type
4961 typedef typename iterator_traits
<_InputIterator2
>::value_type
4964 // concept requirements
4965 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4966 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4967 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4969 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4970 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4971 __glibcxx_requires_sorted(__first1
, __last1
);
4972 __glibcxx_requires_sorted(__first2
, __last2
);
4974 while (__first1
!= __last1
&& __first2
!= __last2
)
4975 if (*__first1
< *__first2
)
4977 *__result
= *__first1
;
4981 else if (*__first2
< *__first1
)
4988 return std::copy(__first1
, __last1
, __result
);
4992 * @brief Return the difference of two sorted ranges using comparison
4994 * @param first1 Start of first range.
4995 * @param last1 End of first range.
4996 * @param first2 Start of second range.
4997 * @param last2 End of second range.
4998 * @param comp The comparison functor.
4999 * @return End of the output range.
5000 * @ingroup setoperations
5002 * This operation iterates over both ranges, copying elements present in
5003 * the first range but not the second in order to the output range.
5004 * Iterators increment for each range. When the current element of the
5005 * first range is less than the second according to @a comp, that element
5006 * is copied and the iterator advances. If the current element of the
5007 * second range is less, no element is copied and the iterator advances.
5008 * If an element is contained in both ranges according to @a comp, no
5009 * elements are copied and both ranges advance. The output range may not
5010 * overlap either input range.
5012 template<typename _InputIterator1
, typename _InputIterator2
,
5013 typename _OutputIterator
, typename _Compare
>
5015 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5016 _InputIterator2 __first2
, _InputIterator2 __last2
,
5017 _OutputIterator __result
, _Compare __comp
)
5019 typedef typename iterator_traits
<_InputIterator1
>::value_type
5021 typedef typename iterator_traits
<_InputIterator2
>::value_type
5024 // concept requirements
5025 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5026 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5027 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5029 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5030 _ValueType1
, _ValueType2
>)
5031 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5032 _ValueType2
, _ValueType1
>)
5033 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5034 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5036 while (__first1
!= __last1
&& __first2
!= __last2
)
5037 if (__comp(*__first1
, *__first2
))
5039 *__result
= *__first1
;
5043 else if (__comp(*__first2
, *__first1
))
5050 return std::copy(__first1
, __last1
, __result
);
5054 * @brief Return the symmetric difference of two sorted ranges.
5055 * @param first1 Start of first range.
5056 * @param last1 End of first range.
5057 * @param first2 Start of second range.
5058 * @param last2 End of second range.
5059 * @return End of the output range.
5060 * @ingroup setoperations
5062 * This operation iterates over both ranges, copying elements present in
5063 * one range but not the other in order to the output range. Iterators
5064 * increment for each range. When the current element of one range is less
5065 * than the other, that element is copied and the iterator advances. If an
5066 * element is contained in both ranges, no elements are copied and both
5067 * ranges advance. The output range may not overlap either input range.
5069 template<typename _InputIterator1
, typename _InputIterator2
,
5070 typename _OutputIterator
>
5072 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5073 _InputIterator2 __first2
, _InputIterator2 __last2
,
5074 _OutputIterator __result
)
5076 typedef typename iterator_traits
<_InputIterator1
>::value_type
5078 typedef typename iterator_traits
<_InputIterator2
>::value_type
5081 // concept requirements
5082 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5083 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5084 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5086 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5088 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5089 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5090 __glibcxx_requires_sorted(__first1
, __last1
);
5091 __glibcxx_requires_sorted(__first2
, __last2
);
5093 while (__first1
!= __last1
&& __first2
!= __last2
)
5094 if (*__first1
< *__first2
)
5096 *__result
= *__first1
;
5100 else if (*__first2
< *__first1
)
5102 *__result
= *__first2
;
5111 return std::copy(__first2
, __last2
, std::copy(__first1
,
5112 __last1
, __result
));
5116 * @brief Return the symmetric difference of two sorted ranges using
5117 * comparison functor.
5118 * @param first1 Start of first range.
5119 * @param last1 End of first range.
5120 * @param first2 Start of second range.
5121 * @param last2 End of second range.
5122 * @param comp The comparison functor.
5123 * @return End of the output range.
5124 * @ingroup setoperations
5126 * This operation iterates over both ranges, copying elements present in
5127 * one range but not the other in order to the output range. Iterators
5128 * increment for each range. When the current element of one range is less
5129 * than the other according to @a comp, that element is copied and the
5130 * iterator advances. If an element is contained in both ranges according
5131 * to @a comp, no elements are copied and both ranges advance. The output
5132 * range may not overlap either input range.
5134 template<typename _InputIterator1
, typename _InputIterator2
,
5135 typename _OutputIterator
, typename _Compare
>
5137 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5138 _InputIterator2 __first2
, _InputIterator2 __last2
,
5139 _OutputIterator __result
,
5142 typedef typename iterator_traits
<_InputIterator1
>::value_type
5144 typedef typename iterator_traits
<_InputIterator2
>::value_type
5147 // concept requirements
5148 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5149 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5150 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5152 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5154 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5155 _ValueType1
, _ValueType2
>)
5156 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5157 _ValueType2
, _ValueType1
>)
5158 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5159 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5161 while (__first1
!= __last1
&& __first2
!= __last2
)
5162 if (__comp(*__first1
, *__first2
))
5164 *__result
= *__first1
;
5168 else if (__comp(*__first2
, *__first1
))
5170 *__result
= *__first2
;
5179 return std::copy(__first2
, __last2
, std::copy(__first1
,
5180 __last1
, __result
));
5183 // min_element and max_element, with and without an explicitly supplied
5184 // comparison function.
5187 * @brief Return the minimum element in a range.
5188 * @param first Start of range.
5189 * @param last End of range.
5190 * @return Iterator referencing the first instance of the smallest value.
5192 template<typename _ForwardIterator
>
5194 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5196 // concept requirements
5197 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5198 __glibcxx_function_requires(_LessThanComparableConcept
<
5199 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5200 __glibcxx_requires_valid_range(__first
, __last
);
5202 if (__first
== __last
)
5204 _ForwardIterator __result
= __first
;
5205 while (++__first
!= __last
)
5206 if (*__first
< *__result
)
5212 * @brief Return the minimum element in a range using comparison functor.
5213 * @param first Start of range.
5214 * @param last End of range.
5215 * @param comp Comparison functor.
5216 * @return Iterator referencing the first instance of the smallest value
5217 * according to comp.
5219 template<typename _ForwardIterator
, typename _Compare
>
5221 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5224 // concept requirements
5225 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5226 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5227 typename iterator_traits
<_ForwardIterator
>::value_type
,
5228 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5229 __glibcxx_requires_valid_range(__first
, __last
);
5231 if (__first
== __last
)
5233 _ForwardIterator __result
= __first
;
5234 while (++__first
!= __last
)
5235 if (__comp(*__first
, *__result
))
5241 * @brief Return the maximum element in a range.
5242 * @param first Start of range.
5243 * @param last End of range.
5244 * @return Iterator referencing the first instance of the largest value.
5246 template<typename _ForwardIterator
>
5248 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5250 // concept requirements
5251 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5252 __glibcxx_function_requires(_LessThanComparableConcept
<
5253 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5254 __glibcxx_requires_valid_range(__first
, __last
);
5256 if (__first
== __last
)
5258 _ForwardIterator __result
= __first
;
5259 while (++__first
!= __last
)
5260 if (*__result
< *__first
)
5266 * @brief Return the maximum element in a range using comparison functor.
5267 * @param first Start of range.
5268 * @param last End of range.
5269 * @param comp Comparison functor.
5270 * @return Iterator referencing the first instance of the largest value
5271 * according to comp.
5273 template<typename _ForwardIterator
, typename _Compare
>
5275 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5278 // concept requirements
5279 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5280 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5281 typename iterator_traits
<_ForwardIterator
>::value_type
,
5282 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5283 __glibcxx_requires_valid_range(__first
, __last
);
5285 if (__first
== __last
) return __first
;
5286 _ForwardIterator __result
= __first
;
5287 while (++__first
!= __last
)
5288 if (__comp(*__result
, *__first
))
5293 // next_permutation and prev_permutation, with and without an explicitly
5294 // supplied comparison function.
5297 * @brief Permute range into the next "dictionary" ordering.
5298 * @param first Start of range.
5299 * @param last End of range.
5300 * @return False if wrapped to first permutation, true otherwise.
5302 * Treats all permutations of the range as a set of "dictionary" sorted
5303 * sequences. Permutes the current sequence into the next one of this set.
5304 * Returns true if there are more sequences to generate. If the sequence
5305 * is the largest of the set, the smallest is generated and false returned.
5307 template<typename _BidirectionalIterator
>
5309 next_permutation(_BidirectionalIterator __first
,
5310 _BidirectionalIterator __last
)
5312 // concept requirements
5313 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5314 _BidirectionalIterator
>)
5315 __glibcxx_function_requires(_LessThanComparableConcept
<
5316 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5317 __glibcxx_requires_valid_range(__first
, __last
);
5319 if (__first
== __last
)
5321 _BidirectionalIterator __i
= __first
;
5330 _BidirectionalIterator __ii
= __i
;
5334 _BidirectionalIterator __j
= __last
;
5335 while (!(*__i
< *--__j
))
5337 std::iter_swap(__i
, __j
);
5338 std::reverse(__ii
, __last
);
5343 std::reverse(__first
, __last
);
5350 * @brief Permute range into the next "dictionary" ordering using
5351 * comparison functor.
5352 * @param first Start of range.
5353 * @param last End of range.
5355 * @return False if wrapped to first permutation, true otherwise.
5357 * Treats all permutations of the range [first,last) as a set of
5358 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5359 * sequence into the next one of this set. Returns true if there are more
5360 * sequences to generate. If the sequence is the largest of the set, the
5361 * smallest is generated and false returned.
5363 template<typename _BidirectionalIterator
, typename _Compare
>
5365 next_permutation(_BidirectionalIterator __first
,
5366 _BidirectionalIterator __last
, _Compare __comp
)
5368 // concept requirements
5369 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5370 _BidirectionalIterator
>)
5371 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5372 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
5373 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5374 __glibcxx_requires_valid_range(__first
, __last
);
5376 if (__first
== __last
)
5378 _BidirectionalIterator __i
= __first
;
5387 _BidirectionalIterator __ii
= __i
;
5389 if (__comp(*__i
, *__ii
))
5391 _BidirectionalIterator __j
= __last
;
5392 while (!bool(__comp(*__i
, *--__j
)))
5394 std::iter_swap(__i
, __j
);
5395 std::reverse(__ii
, __last
);
5400 std::reverse(__first
, __last
);
5407 * @brief Permute range into the previous "dictionary" ordering.
5408 * @param first Start of range.
5409 * @param last End of range.
5410 * @return False if wrapped to last permutation, true otherwise.
5412 * Treats all permutations of the range as a set of "dictionary" sorted
5413 * sequences. Permutes the current sequence into the previous one of this
5414 * set. Returns true if there are more sequences to generate. If the
5415 * sequence is the smallest of the set, the largest is generated and false
5418 template<typename _BidirectionalIterator
>
5420 prev_permutation(_BidirectionalIterator __first
,
5421 _BidirectionalIterator __last
)
5423 // concept requirements
5424 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5425 _BidirectionalIterator
>)
5426 __glibcxx_function_requires(_LessThanComparableConcept
<
5427 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5428 __glibcxx_requires_valid_range(__first
, __last
);
5430 if (__first
== __last
)
5432 _BidirectionalIterator __i
= __first
;
5441 _BidirectionalIterator __ii
= __i
;
5445 _BidirectionalIterator __j
= __last
;
5446 while (!(*--__j
< *__i
))
5448 std::iter_swap(__i
, __j
);
5449 std::reverse(__ii
, __last
);
5454 std::reverse(__first
, __last
);
5461 * @brief Permute range into the previous "dictionary" ordering using
5462 * comparison functor.
5463 * @param first Start of range.
5464 * @param last End of range.
5466 * @return False if wrapped to last permutation, true otherwise.
5468 * Treats all permutations of the range [first,last) as a set of
5469 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5470 * sequence into the previous one of this set. Returns true if there are
5471 * more sequences to generate. If the sequence is the smallest of the set,
5472 * the largest is generated and false returned.
5474 template<typename _BidirectionalIterator
, typename _Compare
>
5476 prev_permutation(_BidirectionalIterator __first
,
5477 _BidirectionalIterator __last
, _Compare __comp
)
5479 // concept requirements
5480 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5481 _BidirectionalIterator
>)
5482 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5483 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
5484 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5485 __glibcxx_requires_valid_range(__first
, __last
);
5487 if (__first
== __last
)
5489 _BidirectionalIterator __i
= __first
;
5498 _BidirectionalIterator __ii
= __i
;
5500 if (__comp(*__ii
, *__i
))
5502 _BidirectionalIterator __j
= __last
;
5503 while (!bool(__comp(*--__j
, *__i
)))
5505 std::iter_swap(__i
, __j
);
5506 std::reverse(__ii
, __last
);
5511 std::reverse(__first
, __last
);
5517 _GLIBCXX_END_NAMESPACE
5519 #endif /* _STL_ALGO_H */