2009-04-07 Andrew Stubbs <ams@codesourcery.com>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_algo.h
blob530999797f09b2f4f96ecfb41ed56b62be497e9c
1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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.
33 * Copyright (c) 1994
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.
45 * Copyright (c) 1996
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.
57 /** @file stl_algo.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _STL_ALGO_H
63 #define _STL_ALGO_H 1
65 #include <cstdlib> // for rand
66 #include <bits/algorithmfwd.h>
67 #include <bits/stl_heap.h>
68 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
69 #include <debug/debug.h>
70 #include <initializer_list>
72 // See concept_check.h for the __glibcxx_*_requires macros.
74 _GLIBCXX_BEGIN_NAMESPACE(std)
76 /**
77 * @brief Find the median of three values.
78 * @param a A value.
79 * @param b A value.
80 * @param c A value.
81 * @return One of @p a, @p b or @p c.
83 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
84 * then the value returned will be @c m.
85 * This is an SGI extension.
86 * @ingroup SGIextensions
88 template<typename _Tp>
89 inline const _Tp&
90 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
92 // concept requirements
93 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
94 if (__a < __b)
95 if (__b < __c)
96 return __b;
97 else if (__a < __c)
98 return __c;
99 else
100 return __a;
101 else if (__a < __c)
102 return __a;
103 else if (__b < __c)
104 return __c;
105 else
106 return __b;
110 * @brief Find the median of three values using a predicate for comparison.
111 * @param a A value.
112 * @param b A value.
113 * @param c A value.
114 * @param comp A binary predicate.
115 * @return One of @p a, @p b or @p c.
117 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
118 * and @p comp(m,n) are both true then the value returned will be @c m.
119 * This is an SGI extension.
120 * @ingroup SGIextensions
122 template<typename _Tp, typename _Compare>
123 inline const _Tp&
124 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
126 // concept requirements
127 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
128 _Tp, _Tp>)
129 if (__comp(__a, __b))
130 if (__comp(__b, __c))
131 return __b;
132 else if (__comp(__a, __c))
133 return __c;
134 else
135 return __a;
136 else if (__comp(__a, __c))
137 return __a;
138 else if (__comp(__b, __c))
139 return __c;
140 else
141 return __b;
144 // for_each
146 /// This is an overload used by find() for the Input Iterator case.
147 template<typename _InputIterator, typename _Tp>
148 inline _InputIterator
149 __find(_InputIterator __first, _InputIterator __last,
150 const _Tp& __val, input_iterator_tag)
152 while (__first != __last && !(*__first == __val))
153 ++__first;
154 return __first;
157 /// This is an overload used by find_if() for the Input Iterator case.
158 template<typename _InputIterator, typename _Predicate>
159 inline _InputIterator
160 __find_if(_InputIterator __first, _InputIterator __last,
161 _Predicate __pred, input_iterator_tag)
163 while (__first != __last && !bool(__pred(*__first)))
164 ++__first;
165 return __first;
168 /// This is an overload used by find() for the RAI case.
169 template<typename _RandomAccessIterator, typename _Tp>
170 _RandomAccessIterator
171 __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
172 const _Tp& __val, random_access_iterator_tag)
174 typename iterator_traits<_RandomAccessIterator>::difference_type
175 __trip_count = (__last - __first) >> 2;
177 for (; __trip_count > 0; --__trip_count)
179 if (*__first == __val)
180 return __first;
181 ++__first;
183 if (*__first == __val)
184 return __first;
185 ++__first;
187 if (*__first == __val)
188 return __first;
189 ++__first;
191 if (*__first == __val)
192 return __first;
193 ++__first;
196 switch (__last - __first)
198 case 3:
199 if (*__first == __val)
200 return __first;
201 ++__first;
202 case 2:
203 if (*__first == __val)
204 return __first;
205 ++__first;
206 case 1:
207 if (*__first == __val)
208 return __first;
209 ++__first;
210 case 0:
211 default:
212 return __last;
216 /// This is an overload used by find_if() for the RAI case.
217 template<typename _RandomAccessIterator, typename _Predicate>
218 _RandomAccessIterator
219 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
220 _Predicate __pred, random_access_iterator_tag)
222 typename iterator_traits<_RandomAccessIterator>::difference_type
223 __trip_count = (__last - __first) >> 2;
225 for (; __trip_count > 0; --__trip_count)
227 if (__pred(*__first))
228 return __first;
229 ++__first;
231 if (__pred(*__first))
232 return __first;
233 ++__first;
235 if (__pred(*__first))
236 return __first;
237 ++__first;
239 if (__pred(*__first))
240 return __first;
241 ++__first;
244 switch (__last - __first)
246 case 3:
247 if (__pred(*__first))
248 return __first;
249 ++__first;
250 case 2:
251 if (__pred(*__first))
252 return __first;
253 ++__first;
254 case 1:
255 if (__pred(*__first))
256 return __first;
257 ++__first;
258 case 0:
259 default:
260 return __last;
264 #ifdef __GXX_EXPERIMENTAL_CXX0X__
265 /// This is an overload used by find_if_not() for the Input Iterator case.
266 template<typename _InputIterator, typename _Predicate>
267 inline _InputIterator
268 __find_if_not(_InputIterator __first, _InputIterator __last,
269 _Predicate __pred, input_iterator_tag)
271 while (__first != __last && bool(__pred(*__first)))
272 ++__first;
273 return __first;
276 /// This is an overload used by find_if_not() for the RAI case.
277 template<typename _RandomAccessIterator, typename _Predicate>
278 _RandomAccessIterator
279 __find_if_not(_RandomAccessIterator __first, _RandomAccessIterator __last,
280 _Predicate __pred, random_access_iterator_tag)
282 typename iterator_traits<_RandomAccessIterator>::difference_type
283 __trip_count = (__last - __first) >> 2;
285 for (; __trip_count > 0; --__trip_count)
287 if (!bool(__pred(*__first)))
288 return __first;
289 ++__first;
291 if (!bool(__pred(*__first)))
292 return __first;
293 ++__first;
295 if (!bool(__pred(*__first)))
296 return __first;
297 ++__first;
299 if (!bool(__pred(*__first)))
300 return __first;
301 ++__first;
304 switch (__last - __first)
306 case 3:
307 if (!bool(__pred(*__first)))
308 return __first;
309 ++__first;
310 case 2:
311 if (!bool(__pred(*__first)))
312 return __first;
313 ++__first;
314 case 1:
315 if (!bool(__pred(*__first)))
316 return __first;
317 ++__first;
318 case 0:
319 default:
320 return __last;
323 #endif
325 // set_difference
326 // set_intersection
327 // set_symmetric_difference
328 // set_union
329 // for_each
330 // find
331 // find_if
332 // find_first_of
333 // adjacent_find
334 // count
335 // count_if
336 // search
339 * This is an uglified
340 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
341 * overloaded for forward iterators.
343 template<typename _ForwardIterator, typename _Integer, typename _Tp>
344 _ForwardIterator
345 __search_n(_ForwardIterator __first, _ForwardIterator __last,
346 _Integer __count, const _Tp& __val,
347 std::forward_iterator_tag)
349 __first = _GLIBCXX_STD_P::find(__first, __last, __val);
350 while (__first != __last)
352 typename iterator_traits<_ForwardIterator>::difference_type
353 __n = __count;
354 _ForwardIterator __i = __first;
355 ++__i;
356 while (__i != __last && __n != 1 && *__i == __val)
358 ++__i;
359 --__n;
361 if (__n == 1)
362 return __first;
363 if (__i == __last)
364 return __last;
365 __first = _GLIBCXX_STD_P::find(++__i, __last, __val);
367 return __last;
371 * This is an uglified
372 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
373 * overloaded for random access iterators.
375 template<typename _RandomAccessIter, typename _Integer, typename _Tp>
376 _RandomAccessIter
377 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
378 _Integer __count, const _Tp& __val,
379 std::random_access_iterator_tag)
382 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
383 _DistanceType;
385 _DistanceType __tailSize = __last - __first;
386 const _DistanceType __pattSize = __count;
388 if (__tailSize < __pattSize)
389 return __last;
391 const _DistanceType __skipOffset = __pattSize - 1;
392 _RandomAccessIter __lookAhead = __first + __skipOffset;
393 __tailSize -= __pattSize;
395 while (1) // the main loop...
397 // __lookAhead here is always pointing to the last element of next
398 // possible match.
399 while (!(*__lookAhead == __val)) // the skip loop...
401 if (__tailSize < __pattSize)
402 return __last; // Failure
403 __lookAhead += __pattSize;
404 __tailSize -= __pattSize;
406 _DistanceType __remainder = __skipOffset;
407 for (_RandomAccessIter __backTrack = __lookAhead - 1;
408 *__backTrack == __val; --__backTrack)
410 if (--__remainder == 0)
411 return (__lookAhead - __skipOffset); // Success
413 if (__remainder > __tailSize)
414 return __last; // Failure
415 __lookAhead += __remainder;
416 __tailSize -= __remainder;
420 // search_n
423 * This is an uglified
424 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
425 * _BinaryPredicate)
426 * overloaded for forward iterators.
428 template<typename _ForwardIterator, typename _Integer, typename _Tp,
429 typename _BinaryPredicate>
430 _ForwardIterator
431 __search_n(_ForwardIterator __first, _ForwardIterator __last,
432 _Integer __count, const _Tp& __val,
433 _BinaryPredicate __binary_pred, std::forward_iterator_tag)
435 while (__first != __last && !bool(__binary_pred(*__first, __val)))
436 ++__first;
438 while (__first != __last)
440 typename iterator_traits<_ForwardIterator>::difference_type
441 __n = __count;
442 _ForwardIterator __i = __first;
443 ++__i;
444 while (__i != __last && __n != 1 && bool(__binary_pred(*__i, __val)))
446 ++__i;
447 --__n;
449 if (__n == 1)
450 return __first;
451 if (__i == __last)
452 return __last;
453 __first = ++__i;
454 while (__first != __last
455 && !bool(__binary_pred(*__first, __val)))
456 ++__first;
458 return __last;
462 * This is an uglified
463 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
464 * _BinaryPredicate)
465 * overloaded for random access iterators.
467 template<typename _RandomAccessIter, typename _Integer, typename _Tp,
468 typename _BinaryPredicate>
469 _RandomAccessIter
470 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
471 _Integer __count, const _Tp& __val,
472 _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
475 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
476 _DistanceType;
478 _DistanceType __tailSize = __last - __first;
479 const _DistanceType __pattSize = __count;
481 if (__tailSize < __pattSize)
482 return __last;
484 const _DistanceType __skipOffset = __pattSize - 1;
485 _RandomAccessIter __lookAhead = __first + __skipOffset;
486 __tailSize -= __pattSize;
488 while (1) // the main loop...
490 // __lookAhead here is always pointing to the last element of next
491 // possible match.
492 while (!bool(__binary_pred(*__lookAhead, __val))) // the skip loop...
494 if (__tailSize < __pattSize)
495 return __last; // Failure
496 __lookAhead += __pattSize;
497 __tailSize -= __pattSize;
499 _DistanceType __remainder = __skipOffset;
500 for (_RandomAccessIter __backTrack = __lookAhead - 1;
501 __binary_pred(*__backTrack, __val); --__backTrack)
503 if (--__remainder == 0)
504 return (__lookAhead - __skipOffset); // Success
506 if (__remainder > __tailSize)
507 return __last; // Failure
508 __lookAhead += __remainder;
509 __tailSize -= __remainder;
513 // find_end for forward iterators.
514 template<typename _ForwardIterator1, typename _ForwardIterator2>
515 _ForwardIterator1
516 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
517 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
518 forward_iterator_tag, forward_iterator_tag)
520 if (__first2 == __last2)
521 return __last1;
522 else
524 _ForwardIterator1 __result = __last1;
525 while (1)
527 _ForwardIterator1 __new_result
528 = _GLIBCXX_STD_P::search(__first1, __last1, __first2, __last2);
529 if (__new_result == __last1)
530 return __result;
531 else
533 __result = __new_result;
534 __first1 = __new_result;
535 ++__first1;
541 template<typename _ForwardIterator1, typename _ForwardIterator2,
542 typename _BinaryPredicate>
543 _ForwardIterator1
544 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
545 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
546 forward_iterator_tag, forward_iterator_tag,
547 _BinaryPredicate __comp)
549 if (__first2 == __last2)
550 return __last1;
551 else
553 _ForwardIterator1 __result = __last1;
554 while (1)
556 _ForwardIterator1 __new_result
557 = _GLIBCXX_STD_P::search(__first1, __last1, __first2,
558 __last2, __comp);
559 if (__new_result == __last1)
560 return __result;
561 else
563 __result = __new_result;
564 __first1 = __new_result;
565 ++__first1;
571 // find_end for bidirectional iterators (much faster).
572 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
573 _BidirectionalIterator1
574 __find_end(_BidirectionalIterator1 __first1,
575 _BidirectionalIterator1 __last1,
576 _BidirectionalIterator2 __first2,
577 _BidirectionalIterator2 __last2,
578 bidirectional_iterator_tag, bidirectional_iterator_tag)
580 // concept requirements
581 __glibcxx_function_requires(_BidirectionalIteratorConcept<
582 _BidirectionalIterator1>)
583 __glibcxx_function_requires(_BidirectionalIteratorConcept<
584 _BidirectionalIterator2>)
586 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
587 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
589 _RevIterator1 __rlast1(__first1);
590 _RevIterator2 __rlast2(__first2);
591 _RevIterator1 __rresult = _GLIBCXX_STD_P::search(_RevIterator1(__last1),
592 __rlast1,
593 _RevIterator2(__last2),
594 __rlast2);
596 if (__rresult == __rlast1)
597 return __last1;
598 else
600 _BidirectionalIterator1 __result = __rresult.base();
601 std::advance(__result, -std::distance(__first2, __last2));
602 return __result;
606 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
607 typename _BinaryPredicate>
608 _BidirectionalIterator1
609 __find_end(_BidirectionalIterator1 __first1,
610 _BidirectionalIterator1 __last1,
611 _BidirectionalIterator2 __first2,
612 _BidirectionalIterator2 __last2,
613 bidirectional_iterator_tag, bidirectional_iterator_tag,
614 _BinaryPredicate __comp)
616 // concept requirements
617 __glibcxx_function_requires(_BidirectionalIteratorConcept<
618 _BidirectionalIterator1>)
619 __glibcxx_function_requires(_BidirectionalIteratorConcept<
620 _BidirectionalIterator2>)
622 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
623 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
625 _RevIterator1 __rlast1(__first1);
626 _RevIterator2 __rlast2(__first2);
627 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
628 _RevIterator2(__last2), __rlast2,
629 __comp);
631 if (__rresult == __rlast1)
632 return __last1;
633 else
635 _BidirectionalIterator1 __result = __rresult.base();
636 std::advance(__result, -std::distance(__first2, __last2));
637 return __result;
642 * @brief Find last matching subsequence in a sequence.
643 * @ingroup non_mutating_algorithms
644 * @param first1 Start of range to search.
645 * @param last1 End of range to search.
646 * @param first2 Start of sequence to match.
647 * @param last2 End of sequence to match.
648 * @return The last iterator @c i in the range
649 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
650 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
651 * such iterator exists.
653 * Searches the range @p [first1,last1) for a sub-sequence that compares
654 * equal value-by-value with the sequence given by @p [first2,last2) and
655 * returns an iterator to the first element of the sub-sequence, or
656 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
657 * last such subsequence contained in [first,last1).
659 * Because the sub-sequence must lie completely within the range
660 * @p [first1,last1) it must start at a position less than
661 * @p last1-(last2-first2) where @p last2-first2 is the length of the
662 * sub-sequence.
663 * This means that the returned iterator @c i will be in the range
664 * @p [first1,last1-(last2-first2))
666 template<typename _ForwardIterator1, typename _ForwardIterator2>
667 inline _ForwardIterator1
668 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
669 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
671 // concept requirements
672 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
673 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
674 __glibcxx_function_requires(_EqualOpConcept<
675 typename iterator_traits<_ForwardIterator1>::value_type,
676 typename iterator_traits<_ForwardIterator2>::value_type>)
677 __glibcxx_requires_valid_range(__first1, __last1);
678 __glibcxx_requires_valid_range(__first2, __last2);
680 return std::__find_end(__first1, __last1, __first2, __last2,
681 std::__iterator_category(__first1),
682 std::__iterator_category(__first2));
686 * @brief Find last matching subsequence in a sequence using a predicate.
687 * @ingroup non_mutating_algorithms
688 * @param first1 Start of range to search.
689 * @param last1 End of range to search.
690 * @param first2 Start of sequence to match.
691 * @param last2 End of sequence to match.
692 * @param comp The predicate to use.
693 * @return The last iterator @c i in the range
694 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
695 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
696 * @p last1 if no such iterator exists.
698 * Searches the range @p [first1,last1) for a sub-sequence that compares
699 * equal value-by-value with the sequence given by @p [first2,last2) using
700 * comp as a predicate and returns an iterator to the first element of the
701 * sub-sequence, or @p last1 if the sub-sequence is not found. The
702 * sub-sequence will be the last such subsequence contained in
703 * [first,last1).
705 * Because the sub-sequence must lie completely within the range
706 * @p [first1,last1) it must start at a position less than
707 * @p last1-(last2-first2) where @p last2-first2 is the length of the
708 * sub-sequence.
709 * This means that the returned iterator @c i will be in the range
710 * @p [first1,last1-(last2-first2))
712 template<typename _ForwardIterator1, typename _ForwardIterator2,
713 typename _BinaryPredicate>
714 inline _ForwardIterator1
715 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
716 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
717 _BinaryPredicate __comp)
719 // concept requirements
720 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
721 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
722 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
723 typename iterator_traits<_ForwardIterator1>::value_type,
724 typename iterator_traits<_ForwardIterator2>::value_type>)
725 __glibcxx_requires_valid_range(__first1, __last1);
726 __glibcxx_requires_valid_range(__first2, __last2);
728 return std::__find_end(__first1, __last1, __first2, __last2,
729 std::__iterator_category(__first1),
730 std::__iterator_category(__first2),
731 __comp);
734 #ifdef __GXX_EXPERIMENTAL_CXX0X__
736 * @brief Checks that a predicate is true for all the elements
737 * of a sequence.
738 * @ingroup non_mutating_algorithms
739 * @param first An input iterator.
740 * @param last An input iterator.
741 * @param pred A predicate.
742 * @return True if the check is true, false otherwise.
744 * Returns true if @p pred is true for each element in the range
745 * @p [first,last), and false otherwise.
747 template<typename _InputIterator, typename _Predicate>
748 inline bool
749 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
750 { return __last == std::find_if_not(__first, __last, __pred); }
753 * @brief Checks that a predicate is false for all the elements
754 * of a sequence.
755 * @ingroup non_mutating_algorithms
756 * @param first An input iterator.
757 * @param last An input iterator.
758 * @param pred A predicate.
759 * @return True if the check is true, false otherwise.
761 * Returns true if @p pred is false for each element in the range
762 * @p [first,last), and false otherwise.
764 template<typename _InputIterator, typename _Predicate>
765 inline bool
766 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
767 { return __last == _GLIBCXX_STD_P::find_if(__first, __last, __pred); }
770 * @brief Checks that a predicate is false for at least an element
771 * of a sequence.
772 * @ingroup non_mutating_algorithms
773 * @param first An input iterator.
774 * @param last An input iterator.
775 * @param pred A predicate.
776 * @return True if the check is true, false otherwise.
778 * Returns true if an element exists in the range @p [first,last) such that
779 * @p pred is true, and false otherwise.
781 template<typename _InputIterator, typename _Predicate>
782 inline bool
783 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
784 { return !std::none_of(__first, __last, __pred); }
787 * @brief Find the first element in a sequence for which a
788 * predicate is false.
789 * @ingroup non_mutating_algorithms
790 * @param first An input iterator.
791 * @param last An input iterator.
792 * @param pred A predicate.
793 * @return The first iterator @c i in the range @p [first,last)
794 * such that @p pred(*i) is false, or @p last if no such iterator exists.
796 template<typename _InputIterator, typename _Predicate>
797 inline _InputIterator
798 find_if_not(_InputIterator __first, _InputIterator __last,
799 _Predicate __pred)
801 // concept requirements
802 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
803 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
804 typename iterator_traits<_InputIterator>::value_type>)
805 __glibcxx_requires_valid_range(__first, __last);
806 return std::__find_if_not(__first, __last, __pred,
807 std::__iterator_category(__first));
811 * @brief Checks whether the sequence is partitioned.
812 * @ingroup mutating_algorithms
813 * @param first An input iterator.
814 * @param last An input iterator.
815 * @param pred A predicate.
816 * @return True if the range @p [first,last) is partioned by @p pred,
817 * i.e. if all elements that satisfy @p pred appear before those that
818 * do not.
820 template<typename _InputIterator, typename _Predicate>
821 inline bool
822 is_partitioned(_InputIterator __first, _InputIterator __last,
823 _Predicate __pred)
825 __first = std::find_if_not(__first, __last, __pred);
826 return std::none_of(__first, __last, __pred);
830 * @brief Find the partition point of a partitioned range.
831 * @ingroup mutating_algorithms
832 * @param first An iterator.
833 * @param last Another iterator.
834 * @param pred A predicate.
835 * @return An iterator @p mid such that @p all_of(first, mid, pred)
836 * and @p none_of(mid, last, pred) are both true.
838 template<typename _ForwardIterator, typename _Predicate>
839 _ForwardIterator
840 partition_point(_ForwardIterator __first, _ForwardIterator __last,
841 _Predicate __pred)
843 // concept requirements
844 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
845 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
846 typename iterator_traits<_ForwardIterator>::value_type>)
848 // A specific debug-mode test will be necessary...
849 __glibcxx_requires_valid_range(__first, __last);
851 typedef typename iterator_traits<_ForwardIterator>::difference_type
852 _DistanceType;
854 _DistanceType __len = std::distance(__first, __last);
855 _DistanceType __half;
856 _ForwardIterator __middle;
858 while (__len > 0)
860 __half = __len >> 1;
861 __middle = __first;
862 std::advance(__middle, __half);
863 if (__pred(*__middle))
865 __first = __middle;
866 ++__first;
867 __len = __len - __half - 1;
869 else
870 __len = __half;
872 return __first;
874 #endif
878 * @brief Copy a sequence, removing elements of a given value.
879 * @ingroup mutating_algorithms
880 * @param first An input iterator.
881 * @param last An input iterator.
882 * @param result An output iterator.
883 * @param value The value to be removed.
884 * @return An iterator designating the end of the resulting sequence.
886 * Copies each element in the range @p [first,last) not equal to @p value
887 * to the range beginning at @p result.
888 * remove_copy() is stable, so the relative order of elements that are
889 * copied is unchanged.
891 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
892 _OutputIterator
893 remove_copy(_InputIterator __first, _InputIterator __last,
894 _OutputIterator __result, const _Tp& __value)
896 // concept requirements
897 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
898 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
899 typename iterator_traits<_InputIterator>::value_type>)
900 __glibcxx_function_requires(_EqualOpConcept<
901 typename iterator_traits<_InputIterator>::value_type, _Tp>)
902 __glibcxx_requires_valid_range(__first, __last);
904 for (; __first != __last; ++__first)
905 if (!(*__first == __value))
907 *__result = *__first;
908 ++__result;
910 return __result;
914 * @brief Copy a sequence, removing elements for which a predicate is true.
915 * @ingroup mutating_algorithms
916 * @param first An input iterator.
917 * @param last An input iterator.
918 * @param result An output iterator.
919 * @param pred A predicate.
920 * @return An iterator designating the end of the resulting sequence.
922 * Copies each element in the range @p [first,last) for which
923 * @p pred returns false to the range beginning at @p result.
925 * remove_copy_if() is stable, so the relative order of elements that are
926 * copied is unchanged.
928 template<typename _InputIterator, typename _OutputIterator,
929 typename _Predicate>
930 _OutputIterator
931 remove_copy_if(_InputIterator __first, _InputIterator __last,
932 _OutputIterator __result, _Predicate __pred)
934 // concept requirements
935 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
936 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
937 typename iterator_traits<_InputIterator>::value_type>)
938 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
939 typename iterator_traits<_InputIterator>::value_type>)
940 __glibcxx_requires_valid_range(__first, __last);
942 for (; __first != __last; ++__first)
943 if (!bool(__pred(*__first)))
945 *__result = *__first;
946 ++__result;
948 return __result;
951 #ifdef __GXX_EXPERIMENTAL_CXX0X__
953 * @brief Copy the elements of a sequence for which a predicate is true.
954 * @ingroup mutating_algorithms
955 * @param first An input iterator.
956 * @param last An input iterator.
957 * @param result An output iterator.
958 * @param pred A predicate.
959 * @return An iterator designating the end of the resulting sequence.
961 * Copies each element in the range @p [first,last) for which
962 * @p pred returns true to the range beginning at @p result.
964 * copy_if() is stable, so the relative order of elements that are
965 * copied is unchanged.
967 template<typename _InputIterator, typename _OutputIterator,
968 typename _Predicate>
969 _OutputIterator
970 copy_if(_InputIterator __first, _InputIterator __last,
971 _OutputIterator __result, _Predicate __pred)
973 // concept requirements
974 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
975 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
976 typename iterator_traits<_InputIterator>::value_type>)
977 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
978 typename iterator_traits<_InputIterator>::value_type>)
979 __glibcxx_requires_valid_range(__first, __last);
981 for (; __first != __last; ++__first)
982 if (__pred(*__first))
984 *__result = *__first;
985 ++__result;
987 return __result;
991 template<typename _InputIterator, typename _Size, typename _OutputIterator>
992 _OutputIterator
993 __copy_n(_InputIterator __first, _Size __n,
994 _OutputIterator __result, input_iterator_tag)
996 for (; __n > 0; --__n)
998 *__result = *__first;
999 ++__first;
1000 ++__result;
1002 return __result;
1005 template<typename _RandomAccessIterator, typename _Size,
1006 typename _OutputIterator>
1007 inline _OutputIterator
1008 __copy_n(_RandomAccessIterator __first, _Size __n,
1009 _OutputIterator __result, random_access_iterator_tag)
1010 { return std::copy(__first, __first + __n, __result); }
1013 * @brief Copies the range [first,first+n) into [result,result+n).
1014 * @ingroup mutating_algorithms
1015 * @param first An input iterator.
1016 * @param n The number of elements to copy.
1017 * @param result An output iterator.
1018 * @return result+n.
1020 * This inline function will boil down to a call to @c memmove whenever
1021 * possible. Failing that, if random access iterators are passed, then the
1022 * loop count will be known (and therefore a candidate for compiler
1023 * optimizations such as unrolling).
1025 template<typename _InputIterator, typename _Size, typename _OutputIterator>
1026 inline _OutputIterator
1027 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1029 // concept requirements
1030 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1031 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1032 typename iterator_traits<_InputIterator>::value_type>)
1034 return std::__copy_n(__first, __n, __result,
1035 std::__iterator_category(__first));
1039 * @brief Copy the elements of a sequence to separate output sequences
1040 * depending on the truth value of a predicate.
1041 * @ingroup mutating_algorithms
1042 * @param first An input iterator.
1043 * @param last An input iterator.
1044 * @param out_true An output iterator.
1045 * @param out_false An output iterator.
1046 * @param pred A predicate.
1047 * @return A pair designating the ends of the resulting sequences.
1049 * Copies each element in the range @p [first,last) for which
1050 * @p pred returns true to the range beginning at @p out_true
1051 * and each element for which @p pred returns false to @p out_false.
1053 template<typename _InputIterator, typename _OutputIterator1,
1054 typename _OutputIterator2, typename _Predicate>
1055 pair<_OutputIterator1, _OutputIterator2>
1056 partition_copy(_InputIterator __first, _InputIterator __last,
1057 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
1058 _Predicate __pred)
1060 // concept requirements
1061 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1062 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
1063 typename iterator_traits<_InputIterator>::value_type>)
1064 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
1065 typename iterator_traits<_InputIterator>::value_type>)
1066 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1067 typename iterator_traits<_InputIterator>::value_type>)
1068 __glibcxx_requires_valid_range(__first, __last);
1070 for (; __first != __last; ++__first)
1071 if (__pred(*__first))
1073 *__out_true = *__first;
1074 ++__out_true;
1076 else
1078 *__out_false = *__first;
1079 ++__out_false;
1082 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
1084 #endif
1087 * @brief Remove elements from a sequence.
1088 * @ingroup mutating_algorithms
1089 * @param first An input iterator.
1090 * @param last An input iterator.
1091 * @param value The value to be removed.
1092 * @return An iterator designating the end of the resulting sequence.
1094 * All elements equal to @p value are removed from the range
1095 * @p [first,last).
1097 * remove() is stable, so the relative order of elements that are
1098 * not removed is unchanged.
1100 * Elements between the end of the resulting sequence and @p last
1101 * are still present, but their value is unspecified.
1103 template<typename _ForwardIterator, typename _Tp>
1104 _ForwardIterator
1105 remove(_ForwardIterator __first, _ForwardIterator __last,
1106 const _Tp& __value)
1108 // concept requirements
1109 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1110 _ForwardIterator>)
1111 __glibcxx_function_requires(_EqualOpConcept<
1112 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1113 __glibcxx_requires_valid_range(__first, __last);
1115 __first = _GLIBCXX_STD_P::find(__first, __last, __value);
1116 if(__first == __last)
1117 return __first;
1118 _ForwardIterator __result = __first;
1119 ++__first;
1120 for(; __first != __last; ++__first)
1121 if(!(*__first == __value))
1123 *__result = _GLIBCXX_MOVE(*__first);
1124 ++__result;
1126 return __result;
1130 * @brief Remove elements from a sequence using a predicate.
1131 * @ingroup mutating_algorithms
1132 * @param first A forward iterator.
1133 * @param last A forward iterator.
1134 * @param pred A predicate.
1135 * @return An iterator designating the end of the resulting sequence.
1137 * All elements for which @p pred returns true are removed from the range
1138 * @p [first,last).
1140 * remove_if() is stable, so the relative order of elements that are
1141 * not removed is unchanged.
1143 * Elements between the end of the resulting sequence and @p last
1144 * are still present, but their value is unspecified.
1146 template<typename _ForwardIterator, typename _Predicate>
1147 _ForwardIterator
1148 remove_if(_ForwardIterator __first, _ForwardIterator __last,
1149 _Predicate __pred)
1151 // concept requirements
1152 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1153 _ForwardIterator>)
1154 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1155 typename iterator_traits<_ForwardIterator>::value_type>)
1156 __glibcxx_requires_valid_range(__first, __last);
1158 __first = _GLIBCXX_STD_P::find_if(__first, __last, __pred);
1159 if(__first == __last)
1160 return __first;
1161 _ForwardIterator __result = __first;
1162 ++__first;
1163 for(; __first != __last; ++__first)
1164 if(!bool(__pred(*__first)))
1166 *__result = _GLIBCXX_MOVE(*__first);
1167 ++__result;
1169 return __result;
1173 * @brief Remove consecutive duplicate values from a sequence.
1174 * @ingroup mutating_algorithms
1175 * @param first A forward iterator.
1176 * @param last A forward iterator.
1177 * @return An iterator designating the end of the resulting sequence.
1179 * Removes all but the first element from each group of consecutive
1180 * values that compare equal.
1181 * unique() is stable, so the relative order of elements that are
1182 * not removed is unchanged.
1183 * Elements between the end of the resulting sequence and @p last
1184 * are still present, but their value is unspecified.
1186 template<typename _ForwardIterator>
1187 _ForwardIterator
1188 unique(_ForwardIterator __first, _ForwardIterator __last)
1190 // concept requirements
1191 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1192 _ForwardIterator>)
1193 __glibcxx_function_requires(_EqualityComparableConcept<
1194 typename iterator_traits<_ForwardIterator>::value_type>)
1195 __glibcxx_requires_valid_range(__first, __last);
1197 // Skip the beginning, if already unique.
1198 __first = _GLIBCXX_STD_P::adjacent_find(__first, __last);
1199 if (__first == __last)
1200 return __last;
1202 // Do the real copy work.
1203 _ForwardIterator __dest = __first;
1204 ++__first;
1205 while (++__first != __last)
1206 if (!(*__dest == *__first))
1207 *++__dest = _GLIBCXX_MOVE(*__first);
1208 return ++__dest;
1212 * @brief Remove consecutive values from a sequence using a predicate.
1213 * @ingroup mutating_algorithms
1214 * @param first A forward iterator.
1215 * @param last A forward iterator.
1216 * @param binary_pred A binary predicate.
1217 * @return An iterator designating the end of the resulting sequence.
1219 * Removes all but the first element from each group of consecutive
1220 * values for which @p binary_pred returns true.
1221 * unique() is stable, so the relative order of elements that are
1222 * not removed is unchanged.
1223 * Elements between the end of the resulting sequence and @p last
1224 * are still present, but their value is unspecified.
1226 template<typename _ForwardIterator, typename _BinaryPredicate>
1227 _ForwardIterator
1228 unique(_ForwardIterator __first, _ForwardIterator __last,
1229 _BinaryPredicate __binary_pred)
1231 // concept requirements
1232 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1233 _ForwardIterator>)
1234 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1235 typename iterator_traits<_ForwardIterator>::value_type,
1236 typename iterator_traits<_ForwardIterator>::value_type>)
1237 __glibcxx_requires_valid_range(__first, __last);
1239 // Skip the beginning, if already unique.
1240 __first = _GLIBCXX_STD_P::adjacent_find(__first, __last, __binary_pred);
1241 if (__first == __last)
1242 return __last;
1244 // Do the real copy work.
1245 _ForwardIterator __dest = __first;
1246 ++__first;
1247 while (++__first != __last)
1248 if (!bool(__binary_pred(*__dest, *__first)))
1249 *++__dest = _GLIBCXX_MOVE(*__first);
1250 return ++__dest;
1254 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1255 * _OutputIterator)
1256 * overloaded for forward iterators and output iterator as result.
1258 template<typename _ForwardIterator, typename _OutputIterator>
1259 _OutputIterator
1260 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1261 _OutputIterator __result,
1262 forward_iterator_tag, output_iterator_tag)
1264 // concept requirements -- taken care of in dispatching function
1265 _ForwardIterator __next = __first;
1266 *__result = *__first;
1267 while (++__next != __last)
1268 if (!(*__first == *__next))
1270 __first = __next;
1271 *++__result = *__first;
1273 return ++__result;
1277 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1278 * _OutputIterator)
1279 * overloaded for input iterators and output iterator as result.
1281 template<typename _InputIterator, typename _OutputIterator>
1282 _OutputIterator
1283 __unique_copy(_InputIterator __first, _InputIterator __last,
1284 _OutputIterator __result,
1285 input_iterator_tag, output_iterator_tag)
1287 // concept requirements -- taken care of in dispatching function
1288 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1289 *__result = __value;
1290 while (++__first != __last)
1291 if (!(__value == *__first))
1293 __value = *__first;
1294 *++__result = __value;
1296 return ++__result;
1300 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1301 * _OutputIterator)
1302 * overloaded for input iterators and forward iterator as result.
1304 template<typename _InputIterator, typename _ForwardIterator>
1305 _ForwardIterator
1306 __unique_copy(_InputIterator __first, _InputIterator __last,
1307 _ForwardIterator __result,
1308 input_iterator_tag, forward_iterator_tag)
1310 // concept requirements -- taken care of in dispatching function
1311 *__result = *__first;
1312 while (++__first != __last)
1313 if (!(*__result == *__first))
1314 *++__result = *__first;
1315 return ++__result;
1319 * This is an uglified
1320 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1321 * _BinaryPredicate)
1322 * overloaded for forward iterators and output iterator as result.
1324 template<typename _ForwardIterator, typename _OutputIterator,
1325 typename _BinaryPredicate>
1326 _OutputIterator
1327 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1328 _OutputIterator __result, _BinaryPredicate __binary_pred,
1329 forward_iterator_tag, output_iterator_tag)
1331 // concept requirements -- iterators already checked
1332 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1333 typename iterator_traits<_ForwardIterator>::value_type,
1334 typename iterator_traits<_ForwardIterator>::value_type>)
1336 _ForwardIterator __next = __first;
1337 *__result = *__first;
1338 while (++__next != __last)
1339 if (!bool(__binary_pred(*__first, *__next)))
1341 __first = __next;
1342 *++__result = *__first;
1344 return ++__result;
1348 * This is an uglified
1349 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1350 * _BinaryPredicate)
1351 * overloaded for input iterators and output iterator as result.
1353 template<typename _InputIterator, typename _OutputIterator,
1354 typename _BinaryPredicate>
1355 _OutputIterator
1356 __unique_copy(_InputIterator __first, _InputIterator __last,
1357 _OutputIterator __result, _BinaryPredicate __binary_pred,
1358 input_iterator_tag, output_iterator_tag)
1360 // concept requirements -- iterators already checked
1361 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1362 typename iterator_traits<_InputIterator>::value_type,
1363 typename iterator_traits<_InputIterator>::value_type>)
1365 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1366 *__result = __value;
1367 while (++__first != __last)
1368 if (!bool(__binary_pred(__value, *__first)))
1370 __value = *__first;
1371 *++__result = __value;
1373 return ++__result;
1377 * This is an uglified
1378 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1379 * _BinaryPredicate)
1380 * overloaded for input iterators and forward iterator as result.
1382 template<typename _InputIterator, typename _ForwardIterator,
1383 typename _BinaryPredicate>
1384 _ForwardIterator
1385 __unique_copy(_InputIterator __first, _InputIterator __last,
1386 _ForwardIterator __result, _BinaryPredicate __binary_pred,
1387 input_iterator_tag, forward_iterator_tag)
1389 // concept requirements -- iterators already checked
1390 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1391 typename iterator_traits<_ForwardIterator>::value_type,
1392 typename iterator_traits<_InputIterator>::value_type>)
1394 *__result = *__first;
1395 while (++__first != __last)
1396 if (!bool(__binary_pred(*__result, *__first)))
1397 *++__result = *__first;
1398 return ++__result;
1402 * This is an uglified reverse(_BidirectionalIterator,
1403 * _BidirectionalIterator)
1404 * overloaded for bidirectional iterators.
1406 template<typename _BidirectionalIterator>
1407 void
1408 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1409 bidirectional_iterator_tag)
1411 while (true)
1412 if (__first == __last || __first == --__last)
1413 return;
1414 else
1416 std::iter_swap(__first, __last);
1417 ++__first;
1422 * This is an uglified reverse(_BidirectionalIterator,
1423 * _BidirectionalIterator)
1424 * overloaded for random access iterators.
1426 template<typename _RandomAccessIterator>
1427 void
1428 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1429 random_access_iterator_tag)
1431 if (__first == __last)
1432 return;
1433 --__last;
1434 while (__first < __last)
1436 std::iter_swap(__first, __last);
1437 ++__first;
1438 --__last;
1443 * @brief Reverse a sequence.
1444 * @ingroup mutating_algorithms
1445 * @param first A bidirectional iterator.
1446 * @param last A bidirectional iterator.
1447 * @return reverse() returns no value.
1449 * Reverses the order of the elements in the range @p [first,last),
1450 * so that the first element becomes the last etc.
1451 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1452 * swaps @p *(first+i) and @p *(last-(i+1))
1454 template<typename _BidirectionalIterator>
1455 inline void
1456 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1458 // concept requirements
1459 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1460 _BidirectionalIterator>)
1461 __glibcxx_requires_valid_range(__first, __last);
1462 std::__reverse(__first, __last, std::__iterator_category(__first));
1466 * @brief Copy a sequence, reversing its elements.
1467 * @ingroup mutating_algorithms
1468 * @param first A bidirectional iterator.
1469 * @param last A bidirectional iterator.
1470 * @param result An output iterator.
1471 * @return An iterator designating the end of the resulting sequence.
1473 * Copies the elements in the range @p [first,last) to the range
1474 * @p [result,result+(last-first)) such that the order of the
1475 * elements is reversed.
1476 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1477 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1478 * The ranges @p [first,last) and @p [result,result+(last-first))
1479 * must not overlap.
1481 template<typename _BidirectionalIterator, typename _OutputIterator>
1482 _OutputIterator
1483 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1484 _OutputIterator __result)
1486 // concept requirements
1487 __glibcxx_function_requires(_BidirectionalIteratorConcept<
1488 _BidirectionalIterator>)
1489 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1490 typename iterator_traits<_BidirectionalIterator>::value_type>)
1491 __glibcxx_requires_valid_range(__first, __last);
1493 while (__first != __last)
1495 --__last;
1496 *__result = *__last;
1497 ++__result;
1499 return __result;
1503 * This is a helper function for the rotate algorithm specialized on RAIs.
1504 * It returns the greatest common divisor of two integer values.
1506 template<typename _EuclideanRingElement>
1507 _EuclideanRingElement
1508 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1510 while (__n != 0)
1512 _EuclideanRingElement __t = __m % __n;
1513 __m = __n;
1514 __n = __t;
1516 return __m;
1519 /// This is a helper function for the rotate algorithm.
1520 template<typename _ForwardIterator>
1521 void
1522 __rotate(_ForwardIterator __first,
1523 _ForwardIterator __middle,
1524 _ForwardIterator __last,
1525 forward_iterator_tag)
1527 if (__first == __middle || __last == __middle)
1528 return;
1530 _ForwardIterator __first2 = __middle;
1533 std::iter_swap(__first, __first2);
1534 ++__first;
1535 ++__first2;
1536 if (__first == __middle)
1537 __middle = __first2;
1539 while (__first2 != __last);
1541 __first2 = __middle;
1543 while (__first2 != __last)
1545 std::iter_swap(__first, __first2);
1546 ++__first;
1547 ++__first2;
1548 if (__first == __middle)
1549 __middle = __first2;
1550 else if (__first2 == __last)
1551 __first2 = __middle;
1555 /// This is a helper function for the rotate algorithm.
1556 template<typename _BidirectionalIterator>
1557 void
1558 __rotate(_BidirectionalIterator __first,
1559 _BidirectionalIterator __middle,
1560 _BidirectionalIterator __last,
1561 bidirectional_iterator_tag)
1563 // concept requirements
1564 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1565 _BidirectionalIterator>)
1567 if (__first == __middle || __last == __middle)
1568 return;
1570 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1571 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1573 while (__first != __middle && __middle != __last)
1575 std::iter_swap(__first, --__last);
1576 ++__first;
1579 if (__first == __middle)
1580 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1581 else
1582 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1585 /// This is a helper function for the rotate algorithm.
1586 template<typename _RandomAccessIterator>
1587 void
1588 __rotate(_RandomAccessIterator __first,
1589 _RandomAccessIterator __middle,
1590 _RandomAccessIterator __last,
1591 random_access_iterator_tag)
1593 // concept requirements
1594 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1595 _RandomAccessIterator>)
1597 if (__first == __middle || __last == __middle)
1598 return;
1600 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1601 _Distance;
1602 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1603 _ValueType;
1605 const _Distance __n = __last - __first;
1606 const _Distance __k = __middle - __first;
1607 const _Distance __l = __n - __k;
1609 if (__k == __l)
1611 std::swap_ranges(__first, __middle, __middle);
1612 return;
1615 const _Distance __d = std::__gcd(__n, __k);
1617 for (_Distance __i = 0; __i < __d; __i++)
1619 _ValueType __tmp = _GLIBCXX_MOVE(*__first);
1620 _RandomAccessIterator __p = __first;
1622 if (__k < __l)
1624 for (_Distance __j = 0; __j < __l / __d; __j++)
1626 if (__p > __first + __l)
1628 *__p = _GLIBCXX_MOVE(*(__p - __l));
1629 __p -= __l;
1632 *__p = _GLIBCXX_MOVE(*(__p + __k));
1633 __p += __k;
1636 else
1638 for (_Distance __j = 0; __j < __k / __d - 1; __j ++)
1640 if (__p < __last - __k)
1642 *__p = _GLIBCXX_MOVE(*(__p + __k));
1643 __p += __k;
1645 *__p = _GLIBCXX_MOVE(*(__p - __l));
1646 __p -= __l;
1650 *__p = _GLIBCXX_MOVE(__tmp);
1651 ++__first;
1656 * @brief Rotate the elements of a sequence.
1657 * @ingroup mutating_algorithms
1658 * @param first A forward iterator.
1659 * @param middle A forward iterator.
1660 * @param last A forward iterator.
1661 * @return Nothing.
1663 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1664 * positions so that the element at @p middle is moved to @p first, the
1665 * element at @p middle+1 is moved to @first+1 and so on for each element
1666 * in the range @p [first,last).
1668 * This effectively swaps the ranges @p [first,middle) and
1669 * @p [middle,last).
1671 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1672 * each @p n in the range @p [0,last-first).
1674 template<typename _ForwardIterator>
1675 inline void
1676 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1677 _ForwardIterator __last)
1679 // concept requirements
1680 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1681 _ForwardIterator>)
1682 __glibcxx_requires_valid_range(__first, __middle);
1683 __glibcxx_requires_valid_range(__middle, __last);
1685 typedef typename iterator_traits<_ForwardIterator>::iterator_category
1686 _IterType;
1687 std::__rotate(__first, __middle, __last, _IterType());
1691 * @brief Copy a sequence, rotating its elements.
1692 * @ingroup mutating_algorithms
1693 * @param first A forward iterator.
1694 * @param middle A forward iterator.
1695 * @param last A forward iterator.
1696 * @param result An output iterator.
1697 * @return An iterator designating the end of the resulting sequence.
1699 * Copies the elements of the range @p [first,last) to the range
1700 * beginning at @result, rotating the copied elements by @p (middle-first)
1701 * positions so that the element at @p middle is moved to @p result, the
1702 * element at @p middle+1 is moved to @result+1 and so on for each element
1703 * in the range @p [first,last).
1705 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1706 * each @p n in the range @p [0,last-first).
1708 template<typename _ForwardIterator, typename _OutputIterator>
1709 _OutputIterator
1710 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1711 _ForwardIterator __last, _OutputIterator __result)
1713 // concept requirements
1714 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1715 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1716 typename iterator_traits<_ForwardIterator>::value_type>)
1717 __glibcxx_requires_valid_range(__first, __middle);
1718 __glibcxx_requires_valid_range(__middle, __last);
1720 return std::copy(__first, __middle,
1721 std::copy(__middle, __last, __result));
1724 /// This is a helper function...
1725 template<typename _ForwardIterator, typename _Predicate>
1726 _ForwardIterator
1727 __partition(_ForwardIterator __first, _ForwardIterator __last,
1728 _Predicate __pred, forward_iterator_tag)
1730 if (__first == __last)
1731 return __first;
1733 while (__pred(*__first))
1734 if (++__first == __last)
1735 return __first;
1737 _ForwardIterator __next = __first;
1739 while (++__next != __last)
1740 if (__pred(*__next))
1742 std::iter_swap(__first, __next);
1743 ++__first;
1746 return __first;
1749 /// This is a helper function...
1750 template<typename _BidirectionalIterator, typename _Predicate>
1751 _BidirectionalIterator
1752 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1753 _Predicate __pred, bidirectional_iterator_tag)
1755 while (true)
1757 while (true)
1758 if (__first == __last)
1759 return __first;
1760 else if (__pred(*__first))
1761 ++__first;
1762 else
1763 break;
1764 --__last;
1765 while (true)
1766 if (__first == __last)
1767 return __first;
1768 else if (!bool(__pred(*__last)))
1769 --__last;
1770 else
1771 break;
1772 std::iter_swap(__first, __last);
1773 ++__first;
1777 // partition
1779 /// This is a helper function...
1780 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
1781 _ForwardIterator
1782 __inplace_stable_partition(_ForwardIterator __first,
1783 _ForwardIterator __last,
1784 _Predicate __pred, _Distance __len)
1786 if (__len == 1)
1787 return __pred(*__first) ? __last : __first;
1788 _ForwardIterator __middle = __first;
1789 std::advance(__middle, __len / 2);
1790 _ForwardIterator __begin = std::__inplace_stable_partition(__first,
1791 __middle,
1792 __pred,
1793 __len / 2);
1794 _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
1795 __pred,
1796 __len
1797 - __len / 2);
1798 std::rotate(__begin, __middle, __end);
1799 std::advance(__begin, std::distance(__middle, __end));
1800 return __begin;
1803 /// This is a helper function...
1804 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1805 typename _Distance>
1806 _ForwardIterator
1807 __stable_partition_adaptive(_ForwardIterator __first,
1808 _ForwardIterator __last,
1809 _Predicate __pred, _Distance __len,
1810 _Pointer __buffer,
1811 _Distance __buffer_size)
1813 if (__len <= __buffer_size)
1815 _ForwardIterator __result1 = __first;
1816 _Pointer __result2 = __buffer;
1817 for (; __first != __last; ++__first)
1818 if (__pred(*__first))
1820 *__result1 = *__first;
1821 ++__result1;
1823 else
1825 *__result2 = *__first;
1826 ++__result2;
1828 std::copy(__buffer, __result2, __result1);
1829 return __result1;
1831 else
1833 _ForwardIterator __middle = __first;
1834 std::advance(__middle, __len / 2);
1835 _ForwardIterator __begin =
1836 std::__stable_partition_adaptive(__first, __middle, __pred,
1837 __len / 2, __buffer,
1838 __buffer_size);
1839 _ForwardIterator __end =
1840 std::__stable_partition_adaptive(__middle, __last, __pred,
1841 __len - __len / 2,
1842 __buffer, __buffer_size);
1843 std::rotate(__begin, __middle, __end);
1844 std::advance(__begin, std::distance(__middle, __end));
1845 return __begin;
1850 * @brief Move elements for which a predicate is true to the beginning
1851 * of a sequence, preserving relative ordering.
1852 * @ingroup mutating_algorithms
1853 * @param first A forward iterator.
1854 * @param last A forward iterator.
1855 * @param pred A predicate functor.
1856 * @return An iterator @p middle such that @p pred(i) is true for each
1857 * iterator @p i in the range @p [first,middle) and false for each @p i
1858 * in the range @p [middle,last).
1860 * Performs the same function as @p partition() with the additional
1861 * guarantee that the relative ordering of elements in each group is
1862 * preserved, so any two elements @p x and @p y in the range
1863 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1864 * relative ordering after calling @p stable_partition().
1866 template<typename _ForwardIterator, typename _Predicate>
1867 _ForwardIterator
1868 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1869 _Predicate __pred)
1871 // concept requirements
1872 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1873 _ForwardIterator>)
1874 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1875 typename iterator_traits<_ForwardIterator>::value_type>)
1876 __glibcxx_requires_valid_range(__first, __last);
1878 if (__first == __last)
1879 return __first;
1880 else
1882 typedef typename iterator_traits<_ForwardIterator>::value_type
1883 _ValueType;
1884 typedef typename iterator_traits<_ForwardIterator>::difference_type
1885 _DistanceType;
1887 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
1888 __last);
1889 if (__buf.size() > 0)
1890 return
1891 std::__stable_partition_adaptive(__first, __last, __pred,
1892 _DistanceType(__buf.requested_size()),
1893 __buf.begin(),
1894 _DistanceType(__buf.size()));
1895 else
1896 return
1897 std::__inplace_stable_partition(__first, __last, __pred,
1898 _DistanceType(__buf.requested_size()));
1902 /// This is a helper function for the sort routines.
1903 template<typename _RandomAccessIterator>
1904 void
1905 __heap_select(_RandomAccessIterator __first,
1906 _RandomAccessIterator __middle,
1907 _RandomAccessIterator __last)
1909 std::make_heap(__first, __middle);
1910 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1911 if (*__i < *__first)
1912 std::__pop_heap(__first, __middle, __i);
1915 /// This is a helper function for the sort routines.
1916 template<typename _RandomAccessIterator, typename _Compare>
1917 void
1918 __heap_select(_RandomAccessIterator __first,
1919 _RandomAccessIterator __middle,
1920 _RandomAccessIterator __last, _Compare __comp)
1922 std::make_heap(__first, __middle, __comp);
1923 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1924 if (__comp(*__i, *__first))
1925 std::__pop_heap(__first, __middle, __i, __comp);
1928 // partial_sort
1931 * @brief Copy the smallest elements of a sequence.
1932 * @ingroup sorting_algorithms
1933 * @param first An iterator.
1934 * @param last Another iterator.
1935 * @param result_first A random-access iterator.
1936 * @param result_last Another random-access iterator.
1937 * @return An iterator indicating the end of the resulting sequence.
1939 * Copies and sorts the smallest N values from the range @p [first,last)
1940 * to the range beginning at @p result_first, where the number of
1941 * elements to be copied, @p N, is the smaller of @p (last-first) and
1942 * @p (result_last-result_first).
1943 * After the sort if @p i and @j are iterators in the range
1944 * @p [result_first,result_first+N) such that @i precedes @j then
1945 * @p *j<*i is false.
1946 * The value returned is @p result_first+N.
1948 template<typename _InputIterator, typename _RandomAccessIterator>
1949 _RandomAccessIterator
1950 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1951 _RandomAccessIterator __result_first,
1952 _RandomAccessIterator __result_last)
1954 typedef typename iterator_traits<_InputIterator>::value_type
1955 _InputValueType;
1956 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1957 _OutputValueType;
1958 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1959 _DistanceType;
1961 // concept requirements
1962 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1963 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1964 _OutputValueType>)
1965 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
1966 _OutputValueType>)
1967 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
1968 __glibcxx_requires_valid_range(__first, __last);
1969 __glibcxx_requires_valid_range(__result_first, __result_last);
1971 if (__result_first == __result_last)
1972 return __result_last;
1973 _RandomAccessIterator __result_real_last = __result_first;
1974 while(__first != __last && __result_real_last != __result_last)
1976 *__result_real_last = *__first;
1977 ++__result_real_last;
1978 ++__first;
1980 std::make_heap(__result_first, __result_real_last);
1981 while (__first != __last)
1983 if (*__first < *__result_first)
1984 std::__adjust_heap(__result_first, _DistanceType(0),
1985 _DistanceType(__result_real_last
1986 - __result_first),
1987 _InputValueType(*__first));
1988 ++__first;
1990 std::sort_heap(__result_first, __result_real_last);
1991 return __result_real_last;
1995 * @brief Copy the smallest elements of a sequence using a predicate for
1996 * comparison.
1997 * @ingroup sorting_algorithms
1998 * @param first An input iterator.
1999 * @param last Another input iterator.
2000 * @param result_first A random-access iterator.
2001 * @param result_last Another random-access iterator.
2002 * @param comp A comparison functor.
2003 * @return An iterator indicating the end of the resulting sequence.
2005 * Copies and sorts the smallest N values from the range @p [first,last)
2006 * to the range beginning at @p result_first, where the number of
2007 * elements to be copied, @p N, is the smaller of @p (last-first) and
2008 * @p (result_last-result_first).
2009 * After the sort if @p i and @j are iterators in the range
2010 * @p [result_first,result_first+N) such that @i precedes @j then
2011 * @p comp(*j,*i) is false.
2012 * The value returned is @p result_first+N.
2014 template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
2015 _RandomAccessIterator
2016 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2017 _RandomAccessIterator __result_first,
2018 _RandomAccessIterator __result_last,
2019 _Compare __comp)
2021 typedef typename iterator_traits<_InputIterator>::value_type
2022 _InputValueType;
2023 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2024 _OutputValueType;
2025 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2026 _DistanceType;
2028 // concept requirements
2029 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2030 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2031 _RandomAccessIterator>)
2032 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2033 _OutputValueType>)
2034 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2035 _InputValueType, _OutputValueType>)
2036 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2037 _OutputValueType, _OutputValueType>)
2038 __glibcxx_requires_valid_range(__first, __last);
2039 __glibcxx_requires_valid_range(__result_first, __result_last);
2041 if (__result_first == __result_last)
2042 return __result_last;
2043 _RandomAccessIterator __result_real_last = __result_first;
2044 while(__first != __last && __result_real_last != __result_last)
2046 *__result_real_last = *__first;
2047 ++__result_real_last;
2048 ++__first;
2050 std::make_heap(__result_first, __result_real_last, __comp);
2051 while (__first != __last)
2053 if (__comp(*__first, *__result_first))
2054 std::__adjust_heap(__result_first, _DistanceType(0),
2055 _DistanceType(__result_real_last
2056 - __result_first),
2057 _InputValueType(*__first),
2058 __comp);
2059 ++__first;
2061 std::sort_heap(__result_first, __result_real_last, __comp);
2062 return __result_real_last;
2065 /// This is a helper function for the sort routine.
2066 template<typename _RandomAccessIterator, typename _Tp>
2067 void
2068 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
2070 _RandomAccessIterator __next = __last;
2071 --__next;
2072 while (__val < *__next)
2074 *__last = *__next;
2075 __last = __next;
2076 --__next;
2078 *__last = __val;
2081 /// This is a helper function for the sort routine.
2082 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2083 void
2084 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
2085 _Compare __comp)
2087 _RandomAccessIterator __next = __last;
2088 --__next;
2089 while (__comp(__val, *__next))
2091 *__last = *__next;
2092 __last = __next;
2093 --__next;
2095 *__last = __val;
2098 /// This is a helper function for the sort routine.
2099 template<typename _RandomAccessIterator>
2100 void
2101 __insertion_sort(_RandomAccessIterator __first,
2102 _RandomAccessIterator __last)
2104 if (__first == __last)
2105 return;
2107 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2109 typename iterator_traits<_RandomAccessIterator>::value_type
2110 __val = *__i;
2111 if (__val < *__first)
2113 std::copy_backward(__first, __i, __i + 1);
2114 *__first = __val;
2116 else
2117 std::__unguarded_linear_insert(__i, __val);
2121 /// This is a helper function for the sort routine.
2122 template<typename _RandomAccessIterator, typename _Compare>
2123 void
2124 __insertion_sort(_RandomAccessIterator __first,
2125 _RandomAccessIterator __last, _Compare __comp)
2127 if (__first == __last) return;
2129 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2131 typename iterator_traits<_RandomAccessIterator>::value_type
2132 __val = *__i;
2133 if (__comp(__val, *__first))
2135 std::copy_backward(__first, __i, __i + 1);
2136 *__first = __val;
2138 else
2139 std::__unguarded_linear_insert(__i, __val, __comp);
2143 /// This is a helper function for the sort routine.
2144 template<typename _RandomAccessIterator>
2145 inline void
2146 __unguarded_insertion_sort(_RandomAccessIterator __first,
2147 _RandomAccessIterator __last)
2149 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2150 _ValueType;
2152 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2153 std::__unguarded_linear_insert(__i, _ValueType(*__i));
2156 /// This is a helper function for the sort routine.
2157 template<typename _RandomAccessIterator, typename _Compare>
2158 inline void
2159 __unguarded_insertion_sort(_RandomAccessIterator __first,
2160 _RandomAccessIterator __last, _Compare __comp)
2162 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2163 _ValueType;
2165 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2166 std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
2170 * @doctodo
2171 * This controls some aspect of the sort routines.
2173 enum { _S_threshold = 16 };
2175 /// This is a helper function for the sort routine.
2176 template<typename _RandomAccessIterator>
2177 void
2178 __final_insertion_sort(_RandomAccessIterator __first,
2179 _RandomAccessIterator __last)
2181 if (__last - __first > int(_S_threshold))
2183 std::__insertion_sort(__first, __first + int(_S_threshold));
2184 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
2186 else
2187 std::__insertion_sort(__first, __last);
2190 /// This is a helper function for the sort routine.
2191 template<typename _RandomAccessIterator, typename _Compare>
2192 void
2193 __final_insertion_sort(_RandomAccessIterator __first,
2194 _RandomAccessIterator __last, _Compare __comp)
2196 if (__last - __first > int(_S_threshold))
2198 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
2199 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
2200 __comp);
2202 else
2203 std::__insertion_sort(__first, __last, __comp);
2206 /// This is a helper function...
2207 template<typename _RandomAccessIterator, typename _Tp>
2208 _RandomAccessIterator
2209 __unguarded_partition(_RandomAccessIterator __first,
2210 _RandomAccessIterator __last, _Tp __pivot)
2212 while (true)
2214 while (*__first < __pivot)
2215 ++__first;
2216 --__last;
2217 while (__pivot < *__last)
2218 --__last;
2219 if (!(__first < __last))
2220 return __first;
2221 std::iter_swap(__first, __last);
2222 ++__first;
2226 /// This is a helper function...
2227 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2228 _RandomAccessIterator
2229 __unguarded_partition(_RandomAccessIterator __first,
2230 _RandomAccessIterator __last,
2231 _Tp __pivot, _Compare __comp)
2233 while (true)
2235 while (__comp(*__first, __pivot))
2236 ++__first;
2237 --__last;
2238 while (__comp(__pivot, *__last))
2239 --__last;
2240 if (!(__first < __last))
2241 return __first;
2242 std::iter_swap(__first, __last);
2243 ++__first;
2247 /// This is a helper function for the sort routine.
2248 template<typename _RandomAccessIterator, typename _Size>
2249 void
2250 __introsort_loop(_RandomAccessIterator __first,
2251 _RandomAccessIterator __last,
2252 _Size __depth_limit)
2254 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2255 _ValueType;
2257 while (__last - __first > int(_S_threshold))
2259 if (__depth_limit == 0)
2261 _GLIBCXX_STD_P::partial_sort(__first, __last, __last);
2262 return;
2264 --__depth_limit;
2265 _RandomAccessIterator __cut =
2266 std::__unguarded_partition(__first, __last,
2267 _ValueType(std::__median(*__first,
2268 *(__first
2269 + (__last
2270 - __first)
2271 / 2),
2272 *(__last
2273 - 1))));
2274 std::__introsort_loop(__cut, __last, __depth_limit);
2275 __last = __cut;
2279 /// This is a helper function for the sort routine.
2280 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2281 void
2282 __introsort_loop(_RandomAccessIterator __first,
2283 _RandomAccessIterator __last,
2284 _Size __depth_limit, _Compare __comp)
2286 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2287 _ValueType;
2289 while (__last - __first > int(_S_threshold))
2291 if (__depth_limit == 0)
2293 _GLIBCXX_STD_P::partial_sort(__first, __last, __last, __comp);
2294 return;
2296 --__depth_limit;
2297 _RandomAccessIterator __cut =
2298 std::__unguarded_partition(__first, __last,
2299 _ValueType(std::__median(*__first,
2300 *(__first
2301 + (__last
2302 - __first)
2303 / 2),
2304 *(__last - 1),
2305 __comp)),
2306 __comp);
2307 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
2308 __last = __cut;
2312 /// This is a helper function for the sort routines. Precondition: __n > 0.
2313 template<typename _Size>
2314 inline _Size
2315 __lg(_Size __n)
2317 _Size __k;
2318 for (__k = 0; __n != 0; __n >>= 1)
2319 ++__k;
2320 return __k - 1;
2323 inline int
2324 __lg(int __n)
2325 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
2327 inline long
2328 __lg(long __n)
2329 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
2331 inline long long
2332 __lg(long long __n)
2333 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
2335 // sort
2337 template<typename _RandomAccessIterator, typename _Size>
2338 void
2339 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2340 _RandomAccessIterator __last, _Size __depth_limit)
2342 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2343 _ValueType;
2345 while (__last - __first > 3)
2347 if (__depth_limit == 0)
2349 std::__heap_select(__first, __nth + 1, __last);
2351 // Place the nth largest element in its final position.
2352 std::iter_swap(__first, __nth);
2353 return;
2355 --__depth_limit;
2356 _RandomAccessIterator __cut =
2357 std::__unguarded_partition(__first, __last,
2358 _ValueType(std::__median(*__first,
2359 *(__first
2360 + (__last
2361 - __first)
2362 / 2),
2363 *(__last
2364 - 1))));
2365 if (__cut <= __nth)
2366 __first = __cut;
2367 else
2368 __last = __cut;
2370 std::__insertion_sort(__first, __last);
2373 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2374 void
2375 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2376 _RandomAccessIterator __last, _Size __depth_limit,
2377 _Compare __comp)
2379 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2380 _ValueType;
2382 while (__last - __first > 3)
2384 if (__depth_limit == 0)
2386 std::__heap_select(__first, __nth + 1, __last, __comp);
2387 // Place the nth largest element in its final position.
2388 std::iter_swap(__first, __nth);
2389 return;
2391 --__depth_limit;
2392 _RandomAccessIterator __cut =
2393 std::__unguarded_partition(__first, __last,
2394 _ValueType(std::__median(*__first,
2395 *(__first
2396 + (__last
2397 - __first)
2398 / 2),
2399 *(__last - 1),
2400 __comp)),
2401 __comp);
2402 if (__cut <= __nth)
2403 __first = __cut;
2404 else
2405 __last = __cut;
2407 std::__insertion_sort(__first, __last, __comp);
2410 // nth_element
2413 * @brief Finds the first position in which @a val could be inserted
2414 * without changing the ordering.
2415 * @param first An iterator.
2416 * @param last Another iterator.
2417 * @param val The search term.
2418 * @return An iterator pointing to the first element "not less
2419 * than" @a val, or end() if every element is less than
2420 * @a val.
2421 * @ingroup binary_search_algorithms
2423 template<typename _ForwardIterator, typename _Tp>
2424 _ForwardIterator
2425 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2426 const _Tp& __val)
2428 typedef typename iterator_traits<_ForwardIterator>::value_type
2429 _ValueType;
2430 typedef typename iterator_traits<_ForwardIterator>::difference_type
2431 _DistanceType;
2433 // concept requirements
2434 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2435 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2436 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2438 _DistanceType __len = std::distance(__first, __last);
2439 _DistanceType __half;
2440 _ForwardIterator __middle;
2442 while (__len > 0)
2444 __half = __len >> 1;
2445 __middle = __first;
2446 std::advance(__middle, __half);
2447 if (*__middle < __val)
2449 __first = __middle;
2450 ++__first;
2451 __len = __len - __half - 1;
2453 else
2454 __len = __half;
2456 return __first;
2460 * @brief Finds the first position in which @a val could be inserted
2461 * without changing the ordering.
2462 * @ingroup binary_search_algorithms
2463 * @param first An iterator.
2464 * @param last Another iterator.
2465 * @param val The search term.
2466 * @param comp A functor to use for comparisons.
2467 * @return An iterator pointing to the first element "not less than" @a val,
2468 * or end() if every element is less than @a val.
2469 * @ingroup binary_search_algorithms
2471 * The comparison function should have the same effects on ordering as
2472 * the function used for the initial sort.
2474 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2475 _ForwardIterator
2476 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2477 const _Tp& __val, _Compare __comp)
2479 typedef typename iterator_traits<_ForwardIterator>::value_type
2480 _ValueType;
2481 typedef typename iterator_traits<_ForwardIterator>::difference_type
2482 _DistanceType;
2484 // concept requirements
2485 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2486 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2487 _ValueType, _Tp>)
2488 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2489 __val, __comp);
2491 _DistanceType __len = std::distance(__first, __last);
2492 _DistanceType __half;
2493 _ForwardIterator __middle;
2495 while (__len > 0)
2497 __half = __len >> 1;
2498 __middle = __first;
2499 std::advance(__middle, __half);
2500 if (__comp(*__middle, __val))
2502 __first = __middle;
2503 ++__first;
2504 __len = __len - __half - 1;
2506 else
2507 __len = __half;
2509 return __first;
2513 * @brief Finds the last position in which @a val could be inserted
2514 * without changing the ordering.
2515 * @ingroup binary_search_algorithms
2516 * @param first An iterator.
2517 * @param last Another iterator.
2518 * @param val The search term.
2519 * @return An iterator pointing to the first element greater than @a val,
2520 * or end() if no elements are greater than @a val.
2521 * @ingroup binary_search_algorithms
2523 template<typename _ForwardIterator, typename _Tp>
2524 _ForwardIterator
2525 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2526 const _Tp& __val)
2528 typedef typename iterator_traits<_ForwardIterator>::value_type
2529 _ValueType;
2530 typedef typename iterator_traits<_ForwardIterator>::difference_type
2531 _DistanceType;
2533 // concept requirements
2534 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2535 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2536 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2538 _DistanceType __len = std::distance(__first, __last);
2539 _DistanceType __half;
2540 _ForwardIterator __middle;
2542 while (__len > 0)
2544 __half = __len >> 1;
2545 __middle = __first;
2546 std::advance(__middle, __half);
2547 if (__val < *__middle)
2548 __len = __half;
2549 else
2551 __first = __middle;
2552 ++__first;
2553 __len = __len - __half - 1;
2556 return __first;
2560 * @brief Finds the last position in which @a val could be inserted
2561 * without changing the ordering.
2562 * @ingroup binary_search_algorithms
2563 * @param first An iterator.
2564 * @param last Another iterator.
2565 * @param val The search term.
2566 * @param comp A functor to use for comparisons.
2567 * @return An iterator pointing to the first element greater than @a val,
2568 * or end() if no elements are greater than @a val.
2569 * @ingroup binary_search_algorithms
2571 * The comparison function should have the same effects on ordering as
2572 * the function used for the initial sort.
2574 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2575 _ForwardIterator
2576 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2577 const _Tp& __val, _Compare __comp)
2579 typedef typename iterator_traits<_ForwardIterator>::value_type
2580 _ValueType;
2581 typedef typename iterator_traits<_ForwardIterator>::difference_type
2582 _DistanceType;
2584 // concept requirements
2585 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2586 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2587 _Tp, _ValueType>)
2588 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2589 __val, __comp);
2591 _DistanceType __len = std::distance(__first, __last);
2592 _DistanceType __half;
2593 _ForwardIterator __middle;
2595 while (__len > 0)
2597 __half = __len >> 1;
2598 __middle = __first;
2599 std::advance(__middle, __half);
2600 if (__comp(__val, *__middle))
2601 __len = __half;
2602 else
2604 __first = __middle;
2605 ++__first;
2606 __len = __len - __half - 1;
2609 return __first;
2613 * @brief Finds the largest subrange in which @a val could be inserted
2614 * at any place in it without changing the ordering.
2615 * @ingroup binary_search_algorithms
2616 * @param first An iterator.
2617 * @param last Another iterator.
2618 * @param val The search term.
2619 * @return An pair of iterators defining the subrange.
2620 * @ingroup binary_search_algorithms
2622 * This is equivalent to
2623 * @code
2624 * std::make_pair(lower_bound(first, last, val),
2625 * upper_bound(first, last, val))
2626 * @endcode
2627 * but does not actually call those functions.
2629 template<typename _ForwardIterator, typename _Tp>
2630 pair<_ForwardIterator, _ForwardIterator>
2631 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2632 const _Tp& __val)
2634 typedef typename iterator_traits<_ForwardIterator>::value_type
2635 _ValueType;
2636 typedef typename iterator_traits<_ForwardIterator>::difference_type
2637 _DistanceType;
2639 // concept requirements
2640 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2641 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2642 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2643 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2644 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2646 _DistanceType __len = std::distance(__first, __last);
2647 _DistanceType __half;
2648 _ForwardIterator __middle, __left, __right;
2650 while (__len > 0)
2652 __half = __len >> 1;
2653 __middle = __first;
2654 std::advance(__middle, __half);
2655 if (*__middle < __val)
2657 __first = __middle;
2658 ++__first;
2659 __len = __len - __half - 1;
2661 else if (__val < *__middle)
2662 __len = __half;
2663 else
2665 __left = std::lower_bound(__first, __middle, __val);
2666 std::advance(__first, __len);
2667 __right = std::upper_bound(++__middle, __first, __val);
2668 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2671 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2675 * @brief Finds the largest subrange in which @a val could be inserted
2676 * at any place in it without changing the ordering.
2677 * @param first An iterator.
2678 * @param last Another iterator.
2679 * @param val The search term.
2680 * @param comp A functor to use for comparisons.
2681 * @return An pair of iterators defining the subrange.
2682 * @ingroup binary_search_algorithms
2684 * This is equivalent to
2685 * @code
2686 * std::make_pair(lower_bound(first, last, val, comp),
2687 * upper_bound(first, last, val, comp))
2688 * @endcode
2689 * but does not actually call those functions.
2691 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2692 pair<_ForwardIterator, _ForwardIterator>
2693 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2694 const _Tp& __val,
2695 _Compare __comp)
2697 typedef typename iterator_traits<_ForwardIterator>::value_type
2698 _ValueType;
2699 typedef typename iterator_traits<_ForwardIterator>::difference_type
2700 _DistanceType;
2702 // concept requirements
2703 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2704 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2705 _ValueType, _Tp>)
2706 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2707 _Tp, _ValueType>)
2708 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2709 __val, __comp);
2710 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2711 __val, __comp);
2713 _DistanceType __len = std::distance(__first, __last);
2714 _DistanceType __half;
2715 _ForwardIterator __middle, __left, __right;
2717 while (__len > 0)
2719 __half = __len >> 1;
2720 __middle = __first;
2721 std::advance(__middle, __half);
2722 if (__comp(*__middle, __val))
2724 __first = __middle;
2725 ++__first;
2726 __len = __len - __half - 1;
2728 else if (__comp(__val, *__middle))
2729 __len = __half;
2730 else
2732 __left = std::lower_bound(__first, __middle, __val, __comp);
2733 std::advance(__first, __len);
2734 __right = std::upper_bound(++__middle, __first, __val, __comp);
2735 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2738 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2742 * @brief Determines whether an element exists in a range.
2743 * @ingroup binary_search_algorithms
2744 * @param first An iterator.
2745 * @param last Another iterator.
2746 * @param val The search term.
2747 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2749 * Note that this does not actually return an iterator to @a val. For
2750 * that, use std::find or a container's specialized find member functions.
2752 template<typename _ForwardIterator, typename _Tp>
2753 bool
2754 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2755 const _Tp& __val)
2757 typedef typename iterator_traits<_ForwardIterator>::value_type
2758 _ValueType;
2760 // concept requirements
2761 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2762 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2763 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2764 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2766 _ForwardIterator __i = std::lower_bound(__first, __last, __val);
2767 return __i != __last && !(__val < *__i);
2771 * @brief Determines whether an element exists in a range.
2772 * @ingroup binary_search_algorithms
2773 * @param first An iterator.
2774 * @param last Another iterator.
2775 * @param val The search term.
2776 * @param comp A functor to use for comparisons.
2777 * @return True if @a val (or its equivalent) is in [@a first,@a last ].
2779 * Note that this does not actually return an iterator to @a val. For
2780 * that, use std::find or a container's specialized find member functions.
2782 * The comparison function should have the same effects on ordering as
2783 * the function used for the initial sort.
2785 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2786 bool
2787 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2788 const _Tp& __val, _Compare __comp)
2790 typedef typename iterator_traits<_ForwardIterator>::value_type
2791 _ValueType;
2793 // concept requirements
2794 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2795 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2796 _Tp, _ValueType>)
2797 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2798 __val, __comp);
2799 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2800 __val, __comp);
2802 _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
2803 return __i != __last && !bool(__comp(__val, *__i));
2806 // merge
2808 /// This is a helper function for the merge routines.
2809 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2810 typename _BidirectionalIterator3>
2811 _BidirectionalIterator3
2812 __merge_backward(_BidirectionalIterator1 __first1,
2813 _BidirectionalIterator1 __last1,
2814 _BidirectionalIterator2 __first2,
2815 _BidirectionalIterator2 __last2,
2816 _BidirectionalIterator3 __result)
2818 if (__first1 == __last1)
2819 return std::copy_backward(__first2, __last2, __result);
2820 if (__first2 == __last2)
2821 return std::copy_backward(__first1, __last1, __result);
2822 --__last1;
2823 --__last2;
2824 while (true)
2826 if (*__last2 < *__last1)
2828 *--__result = *__last1;
2829 if (__first1 == __last1)
2830 return std::copy_backward(__first2, ++__last2, __result);
2831 --__last1;
2833 else
2835 *--__result = *__last2;
2836 if (__first2 == __last2)
2837 return std::copy_backward(__first1, ++__last1, __result);
2838 --__last2;
2843 /// This is a helper function for the merge routines.
2844 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2845 typename _BidirectionalIterator3, typename _Compare>
2846 _BidirectionalIterator3
2847 __merge_backward(_BidirectionalIterator1 __first1,
2848 _BidirectionalIterator1 __last1,
2849 _BidirectionalIterator2 __first2,
2850 _BidirectionalIterator2 __last2,
2851 _BidirectionalIterator3 __result,
2852 _Compare __comp)
2854 if (__first1 == __last1)
2855 return std::copy_backward(__first2, __last2, __result);
2856 if (__first2 == __last2)
2857 return std::copy_backward(__first1, __last1, __result);
2858 --__last1;
2859 --__last2;
2860 while (true)
2862 if (__comp(*__last2, *__last1))
2864 *--__result = *__last1;
2865 if (__first1 == __last1)
2866 return std::copy_backward(__first2, ++__last2, __result);
2867 --__last1;
2869 else
2871 *--__result = *__last2;
2872 if (__first2 == __last2)
2873 return std::copy_backward(__first1, ++__last1, __result);
2874 --__last2;
2879 /// This is a helper function for the merge routines.
2880 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2881 typename _Distance>
2882 _BidirectionalIterator1
2883 __rotate_adaptive(_BidirectionalIterator1 __first,
2884 _BidirectionalIterator1 __middle,
2885 _BidirectionalIterator1 __last,
2886 _Distance __len1, _Distance __len2,
2887 _BidirectionalIterator2 __buffer,
2888 _Distance __buffer_size)
2890 _BidirectionalIterator2 __buffer_end;
2891 if (__len1 > __len2 && __len2 <= __buffer_size)
2893 __buffer_end = std::copy(__middle, __last, __buffer);
2894 std::copy_backward(__first, __middle, __last);
2895 return std::copy(__buffer, __buffer_end, __first);
2897 else if (__len1 <= __buffer_size)
2899 __buffer_end = std::copy(__first, __middle, __buffer);
2900 std::copy(__middle, __last, __first);
2901 return std::copy_backward(__buffer, __buffer_end, __last);
2903 else
2905 std::rotate(__first, __middle, __last);
2906 std::advance(__first, std::distance(__middle, __last));
2907 return __first;
2911 /// This is a helper function for the merge routines.
2912 template<typename _BidirectionalIterator, typename _Distance,
2913 typename _Pointer>
2914 void
2915 __merge_adaptive(_BidirectionalIterator __first,
2916 _BidirectionalIterator __middle,
2917 _BidirectionalIterator __last,
2918 _Distance __len1, _Distance __len2,
2919 _Pointer __buffer, _Distance __buffer_size)
2921 if (__len1 <= __len2 && __len1 <= __buffer_size)
2923 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
2924 _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last,
2925 __first);
2927 else if (__len2 <= __buffer_size)
2929 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
2930 std::__merge_backward(__first, __middle, __buffer,
2931 __buffer_end, __last);
2933 else
2935 _BidirectionalIterator __first_cut = __first;
2936 _BidirectionalIterator __second_cut = __middle;
2937 _Distance __len11 = 0;
2938 _Distance __len22 = 0;
2939 if (__len1 > __len2)
2941 __len11 = __len1 / 2;
2942 std::advance(__first_cut, __len11);
2943 __second_cut = std::lower_bound(__middle, __last,
2944 *__first_cut);
2945 __len22 = std::distance(__middle, __second_cut);
2947 else
2949 __len22 = __len2 / 2;
2950 std::advance(__second_cut, __len22);
2951 __first_cut = std::upper_bound(__first, __middle,
2952 *__second_cut);
2953 __len11 = std::distance(__first, __first_cut);
2955 _BidirectionalIterator __new_middle =
2956 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2957 __len1 - __len11, __len22, __buffer,
2958 __buffer_size);
2959 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2960 __len22, __buffer, __buffer_size);
2961 std::__merge_adaptive(__new_middle, __second_cut, __last,
2962 __len1 - __len11,
2963 __len2 - __len22, __buffer, __buffer_size);
2967 /// This is a helper function for the merge routines.
2968 template<typename _BidirectionalIterator, typename _Distance,
2969 typename _Pointer, typename _Compare>
2970 void
2971 __merge_adaptive(_BidirectionalIterator __first,
2972 _BidirectionalIterator __middle,
2973 _BidirectionalIterator __last,
2974 _Distance __len1, _Distance __len2,
2975 _Pointer __buffer, _Distance __buffer_size,
2976 _Compare __comp)
2978 if (__len1 <= __len2 && __len1 <= __buffer_size)
2980 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
2981 _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last,
2982 __first, __comp);
2984 else if (__len2 <= __buffer_size)
2986 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
2987 std::__merge_backward(__first, __middle, __buffer, __buffer_end,
2988 __last, __comp);
2990 else
2992 _BidirectionalIterator __first_cut = __first;
2993 _BidirectionalIterator __second_cut = __middle;
2994 _Distance __len11 = 0;
2995 _Distance __len22 = 0;
2996 if (__len1 > __len2)
2998 __len11 = __len1 / 2;
2999 std::advance(__first_cut, __len11);
3000 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3001 __comp);
3002 __len22 = std::distance(__middle, __second_cut);
3004 else
3006 __len22 = __len2 / 2;
3007 std::advance(__second_cut, __len22);
3008 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3009 __comp);
3010 __len11 = std::distance(__first, __first_cut);
3012 _BidirectionalIterator __new_middle =
3013 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3014 __len1 - __len11, __len22, __buffer,
3015 __buffer_size);
3016 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3017 __len22, __buffer, __buffer_size, __comp);
3018 std::__merge_adaptive(__new_middle, __second_cut, __last,
3019 __len1 - __len11,
3020 __len2 - __len22, __buffer,
3021 __buffer_size, __comp);
3025 /// This is a helper function for the merge routines.
3026 template<typename _BidirectionalIterator, typename _Distance>
3027 void
3028 __merge_without_buffer(_BidirectionalIterator __first,
3029 _BidirectionalIterator __middle,
3030 _BidirectionalIterator __last,
3031 _Distance __len1, _Distance __len2)
3033 if (__len1 == 0 || __len2 == 0)
3034 return;
3035 if (__len1 + __len2 == 2)
3037 if (*__middle < *__first)
3038 std::iter_swap(__first, __middle);
3039 return;
3041 _BidirectionalIterator __first_cut = __first;
3042 _BidirectionalIterator __second_cut = __middle;
3043 _Distance __len11 = 0;
3044 _Distance __len22 = 0;
3045 if (__len1 > __len2)
3047 __len11 = __len1 / 2;
3048 std::advance(__first_cut, __len11);
3049 __second_cut = std::lower_bound(__middle, __last, *__first_cut);
3050 __len22 = std::distance(__middle, __second_cut);
3052 else
3054 __len22 = __len2 / 2;
3055 std::advance(__second_cut, __len22);
3056 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
3057 __len11 = std::distance(__first, __first_cut);
3059 std::rotate(__first_cut, __middle, __second_cut);
3060 _BidirectionalIterator __new_middle = __first_cut;
3061 std::advance(__new_middle, std::distance(__middle, __second_cut));
3062 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3063 __len11, __len22);
3064 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3065 __len1 - __len11, __len2 - __len22);
3068 /// This is a helper function for the merge routines.
3069 template<typename _BidirectionalIterator, typename _Distance,
3070 typename _Compare>
3071 void
3072 __merge_without_buffer(_BidirectionalIterator __first,
3073 _BidirectionalIterator __middle,
3074 _BidirectionalIterator __last,
3075 _Distance __len1, _Distance __len2,
3076 _Compare __comp)
3078 if (__len1 == 0 || __len2 == 0)
3079 return;
3080 if (__len1 + __len2 == 2)
3082 if (__comp(*__middle, *__first))
3083 std::iter_swap(__first, __middle);
3084 return;
3086 _BidirectionalIterator __first_cut = __first;
3087 _BidirectionalIterator __second_cut = __middle;
3088 _Distance __len11 = 0;
3089 _Distance __len22 = 0;
3090 if (__len1 > __len2)
3092 __len11 = __len1 / 2;
3093 std::advance(__first_cut, __len11);
3094 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3095 __comp);
3096 __len22 = std::distance(__middle, __second_cut);
3098 else
3100 __len22 = __len2 / 2;
3101 std::advance(__second_cut, __len22);
3102 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3103 __comp);
3104 __len11 = std::distance(__first, __first_cut);
3106 std::rotate(__first_cut, __middle, __second_cut);
3107 _BidirectionalIterator __new_middle = __first_cut;
3108 std::advance(__new_middle, std::distance(__middle, __second_cut));
3109 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3110 __len11, __len22, __comp);
3111 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3112 __len1 - __len11, __len2 - __len22, __comp);
3116 * @brief Merges two sorted ranges in place.
3117 * @ingroup sorting_algorithms
3118 * @param first An iterator.
3119 * @param middle Another iterator.
3120 * @param last Another iterator.
3121 * @return Nothing.
3123 * Merges two sorted and consecutive ranges, [first,middle) and
3124 * [middle,last), and puts the result in [first,last). The output will
3125 * be sorted. The sort is @e stable, that is, for equivalent
3126 * elements in the two ranges, elements from the first range will always
3127 * come before elements from the second.
3129 * If enough additional memory is available, this takes (last-first)-1
3130 * comparisons. Otherwise an NlogN algorithm is used, where N is
3131 * distance(first,last).
3133 template<typename _BidirectionalIterator>
3134 void
3135 inplace_merge(_BidirectionalIterator __first,
3136 _BidirectionalIterator __middle,
3137 _BidirectionalIterator __last)
3139 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3140 _ValueType;
3141 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3142 _DistanceType;
3144 // concept requirements
3145 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3146 _BidirectionalIterator>)
3147 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3148 __glibcxx_requires_sorted(__first, __middle);
3149 __glibcxx_requires_sorted(__middle, __last);
3151 if (__first == __middle || __middle == __last)
3152 return;
3154 _DistanceType __len1 = std::distance(__first, __middle);
3155 _DistanceType __len2 = std::distance(__middle, __last);
3157 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3158 __last);
3159 if (__buf.begin() == 0)
3160 std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
3161 else
3162 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3163 __buf.begin(), _DistanceType(__buf.size()));
3167 * @brief Merges two sorted ranges in place.
3168 * @ingroup sorting_algorithms
3169 * @param first An iterator.
3170 * @param middle Another iterator.
3171 * @param last Another iterator.
3172 * @param comp A functor to use for comparisons.
3173 * @return Nothing.
3175 * Merges two sorted and consecutive ranges, [first,middle) and
3176 * [middle,last), and puts the result in [first,last). The output will
3177 * be sorted. The sort is @e stable, that is, for equivalent
3178 * elements in the two ranges, elements from the first range will always
3179 * come before elements from the second.
3181 * If enough additional memory is available, this takes (last-first)-1
3182 * comparisons. Otherwise an NlogN algorithm is used, where N is
3183 * distance(first,last).
3185 * The comparison function should have the same effects on ordering as
3186 * the function used for the initial sort.
3188 template<typename _BidirectionalIterator, typename _Compare>
3189 void
3190 inplace_merge(_BidirectionalIterator __first,
3191 _BidirectionalIterator __middle,
3192 _BidirectionalIterator __last,
3193 _Compare __comp)
3195 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3196 _ValueType;
3197 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3198 _DistanceType;
3200 // concept requirements
3201 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3202 _BidirectionalIterator>)
3203 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3204 _ValueType, _ValueType>)
3205 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
3206 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
3208 if (__first == __middle || __middle == __last)
3209 return;
3211 const _DistanceType __len1 = std::distance(__first, __middle);
3212 const _DistanceType __len2 = std::distance(__middle, __last);
3214 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3215 __last);
3216 if (__buf.begin() == 0)
3217 std::__merge_without_buffer(__first, __middle, __last, __len1,
3218 __len2, __comp);
3219 else
3220 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3221 __buf.begin(), _DistanceType(__buf.size()),
3222 __comp);
3225 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3226 typename _Distance>
3227 void
3228 __merge_sort_loop(_RandomAccessIterator1 __first,
3229 _RandomAccessIterator1 __last,
3230 _RandomAccessIterator2 __result,
3231 _Distance __step_size)
3233 const _Distance __two_step = 2 * __step_size;
3235 while (__last - __first >= __two_step)
3237 __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3238 __first + __step_size,
3239 __first + __two_step,
3240 __result);
3241 __first += __two_step;
3244 __step_size = std::min(_Distance(__last - __first), __step_size);
3245 _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3246 __first + __step_size, __last,
3247 __result);
3250 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3251 typename _Distance, typename _Compare>
3252 void
3253 __merge_sort_loop(_RandomAccessIterator1 __first,
3254 _RandomAccessIterator1 __last,
3255 _RandomAccessIterator2 __result, _Distance __step_size,
3256 _Compare __comp)
3258 const _Distance __two_step = 2 * __step_size;
3260 while (__last - __first >= __two_step)
3262 __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3263 __first + __step_size, __first + __two_step,
3264 __result,
3265 __comp);
3266 __first += __two_step;
3268 __step_size = std::min(_Distance(__last - __first), __step_size);
3270 _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3271 __first + __step_size, __last, __result, __comp);
3274 template<typename _RandomAccessIterator, typename _Distance>
3275 void
3276 __chunk_insertion_sort(_RandomAccessIterator __first,
3277 _RandomAccessIterator __last,
3278 _Distance __chunk_size)
3280 while (__last - __first >= __chunk_size)
3282 std::__insertion_sort(__first, __first + __chunk_size);
3283 __first += __chunk_size;
3285 std::__insertion_sort(__first, __last);
3288 template<typename _RandomAccessIterator, typename _Distance,
3289 typename _Compare>
3290 void
3291 __chunk_insertion_sort(_RandomAccessIterator __first,
3292 _RandomAccessIterator __last,
3293 _Distance __chunk_size, _Compare __comp)
3295 while (__last - __first >= __chunk_size)
3297 std::__insertion_sort(__first, __first + __chunk_size, __comp);
3298 __first += __chunk_size;
3300 std::__insertion_sort(__first, __last, __comp);
3303 enum { _S_chunk_size = 7 };
3305 template<typename _RandomAccessIterator, typename _Pointer>
3306 void
3307 __merge_sort_with_buffer(_RandomAccessIterator __first,
3308 _RandomAccessIterator __last,
3309 _Pointer __buffer)
3311 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3312 _Distance;
3314 const _Distance __len = __last - __first;
3315 const _Pointer __buffer_last = __buffer + __len;
3317 _Distance __step_size = _S_chunk_size;
3318 std::__chunk_insertion_sort(__first, __last, __step_size);
3320 while (__step_size < __len)
3322 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3323 __step_size *= 2;
3324 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3325 __step_size *= 2;
3329 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3330 void
3331 __merge_sort_with_buffer(_RandomAccessIterator __first,
3332 _RandomAccessIterator __last,
3333 _Pointer __buffer, _Compare __comp)
3335 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3336 _Distance;
3338 const _Distance __len = __last - __first;
3339 const _Pointer __buffer_last = __buffer + __len;
3341 _Distance __step_size = _S_chunk_size;
3342 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3344 while (__step_size < __len)
3346 std::__merge_sort_loop(__first, __last, __buffer,
3347 __step_size, __comp);
3348 __step_size *= 2;
3349 std::__merge_sort_loop(__buffer, __buffer_last, __first,
3350 __step_size, __comp);
3351 __step_size *= 2;
3355 template<typename _RandomAccessIterator, typename _Pointer,
3356 typename _Distance>
3357 void
3358 __stable_sort_adaptive(_RandomAccessIterator __first,
3359 _RandomAccessIterator __last,
3360 _Pointer __buffer, _Distance __buffer_size)
3362 const _Distance __len = (__last - __first + 1) / 2;
3363 const _RandomAccessIterator __middle = __first + __len;
3364 if (__len > __buffer_size)
3366 std::__stable_sort_adaptive(__first, __middle,
3367 __buffer, __buffer_size);
3368 std::__stable_sort_adaptive(__middle, __last,
3369 __buffer, __buffer_size);
3371 else
3373 std::__merge_sort_with_buffer(__first, __middle, __buffer);
3374 std::__merge_sort_with_buffer(__middle, __last, __buffer);
3376 std::__merge_adaptive(__first, __middle, __last,
3377 _Distance(__middle - __first),
3378 _Distance(__last - __middle),
3379 __buffer, __buffer_size);
3382 template<typename _RandomAccessIterator, typename _Pointer,
3383 typename _Distance, typename _Compare>
3384 void
3385 __stable_sort_adaptive(_RandomAccessIterator __first,
3386 _RandomAccessIterator __last,
3387 _Pointer __buffer, _Distance __buffer_size,
3388 _Compare __comp)
3390 const _Distance __len = (__last - __first + 1) / 2;
3391 const _RandomAccessIterator __middle = __first + __len;
3392 if (__len > __buffer_size)
3394 std::__stable_sort_adaptive(__first, __middle, __buffer,
3395 __buffer_size, __comp);
3396 std::__stable_sort_adaptive(__middle, __last, __buffer,
3397 __buffer_size, __comp);
3399 else
3401 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3402 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3404 std::__merge_adaptive(__first, __middle, __last,
3405 _Distance(__middle - __first),
3406 _Distance(__last - __middle),
3407 __buffer, __buffer_size,
3408 __comp);
3411 /// This is a helper function for the stable sorting routines.
3412 template<typename _RandomAccessIterator>
3413 void
3414 __inplace_stable_sort(_RandomAccessIterator __first,
3415 _RandomAccessIterator __last)
3417 if (__last - __first < 15)
3419 std::__insertion_sort(__first, __last);
3420 return;
3422 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3423 std::__inplace_stable_sort(__first, __middle);
3424 std::__inplace_stable_sort(__middle, __last);
3425 std::__merge_without_buffer(__first, __middle, __last,
3426 __middle - __first,
3427 __last - __middle);
3430 /// This is a helper function for the stable sorting routines.
3431 template<typename _RandomAccessIterator, typename _Compare>
3432 void
3433 __inplace_stable_sort(_RandomAccessIterator __first,
3434 _RandomAccessIterator __last, _Compare __comp)
3436 if (__last - __first < 15)
3438 std::__insertion_sort(__first, __last, __comp);
3439 return;
3441 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3442 std::__inplace_stable_sort(__first, __middle, __comp);
3443 std::__inplace_stable_sort(__middle, __last, __comp);
3444 std::__merge_without_buffer(__first, __middle, __last,
3445 __middle - __first,
3446 __last - __middle,
3447 __comp);
3450 // stable_sort
3452 // Set algorithms: includes, set_union, set_intersection, set_difference,
3453 // set_symmetric_difference. All of these algorithms have the precondition
3454 // that their input ranges are sorted and the postcondition that their output
3455 // ranges are sorted.
3458 * @brief Determines whether all elements of a sequence exists in a range.
3459 * @param first1 Start of search range.
3460 * @param last1 End of search range.
3461 * @param first2 Start of sequence
3462 * @param last2 End of sequence.
3463 * @return True if each element in [first2,last2) is contained in order
3464 * within [first1,last1). False otherwise.
3465 * @ingroup set_algorithms
3467 * This operation expects both [first1,last1) and [first2,last2) to be
3468 * sorted. Searches for the presence of each element in [first2,last2)
3469 * within [first1,last1). The iterators over each range only move forward,
3470 * so this is a linear algorithm. If an element in [first2,last2) is not
3471 * found before the search iterator reaches @a last2, false is returned.
3473 template<typename _InputIterator1, typename _InputIterator2>
3474 bool
3475 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3476 _InputIterator2 __first2, _InputIterator2 __last2)
3478 typedef typename iterator_traits<_InputIterator1>::value_type
3479 _ValueType1;
3480 typedef typename iterator_traits<_InputIterator2>::value_type
3481 _ValueType2;
3483 // concept requirements
3484 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3485 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3486 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
3487 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
3488 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
3489 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
3491 while (__first1 != __last1 && __first2 != __last2)
3492 if (*__first2 < *__first1)
3493 return false;
3494 else if(*__first1 < *__first2)
3495 ++__first1;
3496 else
3497 ++__first1, ++__first2;
3499 return __first2 == __last2;
3503 * @brief Determines whether all elements of a sequence exists in a range
3504 * using comparison.
3505 * @ingroup set_algorithms
3506 * @param first1 Start of search range.
3507 * @param last1 End of search range.
3508 * @param first2 Start of sequence
3509 * @param last2 End of sequence.
3510 * @param comp Comparison function to use.
3511 * @return True if each element in [first2,last2) is contained in order
3512 * within [first1,last1) according to comp. False otherwise.
3513 * @ingroup set_algorithms
3515 * This operation expects both [first1,last1) and [first2,last2) to be
3516 * sorted. Searches for the presence of each element in [first2,last2)
3517 * within [first1,last1), using comp to decide. The iterators over each
3518 * range only move forward, so this is a linear algorithm. If an element
3519 * in [first2,last2) is not found before the search iterator reaches @a
3520 * last2, false is returned.
3522 template<typename _InputIterator1, typename _InputIterator2,
3523 typename _Compare>
3524 bool
3525 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3526 _InputIterator2 __first2, _InputIterator2 __last2,
3527 _Compare __comp)
3529 typedef typename iterator_traits<_InputIterator1>::value_type
3530 _ValueType1;
3531 typedef typename iterator_traits<_InputIterator2>::value_type
3532 _ValueType2;
3534 // concept requirements
3535 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3536 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3537 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3538 _ValueType1, _ValueType2>)
3539 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3540 _ValueType2, _ValueType1>)
3541 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
3542 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
3544 while (__first1 != __last1 && __first2 != __last2)
3545 if (__comp(*__first2, *__first1))
3546 return false;
3547 else if(__comp(*__first1, *__first2))
3548 ++__first1;
3549 else
3550 ++__first1, ++__first2;
3552 return __first2 == __last2;
3555 // nth_element
3556 // merge
3557 // set_difference
3558 // set_intersection
3559 // set_union
3560 // stable_sort
3561 // set_symmetric_difference
3562 // min_element
3563 // max_element
3566 * @brief Permute range into the next "dictionary" ordering.
3567 * @ingroup sorting_algorithms
3568 * @param first Start of range.
3569 * @param last End of range.
3570 * @return False if wrapped to first permutation, true otherwise.
3572 * Treats all permutations of the range as a set of "dictionary" sorted
3573 * sequences. Permutes the current sequence into the next one of this set.
3574 * Returns true if there are more sequences to generate. If the sequence
3575 * is the largest of the set, the smallest is generated and false returned.
3577 template<typename _BidirectionalIterator>
3578 bool
3579 next_permutation(_BidirectionalIterator __first,
3580 _BidirectionalIterator __last)
3582 // concept requirements
3583 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3584 _BidirectionalIterator>)
3585 __glibcxx_function_requires(_LessThanComparableConcept<
3586 typename iterator_traits<_BidirectionalIterator>::value_type>)
3587 __glibcxx_requires_valid_range(__first, __last);
3589 if (__first == __last)
3590 return false;
3591 _BidirectionalIterator __i = __first;
3592 ++__i;
3593 if (__i == __last)
3594 return false;
3595 __i = __last;
3596 --__i;
3598 for(;;)
3600 _BidirectionalIterator __ii = __i;
3601 --__i;
3602 if (*__i < *__ii)
3604 _BidirectionalIterator __j = __last;
3605 while (!(*__i < *--__j))
3607 std::iter_swap(__i, __j);
3608 std::reverse(__ii, __last);
3609 return true;
3611 if (__i == __first)
3613 std::reverse(__first, __last);
3614 return false;
3620 * @brief Permute range into the next "dictionary" ordering using
3621 * comparison functor.
3622 * @ingroup sorting_algorithms
3623 * @param first Start of range.
3624 * @param last End of range.
3625 * @param comp A comparison functor.
3626 * @return False if wrapped to first permutation, true otherwise.
3628 * Treats all permutations of the range [first,last) as a set of
3629 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3630 * sequence into the next one of this set. Returns true if there are more
3631 * sequences to generate. If the sequence is the largest of the set, the
3632 * smallest is generated and false returned.
3634 template<typename _BidirectionalIterator, typename _Compare>
3635 bool
3636 next_permutation(_BidirectionalIterator __first,
3637 _BidirectionalIterator __last, _Compare __comp)
3639 // concept requirements
3640 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3641 _BidirectionalIterator>)
3642 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3643 typename iterator_traits<_BidirectionalIterator>::value_type,
3644 typename iterator_traits<_BidirectionalIterator>::value_type>)
3645 __glibcxx_requires_valid_range(__first, __last);
3647 if (__first == __last)
3648 return false;
3649 _BidirectionalIterator __i = __first;
3650 ++__i;
3651 if (__i == __last)
3652 return false;
3653 __i = __last;
3654 --__i;
3656 for(;;)
3658 _BidirectionalIterator __ii = __i;
3659 --__i;
3660 if (__comp(*__i, *__ii))
3662 _BidirectionalIterator __j = __last;
3663 while (!bool(__comp(*__i, *--__j)))
3665 std::iter_swap(__i, __j);
3666 std::reverse(__ii, __last);
3667 return true;
3669 if (__i == __first)
3671 std::reverse(__first, __last);
3672 return false;
3678 * @brief Permute range into the previous "dictionary" ordering.
3679 * @ingroup sorting_algorithms
3680 * @param first Start of range.
3681 * @param last End of range.
3682 * @return False if wrapped to last permutation, true otherwise.
3684 * Treats all permutations of the range as a set of "dictionary" sorted
3685 * sequences. Permutes the current sequence into the previous one of this
3686 * set. Returns true if there are more sequences to generate. If the
3687 * sequence is the smallest of the set, the largest is generated and false
3688 * returned.
3690 template<typename _BidirectionalIterator>
3691 bool
3692 prev_permutation(_BidirectionalIterator __first,
3693 _BidirectionalIterator __last)
3695 // concept requirements
3696 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3697 _BidirectionalIterator>)
3698 __glibcxx_function_requires(_LessThanComparableConcept<
3699 typename iterator_traits<_BidirectionalIterator>::value_type>)
3700 __glibcxx_requires_valid_range(__first, __last);
3702 if (__first == __last)
3703 return false;
3704 _BidirectionalIterator __i = __first;
3705 ++__i;
3706 if (__i == __last)
3707 return false;
3708 __i = __last;
3709 --__i;
3711 for(;;)
3713 _BidirectionalIterator __ii = __i;
3714 --__i;
3715 if (*__ii < *__i)
3717 _BidirectionalIterator __j = __last;
3718 while (!(*--__j < *__i))
3720 std::iter_swap(__i, __j);
3721 std::reverse(__ii, __last);
3722 return true;
3724 if (__i == __first)
3726 std::reverse(__first, __last);
3727 return false;
3733 * @brief Permute range into the previous "dictionary" ordering using
3734 * comparison functor.
3735 * @ingroup sorting_algorithms
3736 * @param first Start of range.
3737 * @param last End of range.
3738 * @param comp A comparison functor.
3739 * @return False if wrapped to last permutation, true otherwise.
3741 * Treats all permutations of the range [first,last) as a set of
3742 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3743 * sequence into the previous one of this set. Returns true if there are
3744 * more sequences to generate. If the sequence is the smallest of the set,
3745 * the largest is generated and false returned.
3747 template<typename _BidirectionalIterator, typename _Compare>
3748 bool
3749 prev_permutation(_BidirectionalIterator __first,
3750 _BidirectionalIterator __last, _Compare __comp)
3752 // concept requirements
3753 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3754 _BidirectionalIterator>)
3755 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3756 typename iterator_traits<_BidirectionalIterator>::value_type,
3757 typename iterator_traits<_BidirectionalIterator>::value_type>)
3758 __glibcxx_requires_valid_range(__first, __last);
3760 if (__first == __last)
3761 return false;
3762 _BidirectionalIterator __i = __first;
3763 ++__i;
3764 if (__i == __last)
3765 return false;
3766 __i = __last;
3767 --__i;
3769 for(;;)
3771 _BidirectionalIterator __ii = __i;
3772 --__i;
3773 if (__comp(*__ii, *__i))
3775 _BidirectionalIterator __j = __last;
3776 while (!bool(__comp(*--__j, *__i)))
3778 std::iter_swap(__i, __j);
3779 std::reverse(__ii, __last);
3780 return true;
3782 if (__i == __first)
3784 std::reverse(__first, __last);
3785 return false;
3790 // replace
3791 // replace_if
3794 * @brief Copy a sequence, replacing each element of one value with another
3795 * value.
3796 * @param first An input iterator.
3797 * @param last An input iterator.
3798 * @param result An output iterator.
3799 * @param old_value The value to be replaced.
3800 * @param new_value The replacement value.
3801 * @return The end of the output sequence, @p result+(last-first).
3803 * Copies each element in the input range @p [first,last) to the
3804 * output range @p [result,result+(last-first)) replacing elements
3805 * equal to @p old_value with @p new_value.
3807 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3808 _OutputIterator
3809 replace_copy(_InputIterator __first, _InputIterator __last,
3810 _OutputIterator __result,
3811 const _Tp& __old_value, const _Tp& __new_value)
3813 // concept requirements
3814 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3815 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3816 typename iterator_traits<_InputIterator>::value_type>)
3817 __glibcxx_function_requires(_EqualOpConcept<
3818 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3819 __glibcxx_requires_valid_range(__first, __last);
3821 for (; __first != __last; ++__first, ++__result)
3822 if (*__first == __old_value)
3823 *__result = __new_value;
3824 else
3825 *__result = *__first;
3826 return __result;
3830 * @brief Copy a sequence, replacing each value for which a predicate
3831 * returns true with another value.
3832 * @ingroup mutating_algorithms
3833 * @param first An input iterator.
3834 * @param last An input iterator.
3835 * @param result An output iterator.
3836 * @param pred A predicate.
3837 * @param new_value The replacement value.
3838 * @return The end of the output sequence, @p result+(last-first).
3840 * Copies each element in the range @p [first,last) to the range
3841 * @p [result,result+(last-first)) replacing elements for which
3842 * @p pred returns true with @p new_value.
3844 template<typename _InputIterator, typename _OutputIterator,
3845 typename _Predicate, typename _Tp>
3846 _OutputIterator
3847 replace_copy_if(_InputIterator __first, _InputIterator __last,
3848 _OutputIterator __result,
3849 _Predicate __pred, const _Tp& __new_value)
3851 // concept requirements
3852 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3853 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3854 typename iterator_traits<_InputIterator>::value_type>)
3855 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3856 typename iterator_traits<_InputIterator>::value_type>)
3857 __glibcxx_requires_valid_range(__first, __last);
3859 for (; __first != __last; ++__first, ++__result)
3860 if (__pred(*__first))
3861 *__result = __new_value;
3862 else
3863 *__result = *__first;
3864 return __result;
3867 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3869 * @brief Determines whether the elements of a sequence are sorted.
3870 * @ingroup sorting_algorithms
3871 * @param first An iterator.
3872 * @param last Another iterator.
3873 * @return True if the elements are sorted, false otherwise.
3875 template<typename _ForwardIterator>
3876 inline bool
3877 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3878 { return std::is_sorted_until(__first, __last) == __last; }
3881 * @brief Determines whether the elements of a sequence are sorted
3882 * according to a comparison functor.
3883 * @ingroup sorting_algorithms
3884 * @param first An iterator.
3885 * @param last Another iterator.
3886 * @param comp A comparison functor.
3887 * @return True if the elements are sorted, false otherwise.
3889 template<typename _ForwardIterator, typename _Compare>
3890 inline bool
3891 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3892 _Compare __comp)
3893 { return std::is_sorted_until(__first, __last, __comp) == __last; }
3896 * @brief Determines the end of a sorted sequence.
3897 * @ingroup sorting_algorithms
3898 * @param first An iterator.
3899 * @param last Another iterator.
3900 * @return An iterator pointing to the last iterator i in [first, last)
3901 * for which the range [first, i) is sorted.
3903 template<typename _ForwardIterator>
3904 _ForwardIterator
3905 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3907 // concept requirements
3908 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3909 __glibcxx_function_requires(_LessThanComparableConcept<
3910 typename iterator_traits<_ForwardIterator>::value_type>)
3911 __glibcxx_requires_valid_range(__first, __last);
3913 if (__first == __last)
3914 return __last;
3916 _ForwardIterator __next = __first;
3917 for (++__next; __next != __last; __first = __next, ++__next)
3918 if (*__next < *__first)
3919 return __next;
3920 return __next;
3924 * @brief Determines the end of a sorted sequence using comparison functor.
3925 * @ingroup sorting_algorithms
3926 * @param first An iterator.
3927 * @param last Another iterator.
3928 * @param comp A comparison functor.
3929 * @return An iterator pointing to the last iterator i in [first, last)
3930 * for which the range [first, i) is sorted.
3932 template<typename _ForwardIterator, typename _Compare>
3933 _ForwardIterator
3934 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3935 _Compare __comp)
3937 // concept requirements
3938 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3939 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3940 typename iterator_traits<_ForwardIterator>::value_type,
3941 typename iterator_traits<_ForwardIterator>::value_type>)
3942 __glibcxx_requires_valid_range(__first, __last);
3944 if (__first == __last)
3945 return __last;
3947 _ForwardIterator __next = __first;
3948 for (++__next; __next != __last; __first = __next, ++__next)
3949 if (__comp(*__next, *__first))
3950 return __next;
3951 return __next;
3955 * @brief Determines min and max at once as an ordered pair.
3956 * @ingroup sorting_algorithms
3957 * @param a A thing of arbitrary type.
3958 * @param b Another thing of arbitrary type.
3959 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3961 template<typename _Tp>
3962 inline pair<const _Tp&, const _Tp&>
3963 minmax(const _Tp& __a, const _Tp& __b)
3965 // concept requirements
3966 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3968 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
3969 : pair<const _Tp&, const _Tp&>(__a, __b);
3973 * @brief Determines min and max at once as an ordered pair.
3974 * @ingroup sorting_algorithms
3975 * @param a A thing of arbitrary type.
3976 * @param b Another thing of arbitrary type.
3977 * @param comp A @link comparison_functor comparison functor@endlink.
3978 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3980 template<typename _Tp, typename _Compare>
3981 inline pair<const _Tp&, const _Tp&>
3982 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
3984 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
3985 : pair<const _Tp&, const _Tp&>(__a, __b);
3989 * @brief Return a pair of iterators pointing to the minimum and maximum
3990 * elements in a range.
3991 * @ingroup sorting_algorithms
3992 * @param first Start of range.
3993 * @param last End of range.
3994 * @return make_pair(m, M), where m is the first iterator i in
3995 * [first, last) such that no other element in the range is
3996 * smaller, and where M is the last iterator i in [first, last)
3997 * such that no other element in the range is larger.
3999 template<typename _ForwardIterator>
4000 pair<_ForwardIterator, _ForwardIterator>
4001 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
4003 // concept requirements
4004 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4005 __glibcxx_function_requires(_LessThanComparableConcept<
4006 typename iterator_traits<_ForwardIterator>::value_type>)
4007 __glibcxx_requires_valid_range(__first, __last);
4009 _ForwardIterator __next = __first;
4010 if (__first == __last
4011 || ++__next == __last)
4012 return std::make_pair(__first, __first);
4014 _ForwardIterator __min, __max;
4015 if (*__next < *__first)
4017 __min = __next;
4018 __max = __first;
4020 else
4022 __min = __first;
4023 __max = __next;
4026 __first = __next;
4027 ++__first;
4029 while (__first != __last)
4031 __next = __first;
4032 if (++__next == __last)
4034 if (*__first < *__min)
4035 __min = __first;
4036 else if (!(*__first < *__max))
4037 __max = __first;
4038 break;
4041 if (*__next < *__first)
4043 if (*__next < *__min)
4044 __min = __next;
4045 if (!(*__first < *__max))
4046 __max = __first;
4048 else
4050 if (*__first < *__min)
4051 __min = __first;
4052 if (!(*__next < *__max))
4053 __max = __next;
4056 __first = __next;
4057 ++__first;
4060 return std::make_pair(__min, __max);
4064 * @brief Return a pair of iterators pointing to the minimum and maximum
4065 * elements in a range.
4066 * @ingroup sorting_algorithms
4067 * @param first Start of range.
4068 * @param last End of range.
4069 * @param comp Comparison functor.
4070 * @return make_pair(m, M), where m is the first iterator i in
4071 * [first, last) such that no other element in the range is
4072 * smaller, and where M is the last iterator i in [first, last)
4073 * such that no other element in the range is larger.
4075 template<typename _ForwardIterator, typename _Compare>
4076 pair<_ForwardIterator, _ForwardIterator>
4077 minmax_element(_ForwardIterator __first, _ForwardIterator __last,
4078 _Compare __comp)
4080 // concept requirements
4081 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4082 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4083 typename iterator_traits<_ForwardIterator>::value_type,
4084 typename iterator_traits<_ForwardIterator>::value_type>)
4085 __glibcxx_requires_valid_range(__first, __last);
4087 _ForwardIterator __next = __first;
4088 if (__first == __last
4089 || ++__next == __last)
4090 return std::make_pair(__first, __first);
4092 _ForwardIterator __min, __max;
4093 if (__comp(*__next, *__first))
4095 __min = __next;
4096 __max = __first;
4098 else
4100 __min = __first;
4101 __max = __next;
4104 __first = __next;
4105 ++__first;
4107 while (__first != __last)
4109 __next = __first;
4110 if (++__next == __last)
4112 if (__comp(*__first, *__min))
4113 __min = __first;
4114 else if (!__comp(*__first, *__max))
4115 __max = __first;
4116 break;
4119 if (__comp(*__next, *__first))
4121 if (__comp(*__next, *__min))
4122 __min = __next;
4123 if (!__comp(*__first, *__max))
4124 __max = __first;
4126 else
4128 if (__comp(*__first, *__min))
4129 __min = __first;
4130 if (!__comp(*__next, *__max))
4131 __max = __next;
4134 __first = __next;
4135 ++__first;
4138 return std::make_pair(__min, __max);
4141 // N2722 + fixes.
4142 template<typename _Tp>
4143 inline _Tp
4144 min(initializer_list<_Tp> __l)
4145 { return *std::min_element(__l.begin(), __l.end()); }
4147 template<typename _Tp, typename _Compare>
4148 inline _Tp
4149 min(initializer_list<_Tp> __l, _Compare __comp)
4150 { return *std::min_element(__l.begin(), __l.end(), __comp); }
4152 template<typename _Tp>
4153 inline _Tp
4154 max(initializer_list<_Tp> __l)
4155 { return *std::max_element(__l.begin(), __l.end()); }
4157 template<typename _Tp, typename _Compare>
4158 inline _Tp
4159 max(initializer_list<_Tp> __l, _Compare __comp)
4160 { return *std::max_element(__l.begin(), __l.end(), __comp); }
4162 template<typename _Tp>
4163 inline pair<_Tp, _Tp>
4164 minmax(initializer_list<_Tp> __l)
4166 pair<const _Tp*, const _Tp*> __p =
4167 std::minmax_element(__l.begin(), __l.end());
4168 return std::make_pair(*__p.first, *__p.second);
4171 template<typename _Tp, typename _Compare>
4172 inline pair<_Tp, _Tp>
4173 minmax(initializer_list<_Tp> __l, _Compare __comp)
4175 pair<const _Tp*, const _Tp*> __p =
4176 std::minmax_element(__l.begin(), __l.end(), __comp);
4177 return std::make_pair(*__p.first, *__p.second);
4179 #endif // __GXX_EXPERIMENTAL_CXX0X__
4181 _GLIBCXX_END_NAMESPACE
4183 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
4186 * @brief Apply a function to every element of a sequence.
4187 * @ingroup non_mutating_algorithms
4188 * @param first An input iterator.
4189 * @param last An input iterator.
4190 * @param f A unary function object.
4191 * @return @p f.
4193 * Applies the function object @p f to each element in the range
4194 * @p [first,last). @p f must not modify the order of the sequence.
4195 * If @p f has a return value it is ignored.
4197 template<typename _InputIterator, typename _Function>
4198 _Function
4199 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
4201 // concept requirements
4202 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4203 __glibcxx_requires_valid_range(__first, __last);
4204 for (; __first != __last; ++__first)
4205 __f(*__first);
4206 return __f;
4210 * @brief Find the first occurrence of a value in a sequence.
4211 * @ingroup non_mutating_algorithms
4212 * @param first An input iterator.
4213 * @param last An input iterator.
4214 * @param val The value to find.
4215 * @return The first iterator @c i in the range @p [first,last)
4216 * such that @c *i == @p val, or @p last if no such iterator exists.
4218 template<typename _InputIterator, typename _Tp>
4219 inline _InputIterator
4220 find(_InputIterator __first, _InputIterator __last,
4221 const _Tp& __val)
4223 // concept requirements
4224 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4225 __glibcxx_function_requires(_EqualOpConcept<
4226 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4227 __glibcxx_requires_valid_range(__first, __last);
4228 return std::__find(__first, __last, __val,
4229 std::__iterator_category(__first));
4233 * @brief Find the first element in a sequence for which a
4234 * predicate is true.
4235 * @ingroup non_mutating_algorithms
4236 * @param first An input iterator.
4237 * @param last An input iterator.
4238 * @param pred A predicate.
4239 * @return The first iterator @c i in the range @p [first,last)
4240 * such that @p pred(*i) is true, or @p last if no such iterator exists.
4242 template<typename _InputIterator, typename _Predicate>
4243 inline _InputIterator
4244 find_if(_InputIterator __first, _InputIterator __last,
4245 _Predicate __pred)
4247 // concept requirements
4248 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4249 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4250 typename iterator_traits<_InputIterator>::value_type>)
4251 __glibcxx_requires_valid_range(__first, __last);
4252 return std::__find_if(__first, __last, __pred,
4253 std::__iterator_category(__first));
4257 * @brief Find element from a set in a sequence.
4258 * @ingroup non_mutating_algorithms
4259 * @param first1 Start of range to search.
4260 * @param last1 End of range to search.
4261 * @param first2 Start of match candidates.
4262 * @param last2 End of match candidates.
4263 * @return The first iterator @c i in the range
4264 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4265 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4267 * Searches the range @p [first1,last1) for an element that is equal to
4268 * some element in the range [first2,last2). If found, returns an iterator
4269 * in the range [first1,last1), otherwise returns @p last1.
4271 template<typename _InputIterator, typename _ForwardIterator>
4272 _InputIterator
4273 find_first_of(_InputIterator __first1, _InputIterator __last1,
4274 _ForwardIterator __first2, _ForwardIterator __last2)
4276 // concept requirements
4277 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4278 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4279 __glibcxx_function_requires(_EqualOpConcept<
4280 typename iterator_traits<_InputIterator>::value_type,
4281 typename iterator_traits<_ForwardIterator>::value_type>)
4282 __glibcxx_requires_valid_range(__first1, __last1);
4283 __glibcxx_requires_valid_range(__first2, __last2);
4285 for (; __first1 != __last1; ++__first1)
4286 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4287 if (*__first1 == *__iter)
4288 return __first1;
4289 return __last1;
4293 * @brief Find element from a set in a sequence using a predicate.
4294 * @ingroup non_mutating_algorithms
4295 * @param first1 Start of range to search.
4296 * @param last1 End of range to search.
4297 * @param first2 Start of match candidates.
4298 * @param last2 End of match candidates.
4299 * @param comp Predicate to use.
4300 * @return The first iterator @c i in the range
4301 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4302 * iterator in [first2,last2), or @p last1 if no such iterator exists.
4305 * Searches the range @p [first1,last1) for an element that is
4306 * equal to some element in the range [first2,last2). If found,
4307 * returns an iterator in the range [first1,last1), otherwise
4308 * returns @p last1.
4310 template<typename _InputIterator, typename _ForwardIterator,
4311 typename _BinaryPredicate>
4312 _InputIterator
4313 find_first_of(_InputIterator __first1, _InputIterator __last1,
4314 _ForwardIterator __first2, _ForwardIterator __last2,
4315 _BinaryPredicate __comp)
4317 // concept requirements
4318 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4319 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4320 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4321 typename iterator_traits<_InputIterator>::value_type,
4322 typename iterator_traits<_ForwardIterator>::value_type>)
4323 __glibcxx_requires_valid_range(__first1, __last1);
4324 __glibcxx_requires_valid_range(__first2, __last2);
4326 for (; __first1 != __last1; ++__first1)
4327 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4328 if (__comp(*__first1, *__iter))
4329 return __first1;
4330 return __last1;
4334 * @brief Find two adjacent values in a sequence that are equal.
4335 * @ingroup non_mutating_algorithms
4336 * @param first A forward iterator.
4337 * @param last A forward iterator.
4338 * @return The first iterator @c i such that @c i and @c i+1 are both
4339 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4340 * or @p last if no such iterator exists.
4342 template<typename _ForwardIterator>
4343 _ForwardIterator
4344 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
4346 // concept requirements
4347 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4348 __glibcxx_function_requires(_EqualityComparableConcept<
4349 typename iterator_traits<_ForwardIterator>::value_type>)
4350 __glibcxx_requires_valid_range(__first, __last);
4351 if (__first == __last)
4352 return __last;
4353 _ForwardIterator __next = __first;
4354 while(++__next != __last)
4356 if (*__first == *__next)
4357 return __first;
4358 __first = __next;
4360 return __last;
4364 * @brief Find two adjacent values in a sequence using a predicate.
4365 * @ingroup non_mutating_algorithms
4366 * @param first A forward iterator.
4367 * @param last A forward iterator.
4368 * @param binary_pred A binary predicate.
4369 * @return The first iterator @c i such that @c i and @c i+1 are both
4370 * valid iterators in @p [first,last) and such that
4371 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4372 * exists.
4374 template<typename _ForwardIterator, typename _BinaryPredicate>
4375 _ForwardIterator
4376 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
4377 _BinaryPredicate __binary_pred)
4379 // concept requirements
4380 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4381 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4382 typename iterator_traits<_ForwardIterator>::value_type,
4383 typename iterator_traits<_ForwardIterator>::value_type>)
4384 __glibcxx_requires_valid_range(__first, __last);
4385 if (__first == __last)
4386 return __last;
4387 _ForwardIterator __next = __first;
4388 while(++__next != __last)
4390 if (__binary_pred(*__first, *__next))
4391 return __first;
4392 __first = __next;
4394 return __last;
4398 * @brief Count the number of copies of a value in a sequence.
4399 * @ingroup non_mutating_algorithms
4400 * @param first An input iterator.
4401 * @param last An input iterator.
4402 * @param value The value to be counted.
4403 * @return The number of iterators @c i in the range @p [first,last)
4404 * for which @c *i == @p value
4406 template<typename _InputIterator, typename _Tp>
4407 typename iterator_traits<_InputIterator>::difference_type
4408 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
4410 // concept requirements
4411 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4412 __glibcxx_function_requires(_EqualOpConcept<
4413 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4414 __glibcxx_requires_valid_range(__first, __last);
4415 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4416 for (; __first != __last; ++__first)
4417 if (*__first == __value)
4418 ++__n;
4419 return __n;
4423 * @brief Count the elements of a sequence for which a predicate is true.
4424 * @ingroup non_mutating_algorithms
4425 * @param first An input iterator.
4426 * @param last An input iterator.
4427 * @param pred A predicate.
4428 * @return The number of iterators @c i in the range @p [first,last)
4429 * for which @p pred(*i) is true.
4431 template<typename _InputIterator, typename _Predicate>
4432 typename iterator_traits<_InputIterator>::difference_type
4433 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4435 // concept requirements
4436 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4437 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4438 typename iterator_traits<_InputIterator>::value_type>)
4439 __glibcxx_requires_valid_range(__first, __last);
4440 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4441 for (; __first != __last; ++__first)
4442 if (__pred(*__first))
4443 ++__n;
4444 return __n;
4448 * @brief Search a sequence for a matching sub-sequence.
4449 * @ingroup non_mutating_algorithms
4450 * @param first1 A forward iterator.
4451 * @param last1 A forward iterator.
4452 * @param first2 A forward iterator.
4453 * @param last2 A forward iterator.
4454 * @return The first iterator @c i in the range
4455 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4456 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4457 * such iterator exists.
4459 * Searches the range @p [first1,last1) for a sub-sequence that compares
4460 * equal value-by-value with the sequence given by @p [first2,last2) and
4461 * returns an iterator to the first element of the sub-sequence, or
4462 * @p last1 if the sub-sequence is not found.
4464 * Because the sub-sequence must lie completely within the range
4465 * @p [first1,last1) it must start at a position less than
4466 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4467 * sub-sequence.
4468 * This means that the returned iterator @c i will be in the range
4469 * @p [first1,last1-(last2-first2))
4471 template<typename _ForwardIterator1, typename _ForwardIterator2>
4472 _ForwardIterator1
4473 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4474 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4476 // concept requirements
4477 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4478 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4479 __glibcxx_function_requires(_EqualOpConcept<
4480 typename iterator_traits<_ForwardIterator1>::value_type,
4481 typename iterator_traits<_ForwardIterator2>::value_type>)
4482 __glibcxx_requires_valid_range(__first1, __last1);
4483 __glibcxx_requires_valid_range(__first2, __last2);
4485 // Test for empty ranges
4486 if (__first1 == __last1 || __first2 == __last2)
4487 return __first1;
4489 // Test for a pattern of length 1.
4490 _ForwardIterator2 __p1(__first2);
4491 if (++__p1 == __last2)
4492 return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4494 // General case.
4495 _ForwardIterator2 __p;
4496 _ForwardIterator1 __current = __first1;
4498 for (;;)
4500 __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4501 if (__first1 == __last1)
4502 return __last1;
4504 __p = __p1;
4505 __current = __first1;
4506 if (++__current == __last1)
4507 return __last1;
4509 while (*__current == *__p)
4511 if (++__p == __last2)
4512 return __first1;
4513 if (++__current == __last1)
4514 return __last1;
4516 ++__first1;
4518 return __first1;
4522 * @brief Search a sequence for a matching sub-sequence using a predicate.
4523 * @ingroup non_mutating_algorithms
4524 * @param first1 A forward iterator.
4525 * @param last1 A forward iterator.
4526 * @param first2 A forward iterator.
4527 * @param last2 A forward iterator.
4528 * @param predicate A binary predicate.
4529 * @return The first iterator @c i in the range
4530 * @p [first1,last1-(last2-first2)) such that
4531 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4532 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4534 * Searches the range @p [first1,last1) for a sub-sequence that compares
4535 * equal value-by-value with the sequence given by @p [first2,last2),
4536 * using @p predicate to determine equality, and returns an iterator
4537 * to the first element of the sub-sequence, or @p last1 if no such
4538 * iterator exists.
4540 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4542 template<typename _ForwardIterator1, typename _ForwardIterator2,
4543 typename _BinaryPredicate>
4544 _ForwardIterator1
4545 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4546 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4547 _BinaryPredicate __predicate)
4549 // concept requirements
4550 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4551 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4552 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4553 typename iterator_traits<_ForwardIterator1>::value_type,
4554 typename iterator_traits<_ForwardIterator2>::value_type>)
4555 __glibcxx_requires_valid_range(__first1, __last1);
4556 __glibcxx_requires_valid_range(__first2, __last2);
4558 // Test for empty ranges
4559 if (__first1 == __last1 || __first2 == __last2)
4560 return __first1;
4562 // Test for a pattern of length 1.
4563 _ForwardIterator2 __p1(__first2);
4564 if (++__p1 == __last2)
4566 while (__first1 != __last1
4567 && !bool(__predicate(*__first1, *__first2)))
4568 ++__first1;
4569 return __first1;
4572 // General case.
4573 _ForwardIterator2 __p;
4574 _ForwardIterator1 __current = __first1;
4576 for (;;)
4578 while (__first1 != __last1
4579 && !bool(__predicate(*__first1, *__first2)))
4580 ++__first1;
4581 if (__first1 == __last1)
4582 return __last1;
4584 __p = __p1;
4585 __current = __first1;
4586 if (++__current == __last1)
4587 return __last1;
4589 while (__predicate(*__current, *__p))
4591 if (++__p == __last2)
4592 return __first1;
4593 if (++__current == __last1)
4594 return __last1;
4596 ++__first1;
4598 return __first1;
4603 * @brief Search a sequence for a number of consecutive values.
4604 * @ingroup non_mutating_algorithms
4605 * @param first A forward iterator.
4606 * @param last A forward iterator.
4607 * @param count The number of consecutive values.
4608 * @param val The value to find.
4609 * @return The first iterator @c i in the range @p [first,last-count)
4610 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4611 * or @p last if no such iterator exists.
4613 * Searches the range @p [first,last) for @p count consecutive elements
4614 * equal to @p val.
4616 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4617 _ForwardIterator
4618 search_n(_ForwardIterator __first, _ForwardIterator __last,
4619 _Integer __count, const _Tp& __val)
4621 // concept requirements
4622 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4623 __glibcxx_function_requires(_EqualOpConcept<
4624 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4625 __glibcxx_requires_valid_range(__first, __last);
4627 if (__count <= 0)
4628 return __first;
4629 if (__count == 1)
4630 return _GLIBCXX_STD_P::find(__first, __last, __val);
4631 return std::__search_n(__first, __last, __count, __val,
4632 std::__iterator_category(__first));
4637 * @brief Search a sequence for a number of consecutive values using a
4638 * predicate.
4639 * @ingroup non_mutating_algorithms
4640 * @param first A forward iterator.
4641 * @param last A forward iterator.
4642 * @param count The number of consecutive values.
4643 * @param val The value to find.
4644 * @param binary_pred A binary predicate.
4645 * @return The first iterator @c i in the range @p [first,last-count)
4646 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4647 * range @p [0,count), or @p last if no such iterator exists.
4649 * Searches the range @p [first,last) for @p count consecutive elements
4650 * for which the predicate returns true.
4652 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4653 typename _BinaryPredicate>
4654 _ForwardIterator
4655 search_n(_ForwardIterator __first, _ForwardIterator __last,
4656 _Integer __count, const _Tp& __val,
4657 _BinaryPredicate __binary_pred)
4659 // concept requirements
4660 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4661 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4662 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4663 __glibcxx_requires_valid_range(__first, __last);
4665 if (__count <= 0)
4666 return __first;
4667 if (__count == 1)
4669 while (__first != __last && !bool(__binary_pred(*__first, __val)))
4670 ++__first;
4671 return __first;
4673 return std::__search_n(__first, __last, __count, __val, __binary_pred,
4674 std::__iterator_category(__first));
4679 * @brief Perform an operation on a sequence.
4680 * @ingroup mutating_algorithms
4681 * @param first An input iterator.
4682 * @param last An input iterator.
4683 * @param result An output iterator.
4684 * @param unary_op A unary operator.
4685 * @return An output iterator equal to @p result+(last-first).
4687 * Applies the operator to each element in the input range and assigns
4688 * the results to successive elements of the output sequence.
4689 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4690 * range @p [0,last-first).
4692 * @p unary_op must not alter its argument.
4694 template<typename _InputIterator, typename _OutputIterator,
4695 typename _UnaryOperation>
4696 _OutputIterator
4697 transform(_InputIterator __first, _InputIterator __last,
4698 _OutputIterator __result, _UnaryOperation __unary_op)
4700 // concept requirements
4701 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4702 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4703 // "the type returned by a _UnaryOperation"
4704 __typeof__(__unary_op(*__first))>)
4705 __glibcxx_requires_valid_range(__first, __last);
4707 for (; __first != __last; ++__first, ++__result)
4708 *__result = __unary_op(*__first);
4709 return __result;
4713 * @brief Perform an operation on corresponding elements of two sequences.
4714 * @ingroup mutating_algorithms
4715 * @param first1 An input iterator.
4716 * @param last1 An input iterator.
4717 * @param first2 An input iterator.
4718 * @param result An output iterator.
4719 * @param binary_op A binary operator.
4720 * @return An output iterator equal to @p result+(last-first).
4722 * Applies the operator to the corresponding elements in the two
4723 * input ranges and assigns the results to successive elements of the
4724 * output sequence.
4725 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4726 * @c N in the range @p [0,last1-first1).
4728 * @p binary_op must not alter either of its arguments.
4730 template<typename _InputIterator1, typename _InputIterator2,
4731 typename _OutputIterator, typename _BinaryOperation>
4732 _OutputIterator
4733 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4734 _InputIterator2 __first2, _OutputIterator __result,
4735 _BinaryOperation __binary_op)
4737 // concept requirements
4738 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4739 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4740 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4741 // "the type returned by a _BinaryOperation"
4742 __typeof__(__binary_op(*__first1,*__first2))>)
4743 __glibcxx_requires_valid_range(__first1, __last1);
4745 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4746 *__result = __binary_op(*__first1, *__first2);
4747 return __result;
4751 * @brief Replace each occurrence of one value in a sequence with another
4752 * value.
4753 * @ingroup mutating_algorithms
4754 * @param first A forward iterator.
4755 * @param last A forward iterator.
4756 * @param old_value The value to be replaced.
4757 * @param new_value The replacement value.
4758 * @return replace() returns no value.
4760 * For each iterator @c i in the range @p [first,last) if @c *i ==
4761 * @p old_value then the assignment @c *i = @p new_value is performed.
4763 template<typename _ForwardIterator, typename _Tp>
4764 void
4765 replace(_ForwardIterator __first, _ForwardIterator __last,
4766 const _Tp& __old_value, const _Tp& __new_value)
4768 // concept requirements
4769 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4770 _ForwardIterator>)
4771 __glibcxx_function_requires(_EqualOpConcept<
4772 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4773 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4774 typename iterator_traits<_ForwardIterator>::value_type>)
4775 __glibcxx_requires_valid_range(__first, __last);
4777 for (; __first != __last; ++__first)
4778 if (*__first == __old_value)
4779 *__first = __new_value;
4783 * @brief Replace each value in a sequence for which a predicate returns
4784 * true with another value.
4785 * @ingroup mutating_algorithms
4786 * @param first A forward iterator.
4787 * @param last A forward iterator.
4788 * @param pred A predicate.
4789 * @param new_value The replacement value.
4790 * @return replace_if() returns no value.
4792 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4793 * is true then the assignment @c *i = @p new_value is performed.
4795 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4796 void
4797 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4798 _Predicate __pred, const _Tp& __new_value)
4800 // concept requirements
4801 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4802 _ForwardIterator>)
4803 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4804 typename iterator_traits<_ForwardIterator>::value_type>)
4805 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4806 typename iterator_traits<_ForwardIterator>::value_type>)
4807 __glibcxx_requires_valid_range(__first, __last);
4809 for (; __first != __last; ++__first)
4810 if (__pred(*__first))
4811 *__first = __new_value;
4815 * @brief Assign the result of a function object to each value in a
4816 * sequence.
4817 * @ingroup mutating_algorithms
4818 * @param first A forward iterator.
4819 * @param last A forward iterator.
4820 * @param gen A function object taking no arguments and returning
4821 * std::iterator_traits<_ForwardIterator>::value_type
4822 * @return generate() returns no value.
4824 * Performs the assignment @c *i = @p gen() for each @c i in the range
4825 * @p [first,last).
4827 template<typename _ForwardIterator, typename _Generator>
4828 void
4829 generate(_ForwardIterator __first, _ForwardIterator __last,
4830 _Generator __gen)
4832 // concept requirements
4833 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4834 __glibcxx_function_requires(_GeneratorConcept<_Generator,
4835 typename iterator_traits<_ForwardIterator>::value_type>)
4836 __glibcxx_requires_valid_range(__first, __last);
4838 for (; __first != __last; ++__first)
4839 *__first = __gen();
4843 * @brief Assign the result of a function object to each value in a
4844 * sequence.
4845 * @ingroup mutating_algorithms
4846 * @param first A forward iterator.
4847 * @param n The length of the sequence.
4848 * @param gen A function object taking no arguments and returning
4849 * std::iterator_traits<_ForwardIterator>::value_type
4850 * @return The end of the sequence, @p first+n
4852 * Performs the assignment @c *i = @p gen() for each @c i in the range
4853 * @p [first,first+n).
4855 template<typename _OutputIterator, typename _Size, typename _Generator>
4856 _OutputIterator
4857 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4859 // concept requirements
4860 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4861 // "the type returned by a _Generator"
4862 __typeof__(__gen())>)
4864 for (; __n > 0; --__n, ++__first)
4865 *__first = __gen();
4866 return __first;
4871 * @brief Copy a sequence, removing consecutive duplicate values.
4872 * @ingroup mutating_algorithms
4873 * @param first An input iterator.
4874 * @param last An input iterator.
4875 * @param result An output iterator.
4876 * @return An iterator designating the end of the resulting sequence.
4878 * Copies each element in the range @p [first,last) to the range
4879 * beginning at @p result, except that only the first element is copied
4880 * from groups of consecutive elements that compare equal.
4881 * unique_copy() is stable, so the relative order of elements that are
4882 * copied is unchanged.
4884 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4885 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4887 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4888 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4889 * Assignable?
4891 template<typename _InputIterator, typename _OutputIterator>
4892 inline _OutputIterator
4893 unique_copy(_InputIterator __first, _InputIterator __last,
4894 _OutputIterator __result)
4896 // concept requirements
4897 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4898 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4899 typename iterator_traits<_InputIterator>::value_type>)
4900 __glibcxx_function_requires(_EqualityComparableConcept<
4901 typename iterator_traits<_InputIterator>::value_type>)
4902 __glibcxx_requires_valid_range(__first, __last);
4904 if (__first == __last)
4905 return __result;
4906 return std::__unique_copy(__first, __last, __result,
4907 std::__iterator_category(__first),
4908 std::__iterator_category(__result));
4912 * @brief Copy a sequence, removing consecutive values using a predicate.
4913 * @ingroup mutating_algorithms
4914 * @param first An input iterator.
4915 * @param last An input iterator.
4916 * @param result An output iterator.
4917 * @param binary_pred A binary predicate.
4918 * @return An iterator designating the end of the resulting sequence.
4920 * Copies each element in the range @p [first,last) to the range
4921 * beginning at @p result, except that only the first element is copied
4922 * from groups of consecutive elements for which @p binary_pred returns
4923 * true.
4924 * unique_copy() is stable, so the relative order of elements that are
4925 * copied is unchanged.
4927 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4928 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4930 template<typename _InputIterator, typename _OutputIterator,
4931 typename _BinaryPredicate>
4932 inline _OutputIterator
4933 unique_copy(_InputIterator __first, _InputIterator __last,
4934 _OutputIterator __result,
4935 _BinaryPredicate __binary_pred)
4937 // concept requirements -- predicates checked later
4938 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4939 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4940 typename iterator_traits<_InputIterator>::value_type>)
4941 __glibcxx_requires_valid_range(__first, __last);
4943 if (__first == __last)
4944 return __result;
4945 return std::__unique_copy(__first, __last, __result, __binary_pred,
4946 std::__iterator_category(__first),
4947 std::__iterator_category(__result));
4952 * @brief Randomly shuffle the elements of a sequence.
4953 * @ingroup mutating_algorithms
4954 * @param first A forward iterator.
4955 * @param last A forward iterator.
4956 * @return Nothing.
4958 * Reorder the elements in the range @p [first,last) using a random
4959 * distribution, so that every possible ordering of the sequence is
4960 * equally likely.
4962 template<typename _RandomAccessIterator>
4963 inline void
4964 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4966 // concept requirements
4967 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4968 _RandomAccessIterator>)
4969 __glibcxx_requires_valid_range(__first, __last);
4971 if (__first != __last)
4972 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4973 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
4977 * @brief Shuffle the elements of a sequence using a random number
4978 * generator.
4979 * @ingroup mutating_algorithms
4980 * @param first A forward iterator.
4981 * @param last A forward iterator.
4982 * @param rand The RNG functor or function.
4983 * @return Nothing.
4985 * Reorders the elements in the range @p [first,last) using @p rand to
4986 * provide a random distribution. Calling @p rand(N) for a positive
4987 * integer @p N should return a randomly chosen integer from the
4988 * range [0,N).
4990 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4991 void
4992 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4993 _RandomNumberGenerator& __rand)
4995 // concept requirements
4996 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4997 _RandomAccessIterator>)
4998 __glibcxx_requires_valid_range(__first, __last);
5000 if (__first == __last)
5001 return;
5002 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
5003 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
5008 * @brief Move elements for which a predicate is true to the beginning
5009 * of a sequence.
5010 * @ingroup mutating_algorithms
5011 * @param first A forward iterator.
5012 * @param last A forward iterator.
5013 * @param pred A predicate functor.
5014 * @return An iterator @p middle such that @p pred(i) is true for each
5015 * iterator @p i in the range @p [first,middle) and false for each @p i
5016 * in the range @p [middle,last).
5018 * @p pred must not modify its operand. @p partition() does not preserve
5019 * the relative ordering of elements in each group, use
5020 * @p stable_partition() if this is needed.
5022 template<typename _ForwardIterator, typename _Predicate>
5023 inline _ForwardIterator
5024 partition(_ForwardIterator __first, _ForwardIterator __last,
5025 _Predicate __pred)
5027 // concept requirements
5028 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
5029 _ForwardIterator>)
5030 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
5031 typename iterator_traits<_ForwardIterator>::value_type>)
5032 __glibcxx_requires_valid_range(__first, __last);
5034 return std::__partition(__first, __last, __pred,
5035 std::__iterator_category(__first));
5041 * @brief Sort the smallest elements of a sequence.
5042 * @ingroup sorting_algorithms
5043 * @param first An iterator.
5044 * @param middle Another iterator.
5045 * @param last Another iterator.
5046 * @return Nothing.
5048 * Sorts the smallest @p (middle-first) elements in the range
5049 * @p [first,last) and moves them to the range @p [first,middle). The
5050 * order of the remaining elements in the range @p [middle,last) is
5051 * undefined.
5052 * After the sort if @p i and @j are iterators in the range
5053 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5054 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
5056 template<typename _RandomAccessIterator>
5057 inline void
5058 partial_sort(_RandomAccessIterator __first,
5059 _RandomAccessIterator __middle,
5060 _RandomAccessIterator __last)
5062 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5063 _ValueType;
5065 // concept requirements
5066 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5067 _RandomAccessIterator>)
5068 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5069 __glibcxx_requires_valid_range(__first, __middle);
5070 __glibcxx_requires_valid_range(__middle, __last);
5072 std::__heap_select(__first, __middle, __last);
5073 std::sort_heap(__first, __middle);
5077 * @brief Sort the smallest elements of a sequence using a predicate
5078 * for comparison.
5079 * @ingroup sorting_algorithms
5080 * @param first An iterator.
5081 * @param middle Another iterator.
5082 * @param last Another iterator.
5083 * @param comp A comparison functor.
5084 * @return Nothing.
5086 * Sorts the smallest @p (middle-first) elements in the range
5087 * @p [first,last) and moves them to the range @p [first,middle). The
5088 * order of the remaining elements in the range @p [middle,last) is
5089 * undefined.
5090 * After the sort if @p i and @j are iterators in the range
5091 * @p [first,middle) such that @i precedes @j and @k is an iterator in
5092 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5093 * are both false.
5095 template<typename _RandomAccessIterator, typename _Compare>
5096 inline void
5097 partial_sort(_RandomAccessIterator __first,
5098 _RandomAccessIterator __middle,
5099 _RandomAccessIterator __last,
5100 _Compare __comp)
5102 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5103 _ValueType;
5105 // concept requirements
5106 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5107 _RandomAccessIterator>)
5108 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5109 _ValueType, _ValueType>)
5110 __glibcxx_requires_valid_range(__first, __middle);
5111 __glibcxx_requires_valid_range(__middle, __last);
5113 std::__heap_select(__first, __middle, __last, __comp);
5114 std::sort_heap(__first, __middle, __comp);
5118 * @brief Sort a sequence just enough to find a particular position.
5119 * @ingroup sorting_algorithms
5120 * @param first An iterator.
5121 * @param nth Another iterator.
5122 * @param last Another iterator.
5123 * @return Nothing.
5125 * Rearranges the elements in the range @p [first,last) so that @p *nth
5126 * is the same element that would have been in that position had the
5127 * whole sequence been sorted.
5128 * whole sequence been sorted. The elements either side of @p *nth are
5129 * not completely sorted, but for any iterator @i in the range
5130 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5131 * holds that @p *j<*i is false.
5133 template<typename _RandomAccessIterator>
5134 inline void
5135 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5136 _RandomAccessIterator __last)
5138 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5139 _ValueType;
5141 // concept requirements
5142 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5143 _RandomAccessIterator>)
5144 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5145 __glibcxx_requires_valid_range(__first, __nth);
5146 __glibcxx_requires_valid_range(__nth, __last);
5148 if (__first == __last || __nth == __last)
5149 return;
5151 std::__introselect(__first, __nth, __last,
5152 std::__lg(__last - __first) * 2);
5156 * @brief Sort a sequence just enough to find a particular position
5157 * using a predicate for comparison.
5158 * @ingroup sorting_algorithms
5159 * @param first An iterator.
5160 * @param nth Another iterator.
5161 * @param last Another iterator.
5162 * @param comp A comparison functor.
5163 * @return Nothing.
5165 * Rearranges the elements in the range @p [first,last) so that @p *nth
5166 * is the same element that would have been in that position had the
5167 * whole sequence been sorted. The elements either side of @p *nth are
5168 * not completely sorted, but for any iterator @i in the range
5169 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
5170 * holds that @p comp(*j,*i) is false.
5172 template<typename _RandomAccessIterator, typename _Compare>
5173 inline void
5174 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5175 _RandomAccessIterator __last, _Compare __comp)
5177 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5178 _ValueType;
5180 // concept requirements
5181 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5182 _RandomAccessIterator>)
5183 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5184 _ValueType, _ValueType>)
5185 __glibcxx_requires_valid_range(__first, __nth);
5186 __glibcxx_requires_valid_range(__nth, __last);
5188 if (__first == __last || __nth == __last)
5189 return;
5191 std::__introselect(__first, __nth, __last,
5192 std::__lg(__last - __first) * 2, __comp);
5197 * @brief Sort the elements of a sequence.
5198 * @ingroup sorting_algorithms
5199 * @param first An iterator.
5200 * @param last Another iterator.
5201 * @return Nothing.
5203 * Sorts the elements in the range @p [first,last) in ascending order,
5204 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5205 * @p [first,last-1).
5207 * The relative ordering of equivalent elements is not preserved, use
5208 * @p stable_sort() if this is needed.
5210 template<typename _RandomAccessIterator>
5211 inline void
5212 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5214 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5215 _ValueType;
5217 // concept requirements
5218 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5219 _RandomAccessIterator>)
5220 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5221 __glibcxx_requires_valid_range(__first, __last);
5223 if (__first != __last)
5225 std::__introsort_loop(__first, __last,
5226 std::__lg(__last - __first) * 2);
5227 std::__final_insertion_sort(__first, __last);
5232 * @brief Sort the elements of a sequence using a predicate for comparison.
5233 * @ingroup sorting_algorithms
5234 * @param first An iterator.
5235 * @param last Another iterator.
5236 * @param comp A comparison functor.
5237 * @return Nothing.
5239 * Sorts the elements in the range @p [first,last) in ascending order,
5240 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5241 * range @p [first,last-1).
5243 * The relative ordering of equivalent elements is not preserved, use
5244 * @p stable_sort() if this is needed.
5246 template<typename _RandomAccessIterator, typename _Compare>
5247 inline void
5248 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5249 _Compare __comp)
5251 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5252 _ValueType;
5254 // concept requirements
5255 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5256 _RandomAccessIterator>)
5257 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
5258 _ValueType>)
5259 __glibcxx_requires_valid_range(__first, __last);
5261 if (__first != __last)
5263 std::__introsort_loop(__first, __last,
5264 std::__lg(__last - __first) * 2, __comp);
5265 std::__final_insertion_sort(__first, __last, __comp);
5270 * @brief Merges two sorted ranges.
5271 * @ingroup sorting_algorithms
5272 * @param first1 An iterator.
5273 * @param first2 Another iterator.
5274 * @param last1 Another iterator.
5275 * @param last2 Another iterator.
5276 * @param result An iterator pointing to the end of the merged range.
5277 * @return An iterator pointing to the first element "not less
5278 * than" @a val.
5280 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5281 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5282 * must be sorted, and the output range must not overlap with either of
5283 * the input ranges. The sort is @e stable, that is, for equivalent
5284 * elements in the two ranges, elements from the first range will always
5285 * come before elements from the second.
5287 template<typename _InputIterator1, typename _InputIterator2,
5288 typename _OutputIterator>
5289 _OutputIterator
5290 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5291 _InputIterator2 __first2, _InputIterator2 __last2,
5292 _OutputIterator __result)
5294 typedef typename iterator_traits<_InputIterator1>::value_type
5295 _ValueType1;
5296 typedef typename iterator_traits<_InputIterator2>::value_type
5297 _ValueType2;
5299 // concept requirements
5300 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5301 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5302 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5303 _ValueType1>)
5304 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5305 _ValueType2>)
5306 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5307 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5308 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5310 while (__first1 != __last1 && __first2 != __last2)
5312 if (*__first2 < *__first1)
5314 *__result = *__first2;
5315 ++__first2;
5317 else
5319 *__result = *__first1;
5320 ++__first1;
5322 ++__result;
5324 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5325 __result));
5329 * @brief Merges two sorted ranges.
5330 * @ingroup sorting_algorithms
5331 * @param first1 An iterator.
5332 * @param first2 Another iterator.
5333 * @param last1 Another iterator.
5334 * @param last2 Another iterator.
5335 * @param result An iterator pointing to the end of the merged range.
5336 * @param comp A functor to use for comparisons.
5337 * @return An iterator pointing to the first element "not less
5338 * than" @a val.
5340 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5341 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5342 * must be sorted, and the output range must not overlap with either of
5343 * the input ranges. The sort is @e stable, that is, for equivalent
5344 * elements in the two ranges, elements from the first range will always
5345 * come before elements from the second.
5347 * The comparison function should have the same effects on ordering as
5348 * the function used for the initial sort.
5350 template<typename _InputIterator1, typename _InputIterator2,
5351 typename _OutputIterator, typename _Compare>
5352 _OutputIterator
5353 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5354 _InputIterator2 __first2, _InputIterator2 __last2,
5355 _OutputIterator __result, _Compare __comp)
5357 typedef typename iterator_traits<_InputIterator1>::value_type
5358 _ValueType1;
5359 typedef typename iterator_traits<_InputIterator2>::value_type
5360 _ValueType2;
5362 // concept requirements
5363 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5364 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5365 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5366 _ValueType1>)
5367 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5368 _ValueType2>)
5369 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5370 _ValueType2, _ValueType1>)
5371 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5372 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5374 while (__first1 != __last1 && __first2 != __last2)
5376 if (__comp(*__first2, *__first1))
5378 *__result = *__first2;
5379 ++__first2;
5381 else
5383 *__result = *__first1;
5384 ++__first1;
5386 ++__result;
5388 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5389 __result));
5394 * @brief Sort the elements of a sequence, preserving the relative order
5395 * of equivalent elements.
5396 * @ingroup sorting_algorithms
5397 * @param first An iterator.
5398 * @param last Another iterator.
5399 * @return Nothing.
5401 * Sorts the elements in the range @p [first,last) in ascending order,
5402 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5403 * @p [first,last-1).
5405 * The relative ordering of equivalent elements is preserved, so any two
5406 * elements @p x and @p y in the range @p [first,last) such that
5407 * @p x<y is false and @p y<x is false will have the same relative
5408 * ordering after calling @p stable_sort().
5410 template<typename _RandomAccessIterator>
5411 inline void
5412 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5414 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5415 _ValueType;
5416 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5417 _DistanceType;
5419 // concept requirements
5420 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5421 _RandomAccessIterator>)
5422 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5423 __glibcxx_requires_valid_range(__first, __last);
5425 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5426 __last);
5427 if (__buf.begin() == 0)
5428 std::__inplace_stable_sort(__first, __last);
5429 else
5430 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5431 _DistanceType(__buf.size()));
5435 * @brief Sort the elements of a sequence using a predicate for comparison,
5436 * preserving the relative order of equivalent elements.
5437 * @ingroup sorting_algorithms
5438 * @param first An iterator.
5439 * @param last Another iterator.
5440 * @param comp A comparison functor.
5441 * @return Nothing.
5443 * Sorts the elements in the range @p [first,last) in ascending order,
5444 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5445 * range @p [first,last-1).
5447 * The relative ordering of equivalent elements is preserved, so any two
5448 * elements @p x and @p y in the range @p [first,last) such that
5449 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5450 * relative ordering after calling @p stable_sort().
5452 template<typename _RandomAccessIterator, typename _Compare>
5453 inline void
5454 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5455 _Compare __comp)
5457 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5458 _ValueType;
5459 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5460 _DistanceType;
5462 // concept requirements
5463 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5464 _RandomAccessIterator>)
5465 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5466 _ValueType,
5467 _ValueType>)
5468 __glibcxx_requires_valid_range(__first, __last);
5470 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5471 __last);
5472 if (__buf.begin() == 0)
5473 std::__inplace_stable_sort(__first, __last, __comp);
5474 else
5475 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5476 _DistanceType(__buf.size()), __comp);
5481 * @brief Return the union of two sorted ranges.
5482 * @ingroup set_algorithms
5483 * @param first1 Start of first range.
5484 * @param last1 End of first range.
5485 * @param first2 Start of second range.
5486 * @param last2 End of second range.
5487 * @return End of the output range.
5488 * @ingroup set_algorithms
5490 * This operation iterates over both ranges, copying elements present in
5491 * each range in order to the output range. Iterators increment for each
5492 * range. When the current element of one range is less than the other,
5493 * that element is copied and the iterator advanced. If an element is
5494 * contained in both ranges, the element from the first range is copied and
5495 * both ranges advance. The output range may not overlap either input
5496 * range.
5498 template<typename _InputIterator1, typename _InputIterator2,
5499 typename _OutputIterator>
5500 _OutputIterator
5501 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5502 _InputIterator2 __first2, _InputIterator2 __last2,
5503 _OutputIterator __result)
5505 typedef typename iterator_traits<_InputIterator1>::value_type
5506 _ValueType1;
5507 typedef typename iterator_traits<_InputIterator2>::value_type
5508 _ValueType2;
5510 // concept requirements
5511 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5512 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5513 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5514 _ValueType1>)
5515 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5516 _ValueType2>)
5517 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5518 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5519 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5520 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5522 while (__first1 != __last1 && __first2 != __last2)
5524 if (*__first1 < *__first2)
5526 *__result = *__first1;
5527 ++__first1;
5529 else if (*__first2 < *__first1)
5531 *__result = *__first2;
5532 ++__first2;
5534 else
5536 *__result = *__first1;
5537 ++__first1;
5538 ++__first2;
5540 ++__result;
5542 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5543 __result));
5547 * @brief Return the union of two sorted ranges using a comparison functor.
5548 * @ingroup set_algorithms
5549 * @param first1 Start of first range.
5550 * @param last1 End of first range.
5551 * @param first2 Start of second range.
5552 * @param last2 End of second range.
5553 * @param comp The comparison functor.
5554 * @return End of the output range.
5555 * @ingroup set_algorithms
5557 * This operation iterates over both ranges, copying elements present in
5558 * each range in order to the output range. Iterators increment for each
5559 * range. When the current element of one range is less than the other
5560 * according to @a comp, that element is copied and the iterator advanced.
5561 * If an equivalent element according to @a comp is contained in both
5562 * ranges, the element from the first range is copied and both ranges
5563 * advance. The output range may not overlap either input range.
5565 template<typename _InputIterator1, typename _InputIterator2,
5566 typename _OutputIterator, typename _Compare>
5567 _OutputIterator
5568 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5569 _InputIterator2 __first2, _InputIterator2 __last2,
5570 _OutputIterator __result, _Compare __comp)
5572 typedef typename iterator_traits<_InputIterator1>::value_type
5573 _ValueType1;
5574 typedef typename iterator_traits<_InputIterator2>::value_type
5575 _ValueType2;
5577 // concept requirements
5578 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5579 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5580 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5581 _ValueType1>)
5582 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5583 _ValueType2>)
5584 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5585 _ValueType1, _ValueType2>)
5586 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5587 _ValueType2, _ValueType1>)
5588 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5589 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5591 while (__first1 != __last1 && __first2 != __last2)
5593 if (__comp(*__first1, *__first2))
5595 *__result = *__first1;
5596 ++__first1;
5598 else if (__comp(*__first2, *__first1))
5600 *__result = *__first2;
5601 ++__first2;
5603 else
5605 *__result = *__first1;
5606 ++__first1;
5607 ++__first2;
5609 ++__result;
5611 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5612 __result));
5616 * @brief Return the intersection of two sorted ranges.
5617 * @ingroup set_algorithms
5618 * @param first1 Start of first range.
5619 * @param last1 End of first range.
5620 * @param first2 Start of second range.
5621 * @param last2 End of second range.
5622 * @return End of the output range.
5623 * @ingroup set_algorithms
5625 * This operation iterates over both ranges, copying elements present in
5626 * both ranges in order to the output range. Iterators increment for each
5627 * range. When the current element of one range is less than the other,
5628 * that iterator advances. If an element is contained in both ranges, the
5629 * element from the first range is copied and both ranges advance. The
5630 * output range may not overlap either input range.
5632 template<typename _InputIterator1, typename _InputIterator2,
5633 typename _OutputIterator>
5634 _OutputIterator
5635 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5636 _InputIterator2 __first2, _InputIterator2 __last2,
5637 _OutputIterator __result)
5639 typedef typename iterator_traits<_InputIterator1>::value_type
5640 _ValueType1;
5641 typedef typename iterator_traits<_InputIterator2>::value_type
5642 _ValueType2;
5644 // concept requirements
5645 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5646 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5647 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5648 _ValueType1>)
5649 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5650 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5651 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5652 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5654 while (__first1 != __last1 && __first2 != __last2)
5655 if (*__first1 < *__first2)
5656 ++__first1;
5657 else if (*__first2 < *__first1)
5658 ++__first2;
5659 else
5661 *__result = *__first1;
5662 ++__first1;
5663 ++__first2;
5664 ++__result;
5666 return __result;
5670 * @brief Return the intersection of two sorted ranges using comparison
5671 * functor.
5672 * @ingroup set_algorithms
5673 * @param first1 Start of first range.
5674 * @param last1 End of first range.
5675 * @param first2 Start of second range.
5676 * @param last2 End of second range.
5677 * @param comp The comparison functor.
5678 * @return End of the output range.
5679 * @ingroup set_algorithms
5681 * This operation iterates over both ranges, copying elements present in
5682 * both ranges in order to the output range. Iterators increment for each
5683 * range. When the current element of one range is less than the other
5684 * according to @a comp, that iterator advances. If an element is
5685 * contained in both ranges according to @a comp, the element from the
5686 * first range is copied and both ranges advance. The output range may not
5687 * overlap either input range.
5689 template<typename _InputIterator1, typename _InputIterator2,
5690 typename _OutputIterator, typename _Compare>
5691 _OutputIterator
5692 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5693 _InputIterator2 __first2, _InputIterator2 __last2,
5694 _OutputIterator __result, _Compare __comp)
5696 typedef typename iterator_traits<_InputIterator1>::value_type
5697 _ValueType1;
5698 typedef typename iterator_traits<_InputIterator2>::value_type
5699 _ValueType2;
5701 // concept requirements
5702 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5703 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5704 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5705 _ValueType1>)
5706 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5707 _ValueType1, _ValueType2>)
5708 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5709 _ValueType2, _ValueType1>)
5710 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5711 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5713 while (__first1 != __last1 && __first2 != __last2)
5714 if (__comp(*__first1, *__first2))
5715 ++__first1;
5716 else if (__comp(*__first2, *__first1))
5717 ++__first2;
5718 else
5720 *__result = *__first1;
5721 ++__first1;
5722 ++__first2;
5723 ++__result;
5725 return __result;
5729 * @brief Return the difference of two sorted ranges.
5730 * @ingroup set_algorithms
5731 * @param first1 Start of first range.
5732 * @param last1 End of first range.
5733 * @param first2 Start of second range.
5734 * @param last2 End of second range.
5735 * @return End of the output range.
5736 * @ingroup set_algorithms
5738 * This operation iterates over both ranges, copying elements present in
5739 * the first range but not the second in order to the output range.
5740 * Iterators increment for each range. When the current element of the
5741 * first range is less than the second, that element is copied and the
5742 * iterator advances. If the current element of the second range is less,
5743 * the iterator advances, but no element is copied. If an element is
5744 * contained in both ranges, no elements are copied and both ranges
5745 * advance. The output range may not overlap either input range.
5747 template<typename _InputIterator1, typename _InputIterator2,
5748 typename _OutputIterator>
5749 _OutputIterator
5750 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5751 _InputIterator2 __first2, _InputIterator2 __last2,
5752 _OutputIterator __result)
5754 typedef typename iterator_traits<_InputIterator1>::value_type
5755 _ValueType1;
5756 typedef typename iterator_traits<_InputIterator2>::value_type
5757 _ValueType2;
5759 // concept requirements
5760 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5761 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5762 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5763 _ValueType1>)
5764 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5765 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5766 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5767 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5769 while (__first1 != __last1 && __first2 != __last2)
5770 if (*__first1 < *__first2)
5772 *__result = *__first1;
5773 ++__first1;
5774 ++__result;
5776 else if (*__first2 < *__first1)
5777 ++__first2;
5778 else
5780 ++__first1;
5781 ++__first2;
5783 return std::copy(__first1, __last1, __result);
5787 * @brief Return the difference of two sorted ranges using comparison
5788 * functor.
5789 * @ingroup set_algorithms
5790 * @param first1 Start of first range.
5791 * @param last1 End of first range.
5792 * @param first2 Start of second range.
5793 * @param last2 End of second range.
5794 * @param comp The comparison functor.
5795 * @return End of the output range.
5796 * @ingroup set_algorithms
5798 * This operation iterates over both ranges, copying elements present in
5799 * the first range but not the second in order to the output range.
5800 * Iterators increment for each range. When the current element of the
5801 * first range is less than the second according to @a comp, that element
5802 * is copied and the iterator advances. If the current element of the
5803 * second range is less, no element is copied and the iterator advances.
5804 * If an element is contained in both ranges according to @a comp, no
5805 * elements are copied and both ranges advance. The output range may not
5806 * overlap either input range.
5808 template<typename _InputIterator1, typename _InputIterator2,
5809 typename _OutputIterator, typename _Compare>
5810 _OutputIterator
5811 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5812 _InputIterator2 __first2, _InputIterator2 __last2,
5813 _OutputIterator __result, _Compare __comp)
5815 typedef typename iterator_traits<_InputIterator1>::value_type
5816 _ValueType1;
5817 typedef typename iterator_traits<_InputIterator2>::value_type
5818 _ValueType2;
5820 // concept requirements
5821 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5822 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5823 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5824 _ValueType1>)
5825 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5826 _ValueType1, _ValueType2>)
5827 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5828 _ValueType2, _ValueType1>)
5829 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5830 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5832 while (__first1 != __last1 && __first2 != __last2)
5833 if (__comp(*__first1, *__first2))
5835 *__result = *__first1;
5836 ++__first1;
5837 ++__result;
5839 else if (__comp(*__first2, *__first1))
5840 ++__first2;
5841 else
5843 ++__first1;
5844 ++__first2;
5846 return std::copy(__first1, __last1, __result);
5850 * @brief Return the symmetric difference of two sorted ranges.
5851 * @ingroup set_algorithms
5852 * @param first1 Start of first range.
5853 * @param last1 End of first range.
5854 * @param first2 Start of second range.
5855 * @param last2 End of second range.
5856 * @return End of the output range.
5857 * @ingroup set_algorithms
5859 * This operation iterates over both ranges, copying elements present in
5860 * one range but not the other in order to the output range. Iterators
5861 * increment for each range. When the current element of one range is less
5862 * than the other, that element is copied and the iterator advances. If an
5863 * element is contained in both ranges, no elements are copied and both
5864 * ranges advance. The output range may not overlap either input range.
5866 template<typename _InputIterator1, typename _InputIterator2,
5867 typename _OutputIterator>
5868 _OutputIterator
5869 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5870 _InputIterator2 __first2, _InputIterator2 __last2,
5871 _OutputIterator __result)
5873 typedef typename iterator_traits<_InputIterator1>::value_type
5874 _ValueType1;
5875 typedef typename iterator_traits<_InputIterator2>::value_type
5876 _ValueType2;
5878 // concept requirements
5879 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5880 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5881 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5882 _ValueType1>)
5883 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5884 _ValueType2>)
5885 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5886 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5887 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5888 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5890 while (__first1 != __last1 && __first2 != __last2)
5891 if (*__first1 < *__first2)
5893 *__result = *__first1;
5894 ++__first1;
5895 ++__result;
5897 else if (*__first2 < *__first1)
5899 *__result = *__first2;
5900 ++__first2;
5901 ++__result;
5903 else
5905 ++__first1;
5906 ++__first2;
5908 return std::copy(__first2, __last2, std::copy(__first1,
5909 __last1, __result));
5913 * @brief Return the symmetric difference of two sorted ranges using
5914 * comparison functor.
5915 * @ingroup set_algorithms
5916 * @param first1 Start of first range.
5917 * @param last1 End of first range.
5918 * @param first2 Start of second range.
5919 * @param last2 End of second range.
5920 * @param comp The comparison functor.
5921 * @return End of the output range.
5922 * @ingroup set_algorithms
5924 * This operation iterates over both ranges, copying elements present in
5925 * one range but not the other in order to the output range. Iterators
5926 * increment for each range. When the current element of one range is less
5927 * than the other according to @a comp, that element is copied and the
5928 * iterator advances. If an element is contained in both ranges according
5929 * to @a comp, no elements are copied and both ranges advance. The output
5930 * range may not overlap either input range.
5932 template<typename _InputIterator1, typename _InputIterator2,
5933 typename _OutputIterator, typename _Compare>
5934 _OutputIterator
5935 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5936 _InputIterator2 __first2, _InputIterator2 __last2,
5937 _OutputIterator __result,
5938 _Compare __comp)
5940 typedef typename iterator_traits<_InputIterator1>::value_type
5941 _ValueType1;
5942 typedef typename iterator_traits<_InputIterator2>::value_type
5943 _ValueType2;
5945 // concept requirements
5946 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5947 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5948 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5949 _ValueType1>)
5950 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5951 _ValueType2>)
5952 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5953 _ValueType1, _ValueType2>)
5954 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5955 _ValueType2, _ValueType1>)
5956 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5957 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5959 while (__first1 != __last1 && __first2 != __last2)
5960 if (__comp(*__first1, *__first2))
5962 *__result = *__first1;
5963 ++__first1;
5964 ++__result;
5966 else if (__comp(*__first2, *__first1))
5968 *__result = *__first2;
5969 ++__first2;
5970 ++__result;
5972 else
5974 ++__first1;
5975 ++__first2;
5977 return std::copy(__first2, __last2,
5978 std::copy(__first1, __last1, __result));
5983 * @brief Return the minimum element in a range.
5984 * @ingroup sorting_algorithms
5985 * @param first Start of range.
5986 * @param last End of range.
5987 * @return Iterator referencing the first instance of the smallest value.
5989 template<typename _ForwardIterator>
5990 _ForwardIterator
5991 min_element(_ForwardIterator __first, _ForwardIterator __last)
5993 // concept requirements
5994 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5995 __glibcxx_function_requires(_LessThanComparableConcept<
5996 typename iterator_traits<_ForwardIterator>::value_type>)
5997 __glibcxx_requires_valid_range(__first, __last);
5999 if (__first == __last)
6000 return __first;
6001 _ForwardIterator __result = __first;
6002 while (++__first != __last)
6003 if (*__first < *__result)
6004 __result = __first;
6005 return __result;
6009 * @brief Return the minimum element in a range using comparison functor.
6010 * @ingroup sorting_algorithms
6011 * @param first Start of range.
6012 * @param last End of range.
6013 * @param comp Comparison functor.
6014 * @return Iterator referencing the first instance of the smallest value
6015 * according to comp.
6017 template<typename _ForwardIterator, typename _Compare>
6018 _ForwardIterator
6019 min_element(_ForwardIterator __first, _ForwardIterator __last,
6020 _Compare __comp)
6022 // concept requirements
6023 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6024 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6025 typename iterator_traits<_ForwardIterator>::value_type,
6026 typename iterator_traits<_ForwardIterator>::value_type>)
6027 __glibcxx_requires_valid_range(__first, __last);
6029 if (__first == __last)
6030 return __first;
6031 _ForwardIterator __result = __first;
6032 while (++__first != __last)
6033 if (__comp(*__first, *__result))
6034 __result = __first;
6035 return __result;
6039 * @brief Return the maximum element in a range.
6040 * @ingroup sorting_algorithms
6041 * @param first Start of range.
6042 * @param last End of range.
6043 * @return Iterator referencing the first instance of the largest value.
6045 template<typename _ForwardIterator>
6046 _ForwardIterator
6047 max_element(_ForwardIterator __first, _ForwardIterator __last)
6049 // concept requirements
6050 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6051 __glibcxx_function_requires(_LessThanComparableConcept<
6052 typename iterator_traits<_ForwardIterator>::value_type>)
6053 __glibcxx_requires_valid_range(__first, __last);
6055 if (__first == __last)
6056 return __first;
6057 _ForwardIterator __result = __first;
6058 while (++__first != __last)
6059 if (*__result < *__first)
6060 __result = __first;
6061 return __result;
6065 * @brief Return the maximum element in a range using comparison functor.
6066 * @ingroup sorting_algorithms
6067 * @param first Start of range.
6068 * @param last End of range.
6069 * @param comp Comparison functor.
6070 * @return Iterator referencing the first instance of the largest value
6071 * according to comp.
6073 template<typename _ForwardIterator, typename _Compare>
6074 _ForwardIterator
6075 max_element(_ForwardIterator __first, _ForwardIterator __last,
6076 _Compare __comp)
6078 // concept requirements
6079 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6080 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6081 typename iterator_traits<_ForwardIterator>::value_type,
6082 typename iterator_traits<_ForwardIterator>::value_type>)
6083 __glibcxx_requires_valid_range(__first, __last);
6085 if (__first == __last) return __first;
6086 _ForwardIterator __result = __first;
6087 while (++__first != __last)
6088 if (__comp(*__result, *__first))
6089 __result = __first;
6090 return __result;
6093 _GLIBCXX_END_NESTED_NAMESPACE
6095 #endif /* _STL_ALGO_H */