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
);
628 // Test for empty ranges
629 if (__first1
== __last1
|| __first2
== __last2
)
632 // Test for a pattern of length 1.
633 _ForwardIterator2
__p1(__first2
);
634 if (++__p1
== __last2
)
635 return std::find(__first1
, __last1
, *__first2
);
638 _ForwardIterator2 __p
;
639 _ForwardIterator1 __current
= __first1
;
643 __first1
= std::find(__first1
, __last1
, *__first2
);
644 if (__first1
== __last1
)
648 __current
= __first1
;
649 if (++__current
== __last1
)
652 while (*__current
== *__p
)
654 if (++__p
== __last2
)
656 if (++__current
== __last1
)
665 * @brief Search a sequence for a matching sub-sequence using a predicate.
666 * @param first1 A forward iterator.
667 * @param last1 A forward iterator.
668 * @param first2 A forward iterator.
669 * @param last2 A forward iterator.
670 * @param predicate A binary predicate.
671 * @return The first iterator @c i in the range
672 * @p [first1,last1-(last2-first2)) such that
673 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
674 * @p [0,last2-first2), or @p last1 if no such iterator exists.
676 * Searches the range @p [first1,last1) for a sub-sequence that compares
677 * equal value-by-value with the sequence given by @p [first2,last2),
678 * using @p predicate to determine equality, and returns an iterator
679 * to the first element of the sub-sequence, or @p last1 if no such
682 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
684 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
685 typename _BinaryPredicate
>
687 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
688 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
689 _BinaryPredicate __predicate
)
691 // concept requirements
692 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
693 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
694 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
695 typename iterator_traits
<_ForwardIterator1
>::value_type
,
696 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
697 __glibcxx_requires_valid_range(__first1
, __last1
);
698 __glibcxx_requires_valid_range(__first2
, __last2
);
700 // Test for empty ranges
701 if (__first1
== __last1
|| __first2
== __last2
)
704 // Test for a pattern of length 1.
705 _ForwardIterator2
__p1(__first2
);
706 if (++__p1
== __last2
)
708 while (__first1
!= __last1
709 && !bool(__predicate(*__first1
, *__first2
)))
715 _ForwardIterator2 __p
;
716 _ForwardIterator1 __current
= __first1
;
720 while (__first1
!= __last1
721 && !bool(__predicate(*__first1
, *__first2
)))
723 if (__first1
== __last1
)
727 __current
= __first1
;
728 if (++__current
== __last1
)
731 while (__predicate(*__current
, *__p
))
733 if (++__p
== __last2
)
735 if (++__current
== __last1
)
745 * This is an uglified
746 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
747 * overloaded for forward iterators.
750 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
752 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
753 _Integer __count
, const _Tp
& __val
,
754 std::forward_iterator_tag
)
756 __first
= std::find(__first
, __last
, __val
);
757 while (__first
!= __last
)
759 typename iterator_traits
<_ForwardIterator
>::difference_type
761 _ForwardIterator __i
= __first
;
763 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
772 __first
= std::find(++__i
, __last
, __val
);
779 * This is an uglified
780 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
781 * overloaded for random access iterators.
784 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
786 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
787 _Integer __count
, const _Tp
& __val
,
788 std::random_access_iterator_tag
)
791 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
794 _DistanceType __tailSize
= __last
- __first
;
795 const _DistanceType __pattSize
= __count
;
797 if (__tailSize
< __pattSize
)
800 const _DistanceType __skipOffset
= __pattSize
- 1;
801 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
802 __tailSize
-= __pattSize
;
804 while (1) // the main loop...
806 // __lookAhead here is always pointing to the last element of next
808 while (!(*__lookAhead
== __val
)) // the skip loop...
810 if (__tailSize
< __pattSize
)
811 return __last
; // Failure
812 __lookAhead
+= __pattSize
;
813 __tailSize
-= __pattSize
;
815 _DistanceType __remainder
= __skipOffset
;
816 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
817 *__backTrack
== __val
; --__backTrack
)
819 if (--__remainder
== 0)
820 return (__lookAhead
- __skipOffset
); // Success
822 if (__remainder
> __tailSize
)
823 return __last
; // Failure
824 __lookAhead
+= __remainder
;
825 __tailSize
-= __remainder
;
830 * @brief Search a sequence for a number of consecutive values.
831 * @param first A forward iterator.
832 * @param last A forward iterator.
833 * @param count The number of consecutive values.
834 * @param val The value to find.
835 * @return The first iterator @c i in the range @p [first,last-count)
836 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
837 * or @p last if no such iterator exists.
839 * Searches the range @p [first,last) for @p count consecutive elements
842 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
844 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
845 _Integer __count
, const _Tp
& __val
)
847 // concept requirements
848 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
849 __glibcxx_function_requires(_EqualOpConcept
<
850 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
851 __glibcxx_requires_valid_range(__first
, __last
);
856 return std::find(__first
, __last
, __val
);
857 return std::__search_n(__first
, __last
, __count
, __val
,
858 std::__iterator_category(__first
));
863 * This is an uglified
864 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
866 * overloaded for forward iterators.
869 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
870 typename _BinaryPredicate
>
872 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
873 _Integer __count
, const _Tp
& __val
,
874 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
876 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
879 while (__first
!= __last
)
881 typename iterator_traits
<_ForwardIterator
>::difference_type
883 _ForwardIterator __i
= __first
;
885 while (__i
!= __last
&& __n
!= 1 && bool(__binary_pred(*__i
, __val
)))
895 while (__first
!= __last
896 && !bool(__binary_pred(*__first
, __val
)))
904 * This is an uglified
905 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
907 * overloaded for random access iterators.
910 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
911 typename _BinaryPredicate
>
913 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
914 _Integer __count
, const _Tp
& __val
,
915 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
918 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
921 _DistanceType __tailSize
= __last
- __first
;
922 const _DistanceType __pattSize
= __count
;
924 if (__tailSize
< __pattSize
)
927 const _DistanceType __skipOffset
= __pattSize
- 1;
928 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
929 __tailSize
-= __pattSize
;
931 while (1) // the main loop...
933 // __lookAhead here is always pointing to the last element of next
935 while (!bool(__binary_pred(*__lookAhead
, __val
))) // the skip loop...
937 if (__tailSize
< __pattSize
)
938 return __last
; // Failure
939 __lookAhead
+= __pattSize
;
940 __tailSize
-= __pattSize
;
942 _DistanceType __remainder
= __skipOffset
;
943 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
944 __binary_pred(*__backTrack
, __val
); --__backTrack
)
946 if (--__remainder
== 0)
947 return (__lookAhead
- __skipOffset
); // Success
949 if (__remainder
> __tailSize
)
950 return __last
; // Failure
951 __lookAhead
+= __remainder
;
952 __tailSize
-= __remainder
;
957 * @brief Search a sequence for a number of consecutive values using a
959 * @param first A forward iterator.
960 * @param last A forward iterator.
961 * @param count The number of consecutive values.
962 * @param val The value to find.
963 * @param binary_pred A binary predicate.
964 * @return The first iterator @c i in the range @p [first,last-count)
965 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
966 * range @p [0,count), or @p last if no such iterator exists.
968 * Searches the range @p [first,last) for @p count consecutive elements
969 * for which the predicate returns true.
971 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
972 typename _BinaryPredicate
>
974 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
975 _Integer __count
, const _Tp
& __val
,
976 _BinaryPredicate __binary_pred
)
978 // concept requirements
979 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
980 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
981 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
982 __glibcxx_requires_valid_range(__first
, __last
);
988 while (__first
!= __last
&& !bool(__binary_pred(*__first
, __val
)))
992 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
993 std::__iterator_category(__first
));
996 // find_end for forward iterators.
997 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
999 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
1000 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
1001 forward_iterator_tag
, forward_iterator_tag
)
1003 if (__first2
== __last2
)
1007 _ForwardIterator1 __result
= __last1
;
1010 _ForwardIterator1 __new_result
1011 = std::search(__first1
, __last1
, __first2
, __last2
);
1012 if (__new_result
== __last1
)
1016 __result
= __new_result
;
1017 __first1
= __new_result
;
1024 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
1025 typename _BinaryPredicate
>
1027 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
1028 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
1029 forward_iterator_tag
, forward_iterator_tag
,
1030 _BinaryPredicate __comp
)
1032 if (__first2
== __last2
)
1036 _ForwardIterator1 __result
= __last1
;
1039 _ForwardIterator1 __new_result
1040 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
1041 if (__new_result
== __last1
)
1045 __result
= __new_result
;
1046 __first1
= __new_result
;
1053 // find_end for bidirectional iterators (much faster).
1054 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
1055 _BidirectionalIterator1
1056 __find_end(_BidirectionalIterator1 __first1
,
1057 _BidirectionalIterator1 __last1
,
1058 _BidirectionalIterator2 __first2
,
1059 _BidirectionalIterator2 __last2
,
1060 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
1062 // concept requirements
1063 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1064 _BidirectionalIterator1
>)
1065 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1066 _BidirectionalIterator2
>)
1068 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
1069 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
1071 _RevIterator1
__rlast1(__first1
);
1072 _RevIterator2
__rlast2(__first2
);
1073 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
1074 _RevIterator2(__last2
), __rlast2
);
1076 if (__rresult
== __rlast1
)
1080 _BidirectionalIterator1 __result
= __rresult
.base();
1081 std::advance(__result
, -std::distance(__first2
, __last2
));
1086 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
1087 typename _BinaryPredicate
>
1088 _BidirectionalIterator1
1089 __find_end(_BidirectionalIterator1 __first1
,
1090 _BidirectionalIterator1 __last1
,
1091 _BidirectionalIterator2 __first2
,
1092 _BidirectionalIterator2 __last2
,
1093 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
1094 _BinaryPredicate __comp
)
1096 // concept requirements
1097 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1098 _BidirectionalIterator1
>)
1099 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1100 _BidirectionalIterator2
>)
1102 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
1103 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
1105 _RevIterator1
__rlast1(__first1
);
1106 _RevIterator2
__rlast2(__first2
);
1107 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
1108 _RevIterator2(__last2
), __rlast2
,
1111 if (__rresult
== __rlast1
)
1115 _BidirectionalIterator1 __result
= __rresult
.base();
1116 std::advance(__result
, -std::distance(__first2
, __last2
));
1122 * @brief Find last matching subsequence in a sequence.
1123 * @param first1 Start of range to search.
1124 * @param last1 End of range to search.
1125 * @param first2 Start of sequence to match.
1126 * @param last2 End of sequence to match.
1127 * @return The last iterator @c i in the range
1128 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
1129 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
1130 * such iterator exists.
1132 * Searches the range @p [first1,last1) for a sub-sequence that compares
1133 * equal value-by-value with the sequence given by @p [first2,last2) and
1134 * returns an iterator to the first element of the sub-sequence, or
1135 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
1136 * last such subsequence contained in [first,last1).
1138 * Because the sub-sequence must lie completely within the range
1139 * @p [first1,last1) it must start at a position less than
1140 * @p last1-(last2-first2) where @p last2-first2 is the length of the
1142 * This means that the returned iterator @c i will be in the range
1143 * @p [first1,last1-(last2-first2))
1145 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
1146 inline _ForwardIterator1
1147 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
1148 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
1150 // concept requirements
1151 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
1152 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
1153 __glibcxx_function_requires(_EqualOpConcept
<
1154 typename iterator_traits
<_ForwardIterator1
>::value_type
,
1155 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
1156 __glibcxx_requires_valid_range(__first1
, __last1
);
1157 __glibcxx_requires_valid_range(__first2
, __last2
);
1159 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
1160 std::__iterator_category(__first1
),
1161 std::__iterator_category(__first2
));
1165 * @brief Find last matching subsequence in a sequence using a predicate.
1166 * @param first1 Start of range to search.
1167 * @param last1 End of range to search.
1168 * @param first2 Start of sequence to match.
1169 * @param last2 End of sequence to match.
1170 * @param comp The predicate to use.
1171 * @return The last iterator @c i in the range
1172 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
1173 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
1174 * @p last1 if no such iterator exists.
1176 * Searches the range @p [first1,last1) for a sub-sequence that compares
1177 * equal value-by-value with the sequence given by @p [first2,last2) using
1178 * comp as a predicate and returns an iterator to the first element of the
1179 * sub-sequence, or @p last1 if the sub-sequence is not found. The
1180 * sub-sequence will be the last such subsequence contained in
1183 * Because the sub-sequence must lie completely within the range
1184 * @p [first1,last1) it must start at a position less than
1185 * @p last1-(last2-first2) where @p last2-first2 is the length of the
1187 * This means that the returned iterator @c i will be in the range
1188 * @p [first1,last1-(last2-first2))
1190 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
1191 typename _BinaryPredicate
>
1192 inline _ForwardIterator1
1193 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
1194 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
1195 _BinaryPredicate __comp
)
1197 // concept requirements
1198 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
1199 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
1200 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1201 typename iterator_traits
<_ForwardIterator1
>::value_type
,
1202 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
1203 __glibcxx_requires_valid_range(__first1
, __last1
);
1204 __glibcxx_requires_valid_range(__first2
, __last2
);
1206 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
1207 std::__iterator_category(__first1
),
1208 std::__iterator_category(__first2
),
1213 * @brief Perform an operation on a sequence.
1214 * @param first An input iterator.
1215 * @param last An input iterator.
1216 * @param result An output iterator.
1217 * @param unary_op A unary operator.
1218 * @return An output iterator equal to @p result+(last-first).
1220 * Applies the operator to each element in the input range and assigns
1221 * the results to successive elements of the output sequence.
1222 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
1223 * range @p [0,last-first).
1225 * @p unary_op must not alter its argument.
1227 template<typename _InputIterator
, typename _OutputIterator
,
1228 typename _UnaryOperation
>
1230 transform(_InputIterator __first
, _InputIterator __last
,
1231 _OutputIterator __result
, _UnaryOperation __unary_op
)
1233 // concept requirements
1234 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1235 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1236 // "the type returned by a _UnaryOperation"
1237 __typeof__(__unary_op(*__first
))>)
1238 __glibcxx_requires_valid_range(__first
, __last
);
1240 for (; __first
!= __last
; ++__first
, ++__result
)
1241 *__result
= __unary_op(*__first
);
1246 * @brief Perform an operation on corresponding elements of two sequences.
1247 * @param first1 An input iterator.
1248 * @param last1 An input iterator.
1249 * @param first2 An input iterator.
1250 * @param result An output iterator.
1251 * @param binary_op A binary operator.
1252 * @return An output iterator equal to @p result+(last-first).
1254 * Applies the operator to the corresponding elements in the two
1255 * input ranges and assigns the results to successive elements of the
1257 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
1258 * @c N in the range @p [0,last1-first1).
1260 * @p binary_op must not alter either of its arguments.
1262 template<typename _InputIterator1
, typename _InputIterator2
,
1263 typename _OutputIterator
, typename _BinaryOperation
>
1265 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
1266 _InputIterator2 __first2
, _OutputIterator __result
,
1267 _BinaryOperation __binary_op
)
1269 // concept requirements
1270 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1271 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1272 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1273 // "the type returned by a _BinaryOperation"
1274 __typeof__(__binary_op(*__first1
,*__first2
))>)
1275 __glibcxx_requires_valid_range(__first1
, __last1
);
1277 for (; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
1278 *__result
= __binary_op(*__first1
, *__first2
);
1283 * @brief Replace each occurrence of one value in a sequence with another
1285 * @param first A forward iterator.
1286 * @param last A forward iterator.
1287 * @param old_value The value to be replaced.
1288 * @param new_value The replacement value.
1289 * @return replace() returns no value.
1291 * For each iterator @c i in the range @p [first,last) if @c *i ==
1292 * @p old_value then the assignment @c *i = @p new_value is performed.
1294 template<typename _ForwardIterator
, typename _Tp
>
1296 replace(_ForwardIterator __first
, _ForwardIterator __last
,
1297 const _Tp
& __old_value
, const _Tp
& __new_value
)
1299 // concept requirements
1300 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1302 __glibcxx_function_requires(_EqualOpConcept
<
1303 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1304 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
1305 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1306 __glibcxx_requires_valid_range(__first
, __last
);
1308 for (; __first
!= __last
; ++__first
)
1309 if (*__first
== __old_value
)
1310 *__first
= __new_value
;
1314 * @brief Replace each value in a sequence for which a predicate returns
1315 * true with another value.
1316 * @param first A forward iterator.
1317 * @param last A forward iterator.
1318 * @param pred A predicate.
1319 * @param new_value The replacement value.
1320 * @return replace_if() returns no value.
1322 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
1323 * is true then the assignment @c *i = @p new_value is performed.
1325 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
1327 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
1328 _Predicate __pred
, const _Tp
& __new_value
)
1330 // concept requirements
1331 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1333 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
1334 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1335 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1336 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1337 __glibcxx_requires_valid_range(__first
, __last
);
1339 for (; __first
!= __last
; ++__first
)
1340 if (__pred(*__first
))
1341 *__first
= __new_value
;
1345 * @brief Copy a sequence, replacing each element of one value with another
1347 * @param first An input iterator.
1348 * @param last An input iterator.
1349 * @param result An output iterator.
1350 * @param old_value The value to be replaced.
1351 * @param new_value The replacement value.
1352 * @return The end of the output sequence, @p result+(last-first).
1354 * Copies each element in the input range @p [first,last) to the
1355 * output range @p [result,result+(last-first)) replacing elements
1356 * equal to @p old_value with @p new_value.
1358 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1360 replace_copy(_InputIterator __first
, _InputIterator __last
,
1361 _OutputIterator __result
,
1362 const _Tp
& __old_value
, const _Tp
& __new_value
)
1364 // concept requirements
1365 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1366 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1367 typename iterator_traits
<_InputIterator
>::value_type
>)
1368 __glibcxx_function_requires(_EqualOpConcept
<
1369 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1370 __glibcxx_requires_valid_range(__first
, __last
);
1372 for (; __first
!= __last
; ++__first
, ++__result
)
1373 if (*__first
== __old_value
)
1374 *__result
= __new_value
;
1376 *__result
= *__first
;
1381 * @brief Copy a sequence, replacing each value for which a predicate
1382 * returns true with another value.
1383 * @param first An input iterator.
1384 * @param last An input iterator.
1385 * @param result An output iterator.
1386 * @param pred A predicate.
1387 * @param new_value The replacement value.
1388 * @return The end of the output sequence, @p result+(last-first).
1390 * Copies each element in the range @p [first,last) to the range
1391 * @p [result,result+(last-first)) replacing elements for which
1392 * @p pred returns true with @p new_value.
1394 template<typename _InputIterator
, typename _OutputIterator
,
1395 typename _Predicate
, typename _Tp
>
1397 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
1398 _OutputIterator __result
,
1399 _Predicate __pred
, const _Tp
& __new_value
)
1401 // concept requirements
1402 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1403 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1404 typename iterator_traits
<_InputIterator
>::value_type
>)
1405 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1406 typename iterator_traits
<_InputIterator
>::value_type
>)
1407 __glibcxx_requires_valid_range(__first
, __last
);
1409 for (; __first
!= __last
; ++__first
, ++__result
)
1410 if (__pred(*__first
))
1411 *__result
= __new_value
;
1413 *__result
= *__first
;
1418 * @brief Assign the result of a function object to each value in a
1420 * @param first A forward iterator.
1421 * @param last A forward iterator.
1422 * @param gen A function object taking no arguments.
1423 * @return generate() returns no value.
1425 * Performs the assignment @c *i = @p gen() for each @c i in the range
1428 template<typename _ForwardIterator
, typename _Generator
>
1430 generate(_ForwardIterator __first
, _ForwardIterator __last
,
1433 // concept requirements
1434 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1435 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
1436 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1437 __glibcxx_requires_valid_range(__first
, __last
);
1439 for (; __first
!= __last
; ++__first
)
1444 * @brief Assign the result of a function object to each value in a
1446 * @param first A forward iterator.
1447 * @param n The length of the sequence.
1448 * @param gen A function object taking no arguments.
1449 * @return The end of the sequence, @p first+n
1451 * Performs the assignment @c *i = @p gen() for each @c i in the range
1452 * @p [first,first+n).
1454 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
1456 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
1458 // concept requirements
1459 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1460 // "the type returned by a _Generator"
1461 __typeof__(__gen())>)
1463 for (; __n
> 0; --__n
, ++__first
)
1469 * @brief Copy a sequence, removing elements of a given value.
1470 * @param first An input iterator.
1471 * @param last An input iterator.
1472 * @param result An output iterator.
1473 * @param value The value to be removed.
1474 * @return An iterator designating the end of the resulting sequence.
1476 * Copies each element in the range @p [first,last) not equal to @p value
1477 * to the range beginning at @p result.
1478 * remove_copy() is stable, so the relative order of elements that are
1479 * copied is unchanged.
1481 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1483 remove_copy(_InputIterator __first
, _InputIterator __last
,
1484 _OutputIterator __result
, const _Tp
& __value
)
1486 // concept requirements
1487 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1488 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1489 typename iterator_traits
<_InputIterator
>::value_type
>)
1490 __glibcxx_function_requires(_EqualOpConcept
<
1491 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1492 __glibcxx_requires_valid_range(__first
, __last
);
1494 for (; __first
!= __last
; ++__first
)
1495 if (!(*__first
== __value
))
1497 *__result
= *__first
;
1504 * @brief Copy a sequence, removing elements for which a predicate is true.
1505 * @param first An input iterator.
1506 * @param last An input iterator.
1507 * @param result An output iterator.
1508 * @param pred A predicate.
1509 * @return An iterator designating the end of the resulting sequence.
1511 * Copies each element in the range @p [first,last) for which
1512 * @p pred returns true to the range beginning at @p result.
1514 * remove_copy_if() is stable, so the relative order of elements that are
1515 * copied is unchanged.
1517 template<typename _InputIterator
, typename _OutputIterator
,
1518 typename _Predicate
>
1520 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
1521 _OutputIterator __result
, _Predicate __pred
)
1523 // concept requirements
1524 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1525 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1526 typename iterator_traits
<_InputIterator
>::value_type
>)
1527 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1528 typename iterator_traits
<_InputIterator
>::value_type
>)
1529 __glibcxx_requires_valid_range(__first
, __last
);
1531 for (; __first
!= __last
; ++__first
)
1532 if (!bool(__pred(*__first
)))
1534 *__result
= *__first
;
1541 * @brief Remove elements from a sequence.
1542 * @param first An input iterator.
1543 * @param last An input iterator.
1544 * @param value The value to be removed.
1545 * @return An iterator designating the end of the resulting sequence.
1547 * All elements equal to @p value are removed from the range
1550 * remove() is stable, so the relative order of elements that are
1551 * not removed is unchanged.
1553 * Elements between the end of the resulting sequence and @p last
1554 * are still present, but their value is unspecified.
1556 template<typename _ForwardIterator
, typename _Tp
>
1558 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1561 // concept requirements
1562 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1564 __glibcxx_function_requires(_EqualOpConcept
<
1565 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1566 __glibcxx_requires_valid_range(__first
, __last
);
1568 __first
= std::find(__first
, __last
, __value
);
1569 _ForwardIterator __i
= __first
;
1570 return __first
== __last
? __first
1571 : std::remove_copy(++__i
, __last
,
1576 * @brief Remove elements from a sequence using a predicate.
1577 * @param first A forward iterator.
1578 * @param last A forward iterator.
1579 * @param pred A predicate.
1580 * @return An iterator designating the end of the resulting sequence.
1582 * All elements for which @p pred returns true are removed from the range
1585 * remove_if() is stable, so the relative order of elements that are
1586 * not removed is unchanged.
1588 * Elements between the end of the resulting sequence and @p last
1589 * are still present, but their value is unspecified.
1591 template<typename _ForwardIterator
, typename _Predicate
>
1593 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1596 // concept requirements
1597 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1599 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1600 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1601 __glibcxx_requires_valid_range(__first
, __last
);
1603 __first
= std::find_if(__first
, __last
, __pred
);
1604 _ForwardIterator __i
= __first
;
1605 return __first
== __last
? __first
1606 : std::remove_copy_if(++__i
, __last
,
1611 * @brief Remove consecutive duplicate values from a sequence.
1612 * @param first A forward iterator.
1613 * @param last A forward iterator.
1614 * @return An iterator designating the end of the resulting sequence.
1616 * Removes all but the first element from each group of consecutive
1617 * values that compare equal.
1618 * unique() is stable, so the relative order of elements that are
1619 * not removed is unchanged.
1620 * Elements between the end of the resulting sequence and @p last
1621 * are still present, but their value is unspecified.
1623 template<typename _ForwardIterator
>
1625 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1627 // concept requirements
1628 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1630 __glibcxx_function_requires(_EqualityComparableConcept
<
1631 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1632 __glibcxx_requires_valid_range(__first
, __last
);
1634 // Skip the beginning, if already unique.
1635 __first
= std::adjacent_find(__first
, __last
);
1636 if (__first
== __last
)
1639 // Do the real copy work.
1640 _ForwardIterator __dest
= __first
;
1642 while (++__first
!= __last
)
1643 if (!(*__dest
== *__first
))
1644 *++__dest
= *__first
;
1649 * @brief Remove consecutive values from a sequence using a predicate.
1650 * @param first A forward iterator.
1651 * @param last A forward iterator.
1652 * @param binary_pred A binary predicate.
1653 * @return An iterator designating the end of the resulting sequence.
1655 * Removes all but the first element from each group of consecutive
1656 * values for which @p binary_pred returns true.
1657 * unique() is stable, so the relative order of elements that are
1658 * not removed is unchanged.
1659 * Elements between the end of the resulting sequence and @p last
1660 * are still present, but their value is unspecified.
1662 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1664 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1665 _BinaryPredicate __binary_pred
)
1667 // concept requirements
1668 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1670 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1671 typename iterator_traits
<_ForwardIterator
>::value_type
,
1672 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1673 __glibcxx_requires_valid_range(__first
, __last
);
1675 // Skip the beginning, if already unique.
1676 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1677 if (__first
== __last
)
1680 // Do the real copy work.
1681 _ForwardIterator __dest
= __first
;
1683 while (++__first
!= __last
)
1684 if (!bool(__binary_pred(*__dest
, *__first
)))
1685 *++__dest
= *__first
;
1691 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1693 * overloaded for forward iterators and output iterator as result.
1696 template<typename _ForwardIterator
, typename _OutputIterator
>
1698 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1699 _OutputIterator __result
,
1700 forward_iterator_tag
, output_iterator_tag
)
1702 // concept requirements -- taken care of in dispatching function
1703 _ForwardIterator __next
= __first
;
1704 *__result
= *__first
;
1705 while (++__next
!= __last
)
1706 if (!(*__first
== *__next
))
1709 *++__result
= *__first
;
1716 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1718 * overloaded for input iterators and output iterator as result.
1721 template<typename _InputIterator
, typename _OutputIterator
>
1723 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1724 _OutputIterator __result
,
1725 input_iterator_tag
, output_iterator_tag
)
1727 // concept requirements -- taken care of in dispatching function
1728 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1729 *__result
= __value
;
1730 while (++__first
!= __last
)
1731 if (!(__value
== *__first
))
1734 *++__result
= __value
;
1741 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1743 * overloaded for input iterators and forward iterator as result.
1746 template<typename _InputIterator
, typename _ForwardIterator
>
1748 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1749 _ForwardIterator __result
,
1750 input_iterator_tag
, forward_iterator_tag
)
1752 // concept requirements -- taken care of in dispatching function
1753 *__result
= *__first
;
1754 while (++__first
!= __last
)
1755 if (!(*__result
== *__first
))
1756 *++__result
= *__first
;
1762 * This is an uglified
1763 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1765 * overloaded for forward iterators and output iterator as result.
1768 template<typename _ForwardIterator
, typename _OutputIterator
,
1769 typename _BinaryPredicate
>
1771 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1772 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1773 forward_iterator_tag
, output_iterator_tag
)
1775 // concept requirements -- iterators already checked
1776 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1777 typename iterator_traits
<_ForwardIterator
>::value_type
,
1778 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1780 _ForwardIterator __next
= __first
;
1781 *__result
= *__first
;
1782 while (++__next
!= __last
)
1783 if (!bool(__binary_pred(*__first
, *__next
)))
1786 *++__result
= *__first
;
1793 * This is an uglified
1794 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1796 * overloaded for input iterators and output iterator as result.
1799 template<typename _InputIterator
, typename _OutputIterator
,
1800 typename _BinaryPredicate
>
1802 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1803 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1804 input_iterator_tag
, output_iterator_tag
)
1806 // concept requirements -- iterators already checked
1807 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1808 typename iterator_traits
<_InputIterator
>::value_type
,
1809 typename iterator_traits
<_InputIterator
>::value_type
>)
1811 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1812 *__result
= __value
;
1813 while (++__first
!= __last
)
1814 if (!bool(__binary_pred(__value
, *__first
)))
1817 *++__result
= __value
;
1824 * This is an uglified
1825 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1827 * overloaded for input iterators and forward iterator as result.
1830 template<typename _InputIterator
, typename _ForwardIterator
,
1831 typename _BinaryPredicate
>
1833 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1834 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1835 input_iterator_tag
, forward_iterator_tag
)
1837 // concept requirements -- iterators already checked
1838 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1839 typename iterator_traits
<_ForwardIterator
>::value_type
,
1840 typename iterator_traits
<_InputIterator
>::value_type
>)
1842 *__result
= *__first
;
1843 while (++__first
!= __last
)
1844 if (!bool(__binary_pred(*__result
, *__first
)))
1845 *++__result
= *__first
;
1850 * @brief Copy a sequence, removing consecutive duplicate values.
1851 * @param first An input iterator.
1852 * @param last An input iterator.
1853 * @param result An output iterator.
1854 * @return An iterator designating the end of the resulting sequence.
1856 * Copies each element in the range @p [first,last) to the range
1857 * beginning at @p result, except that only the first element is copied
1858 * from groups of consecutive elements that compare equal.
1859 * unique_copy() is stable, so the relative order of elements that are
1860 * copied is unchanged.
1863 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1864 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1866 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1867 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
1871 template<typename _InputIterator
, typename _OutputIterator
>
1872 inline _OutputIterator
1873 unique_copy(_InputIterator __first
, _InputIterator __last
,
1874 _OutputIterator __result
)
1876 // concept requirements
1877 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1878 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1879 typename iterator_traits
<_InputIterator
>::value_type
>)
1880 __glibcxx_function_requires(_EqualityComparableConcept
<
1881 typename iterator_traits
<_InputIterator
>::value_type
>)
1882 __glibcxx_requires_valid_range(__first
, __last
);
1884 if (__first
== __last
)
1886 return std::__unique_copy(__first
, __last
, __result
,
1887 std::__iterator_category(__first
),
1888 std::__iterator_category(__result
));
1892 * @brief Copy a sequence, removing consecutive values using a predicate.
1893 * @param first An input iterator.
1894 * @param last An input iterator.
1895 * @param result An output iterator.
1896 * @param binary_pred A binary predicate.
1897 * @return An iterator designating the end of the resulting sequence.
1899 * Copies each element in the range @p [first,last) to the range
1900 * beginning at @p result, except that only the first element is copied
1901 * from groups of consecutive elements for which @p binary_pred returns
1903 * unique_copy() is stable, so the relative order of elements that are
1904 * copied is unchanged.
1907 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1908 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1911 template<typename _InputIterator
, typename _OutputIterator
,
1912 typename _BinaryPredicate
>
1913 inline _OutputIterator
1914 unique_copy(_InputIterator __first
, _InputIterator __last
,
1915 _OutputIterator __result
,
1916 _BinaryPredicate __binary_pred
)
1918 // concept requirements -- predicates checked later
1919 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1920 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1921 typename iterator_traits
<_InputIterator
>::value_type
>)
1922 __glibcxx_requires_valid_range(__first
, __last
);
1924 if (__first
== __last
)
1926 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
1927 std::__iterator_category(__first
),
1928 std::__iterator_category(__result
));
1933 * This is an uglified reverse(_BidirectionalIterator,
1934 * _BidirectionalIterator)
1935 * overloaded for bidirectional iterators.
1938 template<typename _BidirectionalIterator
>
1940 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1941 bidirectional_iterator_tag
)
1944 if (__first
== __last
|| __first
== --__last
)
1948 std::iter_swap(__first
, __last
);
1955 * This is an uglified reverse(_BidirectionalIterator,
1956 * _BidirectionalIterator)
1957 * overloaded for random access iterators.
1960 template<typename _RandomAccessIterator
>
1962 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1963 random_access_iterator_tag
)
1965 if (__first
== __last
)
1968 while (__first
< __last
)
1970 std::iter_swap(__first
, __last
);
1977 * @brief Reverse a sequence.
1978 * @param first A bidirectional iterator.
1979 * @param last A bidirectional iterator.
1980 * @return reverse() returns no value.
1982 * Reverses the order of the elements in the range @p [first,last),
1983 * so that the first element becomes the last etc.
1984 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1985 * swaps @p *(first+i) and @p *(last-(i+1))
1987 template<typename _BidirectionalIterator
>
1989 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1991 // concept requirements
1992 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1993 _BidirectionalIterator
>)
1994 __glibcxx_requires_valid_range(__first
, __last
);
1995 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1999 * @brief Copy a sequence, reversing its elements.
2000 * @param first A bidirectional iterator.
2001 * @param last A bidirectional iterator.
2002 * @param result An output iterator.
2003 * @return An iterator designating the end of the resulting sequence.
2005 * Copies the elements in the range @p [first,last) to the range
2006 * @p [result,result+(last-first)) such that the order of the
2007 * elements is reversed.
2008 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
2009 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
2010 * The ranges @p [first,last) and @p [result,result+(last-first))
2013 template<typename _BidirectionalIterator
, typename _OutputIterator
>
2015 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
2016 _OutputIterator __result
)
2018 // concept requirements
2019 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
2020 _BidirectionalIterator
>)
2021 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2022 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
2023 __glibcxx_requires_valid_range(__first
, __last
);
2025 while (__first
!= __last
)
2028 *__result
= *__last
;
2036 * This is a helper function for the rotate algorithm specialized on RAIs.
2037 * It returns the greatest common divisor of two integer values.
2040 template<typename _EuclideanRingElement
>
2041 _EuclideanRingElement
2042 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
2046 _EuclideanRingElement __t
= __m
% __n
;
2055 * This is a helper function for the rotate algorithm.
2058 template<typename _ForwardIterator
>
2060 __rotate(_ForwardIterator __first
,
2061 _ForwardIterator __middle
,
2062 _ForwardIterator __last
,
2063 forward_iterator_tag
)
2065 if (__first
== __middle
|| __last
== __middle
)
2068 _ForwardIterator __first2
= __middle
;
2071 swap(*__first
, *__first2
);
2074 if (__first
== __middle
)
2075 __middle
= __first2
;
2077 while (__first2
!= __last
);
2079 __first2
= __middle
;
2081 while (__first2
!= __last
)
2083 swap(*__first
, *__first2
);
2086 if (__first
== __middle
)
2087 __middle
= __first2
;
2088 else if (__first2
== __last
)
2089 __first2
= __middle
;
2095 * This is a helper function for the rotate algorithm.
2098 template<typename _BidirectionalIterator
>
2100 __rotate(_BidirectionalIterator __first
,
2101 _BidirectionalIterator __middle
,
2102 _BidirectionalIterator __last
,
2103 bidirectional_iterator_tag
)
2105 // concept requirements
2106 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
2107 _BidirectionalIterator
>)
2109 if (__first
== __middle
|| __last
== __middle
)
2112 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
2113 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
2115 while (__first
!= __middle
&& __middle
!= __last
)
2117 swap(*__first
, *--__last
);
2121 if (__first
== __middle
)
2122 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
2124 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
2129 * This is a helper function for the rotate algorithm.
2132 template<typename _RandomAccessIterator
>
2134 __rotate(_RandomAccessIterator __first
,
2135 _RandomAccessIterator __middle
,
2136 _RandomAccessIterator __last
,
2137 random_access_iterator_tag
)
2139 // concept requirements
2140 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2141 _RandomAccessIterator
>)
2143 if (__first
== __middle
|| __last
== __middle
)
2146 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2148 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2151 const _Distance __n
= __last
- __first
;
2152 const _Distance __k
= __middle
- __first
;
2153 const _Distance __l
= __n
- __k
;
2157 std::swap_ranges(__first
, __middle
, __middle
);
2161 const _Distance __d
= std::__gcd(__n
, __k
);
2163 for (_Distance __i
= 0; __i
< __d
; __i
++)
2165 _ValueType __tmp
= *__first
;
2166 _RandomAccessIterator __p
= __first
;
2170 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
2172 if (__p
> __first
+ __l
)
2174 *__p
= *(__p
- __l
);
2178 *__p
= *(__p
+ __k
);
2184 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
2186 if (__p
< __last
- __k
)
2188 *__p
= *(__p
+ __k
);
2191 *__p
= * (__p
- __l
);
2202 * @brief Rotate the elements of a sequence.
2203 * @param first A forward iterator.
2204 * @param middle A forward iterator.
2205 * @param last A forward iterator.
2208 * Rotates the elements of the range @p [first,last) by @p (middle-first)
2209 * positions so that the element at @p middle is moved to @p first, the
2210 * element at @p middle+1 is moved to @first+1 and so on for each element
2211 * in the range @p [first,last).
2213 * This effectively swaps the ranges @p [first,middle) and
2216 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
2217 * each @p n in the range @p [0,last-first).
2219 template<typename _ForwardIterator
>
2221 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
2222 _ForwardIterator __last
)
2224 // concept requirements
2225 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2227 __glibcxx_requires_valid_range(__first
, __middle
);
2228 __glibcxx_requires_valid_range(__middle
, __last
);
2230 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
2232 std::__rotate(__first
, __middle
, __last
, _IterType());
2236 * @brief Copy a sequence, rotating its elements.
2237 * @param first A forward iterator.
2238 * @param middle A forward iterator.
2239 * @param last A forward iterator.
2240 * @param result An output iterator.
2241 * @return An iterator designating the end of the resulting sequence.
2243 * Copies the elements of the range @p [first,last) to the range
2244 * beginning at @result, rotating the copied elements by @p (middle-first)
2245 * positions so that the element at @p middle is moved to @p result, the
2246 * element at @p middle+1 is moved to @result+1 and so on for each element
2247 * in the range @p [first,last).
2249 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
2250 * each @p n in the range @p [0,last-first).
2252 template<typename _ForwardIterator
, typename _OutputIterator
>
2254 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
2255 _ForwardIterator __last
, _OutputIterator __result
)
2257 // concept requirements
2258 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2259 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2260 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2261 __glibcxx_requires_valid_range(__first
, __middle
);
2262 __glibcxx_requires_valid_range(__middle
, __last
);
2264 return std::copy(__first
, __middle
,
2265 std::copy(__middle
, __last
, __result
));
2269 * @brief Randomly shuffle the elements of a sequence.
2270 * @param first A forward iterator.
2271 * @param last A forward iterator.
2274 * Reorder the elements in the range @p [first,last) using a random
2275 * distribution, so that every possible ordering of the sequence is
2278 template<typename _RandomAccessIterator
>
2280 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2282 // concept requirements
2283 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2284 _RandomAccessIterator
>)
2285 __glibcxx_requires_valid_range(__first
, __last
);
2287 if (__first
!= __last
)
2288 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2289 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
2293 * @brief Shuffle the elements of a sequence using a random number
2295 * @param first A forward iterator.
2296 * @param last A forward iterator.
2297 * @param rand The RNG functor or function.
2300 * Reorders the elements in the range @p [first,last) using @p rand to
2301 * provide a random distribution. Calling @p rand(N) for a positive
2302 * integer @p N should return a randomly chosen integer from the
2305 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
2307 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2308 _RandomNumberGenerator
& __rand
)
2310 // concept requirements
2311 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2312 _RandomAccessIterator
>)
2313 __glibcxx_requires_valid_range(__first
, __last
);
2315 if (__first
== __last
)
2317 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2318 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
2324 * This is a helper function...
2327 template<typename _ForwardIterator
, typename _Predicate
>
2329 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
2331 forward_iterator_tag
)
2333 if (__first
== __last
)
2336 while (__pred(*__first
))
2337 if (++__first
== __last
)
2340 _ForwardIterator __next
= __first
;
2342 while (++__next
!= __last
)
2343 if (__pred(*__next
))
2345 swap(*__first
, *__next
);
2354 * This is a helper function...
2357 template<typename _BidirectionalIterator
, typename _Predicate
>
2358 _BidirectionalIterator
2359 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
2361 bidirectional_iterator_tag
)
2366 if (__first
== __last
)
2368 else if (__pred(*__first
))
2374 if (__first
== __last
)
2376 else if (!bool(__pred(*__last
)))
2380 std::iter_swap(__first
, __last
);
2386 * @brief Move elements for which a predicate is true to the beginning
2388 * @param first A forward iterator.
2389 * @param last A forward iterator.
2390 * @param pred A predicate functor.
2391 * @return An iterator @p middle such that @p pred(i) is true for each
2392 * iterator @p i in the range @p [first,middle) and false for each @p i
2393 * in the range @p [middle,last).
2395 * @p pred must not modify its operand. @p partition() does not preserve
2396 * the relative ordering of elements in each group, use
2397 * @p stable_partition() if this is needed.
2399 template<typename _ForwardIterator
, typename _Predicate
>
2400 inline _ForwardIterator
2401 partition(_ForwardIterator __first
, _ForwardIterator __last
,
2404 // concept requirements
2405 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2407 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
2408 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2409 __glibcxx_requires_valid_range(__first
, __last
);
2411 return std::__partition(__first
, __last
, __pred
,
2412 std::__iterator_category(__first
));
2418 * This is a helper function...
2421 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
2423 __inplace_stable_partition(_ForwardIterator __first
,
2424 _ForwardIterator __last
,
2425 _Predicate __pred
, _Distance __len
)
2428 return __pred(*__first
) ? __last
: __first
;
2429 _ForwardIterator __middle
= __first
;
2430 std::advance(__middle
, __len
/ 2);
2431 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
2435 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
2439 std::rotate(__begin
, __middle
, __end
);
2440 std::advance(__begin
, std::distance(__middle
, __end
));
2446 * This is a helper function...
2449 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
2452 __stable_partition_adaptive(_ForwardIterator __first
,
2453 _ForwardIterator __last
,
2454 _Predicate __pred
, _Distance __len
,
2456 _Distance __buffer_size
)
2458 if (__len
<= __buffer_size
)
2460 _ForwardIterator __result1
= __first
;
2461 _Pointer __result2
= __buffer
;
2462 for (; __first
!= __last
; ++__first
)
2463 if (__pred(*__first
))
2465 *__result1
= *__first
;
2470 *__result2
= *__first
;
2473 std::copy(__buffer
, __result2
, __result1
);
2478 _ForwardIterator __middle
= __first
;
2479 std::advance(__middle
, __len
/ 2);
2480 _ForwardIterator __begin
=
2481 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
2482 __len
/ 2, __buffer
,
2484 _ForwardIterator __end
=
2485 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
2487 __buffer
, __buffer_size
);
2488 std::rotate(__begin
, __middle
, __end
);
2489 std::advance(__begin
, std::distance(__middle
, __end
));
2495 * @brief Move elements for which a predicate is true to the beginning
2496 * of a sequence, preserving relative ordering.
2497 * @param first A forward iterator.
2498 * @param last A forward iterator.
2499 * @param pred A predicate functor.
2500 * @return An iterator @p middle such that @p pred(i) is true for each
2501 * iterator @p i in the range @p [first,middle) and false for each @p i
2502 * in the range @p [middle,last).
2504 * Performs the same function as @p partition() with the additional
2505 * guarantee that the relative ordering of elements in each group is
2506 * preserved, so any two elements @p x and @p y in the range
2507 * @p [first,last) such that @p pred(x)==pred(y) will have the same
2508 * relative ordering after calling @p stable_partition().
2510 template<typename _ForwardIterator
, typename _Predicate
>
2512 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
2515 // concept requirements
2516 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2518 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
2519 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2520 __glibcxx_requires_valid_range(__first
, __last
);
2522 if (__first
== __last
)
2526 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2528 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2531 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
2533 if (__buf
.size() > 0)
2535 std::__stable_partition_adaptive(__first
, __last
, __pred
,
2536 _DistanceType(__buf
.requested_size()),
2538 _DistanceType(__buf
.size()));
2541 std::__inplace_stable_partition(__first
, __last
, __pred
,
2542 _DistanceType(__buf
.requested_size()));
2548 * This is a helper function for the sort routines.
2551 template<typename _RandomAccessIterator
>
2553 __heap_select(_RandomAccessIterator __first
,
2554 _RandomAccessIterator __middle
,
2555 _RandomAccessIterator __last
)
2557 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2560 std::make_heap(__first
, __middle
);
2561 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2562 if (*__i
< *__first
)
2563 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2568 * This is a helper function for the sort routines.
2571 template<typename _RandomAccessIterator
, typename _Compare
>
2573 __heap_select(_RandomAccessIterator __first
,
2574 _RandomAccessIterator __middle
,
2575 _RandomAccessIterator __last
, _Compare __comp
)
2577 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2580 std::make_heap(__first
, __middle
, __comp
);
2581 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2582 if (__comp(*__i
, *__first
))
2583 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2587 * @brief Sort the smallest elements of a sequence.
2588 * @param first An iterator.
2589 * @param middle Another iterator.
2590 * @param last Another iterator.
2593 * Sorts the smallest @p (middle-first) elements in the range
2594 * @p [first,last) and moves them to the range @p [first,middle). The
2595 * order of the remaining elements in the range @p [middle,last) is
2597 * After the sort if @p i and @j are iterators in the range
2598 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2599 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2601 template<typename _RandomAccessIterator
>
2603 partial_sort(_RandomAccessIterator __first
,
2604 _RandomAccessIterator __middle
,
2605 _RandomAccessIterator __last
)
2607 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2610 // concept requirements
2611 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2612 _RandomAccessIterator
>)
2613 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2614 __glibcxx_requires_valid_range(__first
, __middle
);
2615 __glibcxx_requires_valid_range(__middle
, __last
);
2617 std::__heap_select(__first
, __middle
, __last
);
2618 std::sort_heap(__first
, __middle
);
2622 * @brief Sort the smallest elements of a sequence using a predicate
2624 * @param first An iterator.
2625 * @param middle Another iterator.
2626 * @param last Another iterator.
2627 * @param comp A comparison functor.
2630 * Sorts the smallest @p (middle-first) elements in the range
2631 * @p [first,last) and moves them to the range @p [first,middle). The
2632 * order of the remaining elements in the range @p [middle,last) is
2634 * After the sort if @p i and @j are iterators in the range
2635 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2636 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2639 template<typename _RandomAccessIterator
, typename _Compare
>
2641 partial_sort(_RandomAccessIterator __first
,
2642 _RandomAccessIterator __middle
,
2643 _RandomAccessIterator __last
,
2646 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2649 // concept requirements
2650 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2651 _RandomAccessIterator
>)
2652 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2653 _ValueType
, _ValueType
>)
2654 __glibcxx_requires_valid_range(__first
, __middle
);
2655 __glibcxx_requires_valid_range(__middle
, __last
);
2657 std::__heap_select(__first
, __middle
, __last
, __comp
);
2658 std::sort_heap(__first
, __middle
, __comp
);
2662 * @brief Copy the smallest elements of a sequence.
2663 * @param first An iterator.
2664 * @param last Another iterator.
2665 * @param result_first A random-access iterator.
2666 * @param result_last Another random-access iterator.
2667 * @return An iterator indicating the end of the resulting sequence.
2669 * Copies and sorts the smallest N values from the range @p [first,last)
2670 * to the range beginning at @p result_first, where the number of
2671 * elements to be copied, @p N, is the smaller of @p (last-first) and
2672 * @p (result_last-result_first).
2673 * After the sort if @p i and @j are iterators in the range
2674 * @p [result_first,result_first+N) such that @i precedes @j then
2675 * @p *j<*i is false.
2676 * The value returned is @p result_first+N.
2678 template<typename _InputIterator
, typename _RandomAccessIterator
>
2679 _RandomAccessIterator
2680 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2681 _RandomAccessIterator __result_first
,
2682 _RandomAccessIterator __result_last
)
2684 typedef typename iterator_traits
<_InputIterator
>::value_type
2686 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2688 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2691 // concept requirements
2692 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2693 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2695 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
2697 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2698 __glibcxx_requires_valid_range(__first
, __last
);
2699 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2701 if (__result_first
== __result_last
)
2702 return __result_last
;
2703 _RandomAccessIterator __result_real_last
= __result_first
;
2704 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2706 *__result_real_last
= *__first
;
2707 ++__result_real_last
;
2710 std::make_heap(__result_first
, __result_real_last
);
2711 while (__first
!= __last
)
2713 if (*__first
< *__result_first
)
2714 std::__adjust_heap(__result_first
, _DistanceType(0),
2715 _DistanceType(__result_real_last
2717 _InputValueType(*__first
));
2720 std::sort_heap(__result_first
, __result_real_last
);
2721 return __result_real_last
;
2725 * @brief Copy the smallest elements of a sequence using a predicate for
2727 * @param first An input iterator.
2728 * @param last Another input iterator.
2729 * @param result_first A random-access iterator.
2730 * @param result_last Another random-access iterator.
2731 * @param comp A comparison functor.
2732 * @return An iterator indicating the end of the resulting sequence.
2734 * Copies and sorts the smallest N values from the range @p [first,last)
2735 * to the range beginning at @p result_first, where the number of
2736 * elements to be copied, @p N, is the smaller of @p (last-first) and
2737 * @p (result_last-result_first).
2738 * After the sort if @p i and @j are iterators in the range
2739 * @p [result_first,result_first+N) such that @i precedes @j then
2740 * @p comp(*j,*i) is false.
2741 * The value returned is @p result_first+N.
2743 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2744 _RandomAccessIterator
2745 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2746 _RandomAccessIterator __result_first
,
2747 _RandomAccessIterator __result_last
,
2750 typedef typename iterator_traits
<_InputIterator
>::value_type
2752 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2754 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2757 // concept requirements
2758 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2759 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2760 _RandomAccessIterator
>)
2761 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2763 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2764 _InputValueType
, _OutputValueType
>)
2765 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2766 _OutputValueType
, _OutputValueType
>)
2767 __glibcxx_requires_valid_range(__first
, __last
);
2768 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2770 if (__result_first
== __result_last
)
2771 return __result_last
;
2772 _RandomAccessIterator __result_real_last
= __result_first
;
2773 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2775 *__result_real_last
= *__first
;
2776 ++__result_real_last
;
2779 std::make_heap(__result_first
, __result_real_last
, __comp
);
2780 while (__first
!= __last
)
2782 if (__comp(*__first
, *__result_first
))
2783 std::__adjust_heap(__result_first
, _DistanceType(0),
2784 _DistanceType(__result_real_last
2786 _InputValueType(*__first
),
2790 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2791 return __result_real_last
;
2796 * This is a helper function for the sort routine.
2799 template<typename _RandomAccessIterator
, typename _Tp
>
2801 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2803 _RandomAccessIterator __next
= __last
;
2805 while (__val
< *__next
)
2816 * This is a helper function for the sort routine.
2819 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2821 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2824 _RandomAccessIterator __next
= __last
;
2826 while (__comp(__val
, *__next
))
2837 * This is a helper function for the sort routine.
2840 template<typename _RandomAccessIterator
>
2842 __insertion_sort(_RandomAccessIterator __first
,
2843 _RandomAccessIterator __last
)
2845 if (__first
== __last
)
2848 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2850 typename iterator_traits
<_RandomAccessIterator
>::value_type
2852 if (__val
< *__first
)
2854 std::copy_backward(__first
, __i
, __i
+ 1);
2858 std::__unguarded_linear_insert(__i
, __val
);
2864 * This is a helper function for the sort routine.
2867 template<typename _RandomAccessIterator
, typename _Compare
>
2869 __insertion_sort(_RandomAccessIterator __first
,
2870 _RandomAccessIterator __last
, _Compare __comp
)
2872 if (__first
== __last
) return;
2874 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2876 typename iterator_traits
<_RandomAccessIterator
>::value_type
2878 if (__comp(__val
, *__first
))
2880 std::copy_backward(__first
, __i
, __i
+ 1);
2884 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2890 * This is a helper function for the sort routine.
2893 template<typename _RandomAccessIterator
>
2895 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2896 _RandomAccessIterator __last
)
2898 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2901 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2902 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2907 * This is a helper function for the sort routine.
2910 template<typename _RandomAccessIterator
, typename _Compare
>
2912 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2913 _RandomAccessIterator __last
, _Compare __comp
)
2915 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2918 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2919 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2925 * This controls some aspect of the sort routines.
2928 enum { _S_threshold
= 16 };
2932 * This is a helper function for the sort routine.
2935 template<typename _RandomAccessIterator
>
2937 __final_insertion_sort(_RandomAccessIterator __first
,
2938 _RandomAccessIterator __last
)
2940 if (__last
- __first
> int(_S_threshold
))
2942 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2943 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2946 std::__insertion_sort(__first
, __last
);
2951 * This is a helper function for the sort routine.
2954 template<typename _RandomAccessIterator
, typename _Compare
>
2956 __final_insertion_sort(_RandomAccessIterator __first
,
2957 _RandomAccessIterator __last
, _Compare __comp
)
2959 if (__last
- __first
> int(_S_threshold
))
2961 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2962 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2966 std::__insertion_sort(__first
, __last
, __comp
);
2971 * This is a helper function...
2974 template<typename _RandomAccessIterator
, typename _Tp
>
2975 _RandomAccessIterator
2976 __unguarded_partition(_RandomAccessIterator __first
,
2977 _RandomAccessIterator __last
, _Tp __pivot
)
2981 while (*__first
< __pivot
)
2984 while (__pivot
< *__last
)
2986 if (!(__first
< __last
))
2988 std::iter_swap(__first
, __last
);
2995 * This is a helper function...
2998 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2999 _RandomAccessIterator
3000 __unguarded_partition(_RandomAccessIterator __first
,
3001 _RandomAccessIterator __last
,
3002 _Tp __pivot
, _Compare __comp
)
3006 while (__comp(*__first
, __pivot
))
3009 while (__comp(__pivot
, *__last
))
3011 if (!(__first
< __last
))
3013 std::iter_swap(__first
, __last
);
3020 * This is a helper function for the sort routine.
3023 template<typename _RandomAccessIterator
, typename _Size
>
3025 __introsort_loop(_RandomAccessIterator __first
,
3026 _RandomAccessIterator __last
,
3027 _Size __depth_limit
)
3029 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3032 while (__last
- __first
> int(_S_threshold
))
3034 if (__depth_limit
== 0)
3036 std::partial_sort(__first
, __last
, __last
);
3040 _RandomAccessIterator __cut
=
3041 std::__unguarded_partition(__first
, __last
,
3042 _ValueType(std::__median(*__first
,
3049 std::__introsort_loop(__cut
, __last
, __depth_limit
);
3056 * This is a helper function for the sort routine.
3059 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
3061 __introsort_loop(_RandomAccessIterator __first
,
3062 _RandomAccessIterator __last
,
3063 _Size __depth_limit
, _Compare __comp
)
3065 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3068 while (__last
- __first
> int(_S_threshold
))
3070 if (__depth_limit
== 0)
3072 std::partial_sort(__first
, __last
, __last
, __comp
);
3076 _RandomAccessIterator __cut
=
3077 std::__unguarded_partition(__first
, __last
,
3078 _ValueType(std::__median(*__first
,
3086 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
3093 * This is a helper function for the sort routines.
3096 template<typename _Size
>
3101 for (__k
= 0; __n
!= 1; __n
>>= 1)
3107 * @brief Sort the elements of a sequence.
3108 * @param first An iterator.
3109 * @param last Another iterator.
3112 * Sorts the elements in the range @p [first,last) in ascending order,
3113 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3114 * @p [first,last-1).
3116 * The relative ordering of equivalent elements is not preserved, use
3117 * @p stable_sort() if this is needed.
3119 template<typename _RandomAccessIterator
>
3121 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3123 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3126 // concept requirements
3127 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3128 _RandomAccessIterator
>)
3129 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3130 __glibcxx_requires_valid_range(__first
, __last
);
3132 if (__first
!= __last
)
3134 std::__introsort_loop(__first
, __last
,
3135 std::__lg(__last
- __first
) * 2);
3136 std::__final_insertion_sort(__first
, __last
);
3141 * @brief Sort the elements of a sequence using a predicate for comparison.
3142 * @param first An iterator.
3143 * @param last Another iterator.
3144 * @param comp A comparison functor.
3147 * Sorts the elements in the range @p [first,last) in ascending order,
3148 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
3149 * range @p [first,last-1).
3151 * The relative ordering of equivalent elements is not preserved, use
3152 * @p stable_sort() if this is needed.
3154 template<typename _RandomAccessIterator
, typename _Compare
>
3156 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3159 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3162 // concept requirements
3163 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3164 _RandomAccessIterator
>)
3165 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
3167 __glibcxx_requires_valid_range(__first
, __last
);
3169 if (__first
!= __last
)
3171 std::__introsort_loop(__first
, __last
,
3172 std::__lg(__last
- __first
) * 2, __comp
);
3173 std::__final_insertion_sort(__first
, __last
, __comp
);
3177 template<typename _RandomAccessIterator
, typename _Size
>
3179 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3180 _RandomAccessIterator __last
, _Size __depth_limit
)
3182 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3185 while (__last
- __first
> 3)
3187 if (__depth_limit
== 0)
3189 std::__heap_select(__first
, __nth
+ 1, __last
);
3190 // Place the nth largest element in its final position.
3191 std::iter_swap(__first
, __nth
);
3195 _RandomAccessIterator __cut
=
3196 std::__unguarded_partition(__first
, __last
,
3197 _ValueType(std::__median(*__first
,
3209 std::__insertion_sort(__first
, __last
);
3212 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
3214 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3215 _RandomAccessIterator __last
, _Size __depth_limit
,
3218 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3221 while (__last
- __first
> 3)
3223 if (__depth_limit
== 0)
3225 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
3226 // Place the nth largest element in its final position.
3227 std::iter_swap(__first
, __nth
);
3231 _RandomAccessIterator __cut
=
3232 std::__unguarded_partition(__first
, __last
,
3233 _ValueType(std::__median(*__first
,
3246 std::__insertion_sort(__first
, __last
, __comp
);
3250 * @brief Sort a sequence just enough to find a particular position.
3251 * @param first An iterator.
3252 * @param nth Another iterator.
3253 * @param last Another iterator.
3256 * Rearranges the elements in the range @p [first,last) so that @p *nth
3257 * is the same element that would have been in that position had the
3258 * whole sequence been sorted.
3259 * whole sequence been sorted. The elements either side of @p *nth are
3260 * not completely sorted, but for any iterator @i in the range
3261 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3262 * holds that @p *j<*i is false.
3264 template<typename _RandomAccessIterator
>
3266 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3267 _RandomAccessIterator __last
)
3269 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3272 // concept requirements
3273 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3274 _RandomAccessIterator
>)
3275 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3276 __glibcxx_requires_valid_range(__first
, __nth
);
3277 __glibcxx_requires_valid_range(__nth
, __last
);
3279 if (__first
== __last
|| __nth
== __last
)
3282 std::__introselect(__first
, __nth
, __last
,
3283 std::__lg(__last
- __first
) * 2);
3287 * @brief Sort a sequence just enough to find a particular position
3288 * using a predicate for comparison.
3289 * @param first An iterator.
3290 * @param nth Another iterator.
3291 * @param last Another iterator.
3292 * @param comp A comparison functor.
3295 * Rearranges the elements in the range @p [first,last) so that @p *nth
3296 * is the same element that would have been in that position had the
3297 * whole sequence been sorted. The elements either side of @p *nth are
3298 * not completely sorted, but for any iterator @i in the range
3299 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3300 * holds that @p comp(*j,*i) is false.
3302 template<typename _RandomAccessIterator
, typename _Compare
>
3304 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3305 _RandomAccessIterator __last
, _Compare __comp
)
3307 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3310 // concept requirements
3311 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3312 _RandomAccessIterator
>)
3313 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3314 _ValueType
, _ValueType
>)
3315 __glibcxx_requires_valid_range(__first
, __nth
);
3316 __glibcxx_requires_valid_range(__nth
, __last
);
3318 if (__first
== __last
|| __nth
== __last
)
3321 std::__introselect(__first
, __nth
, __last
,
3322 std::__lg(__last
- __first
) * 2, __comp
);
3326 * @brief Finds the first position in which @a val could be inserted
3327 * without changing the ordering.
3328 * @param first An iterator.
3329 * @param last Another iterator.
3330 * @param val The search term.
3331 * @return An iterator pointing to the first element "not less than" @a val,
3332 * or end() if every element is less than @a val.
3333 * @ingroup binarysearch
3335 template<typename _ForwardIterator
, typename _Tp
>
3337 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
3340 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3342 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3345 // concept requirements
3346 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3347 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
3348 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3350 _DistanceType __len
= std::distance(__first
, __last
);
3351 _DistanceType __half
;
3352 _ForwardIterator __middle
;
3356 __half
= __len
>> 1;
3358 std::advance(__middle
, __half
);
3359 if (*__middle
< __val
)
3363 __len
= __len
- __half
- 1;
3372 * @brief Finds the first position in which @a val could be inserted
3373 * without changing the ordering.
3374 * @param first An iterator.
3375 * @param last Another iterator.
3376 * @param val The search term.
3377 * @param comp A functor to use for comparisons.
3378 * @return An iterator pointing to the first element "not less than" @a val,
3379 * or end() if every element is less than @a val.
3380 * @ingroup binarysearch
3382 * The comparison function should have the same effects on ordering as
3383 * the function used for the initial sort.
3385 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3387 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
3388 const _Tp
& __val
, _Compare __comp
)
3390 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3392 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3395 // concept requirements
3396 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3397 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3399 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3401 _DistanceType __len
= std::distance(__first
, __last
);
3402 _DistanceType __half
;
3403 _ForwardIterator __middle
;
3407 __half
= __len
>> 1;
3409 std::advance(__middle
, __half
);
3410 if (__comp(*__middle
, __val
))
3414 __len
= __len
- __half
- 1;
3423 * @brief Finds the last position in which @a val could be inserted
3424 * without changing the ordering.
3425 * @param first An iterator.
3426 * @param last Another iterator.
3427 * @param val The search term.
3428 * @return An iterator pointing to the first element greater than @a val,
3429 * or end() if no elements are greater than @a val.
3430 * @ingroup binarysearch
3432 template<typename _ForwardIterator
, typename _Tp
>
3434 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
3437 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3439 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3442 // concept requirements
3443 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3444 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
3445 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3447 _DistanceType __len
= std::distance(__first
, __last
);
3448 _DistanceType __half
;
3449 _ForwardIterator __middle
;
3453 __half
= __len
>> 1;
3455 std::advance(__middle
, __half
);
3456 if (__val
< *__middle
)
3462 __len
= __len
- __half
- 1;
3469 * @brief Finds the last position in which @a val could be inserted
3470 * without changing the ordering.
3471 * @param first An iterator.
3472 * @param last Another iterator.
3473 * @param val The search term.
3474 * @param comp A functor to use for comparisons.
3475 * @return An iterator pointing to the first element greater than @a val,
3476 * or end() if no elements are greater than @a val.
3477 * @ingroup binarysearch
3479 * The comparison function should have the same effects on ordering as
3480 * the function used for the initial sort.
3482 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3484 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
3485 const _Tp
& __val
, _Compare __comp
)
3487 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3489 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3492 // concept requirements
3493 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3494 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3496 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3498 _DistanceType __len
= std::distance(__first
, __last
);
3499 _DistanceType __half
;
3500 _ForwardIterator __middle
;
3504 __half
= __len
>> 1;
3506 std::advance(__middle
, __half
);
3507 if (__comp(__val
, *__middle
))
3513 __len
= __len
- __half
- 1;
3520 * @brief Finds the largest subrange in which @a val could be inserted
3521 * at any place in it without changing the ordering.
3522 * @param first An iterator.
3523 * @param last Another iterator.
3524 * @param val The search term.
3525 * @return An pair of iterators defining the subrange.
3526 * @ingroup binarysearch
3528 * This is equivalent to
3530 * std::make_pair(lower_bound(first, last, val),
3531 * upper_bound(first, last, val))
3533 * but does not actually call those functions.
3535 template<typename _ForwardIterator
, typename _Tp
>
3536 pair
<_ForwardIterator
, _ForwardIterator
>
3537 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3540 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3542 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3545 // concept requirements
3546 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3547 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
3548 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
3549 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3551 _DistanceType __len
= std::distance(__first
, __last
);
3552 _DistanceType __half
;
3553 _ForwardIterator __middle
, __left
, __right
;
3557 __half
= __len
>> 1;
3559 std::advance(__middle
, __half
);
3560 if (*__middle
< __val
)
3564 __len
= __len
- __half
- 1;
3566 else if (__val
< *__middle
)
3570 __left
= std::lower_bound(__first
, __middle
, __val
);
3571 std::advance(__first
, __len
);
3572 __right
= std::upper_bound(++__middle
, __first
, __val
);
3573 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3576 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3580 * @brief Finds the largest subrange in which @a val could be inserted
3581 * at any place in it without changing the ordering.
3582 * @param first An iterator.
3583 * @param last Another iterator.
3584 * @param val The search term.
3585 * @param comp A functor to use for comparisons.
3586 * @return An pair of iterators defining the subrange.
3587 * @ingroup binarysearch
3589 * This is equivalent to
3591 * std::make_pair(lower_bound(first, last, val, comp),
3592 * upper_bound(first, last, val, comp))
3594 * but does not actually call those functions.
3596 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3597 pair
<_ForwardIterator
, _ForwardIterator
>
3598 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3602 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3604 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3607 // concept requirements
3608 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3609 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3611 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3613 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3615 _DistanceType __len
= std::distance(__first
, __last
);
3616 _DistanceType __half
;
3617 _ForwardIterator __middle
, __left
, __right
;
3621 __half
= __len
>> 1;
3623 std::advance(__middle
, __half
);
3624 if (__comp(*__middle
, __val
))
3628 __len
= __len
- __half
- 1;
3630 else if (__comp(__val
, *__middle
))
3634 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
3635 std::advance(__first
, __len
);
3636 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
3637 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3640 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3644 * @brief Determines whether an element exists in a range.
3645 * @param first An iterator.
3646 * @param last Another iterator.
3647 * @param val The search term.
3648 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3649 * @ingroup binarysearch
3651 * Note that this does not actually return an iterator to @a val. For
3652 * that, use std::find or a container's specialized find member functions.
3654 template<typename _ForwardIterator
, typename _Tp
>
3656 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3659 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3662 // concept requirements
3663 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3664 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
3665 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3667 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
3668 return __i
!= __last
&& !(__val
< *__i
);
3672 * @brief Determines whether an element exists in a range.
3673 * @param first An iterator.
3674 * @param last Another iterator.
3675 * @param val The search term.
3676 * @param comp A functor to use for comparisons.
3677 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3678 * @ingroup binarysearch
3680 * Note that this does not actually return an iterator to @a val. For
3681 * that, use std::find or a container's specialized find member functions.
3683 * The comparison function should have the same effects on ordering as
3684 * the function used for the initial sort.
3686 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3688 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3689 const _Tp
& __val
, _Compare __comp
)
3691 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3694 // concept requirements
3695 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3696 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3698 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3700 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
3701 return __i
!= __last
&& !bool(__comp(__val
, *__i
));
3705 * @brief Merges two sorted ranges.
3706 * @param first1 An iterator.
3707 * @param first2 Another iterator.
3708 * @param last1 Another iterator.
3709 * @param last2 Another iterator.
3710 * @param result An iterator pointing to the end of the merged range.
3711 * @return An iterator pointing to the first element "not less than" @a val.
3713 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3714 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3715 * must be sorted, and the output range must not overlap with either of
3716 * the input ranges. The sort is @e stable, that is, for equivalent
3717 * elements in the two ranges, elements from the first range will always
3718 * come before elements from the second.
3720 template<typename _InputIterator1
, typename _InputIterator2
,
3721 typename _OutputIterator
>
3723 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3724 _InputIterator2 __first2
, _InputIterator2 __last2
,
3725 _OutputIterator __result
)
3727 typedef typename iterator_traits
<_InputIterator1
>::value_type
3729 typedef typename iterator_traits
<_InputIterator2
>::value_type
3732 // concept requirements
3733 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3734 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3735 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3737 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3739 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3740 __glibcxx_requires_sorted(__first1
, __last1
);
3741 __glibcxx_requires_sorted(__first2
, __last2
);
3743 while (__first1
!= __last1
&& __first2
!= __last2
)
3745 if (*__first2
< *__first1
)
3747 *__result
= *__first2
;
3752 *__result
= *__first1
;
3757 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3762 * @brief Merges two sorted ranges.
3763 * @param first1 An iterator.
3764 * @param first2 Another iterator.
3765 * @param last1 Another iterator.
3766 * @param last2 Another iterator.
3767 * @param result An iterator pointing to the end of the merged range.
3768 * @param comp A functor to use for comparisons.
3769 * @return An iterator pointing to the first element "not less than" @a val.
3771 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3772 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3773 * must be sorted, and the output range must not overlap with either of
3774 * the input ranges. The sort is @e stable, that is, for equivalent
3775 * elements in the two ranges, elements from the first range will always
3776 * come before elements from the second.
3778 * The comparison function should have the same effects on ordering as
3779 * the function used for the initial sort.
3781 template<typename _InputIterator1
, typename _InputIterator2
,
3782 typename _OutputIterator
, typename _Compare
>
3784 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3785 _InputIterator2 __first2
, _InputIterator2 __last2
,
3786 _OutputIterator __result
, _Compare __comp
)
3788 typedef typename iterator_traits
<_InputIterator1
>::value_type
3790 typedef typename iterator_traits
<_InputIterator2
>::value_type
3793 // concept requirements
3794 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3795 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3796 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3798 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3800 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3801 _ValueType2
, _ValueType1
>)
3802 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3803 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3805 while (__first1
!= __last1
&& __first2
!= __last2
)
3807 if (__comp(*__first2
, *__first1
))
3809 *__result
= *__first2
;
3814 *__result
= *__first1
;
3819 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3825 * This is a helper function for the merge routines.
3828 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3829 typename _BidirectionalIterator3
>
3830 _BidirectionalIterator3
3831 __merge_backward(_BidirectionalIterator1 __first1
,
3832 _BidirectionalIterator1 __last1
,
3833 _BidirectionalIterator2 __first2
,
3834 _BidirectionalIterator2 __last2
,
3835 _BidirectionalIterator3 __result
)
3837 if (__first1
== __last1
)
3838 return std::copy_backward(__first2
, __last2
, __result
);
3839 if (__first2
== __last2
)
3840 return std::copy_backward(__first1
, __last1
, __result
);
3845 if (*__last2
< *__last1
)
3847 *--__result
= *__last1
;
3848 if (__first1
== __last1
)
3849 return std::copy_backward(__first2
, ++__last2
, __result
);
3854 *--__result
= *__last2
;
3855 if (__first2
== __last2
)
3856 return std::copy_backward(__first1
, ++__last1
, __result
);
3864 * This is a helper function for the merge routines.
3867 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3868 typename _BidirectionalIterator3
, typename _Compare
>
3869 _BidirectionalIterator3
3870 __merge_backward(_BidirectionalIterator1 __first1
,
3871 _BidirectionalIterator1 __last1
,
3872 _BidirectionalIterator2 __first2
,
3873 _BidirectionalIterator2 __last2
,
3874 _BidirectionalIterator3 __result
,
3877 if (__first1
== __last1
)
3878 return std::copy_backward(__first2
, __last2
, __result
);
3879 if (__first2
== __last2
)
3880 return std::copy_backward(__first1
, __last1
, __result
);
3885 if (__comp(*__last2
, *__last1
))
3887 *--__result
= *__last1
;
3888 if (__first1
== __last1
)
3889 return std::copy_backward(__first2
, ++__last2
, __result
);
3894 *--__result
= *__last2
;
3895 if (__first2
== __last2
)
3896 return std::copy_backward(__first1
, ++__last1
, __result
);
3904 * This is a helper function for the merge routines.
3907 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3909 _BidirectionalIterator1
3910 __rotate_adaptive(_BidirectionalIterator1 __first
,
3911 _BidirectionalIterator1 __middle
,
3912 _BidirectionalIterator1 __last
,
3913 _Distance __len1
, _Distance __len2
,
3914 _BidirectionalIterator2 __buffer
,
3915 _Distance __buffer_size
)
3917 _BidirectionalIterator2 __buffer_end
;
3918 if (__len1
> __len2
&& __len2
<= __buffer_size
)
3920 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3921 std::copy_backward(__first
, __middle
, __last
);
3922 return std::copy(__buffer
, __buffer_end
, __first
);
3924 else if (__len1
<= __buffer_size
)
3926 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3927 std::copy(__middle
, __last
, __first
);
3928 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3932 std::rotate(__first
, __middle
, __last
);
3933 std::advance(__first
, std::distance(__middle
, __last
));
3940 * This is a helper function for the merge routines.
3943 template<typename _BidirectionalIterator
, typename _Distance
,
3946 __merge_adaptive(_BidirectionalIterator __first
,
3947 _BidirectionalIterator __middle
,
3948 _BidirectionalIterator __last
,
3949 _Distance __len1
, _Distance __len2
,
3950 _Pointer __buffer
, _Distance __buffer_size
)
3952 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3954 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3955 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3957 else if (__len2
<= __buffer_size
)
3959 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3960 std::__merge_backward(__first
, __middle
, __buffer
,
3961 __buffer_end
, __last
);
3965 _BidirectionalIterator __first_cut
= __first
;
3966 _BidirectionalIterator __second_cut
= __middle
;
3967 _Distance __len11
= 0;
3968 _Distance __len22
= 0;
3969 if (__len1
> __len2
)
3971 __len11
= __len1
/ 2;
3972 std::advance(__first_cut
, __len11
);
3973 __second_cut
= std::lower_bound(__middle
, __last
,
3975 __len22
= std::distance(__middle
, __second_cut
);
3979 __len22
= __len2
/ 2;
3980 std::advance(__second_cut
, __len22
);
3981 __first_cut
= std::upper_bound(__first
, __middle
,
3983 __len11
= std::distance(__first
, __first_cut
);
3985 _BidirectionalIterator __new_middle
=
3986 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3987 __len1
- __len11
, __len22
, __buffer
,
3989 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3990 __len22
, __buffer
, __buffer_size
);
3991 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3993 __len2
- __len22
, __buffer
, __buffer_size
);
3999 * This is a helper function for the merge routines.
4002 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
4005 __merge_adaptive(_BidirectionalIterator __first
,
4006 _BidirectionalIterator __middle
,
4007 _BidirectionalIterator __last
,
4008 _Distance __len1
, _Distance __len2
,
4009 _Pointer __buffer
, _Distance __buffer_size
,
4012 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
4014 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
4015 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
4017 else if (__len2
<= __buffer_size
)
4019 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
4020 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
4025 _BidirectionalIterator __first_cut
= __first
;
4026 _BidirectionalIterator __second_cut
= __middle
;
4027 _Distance __len11
= 0;
4028 _Distance __len22
= 0;
4029 if (__len1
> __len2
)
4031 __len11
= __len1
/ 2;
4032 std::advance(__first_cut
, __len11
);
4033 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
4035 __len22
= std::distance(__middle
, __second_cut
);
4039 __len22
= __len2
/ 2;
4040 std::advance(__second_cut
, __len22
);
4041 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
4043 __len11
= std::distance(__first
, __first_cut
);
4045 _BidirectionalIterator __new_middle
=
4046 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
4047 __len1
- __len11
, __len22
, __buffer
,
4049 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
4050 __len22
, __buffer
, __buffer_size
, __comp
);
4051 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
4053 __len2
- __len22
, __buffer
,
4054 __buffer_size
, __comp
);
4060 * This is a helper function for the merge routines.
4063 template<typename _BidirectionalIterator
, typename _Distance
>
4065 __merge_without_buffer(_BidirectionalIterator __first
,
4066 _BidirectionalIterator __middle
,
4067 _BidirectionalIterator __last
,
4068 _Distance __len1
, _Distance __len2
)
4070 if (__len1
== 0 || __len2
== 0)
4072 if (__len1
+ __len2
== 2)
4074 if (*__middle
< *__first
)
4075 std::iter_swap(__first
, __middle
);
4078 _BidirectionalIterator __first_cut
= __first
;
4079 _BidirectionalIterator __second_cut
= __middle
;
4080 _Distance __len11
= 0;
4081 _Distance __len22
= 0;
4082 if (__len1
> __len2
)
4084 __len11
= __len1
/ 2;
4085 std::advance(__first_cut
, __len11
);
4086 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
4087 __len22
= std::distance(__middle
, __second_cut
);
4091 __len22
= __len2
/ 2;
4092 std::advance(__second_cut
, __len22
);
4093 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
4094 __len11
= std::distance(__first
, __first_cut
);
4096 std::rotate(__first_cut
, __middle
, __second_cut
);
4097 _BidirectionalIterator __new_middle
= __first_cut
;
4098 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
4099 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
4101 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
4102 __len1
- __len11
, __len2
- __len22
);
4107 * This is a helper function for the merge routines.
4110 template<typename _BidirectionalIterator
, typename _Distance
,
4113 __merge_without_buffer(_BidirectionalIterator __first
,
4114 _BidirectionalIterator __middle
,
4115 _BidirectionalIterator __last
,
4116 _Distance __len1
, _Distance __len2
,
4119 if (__len1
== 0 || __len2
== 0)
4121 if (__len1
+ __len2
== 2)
4123 if (__comp(*__middle
, *__first
))
4124 std::iter_swap(__first
, __middle
);
4127 _BidirectionalIterator __first_cut
= __first
;
4128 _BidirectionalIterator __second_cut
= __middle
;
4129 _Distance __len11
= 0;
4130 _Distance __len22
= 0;
4131 if (__len1
> __len2
)
4133 __len11
= __len1
/ 2;
4134 std::advance(__first_cut
, __len11
);
4135 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
4137 __len22
= std::distance(__middle
, __second_cut
);
4141 __len22
= __len2
/ 2;
4142 std::advance(__second_cut
, __len22
);
4143 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
4145 __len11
= std::distance(__first
, __first_cut
);
4147 std::rotate(__first_cut
, __middle
, __second_cut
);
4148 _BidirectionalIterator __new_middle
= __first_cut
;
4149 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
4150 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
4151 __len11
, __len22
, __comp
);
4152 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
4153 __len1
- __len11
, __len2
- __len22
, __comp
);
4157 * @brief Merges two sorted ranges in place.
4158 * @param first An iterator.
4159 * @param middle Another iterator.
4160 * @param last Another iterator.
4163 * Merges two sorted and consecutive ranges, [first,middle) and
4164 * [middle,last), and puts the result in [first,last). The output will
4165 * be sorted. The sort is @e stable, that is, for equivalent
4166 * elements in the two ranges, elements from the first range will always
4167 * come before elements from the second.
4169 * If enough additional memory is available, this takes (last-first)-1
4170 * comparisons. Otherwise an NlogN algorithm is used, where N is
4171 * distance(first,last).
4173 template<typename _BidirectionalIterator
>
4175 inplace_merge(_BidirectionalIterator __first
,
4176 _BidirectionalIterator __middle
,
4177 _BidirectionalIterator __last
)
4179 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
4181 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
4184 // concept requirements
4185 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
4186 _BidirectionalIterator
>)
4187 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4188 __glibcxx_requires_sorted(__first
, __middle
);
4189 __glibcxx_requires_sorted(__middle
, __last
);
4191 if (__first
== __middle
|| __middle
== __last
)
4194 _DistanceType __len1
= std::distance(__first
, __middle
);
4195 _DistanceType __len2
= std::distance(__middle
, __last
);
4197 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
4199 if (__buf
.begin() == 0)
4200 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
4202 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
4203 __buf
.begin(), _DistanceType(__buf
.size()));
4207 * @brief Merges two sorted ranges in place.
4208 * @param first An iterator.
4209 * @param middle Another iterator.
4210 * @param last Another iterator.
4211 * @param comp A functor to use for comparisons.
4214 * Merges two sorted and consecutive ranges, [first,middle) and
4215 * [middle,last), and puts the result in [first,last). The output will
4216 * be sorted. The sort is @e stable, that is, for equivalent
4217 * elements in the two ranges, elements from the first range will always
4218 * come before elements from the second.
4220 * If enough additional memory is available, this takes (last-first)-1
4221 * comparisons. Otherwise an NlogN algorithm is used, where N is
4222 * distance(first,last).
4224 * The comparison function should have the same effects on ordering as
4225 * the function used for the initial sort.
4227 template<typename _BidirectionalIterator
, typename _Compare
>
4229 inplace_merge(_BidirectionalIterator __first
,
4230 _BidirectionalIterator __middle
,
4231 _BidirectionalIterator __last
,
4234 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
4236 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
4239 // concept requirements
4240 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
4241 _BidirectionalIterator
>)
4242 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4243 _ValueType
, _ValueType
>)
4244 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
4245 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
4247 if (__first
== __middle
|| __middle
== __last
)
4250 const _DistanceType __len1
= std::distance(__first
, __middle
);
4251 const _DistanceType __len2
= std::distance(__middle
, __last
);
4253 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
4255 if (__buf
.begin() == 0)
4256 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
4259 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
4260 __buf
.begin(), _DistanceType(__buf
.size()),
4264 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
4267 __merge_sort_loop(_RandomAccessIterator1 __first
,
4268 _RandomAccessIterator1 __last
,
4269 _RandomAccessIterator2 __result
,
4270 _Distance __step_size
)
4272 const _Distance __two_step
= 2 * __step_size
;
4274 while (__last
- __first
>= __two_step
)
4276 __result
= std::merge(__first
, __first
+ __step_size
,
4277 __first
+ __step_size
, __first
+ __two_step
,
4279 __first
+= __two_step
;
4282 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
4283 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
4287 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
4288 typename _Distance
, typename _Compare
>
4290 __merge_sort_loop(_RandomAccessIterator1 __first
,
4291 _RandomAccessIterator1 __last
,
4292 _RandomAccessIterator2 __result
, _Distance __step_size
,
4295 const _Distance __two_step
= 2 * __step_size
;
4297 while (__last
- __first
>= __two_step
)
4299 __result
= std::merge(__first
, __first
+ __step_size
,
4300 __first
+ __step_size
, __first
+ __two_step
,
4303 __first
+= __two_step
;
4305 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
4307 std::merge(__first
, __first
+ __step_size
,
4308 __first
+ __step_size
, __last
,
4313 template<typename _RandomAccessIterator
, typename _Distance
>
4315 __chunk_insertion_sort(_RandomAccessIterator __first
,
4316 _RandomAccessIterator __last
,
4317 _Distance __chunk_size
)
4319 while (__last
- __first
>= __chunk_size
)
4321 std::__insertion_sort(__first
, __first
+ __chunk_size
);
4322 __first
+= __chunk_size
;
4324 std::__insertion_sort(__first
, __last
);
4327 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
4329 __chunk_insertion_sort(_RandomAccessIterator __first
,
4330 _RandomAccessIterator __last
,
4331 _Distance __chunk_size
, _Compare __comp
)
4333 while (__last
- __first
>= __chunk_size
)
4335 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
4336 __first
+= __chunk_size
;
4338 std::__insertion_sort(__first
, __last
, __comp
);
4341 enum { _S_chunk_size
= 7 };
4343 template<typename _RandomAccessIterator
, typename _Pointer
>
4345 __merge_sort_with_buffer(_RandomAccessIterator __first
,
4346 _RandomAccessIterator __last
,
4349 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4352 const _Distance __len
= __last
- __first
;
4353 const _Pointer __buffer_last
= __buffer
+ __len
;
4355 _Distance __step_size
= _S_chunk_size
;
4356 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
4358 while (__step_size
< __len
)
4360 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
4362 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
4367 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
4369 __merge_sort_with_buffer(_RandomAccessIterator __first
,
4370 _RandomAccessIterator __last
,
4371 _Pointer __buffer
, _Compare __comp
)
4373 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4376 const _Distance __len
= __last
- __first
;
4377 const _Pointer __buffer_last
= __buffer
+ __len
;
4379 _Distance __step_size
= _S_chunk_size
;
4380 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
4382 while (__step_size
< __len
)
4384 std::__merge_sort_loop(__first
, __last
, __buffer
,
4385 __step_size
, __comp
);
4387 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
4388 __step_size
, __comp
);
4393 template<typename _RandomAccessIterator
, typename _Pointer
,
4396 __stable_sort_adaptive(_RandomAccessIterator __first
,
4397 _RandomAccessIterator __last
,
4398 _Pointer __buffer
, _Distance __buffer_size
)
4400 const _Distance __len
= (__last
- __first
+ 1) / 2;
4401 const _RandomAccessIterator __middle
= __first
+ __len
;
4402 if (__len
> __buffer_size
)
4404 std::__stable_sort_adaptive(__first
, __middle
,
4405 __buffer
, __buffer_size
);
4406 std::__stable_sort_adaptive(__middle
, __last
,
4407 __buffer
, __buffer_size
);
4411 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
4412 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
4414 std::__merge_adaptive(__first
, __middle
, __last
,
4415 _Distance(__middle
- __first
),
4416 _Distance(__last
- __middle
),
4417 __buffer
, __buffer_size
);
4420 template<typename _RandomAccessIterator
, typename _Pointer
,
4421 typename _Distance
, typename _Compare
>
4423 __stable_sort_adaptive(_RandomAccessIterator __first
,
4424 _RandomAccessIterator __last
,
4425 _Pointer __buffer
, _Distance __buffer_size
,
4428 const _Distance __len
= (__last
- __first
+ 1) / 2;
4429 const _RandomAccessIterator __middle
= __first
+ __len
;
4430 if (__len
> __buffer_size
)
4432 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
4433 __buffer_size
, __comp
);
4434 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
4435 __buffer_size
, __comp
);
4439 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
4440 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
4442 std::__merge_adaptive(__first
, __middle
, __last
,
4443 _Distance(__middle
- __first
),
4444 _Distance(__last
- __middle
),
4445 __buffer
, __buffer_size
,
4451 * This is a helper function for the stable sorting routines.
4454 template<typename _RandomAccessIterator
>
4456 __inplace_stable_sort(_RandomAccessIterator __first
,
4457 _RandomAccessIterator __last
)
4459 if (__last
- __first
< 15)
4461 std::__insertion_sort(__first
, __last
);
4464 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
4465 std::__inplace_stable_sort(__first
, __middle
);
4466 std::__inplace_stable_sort(__middle
, __last
);
4467 std::__merge_without_buffer(__first
, __middle
, __last
,
4474 * This is a helper function for the stable sorting routines.
4477 template<typename _RandomAccessIterator
, typename _Compare
>
4479 __inplace_stable_sort(_RandomAccessIterator __first
,
4480 _RandomAccessIterator __last
, _Compare __comp
)
4482 if (__last
- __first
< 15)
4484 std::__insertion_sort(__first
, __last
, __comp
);
4487 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
4488 std::__inplace_stable_sort(__first
, __middle
, __comp
);
4489 std::__inplace_stable_sort(__middle
, __last
, __comp
);
4490 std::__merge_without_buffer(__first
, __middle
, __last
,
4497 * @brief Sort the elements of a sequence, preserving the relative order
4498 * of equivalent elements.
4499 * @param first An iterator.
4500 * @param last Another iterator.
4503 * Sorts the elements in the range @p [first,last) in ascending order,
4504 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4505 * @p [first,last-1).
4507 * The relative ordering of equivalent elements is preserved, so any two
4508 * elements @p x and @p y in the range @p [first,last) such that
4509 * @p x<y is false and @p y<x is false will have the same relative
4510 * ordering after calling @p stable_sort().
4512 template<typename _RandomAccessIterator
>
4514 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
4516 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4518 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4521 // concept requirements
4522 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4523 _RandomAccessIterator
>)
4524 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4525 __glibcxx_requires_valid_range(__first
, __last
);
4527 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
4529 if (__buf
.begin() == 0)
4530 std::__inplace_stable_sort(__first
, __last
);
4532 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4533 _DistanceType(__buf
.size()));
4537 * @brief Sort the elements of a sequence using a predicate for comparison,
4538 * preserving the relative order of equivalent elements.
4539 * @param first An iterator.
4540 * @param last Another iterator.
4541 * @param comp A comparison functor.
4544 * Sorts the elements in the range @p [first,last) in ascending order,
4545 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
4546 * range @p [first,last-1).
4548 * The relative ordering of equivalent elements is preserved, so any two
4549 * elements @p x and @p y in the range @p [first,last) such that
4550 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
4551 * relative ordering after calling @p stable_sort().
4553 template<typename _RandomAccessIterator
, typename _Compare
>
4555 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
4558 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4560 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
4563 // concept requirements
4564 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4565 _RandomAccessIterator
>)
4566 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4569 __glibcxx_requires_valid_range(__first
, __last
);
4571 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
4573 if (__buf
.begin() == 0)
4574 std::__inplace_stable_sort(__first
, __last
, __comp
);
4576 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
4577 _DistanceType(__buf
.size()), __comp
);
4580 // Set algorithms: includes, set_union, set_intersection, set_difference,
4581 // set_symmetric_difference. All of these algorithms have the precondition
4582 // that their input ranges are sorted and the postcondition that their output
4583 // ranges are sorted.
4586 * @brief Determines whether all elements of a sequence exists in a range.
4587 * @param first1 Start of search range.
4588 * @param last1 End of search range.
4589 * @param first2 Start of sequence
4590 * @param last2 End of sequence.
4591 * @return True if each element in [first2,last2) is contained in order
4592 * within [first1,last1). False otherwise.
4593 * @ingroup setoperations
4595 * This operation expects both [first1,last1) and [first2,last2) to be
4596 * sorted. Searches for the presence of each element in [first2,last2)
4597 * within [first1,last1). The iterators over each range only move forward,
4598 * so this is a linear algorithm. If an element in [first2,last2) is not
4599 * found before the search iterator reaches @a last2, false is returned.
4601 template<typename _InputIterator1
, typename _InputIterator2
>
4603 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4604 _InputIterator2 __first2
, _InputIterator2 __last2
)
4606 typedef typename iterator_traits
<_InputIterator1
>::value_type
4608 typedef typename iterator_traits
<_InputIterator2
>::value_type
4611 // concept requirements
4612 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4613 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4614 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4615 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4616 __glibcxx_requires_sorted(__first1
, __last1
);
4617 __glibcxx_requires_sorted(__first2
, __last2
);
4619 while (__first1
!= __last1
&& __first2
!= __last2
)
4620 if (*__first2
< *__first1
)
4622 else if(*__first1
< *__first2
)
4625 ++__first1
, ++__first2
;
4627 return __first2
== __last2
;
4631 * @brief Determines whether all elements of a sequence exists in a range
4633 * @param first1 Start of search range.
4634 * @param last1 End of search range.
4635 * @param first2 Start of sequence
4636 * @param last2 End of sequence.
4637 * @param comp Comparison function to use.
4638 * @return True if each element in [first2,last2) is contained in order
4639 * within [first1,last1) according to comp. False otherwise.
4640 * @ingroup setoperations
4642 * This operation expects both [first1,last1) and [first2,last2) to be
4643 * sorted. Searches for the presence of each element in [first2,last2)
4644 * within [first1,last1), using comp to decide. The iterators over each
4645 * range only move forward, so this is a linear algorithm. If an element
4646 * in [first2,last2) is not found before the search iterator reaches @a
4647 * last2, false is returned.
4649 template<typename _InputIterator1
, typename _InputIterator2
,
4652 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4653 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
4655 typedef typename iterator_traits
<_InputIterator1
>::value_type
4657 typedef typename iterator_traits
<_InputIterator2
>::value_type
4660 // concept requirements
4661 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4662 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4663 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4664 _ValueType1
, _ValueType2
>)
4665 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4666 _ValueType2
, _ValueType1
>)
4667 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4668 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4670 while (__first1
!= __last1
&& __first2
!= __last2
)
4671 if (__comp(*__first2
, *__first1
))
4673 else if(__comp(*__first1
, *__first2
))
4676 ++__first1
, ++__first2
;
4678 return __first2
== __last2
;
4682 * @brief Return the union of two sorted ranges.
4683 * @param first1 Start of first range.
4684 * @param last1 End of first range.
4685 * @param first2 Start of second range.
4686 * @param last2 End of second range.
4687 * @return End of the output range.
4688 * @ingroup setoperations
4690 * This operation iterates over both ranges, copying elements present in
4691 * each range in order to the output range. Iterators increment for each
4692 * range. When the current element of one range is less than the other,
4693 * that element is copied and the iterator advanced. If an element is
4694 * contained in both ranges, the element from the first range is copied and
4695 * both ranges advance. The output range may not overlap either input
4698 template<typename _InputIterator1
, typename _InputIterator2
,
4699 typename _OutputIterator
>
4701 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4702 _InputIterator2 __first2
, _InputIterator2 __last2
,
4703 _OutputIterator __result
)
4705 typedef typename iterator_traits
<_InputIterator1
>::value_type
4707 typedef typename iterator_traits
<_InputIterator2
>::value_type
4710 // concept requirements
4711 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4712 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4713 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4715 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4717 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4718 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4719 __glibcxx_requires_sorted(__first1
, __last1
);
4720 __glibcxx_requires_sorted(__first2
, __last2
);
4722 while (__first1
!= __last1
&& __first2
!= __last2
)
4724 if (*__first1
< *__first2
)
4726 *__result
= *__first1
;
4729 else if (*__first2
< *__first1
)
4731 *__result
= *__first2
;
4736 *__result
= *__first1
;
4742 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4747 * @brief Return the union of two sorted ranges using a comparison functor.
4748 * @param first1 Start of first range.
4749 * @param last1 End of first range.
4750 * @param first2 Start of second range.
4751 * @param last2 End of second range.
4752 * @param comp The comparison functor.
4753 * @return End of the output range.
4754 * @ingroup setoperations
4756 * This operation iterates over both ranges, copying elements present in
4757 * each range in order to the output range. Iterators increment for each
4758 * range. When the current element of one range is less than the other
4759 * according to @a comp, that element is copied and the iterator advanced.
4760 * If an equivalent element according to @a comp is contained in both
4761 * ranges, the element from the first range is copied and both ranges
4762 * advance. The output range may not overlap either input range.
4764 template<typename _InputIterator1
, typename _InputIterator2
,
4765 typename _OutputIterator
, typename _Compare
>
4767 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4768 _InputIterator2 __first2
, _InputIterator2 __last2
,
4769 _OutputIterator __result
, _Compare __comp
)
4771 typedef typename iterator_traits
<_InputIterator1
>::value_type
4773 typedef typename iterator_traits
<_InputIterator2
>::value_type
4776 // concept requirements
4777 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4778 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4779 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4781 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4783 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4784 _ValueType1
, _ValueType2
>)
4785 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4786 _ValueType2
, _ValueType1
>)
4787 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4788 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4790 while (__first1
!= __last1
&& __first2
!= __last2
)
4792 if (__comp(*__first1
, *__first2
))
4794 *__result
= *__first1
;
4797 else if (__comp(*__first2
, *__first1
))
4799 *__result
= *__first2
;
4804 *__result
= *__first1
;
4810 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4815 * @brief Return the intersection of two sorted ranges.
4816 * @param first1 Start of first range.
4817 * @param last1 End of first range.
4818 * @param first2 Start of second range.
4819 * @param last2 End of second range.
4820 * @return End of the output range.
4821 * @ingroup setoperations
4823 * This operation iterates over both ranges, copying elements present in
4824 * both ranges in order to the output range. Iterators increment for each
4825 * range. When the current element of one range is less than the other,
4826 * that iterator advances. If an element is contained in both ranges, the
4827 * element from the first range is copied and both ranges advance. The
4828 * output range may not overlap either input range.
4830 template<typename _InputIterator1
, typename _InputIterator2
,
4831 typename _OutputIterator
>
4833 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4834 _InputIterator2 __first2
, _InputIterator2 __last2
,
4835 _OutputIterator __result
)
4837 typedef typename iterator_traits
<_InputIterator1
>::value_type
4839 typedef typename iterator_traits
<_InputIterator2
>::value_type
4842 // concept requirements
4843 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4844 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4845 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4847 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4848 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4849 __glibcxx_requires_sorted(__first1
, __last1
);
4850 __glibcxx_requires_sorted(__first2
, __last2
);
4852 while (__first1
!= __last1
&& __first2
!= __last2
)
4853 if (*__first1
< *__first2
)
4855 else if (*__first2
< *__first1
)
4859 *__result
= *__first1
;
4868 * @brief Return the intersection of two sorted ranges using comparison
4870 * @param first1 Start of first range.
4871 * @param last1 End of first range.
4872 * @param first2 Start of second range.
4873 * @param last2 End of second range.
4874 * @param comp The comparison functor.
4875 * @return End of the output range.
4876 * @ingroup setoperations
4878 * This operation iterates over both ranges, copying elements present in
4879 * both ranges in order to the output range. Iterators increment for each
4880 * range. When the current element of one range is less than the other
4881 * according to @a comp, that iterator advances. If an element is
4882 * contained in both ranges according to @a comp, the element from the
4883 * first range is copied and both ranges advance. The output range may not
4884 * overlap either input range.
4886 template<typename _InputIterator1
, typename _InputIterator2
,
4887 typename _OutputIterator
, typename _Compare
>
4889 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4890 _InputIterator2 __first2
, _InputIterator2 __last2
,
4891 _OutputIterator __result
, _Compare __comp
)
4893 typedef typename iterator_traits
<_InputIterator1
>::value_type
4895 typedef typename iterator_traits
<_InputIterator2
>::value_type
4898 // concept requirements
4899 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4900 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4901 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4903 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4904 _ValueType1
, _ValueType2
>)
4905 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4906 _ValueType2
, _ValueType1
>)
4907 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4908 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4910 while (__first1
!= __last1
&& __first2
!= __last2
)
4911 if (__comp(*__first1
, *__first2
))
4913 else if (__comp(*__first2
, *__first1
))
4917 *__result
= *__first1
;
4926 * @brief Return the difference of two sorted ranges.
4927 * @param first1 Start of first range.
4928 * @param last1 End of first range.
4929 * @param first2 Start of second range.
4930 * @param last2 End of second range.
4931 * @return End of the output range.
4932 * @ingroup setoperations
4934 * This operation iterates over both ranges, copying elements present in
4935 * the first range but not the second in order to the output range.
4936 * Iterators increment for each range. When the current element of the
4937 * first range is less than the second, that element is copied and the
4938 * iterator advances. If the current element of the second range is less,
4939 * the iterator advances, but no element is copied. If an element is
4940 * contained in both ranges, no elements are copied and both ranges
4941 * advance. The output range may not overlap either input range.
4943 template<typename _InputIterator1
, typename _InputIterator2
,
4944 typename _OutputIterator
>
4946 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4947 _InputIterator2 __first2
, _InputIterator2 __last2
,
4948 _OutputIterator __result
)
4950 typedef typename iterator_traits
<_InputIterator1
>::value_type
4952 typedef typename iterator_traits
<_InputIterator2
>::value_type
4955 // concept requirements
4956 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4957 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4958 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4960 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4961 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4962 __glibcxx_requires_sorted(__first1
, __last1
);
4963 __glibcxx_requires_sorted(__first2
, __last2
);
4965 while (__first1
!= __last1
&& __first2
!= __last2
)
4966 if (*__first1
< *__first2
)
4968 *__result
= *__first1
;
4972 else if (*__first2
< *__first1
)
4979 return std::copy(__first1
, __last1
, __result
);
4983 * @brief Return the difference of two sorted ranges using comparison
4985 * @param first1 Start of first range.
4986 * @param last1 End of first range.
4987 * @param first2 Start of second range.
4988 * @param last2 End of second range.
4989 * @param comp The comparison functor.
4990 * @return End of the output range.
4991 * @ingroup setoperations
4993 * This operation iterates over both ranges, copying elements present in
4994 * the first range but not the second in order to the output range.
4995 * Iterators increment for each range. When the current element of the
4996 * first range is less than the second according to @a comp, that element
4997 * is copied and the iterator advances. If the current element of the
4998 * second range is less, no element is copied and the iterator advances.
4999 * If an element is contained in both ranges according to @a comp, no
5000 * elements are copied and both ranges advance. The output range may not
5001 * overlap either input range.
5003 template<typename _InputIterator1
, typename _InputIterator2
,
5004 typename _OutputIterator
, typename _Compare
>
5006 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5007 _InputIterator2 __first2
, _InputIterator2 __last2
,
5008 _OutputIterator __result
, _Compare __comp
)
5010 typedef typename iterator_traits
<_InputIterator1
>::value_type
5012 typedef typename iterator_traits
<_InputIterator2
>::value_type
5015 // concept requirements
5016 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5017 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5018 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5020 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5021 _ValueType1
, _ValueType2
>)
5022 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5023 _ValueType2
, _ValueType1
>)
5024 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5025 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5027 while (__first1
!= __last1
&& __first2
!= __last2
)
5028 if (__comp(*__first1
, *__first2
))
5030 *__result
= *__first1
;
5034 else if (__comp(*__first2
, *__first1
))
5041 return std::copy(__first1
, __last1
, __result
);
5045 * @brief Return the symmetric difference of two sorted ranges.
5046 * @param first1 Start of first range.
5047 * @param last1 End of first range.
5048 * @param first2 Start of second range.
5049 * @param last2 End of second range.
5050 * @return End of the output range.
5051 * @ingroup setoperations
5053 * This operation iterates over both ranges, copying elements present in
5054 * one range but not the other in order to the output range. Iterators
5055 * increment for each range. When the current element of one range is less
5056 * than the other, that element is copied and the iterator advances. If an
5057 * element is contained in both ranges, no elements are copied and both
5058 * ranges advance. The output range may not overlap either input range.
5060 template<typename _InputIterator1
, typename _InputIterator2
,
5061 typename _OutputIterator
>
5063 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5064 _InputIterator2 __first2
, _InputIterator2 __last2
,
5065 _OutputIterator __result
)
5067 typedef typename iterator_traits
<_InputIterator1
>::value_type
5069 typedef typename iterator_traits
<_InputIterator2
>::value_type
5072 // concept requirements
5073 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5074 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5075 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5077 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5079 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
5080 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
5081 __glibcxx_requires_sorted(__first1
, __last1
);
5082 __glibcxx_requires_sorted(__first2
, __last2
);
5084 while (__first1
!= __last1
&& __first2
!= __last2
)
5085 if (*__first1
< *__first2
)
5087 *__result
= *__first1
;
5091 else if (*__first2
< *__first1
)
5093 *__result
= *__first2
;
5102 return std::copy(__first2
, __last2
, std::copy(__first1
,
5103 __last1
, __result
));
5107 * @brief Return the symmetric difference of two sorted ranges using
5108 * comparison functor.
5109 * @param first1 Start of first range.
5110 * @param last1 End of first range.
5111 * @param first2 Start of second range.
5112 * @param last2 End of second range.
5113 * @param comp The comparison functor.
5114 * @return End of the output range.
5115 * @ingroup setoperations
5117 * This operation iterates over both ranges, copying elements present in
5118 * one range but not the other in order to the output range. Iterators
5119 * increment for each range. When the current element of one range is less
5120 * than the other according to @a comp, that element is copied and the
5121 * iterator advances. If an element is contained in both ranges according
5122 * to @a comp, no elements are copied and both ranges advance. The output
5123 * range may not overlap either input range.
5125 template<typename _InputIterator1
, typename _InputIterator2
,
5126 typename _OutputIterator
, typename _Compare
>
5128 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
5129 _InputIterator2 __first2
, _InputIterator2 __last2
,
5130 _OutputIterator __result
,
5133 typedef typename iterator_traits
<_InputIterator1
>::value_type
5135 typedef typename iterator_traits
<_InputIterator2
>::value_type
5138 // concept requirements
5139 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
5140 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
5141 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5143 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
5145 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5146 _ValueType1
, _ValueType2
>)
5147 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5148 _ValueType2
, _ValueType1
>)
5149 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
5150 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
5152 while (__first1
!= __last1
&& __first2
!= __last2
)
5153 if (__comp(*__first1
, *__first2
))
5155 *__result
= *__first1
;
5159 else if (__comp(*__first2
, *__first1
))
5161 *__result
= *__first2
;
5170 return std::copy(__first2
, __last2
, std::copy(__first1
,
5171 __last1
, __result
));
5174 // min_element and max_element, with and without an explicitly supplied
5175 // comparison function.
5178 * @brief Return the minimum element in a range.
5179 * @param first Start of range.
5180 * @param last End of range.
5181 * @return Iterator referencing the first instance of the smallest value.
5183 template<typename _ForwardIterator
>
5185 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
5187 // concept requirements
5188 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5189 __glibcxx_function_requires(_LessThanComparableConcept
<
5190 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5191 __glibcxx_requires_valid_range(__first
, __last
);
5193 if (__first
== __last
)
5195 _ForwardIterator __result
= __first
;
5196 while (++__first
!= __last
)
5197 if (*__first
< *__result
)
5203 * @brief Return the minimum element in a range using comparison functor.
5204 * @param first Start of range.
5205 * @param last End of range.
5206 * @param comp Comparison functor.
5207 * @return Iterator referencing the first instance of the smallest value
5208 * according to comp.
5210 template<typename _ForwardIterator
, typename _Compare
>
5212 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
5215 // concept requirements
5216 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5217 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5218 typename iterator_traits
<_ForwardIterator
>::value_type
,
5219 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5220 __glibcxx_requires_valid_range(__first
, __last
);
5222 if (__first
== __last
)
5224 _ForwardIterator __result
= __first
;
5225 while (++__first
!= __last
)
5226 if (__comp(*__first
, *__result
))
5232 * @brief Return the maximum element in a range.
5233 * @param first Start of range.
5234 * @param last End of range.
5235 * @return Iterator referencing the first instance of the largest value.
5237 template<typename _ForwardIterator
>
5239 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
5241 // concept requirements
5242 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5243 __glibcxx_function_requires(_LessThanComparableConcept
<
5244 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5245 __glibcxx_requires_valid_range(__first
, __last
);
5247 if (__first
== __last
)
5249 _ForwardIterator __result
= __first
;
5250 while (++__first
!= __last
)
5251 if (*__result
< *__first
)
5257 * @brief Return the maximum element in a range using comparison functor.
5258 * @param first Start of range.
5259 * @param last End of range.
5260 * @param comp Comparison functor.
5261 * @return Iterator referencing the first instance of the largest value
5262 * according to comp.
5264 template<typename _ForwardIterator
, typename _Compare
>
5266 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
5269 // concept requirements
5270 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5271 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5272 typename iterator_traits
<_ForwardIterator
>::value_type
,
5273 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5274 __glibcxx_requires_valid_range(__first
, __last
);
5276 if (__first
== __last
) return __first
;
5277 _ForwardIterator __result
= __first
;
5278 while (++__first
!= __last
)
5279 if (__comp(*__result
, *__first
))
5284 // next_permutation and prev_permutation, with and without an explicitly
5285 // supplied comparison function.
5288 * @brief Permute range into the next "dictionary" ordering.
5289 * @param first Start of range.
5290 * @param last End of range.
5291 * @return False if wrapped to first permutation, true otherwise.
5293 * Treats all permutations of the range as a set of "dictionary" sorted
5294 * sequences. Permutes the current sequence into the next one of this set.
5295 * Returns true if there are more sequences to generate. If the sequence
5296 * is the largest of the set, the smallest is generated and false returned.
5298 template<typename _BidirectionalIterator
>
5300 next_permutation(_BidirectionalIterator __first
,
5301 _BidirectionalIterator __last
)
5303 // concept requirements
5304 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5305 _BidirectionalIterator
>)
5306 __glibcxx_function_requires(_LessThanComparableConcept
<
5307 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5308 __glibcxx_requires_valid_range(__first
, __last
);
5310 if (__first
== __last
)
5312 _BidirectionalIterator __i
= __first
;
5321 _BidirectionalIterator __ii
= __i
;
5325 _BidirectionalIterator __j
= __last
;
5326 while (!(*__i
< *--__j
))
5328 std::iter_swap(__i
, __j
);
5329 std::reverse(__ii
, __last
);
5334 std::reverse(__first
, __last
);
5341 * @brief Permute range into the next "dictionary" ordering using
5342 * comparison functor.
5343 * @param first Start of range.
5344 * @param last End of range.
5346 * @return False if wrapped to first permutation, true otherwise.
5348 * Treats all permutations of the range [first,last) as a set of
5349 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5350 * sequence into the next one of this set. Returns true if there are more
5351 * sequences to generate. If the sequence is the largest of the set, the
5352 * smallest is generated and false returned.
5354 template<typename _BidirectionalIterator
, typename _Compare
>
5356 next_permutation(_BidirectionalIterator __first
,
5357 _BidirectionalIterator __last
, _Compare __comp
)
5359 // concept requirements
5360 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5361 _BidirectionalIterator
>)
5362 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5363 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
5364 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5365 __glibcxx_requires_valid_range(__first
, __last
);
5367 if (__first
== __last
)
5369 _BidirectionalIterator __i
= __first
;
5378 _BidirectionalIterator __ii
= __i
;
5380 if (__comp(*__i
, *__ii
))
5382 _BidirectionalIterator __j
= __last
;
5383 while (!bool(__comp(*__i
, *--__j
)))
5385 std::iter_swap(__i
, __j
);
5386 std::reverse(__ii
, __last
);
5391 std::reverse(__first
, __last
);
5398 * @brief Permute range into the previous "dictionary" ordering.
5399 * @param first Start of range.
5400 * @param last End of range.
5401 * @return False if wrapped to last permutation, true otherwise.
5403 * Treats all permutations of the range as a set of "dictionary" sorted
5404 * sequences. Permutes the current sequence into the previous one of this
5405 * set. Returns true if there are more sequences to generate. If the
5406 * sequence is the smallest of the set, the largest is generated and false
5409 template<typename _BidirectionalIterator
>
5411 prev_permutation(_BidirectionalIterator __first
,
5412 _BidirectionalIterator __last
)
5414 // concept requirements
5415 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5416 _BidirectionalIterator
>)
5417 __glibcxx_function_requires(_LessThanComparableConcept
<
5418 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5419 __glibcxx_requires_valid_range(__first
, __last
);
5421 if (__first
== __last
)
5423 _BidirectionalIterator __i
= __first
;
5432 _BidirectionalIterator __ii
= __i
;
5436 _BidirectionalIterator __j
= __last
;
5437 while (!(*--__j
< *__i
))
5439 std::iter_swap(__i
, __j
);
5440 std::reverse(__ii
, __last
);
5445 std::reverse(__first
, __last
);
5452 * @brief Permute range into the previous "dictionary" ordering using
5453 * comparison functor.
5454 * @param first Start of range.
5455 * @param last End of range.
5457 * @return False if wrapped to last permutation, true otherwise.
5459 * Treats all permutations of the range [first,last) as a set of
5460 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5461 * sequence into the previous one of this set. Returns true if there are
5462 * more sequences to generate. If the sequence is the smallest of the set,
5463 * the largest is generated and false returned.
5465 template<typename _BidirectionalIterator
, typename _Compare
>
5467 prev_permutation(_BidirectionalIterator __first
,
5468 _BidirectionalIterator __last
, _Compare __comp
)
5470 // concept requirements
5471 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5472 _BidirectionalIterator
>)
5473 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5474 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
5475 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5476 __glibcxx_requires_valid_range(__first
, __last
);
5478 if (__first
== __last
)
5480 _BidirectionalIterator __i
= __first
;
5489 _BidirectionalIterator __ii
= __i
;
5491 if (__comp(*__ii
, *__i
))
5493 _BidirectionalIterator __j
= __last
;
5494 while (!bool(__comp(*--__j
, *__i
)))
5496 std::iter_swap(__i
, __j
);
5497 std::reverse(__ii
, __last
);
5502 std::reverse(__first
, __last
);
5508 _GLIBCXX_END_NAMESPACE
5510 #endif /* _STL_ALGO_H */