OSDN Git Service

2010-03-19 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_algo.h
1 // Algorithm implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, or (at your option)
10 // any later version.
11
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.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /*
27  *
28  * Copyright (c) 1994
29  * Hewlett-Packard Company
30  *
31  * Permission to use, copy, modify, distribute and sell this software
32  * and its documentation for any purpose is hereby granted without fee,
33  * provided that the above copyright notice appear in all copies and
34  * that both that copyright notice and this permission notice appear
35  * in supporting documentation.  Hewlett-Packard Company makes no
36  * representations about the suitability of this software for any
37  * purpose.  It is provided "as is" without express or implied warranty.
38  *
39  *
40  * Copyright (c) 1996
41  * Silicon Graphics Computer Systems, Inc.
42  *
43  * Permission to use, copy, modify, distribute and sell this software
44  * and its documentation for any purpose is hereby granted without fee,
45  * provided that the above copyright notice appear in all copies and
46  * that both that copyright notice and this permission notice appear
47  * in supporting documentation.  Silicon Graphics makes no
48  * representations about the suitability of this software for any
49  * purpose.  It is provided "as is" without express or implied warranty.
50  */
51
52 /** @file stl_algo.h
53  *  This is an internal header file, included by other library headers.
54  *  You should not attempt to use it directly.
55  */
56
57 #ifndef _STL_ALGO_H
58 #define _STL_ALGO_H 1
59
60 #include <cstdlib>             // for rand
61 #include <bits/algorithmfwd.h>
62 #include <bits/stl_heap.h>
63 #include <bits/stl_tempbuf.h>  // for _Temporary_buffer
64
65 #ifdef __GXX_EXPERIMENTAL_CXX0X__
66 #include <random> // for std::uniform_int_distribution
67 #endif
68
69 // See concept_check.h for the __glibcxx_*_requires macros.
70
71 _GLIBCXX_BEGIN_NAMESPACE(std)
72
73   /// Swaps the median value of *__a, *__b and *__c to *__a
74   template<typename _Iterator>
75     void
76     __move_median_first(_Iterator __a, _Iterator __b, _Iterator __c)
77     {
78       // concept requirements
79       __glibcxx_function_requires(_LessThanComparableConcept<
80             typename iterator_traits<_Iterator>::value_type>)
81
82       if (*__a < *__b)
83         {
84           if (*__b < *__c)
85             std::iter_swap(__a, __b);
86           else if (*__a < *__c)
87             std::iter_swap(__a, __c);
88         }
89       else if (*__a < *__c)
90         return;
91       else if (*__b < *__c)
92         std::iter_swap(__a, __c);
93       else
94         std::iter_swap(__a, __b);
95     }
96
97   /// Swaps the median value of *__a, *__b and *__c under __comp to *__a
98   template<typename _Iterator, typename _Compare>
99     void
100     __move_median_first(_Iterator __a, _Iterator __b, _Iterator __c,
101                         _Compare __comp)
102     {
103       // concept requirements
104       __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
105             typename iterator_traits<_Iterator>::value_type,
106             typename iterator_traits<_Iterator>::value_type>)
107
108       if (__comp(*__a, *__b))
109         {
110           if (__comp(*__b, *__c))
111             std::iter_swap(__a, __b);
112           else if (__comp(*__a, *__c))
113             std::iter_swap(__a, __c);
114         }
115       else if (__comp(*__a, *__c))
116         return;
117       else if (__comp(*__b, *__c))
118         std::iter_swap(__a, __c);
119       else
120         std::iter_swap(__a, __b);
121     }
122
123   // for_each
124
125   /// This is an overload used by find() for the Input Iterator case.
126   template<typename _InputIterator, typename _Tp>
127     inline _InputIterator
128     __find(_InputIterator __first, _InputIterator __last,
129            const _Tp& __val, input_iterator_tag)
130     {
131       while (__first != __last && !(*__first == __val))
132         ++__first;
133       return __first;
134     }
135
136   /// This is an overload used by find_if() for the Input Iterator case.
137   template<typename _InputIterator, typename _Predicate>
138     inline _InputIterator
139     __find_if(_InputIterator __first, _InputIterator __last,
140               _Predicate __pred, input_iterator_tag)
141     {
142       while (__first != __last && !bool(__pred(*__first)))
143         ++__first;
144       return __first;
145     }
146
147   /// This is an overload used by find() for the RAI case.
148   template<typename _RandomAccessIterator, typename _Tp>
149     _RandomAccessIterator
150     __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
151            const _Tp& __val, random_access_iterator_tag)
152     {
153       typename iterator_traits<_RandomAccessIterator>::difference_type
154         __trip_count = (__last - __first) >> 2;
155
156       for (; __trip_count > 0; --__trip_count)
157         {
158           if (*__first == __val)
159             return __first;
160           ++__first;
161
162           if (*__first == __val)
163             return __first;
164           ++__first;
165
166           if (*__first == __val)
167             return __first;
168           ++__first;
169
170           if (*__first == __val)
171             return __first;
172           ++__first;
173         }
174
175       switch (__last - __first)
176         {
177         case 3:
178           if (*__first == __val)
179             return __first;
180           ++__first;
181         case 2:
182           if (*__first == __val)
183             return __first;
184           ++__first;
185         case 1:
186           if (*__first == __val)
187             return __first;
188           ++__first;
189         case 0:
190         default:
191           return __last;
192         }
193     }
194
195   /// This is an overload used by find_if() for the RAI case.
196   template<typename _RandomAccessIterator, typename _Predicate>
197     _RandomAccessIterator
198     __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
199               _Predicate __pred, random_access_iterator_tag)
200     {
201       typename iterator_traits<_RandomAccessIterator>::difference_type
202         __trip_count = (__last - __first) >> 2;
203
204       for (; __trip_count > 0; --__trip_count)
205         {
206           if (__pred(*__first))
207             return __first;
208           ++__first;
209
210           if (__pred(*__first))
211             return __first;
212           ++__first;
213
214           if (__pred(*__first))
215             return __first;
216           ++__first;
217
218           if (__pred(*__first))
219             return __first;
220           ++__first;
221         }
222
223       switch (__last - __first)
224         {
225         case 3:
226           if (__pred(*__first))
227             return __first;
228           ++__first;
229         case 2:
230           if (__pred(*__first))
231             return __first;
232           ++__first;
233         case 1:
234           if (__pred(*__first))
235             return __first;
236           ++__first;
237         case 0:
238         default:
239           return __last;
240         }
241     }
242
243 #ifdef __GXX_EXPERIMENTAL_CXX0X__
244   /// This is an overload used by find_if_not() for the Input Iterator case.
245   template<typename _InputIterator, typename _Predicate>
246     inline _InputIterator
247     __find_if_not(_InputIterator __first, _InputIterator __last,
248                   _Predicate __pred, input_iterator_tag)
249     {
250       while (__first != __last && bool(__pred(*__first)))
251         ++__first;
252       return __first;
253     }
254
255   /// This is an overload used by find_if_not() for the RAI case.
256   template<typename _RandomAccessIterator, typename _Predicate>
257     _RandomAccessIterator
258     __find_if_not(_RandomAccessIterator __first, _RandomAccessIterator __last,
259                   _Predicate __pred, random_access_iterator_tag)
260     {
261       typename iterator_traits<_RandomAccessIterator>::difference_type
262         __trip_count = (__last - __first) >> 2;
263
264       for (; __trip_count > 0; --__trip_count)
265         {
266           if (!bool(__pred(*__first)))
267             return __first;
268           ++__first;
269
270           if (!bool(__pred(*__first)))
271             return __first;
272           ++__first;
273
274           if (!bool(__pred(*__first)))
275             return __first;
276           ++__first;
277
278           if (!bool(__pred(*__first)))
279             return __first;
280           ++__first;
281         }
282
283       switch (__last - __first)
284         {
285         case 3:
286           if (!bool(__pred(*__first)))
287             return __first;
288           ++__first;
289         case 2:
290           if (!bool(__pred(*__first)))
291             return __first;
292           ++__first;
293         case 1:
294           if (!bool(__pred(*__first)))
295             return __first;
296           ++__first;
297         case 0:
298         default:
299           return __last;
300         }
301     }
302 #endif
303
304   // set_difference
305   // set_intersection
306   // set_symmetric_difference
307   // set_union
308   // for_each
309   // find
310   // find_if
311   // find_first_of
312   // adjacent_find
313   // count
314   // count_if
315   // search
316
317   /**
318    *  This is an uglified
319    *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
320    *  overloaded for forward iterators.
321   */
322   template<typename _ForwardIterator, typename _Integer, typename _Tp>
323     _ForwardIterator
324     __search_n(_ForwardIterator __first, _ForwardIterator __last,
325                _Integer __count, const _Tp& __val,
326                std::forward_iterator_tag)
327     {
328       __first = _GLIBCXX_STD_P::find(__first, __last, __val);
329       while (__first != __last)
330         {
331           typename iterator_traits<_ForwardIterator>::difference_type
332             __n = __count;
333           _ForwardIterator __i = __first;
334           ++__i;
335           while (__i != __last && __n != 1 && *__i == __val)
336             {
337               ++__i;
338               --__n;
339             }
340           if (__n == 1)
341             return __first;
342           if (__i == __last)
343             return __last;
344           __first = _GLIBCXX_STD_P::find(++__i, __last, __val);
345         }
346       return __last;
347     }
348
349   /**
350    *  This is an uglified
351    *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
352    *  overloaded for random access iterators.
353   */
354   template<typename _RandomAccessIter, typename _Integer, typename _Tp>
355     _RandomAccessIter
356     __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
357                _Integer __count, const _Tp& __val, 
358                std::random_access_iterator_tag)
359     {
360       
361       typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
362         _DistanceType;
363
364       _DistanceType __tailSize = __last - __first;
365       const _DistanceType __pattSize = __count;
366
367       if (__tailSize < __pattSize)
368         return __last;
369
370       const _DistanceType __skipOffset = __pattSize - 1;
371       _RandomAccessIter __lookAhead = __first + __skipOffset;
372       __tailSize -= __pattSize;
373
374       while (1) // the main loop...
375         {
376           // __lookAhead here is always pointing to the last element of next 
377           // possible match.
378           while (!(*__lookAhead == __val)) // the skip loop...
379             {
380               if (__tailSize < __pattSize)
381                 return __last;  // Failure
382               __lookAhead += __pattSize;
383               __tailSize -= __pattSize;
384             }
385           _DistanceType __remainder = __skipOffset;
386           for (_RandomAccessIter __backTrack = __lookAhead - 1; 
387                *__backTrack == __val; --__backTrack)
388             {
389               if (--__remainder == 0)
390                 return (__lookAhead - __skipOffset); // Success
391             }
392           if (__remainder > __tailSize)
393             return __last; // Failure
394           __lookAhead += __remainder;
395           __tailSize -= __remainder;
396         }
397     }
398
399   // search_n
400
401   /**
402    *  This is an uglified
403    *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
404    *           _BinaryPredicate)
405    *  overloaded for forward iterators.
406   */
407   template<typename _ForwardIterator, typename _Integer, typename _Tp,
408            typename _BinaryPredicate>
409     _ForwardIterator
410     __search_n(_ForwardIterator __first, _ForwardIterator __last,
411                _Integer __count, const _Tp& __val,
412                _BinaryPredicate __binary_pred, std::forward_iterator_tag)
413     {
414       while (__first != __last && !bool(__binary_pred(*__first, __val)))
415         ++__first;
416
417       while (__first != __last)
418         {
419           typename iterator_traits<_ForwardIterator>::difference_type
420             __n = __count;
421           _ForwardIterator __i = __first;
422           ++__i;
423           while (__i != __last && __n != 1 && bool(__binary_pred(*__i, __val)))
424             {
425               ++__i;
426               --__n;
427             }
428           if (__n == 1)
429             return __first;
430           if (__i == __last)
431             return __last;
432           __first = ++__i;
433           while (__first != __last
434                  && !bool(__binary_pred(*__first, __val)))
435             ++__first;
436         }
437       return __last;
438     }
439
440   /**
441    *  This is an uglified
442    *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
443    *           _BinaryPredicate)
444    *  overloaded for random access iterators.
445   */
446   template<typename _RandomAccessIter, typename _Integer, typename _Tp,
447            typename _BinaryPredicate>
448     _RandomAccessIter
449     __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
450                _Integer __count, const _Tp& __val,
451                _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
452     {
453       
454       typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
455         _DistanceType;
456
457       _DistanceType __tailSize = __last - __first;
458       const _DistanceType __pattSize = __count;
459
460       if (__tailSize < __pattSize)
461         return __last;
462
463       const _DistanceType __skipOffset = __pattSize - 1;
464       _RandomAccessIter __lookAhead = __first + __skipOffset;
465       __tailSize -= __pattSize;
466
467       while (1) // the main loop...
468         {
469           // __lookAhead here is always pointing to the last element of next 
470           // possible match.
471           while (!bool(__binary_pred(*__lookAhead, __val))) // the skip loop...
472             {
473               if (__tailSize < __pattSize)
474                 return __last;  // Failure
475               __lookAhead += __pattSize;
476               __tailSize -= __pattSize;
477             }
478           _DistanceType __remainder = __skipOffset;
479           for (_RandomAccessIter __backTrack = __lookAhead - 1; 
480                __binary_pred(*__backTrack, __val); --__backTrack)
481             {
482               if (--__remainder == 0)
483                 return (__lookAhead - __skipOffset); // Success
484             }
485           if (__remainder > __tailSize)
486             return __last; // Failure
487           __lookAhead += __remainder;
488           __tailSize -= __remainder;
489         }
490     }
491
492   // find_end for forward iterators.
493   template<typename _ForwardIterator1, typename _ForwardIterator2>
494     _ForwardIterator1
495     __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
496                _ForwardIterator2 __first2, _ForwardIterator2 __last2,
497                forward_iterator_tag, forward_iterator_tag)
498     {
499       if (__first2 == __last2)
500         return __last1;
501       else
502         {
503           _ForwardIterator1 __result = __last1;
504           while (1)
505             {
506               _ForwardIterator1 __new_result
507                 = _GLIBCXX_STD_P::search(__first1, __last1, __first2, __last2);
508               if (__new_result == __last1)
509                 return __result;
510               else
511                 {
512                   __result = __new_result;
513                   __first1 = __new_result;
514                   ++__first1;
515                 }
516             }
517         }
518     }
519
520   template<typename _ForwardIterator1, typename _ForwardIterator2,
521            typename _BinaryPredicate>
522     _ForwardIterator1
523     __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
524                _ForwardIterator2 __first2, _ForwardIterator2 __last2,
525                forward_iterator_tag, forward_iterator_tag,
526                _BinaryPredicate __comp)
527     {
528       if (__first2 == __last2)
529         return __last1;
530       else
531         {
532           _ForwardIterator1 __result = __last1;
533           while (1)
534             {
535               _ForwardIterator1 __new_result
536                 = _GLIBCXX_STD_P::search(__first1, __last1, __first2,
537                                          __last2, __comp);
538               if (__new_result == __last1)
539                 return __result;
540               else
541                 {
542                   __result = __new_result;
543                   __first1 = __new_result;
544                   ++__first1;
545                 }
546             }
547         }
548     }
549
550   // find_end for bidirectional iterators (much faster).
551   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
552     _BidirectionalIterator1
553     __find_end(_BidirectionalIterator1 __first1,
554                _BidirectionalIterator1 __last1,
555                _BidirectionalIterator2 __first2,
556                _BidirectionalIterator2 __last2,
557                bidirectional_iterator_tag, bidirectional_iterator_tag)
558     {
559       // concept requirements
560       __glibcxx_function_requires(_BidirectionalIteratorConcept<
561                                   _BidirectionalIterator1>)
562       __glibcxx_function_requires(_BidirectionalIteratorConcept<
563                                   _BidirectionalIterator2>)
564
565       typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
566       typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
567
568       _RevIterator1 __rlast1(__first1);
569       _RevIterator2 __rlast2(__first2);
570       _RevIterator1 __rresult = _GLIBCXX_STD_P::search(_RevIterator1(__last1),
571                                                        __rlast1,
572                                                        _RevIterator2(__last2),
573                                                        __rlast2);
574
575       if (__rresult == __rlast1)
576         return __last1;
577       else
578         {
579           _BidirectionalIterator1 __result = __rresult.base();
580           std::advance(__result, -std::distance(__first2, __last2));
581           return __result;
582         }
583     }
584
585   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
586            typename _BinaryPredicate>
587     _BidirectionalIterator1
588     __find_end(_BidirectionalIterator1 __first1,
589                _BidirectionalIterator1 __last1,
590                _BidirectionalIterator2 __first2,
591                _BidirectionalIterator2 __last2,
592                bidirectional_iterator_tag, bidirectional_iterator_tag,
593                _BinaryPredicate __comp)
594     {
595       // concept requirements
596       __glibcxx_function_requires(_BidirectionalIteratorConcept<
597                                   _BidirectionalIterator1>)
598       __glibcxx_function_requires(_BidirectionalIteratorConcept<
599                                   _BidirectionalIterator2>)
600
601       typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
602       typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
603
604       _RevIterator1 __rlast1(__first1);
605       _RevIterator2 __rlast2(__first2);
606       _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
607                                             _RevIterator2(__last2), __rlast2,
608                                             __comp);
609
610       if (__rresult == __rlast1)
611         return __last1;
612       else
613         {
614           _BidirectionalIterator1 __result = __rresult.base();
615           std::advance(__result, -std::distance(__first2, __last2));
616           return __result;
617         }
618     }
619
620   /**
621    *  @brief  Find last matching subsequence in a sequence.
622    *  @ingroup non_mutating_algorithms
623    *  @param  first1  Start of range to search.
624    *  @param  last1   End of range to search.
625    *  @param  first2  Start of sequence to match.
626    *  @param  last2   End of sequence to match.
627    *  @return   The last iterator @c i in the range
628    *  @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
629    *  for each @c N in the range @p [0,last2-first2), or @p last1 if no
630    *  such iterator exists.
631    *
632    *  Searches the range @p [first1,last1) for a sub-sequence that compares
633    *  equal value-by-value with the sequence given by @p [first2,last2) and
634    *  returns an iterator to the first element of the sub-sequence, or
635    *  @p last1 if the sub-sequence is not found.  The sub-sequence will be the
636    *  last such subsequence contained in [first,last1).
637    *
638    *  Because the sub-sequence must lie completely within the range
639    *  @p [first1,last1) it must start at a position less than
640    *  @p last1-(last2-first2) where @p last2-first2 is the length of the
641    *  sub-sequence.
642    *  This means that the returned iterator @c i will be in the range
643    *  @p [first1,last1-(last2-first2))
644   */
645   template<typename _ForwardIterator1, typename _ForwardIterator2>
646     inline _ForwardIterator1
647     find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
648              _ForwardIterator2 __first2, _ForwardIterator2 __last2)
649     {
650       // concept requirements
651       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
652       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
653       __glibcxx_function_requires(_EqualOpConcept<
654             typename iterator_traits<_ForwardIterator1>::value_type,
655             typename iterator_traits<_ForwardIterator2>::value_type>)
656       __glibcxx_requires_valid_range(__first1, __last1);
657       __glibcxx_requires_valid_range(__first2, __last2);
658
659       return std::__find_end(__first1, __last1, __first2, __last2,
660                              std::__iterator_category(__first1),
661                              std::__iterator_category(__first2));
662     }
663
664   /**
665    *  @brief  Find last matching subsequence in a sequence using a predicate.
666    *  @ingroup non_mutating_algorithms
667    *  @param  first1  Start of range to search.
668    *  @param  last1   End of range to search.
669    *  @param  first2  Start of sequence to match.
670    *  @param  last2   End of sequence to match.
671    *  @param  comp    The predicate to use.
672    *  @return   The last iterator @c i in the range
673    *  @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
674    *  (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
675    *  @p last1 if no such iterator exists.
676    *
677    *  Searches the range @p [first1,last1) for a sub-sequence that compares
678    *  equal value-by-value with the sequence given by @p [first2,last2) using
679    *  comp as a predicate and returns an iterator to the first element of the
680    *  sub-sequence, or @p last1 if the sub-sequence is not found.  The
681    *  sub-sequence will be the last such subsequence contained in
682    *  [first,last1).
683    *
684    *  Because the sub-sequence must lie completely within the range
685    *  @p [first1,last1) it must start at a position less than
686    *  @p last1-(last2-first2) where @p last2-first2 is the length of the
687    *  sub-sequence.
688    *  This means that the returned iterator @c i will be in the range
689    *  @p [first1,last1-(last2-first2))
690   */
691   template<typename _ForwardIterator1, typename _ForwardIterator2,
692            typename _BinaryPredicate>
693     inline _ForwardIterator1
694     find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
695              _ForwardIterator2 __first2, _ForwardIterator2 __last2,
696              _BinaryPredicate __comp)
697     {
698       // concept requirements
699       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
700       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
701       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
702             typename iterator_traits<_ForwardIterator1>::value_type,
703             typename iterator_traits<_ForwardIterator2>::value_type>)
704       __glibcxx_requires_valid_range(__first1, __last1);
705       __glibcxx_requires_valid_range(__first2, __last2);
706
707       return std::__find_end(__first1, __last1, __first2, __last2,
708                              std::__iterator_category(__first1),
709                              std::__iterator_category(__first2),
710                              __comp);
711     }
712
713 #ifdef __GXX_EXPERIMENTAL_CXX0X__
714   /**
715    *  @brief  Checks that a predicate is true for all the elements
716    *          of a sequence.
717    *  @ingroup non_mutating_algorithms
718    *  @param  first   An input iterator.
719    *  @param  last    An input iterator.
720    *  @param  pred    A predicate.
721    *  @return  True if the check is true, false otherwise.
722    *
723    *  Returns true if @p pred is true for each element in the range
724    *  @p [first,last), and false otherwise.
725   */
726   template<typename _InputIterator, typename _Predicate>
727     inline bool
728     all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
729     { return __last == std::find_if_not(__first, __last, __pred); }
730
731   /**
732    *  @brief  Checks that a predicate is false for all the elements
733    *          of a sequence.
734    *  @ingroup non_mutating_algorithms
735    *  @param  first   An input iterator.
736    *  @param  last    An input iterator.
737    *  @param  pred    A predicate.
738    *  @return  True if the check is true, false otherwise.
739    *
740    *  Returns true if @p pred is false for each element in the range
741    *  @p [first,last), and false otherwise.
742   */
743   template<typename _InputIterator, typename _Predicate>
744     inline bool
745     none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
746     { return __last == _GLIBCXX_STD_P::find_if(__first, __last, __pred); }
747
748   /**
749    *  @brief  Checks that a predicate is false for at least an element
750    *          of a sequence.
751    *  @ingroup non_mutating_algorithms
752    *  @param  first   An input iterator.
753    *  @param  last    An input iterator.
754    *  @param  pred    A predicate.
755    *  @return  True if the check is true, false otherwise.
756    *
757    *  Returns true if an element exists in the range @p [first,last) such that
758    *  @p pred is true, and false otherwise.
759   */
760   template<typename _InputIterator, typename _Predicate>
761     inline bool
762     any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
763     { return !std::none_of(__first, __last, __pred); }
764
765   /**
766    *  @brief  Find the first element in a sequence for which a
767    *          predicate is false.
768    *  @ingroup non_mutating_algorithms
769    *  @param  first  An input iterator.
770    *  @param  last   An input iterator.
771    *  @param  pred   A predicate.
772    *  @return   The first iterator @c i in the range @p [first,last)
773    *  such that @p pred(*i) is false, or @p last if no such iterator exists.
774   */
775   template<typename _InputIterator, typename _Predicate>
776     inline _InputIterator
777     find_if_not(_InputIterator __first, _InputIterator __last,
778                 _Predicate __pred)
779     {
780       // concept requirements
781       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
782       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
783               typename iterator_traits<_InputIterator>::value_type>)
784       __glibcxx_requires_valid_range(__first, __last);
785       return std::__find_if_not(__first, __last, __pred,
786                                 std::__iterator_category(__first));
787     }
788
789   /**
790    *  @brief  Checks whether the sequence is partitioned.
791    *  @ingroup mutating_algorithms
792    *  @param  first  An input iterator.
793    *  @param  last   An input iterator.
794    *  @param  pred   A predicate.
795    *  @return  True if the range @p [first,last) is partioned by @p pred,
796    *  i.e. if all elements that satisfy @p pred appear before those that
797    *  do not.
798   */
799   template<typename _InputIterator, typename _Predicate>
800     inline bool
801     is_partitioned(_InputIterator __first, _InputIterator __last,
802                    _Predicate __pred)
803     {
804       __first = std::find_if_not(__first, __last, __pred);
805       return std::none_of(__first, __last, __pred);
806     }
807
808   /**
809    *  @brief  Find the partition point of a partitioned range.
810    *  @ingroup mutating_algorithms
811    *  @param  first   An iterator.
812    *  @param  last    Another iterator.
813    *  @param  pred    A predicate.
814    *  @return  An iterator @p mid such that @p all_of(first, mid, pred)
815    *           and @p none_of(mid, last, pred) are both true.
816   */
817   template<typename _ForwardIterator, typename _Predicate>
818     _ForwardIterator
819     partition_point(_ForwardIterator __first, _ForwardIterator __last,
820                     _Predicate __pred)
821     {
822       // concept requirements
823       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
824       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
825               typename iterator_traits<_ForwardIterator>::value_type>)
826
827       // A specific debug-mode test will be necessary...
828       __glibcxx_requires_valid_range(__first, __last);
829
830       typedef typename iterator_traits<_ForwardIterator>::difference_type
831         _DistanceType;
832
833       _DistanceType __len = std::distance(__first, __last);
834       _DistanceType __half;
835       _ForwardIterator __middle;
836
837       while (__len > 0)
838         {
839           __half = __len >> 1;
840           __middle = __first;
841           std::advance(__middle, __half);
842           if (__pred(*__middle))
843             {
844               __first = __middle;
845               ++__first;
846               __len = __len - __half - 1;
847             }
848           else
849             __len = __half;
850         }
851       return __first;
852     }
853 #endif
854
855
856   /**
857    *  @brief Copy a sequence, removing elements of a given value.
858    *  @ingroup mutating_algorithms
859    *  @param  first   An input iterator.
860    *  @param  last    An input iterator.
861    *  @param  result  An output iterator.
862    *  @param  value   The value to be removed.
863    *  @return   An iterator designating the end of the resulting sequence.
864    *
865    *  Copies each element in the range @p [first,last) not equal to @p value
866    *  to the range beginning at @p result.
867    *  remove_copy() is stable, so the relative order of elements that are
868    *  copied is unchanged.
869   */
870   template<typename _InputIterator, typename _OutputIterator, typename _Tp>
871     _OutputIterator
872     remove_copy(_InputIterator __first, _InputIterator __last,
873                 _OutputIterator __result, const _Tp& __value)
874     {
875       // concept requirements
876       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
877       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
878             typename iterator_traits<_InputIterator>::value_type>)
879       __glibcxx_function_requires(_EqualOpConcept<
880             typename iterator_traits<_InputIterator>::value_type, _Tp>)
881       __glibcxx_requires_valid_range(__first, __last);
882
883       for (; __first != __last; ++__first)
884         if (!(*__first == __value))
885           {
886             *__result = *__first;
887             ++__result;
888           }
889       return __result;
890     }
891
892   /**
893    *  @brief Copy a sequence, removing elements for which a predicate is true.
894    *  @ingroup mutating_algorithms
895    *  @param  first   An input iterator.
896    *  @param  last    An input iterator.
897    *  @param  result  An output iterator.
898    *  @param  pred    A predicate.
899    *  @return   An iterator designating the end of the resulting sequence.
900    *
901    *  Copies each element in the range @p [first,last) for which
902    *  @p pred returns false to the range beginning at @p result.
903    *
904    *  remove_copy_if() is stable, so the relative order of elements that are
905    *  copied is unchanged.
906   */
907   template<typename _InputIterator, typename _OutputIterator,
908            typename _Predicate>
909     _OutputIterator
910     remove_copy_if(_InputIterator __first, _InputIterator __last,
911                    _OutputIterator __result, _Predicate __pred)
912     {
913       // concept requirements
914       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
915       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
916             typename iterator_traits<_InputIterator>::value_type>)
917       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
918             typename iterator_traits<_InputIterator>::value_type>)
919       __glibcxx_requires_valid_range(__first, __last);
920
921       for (; __first != __last; ++__first)
922         if (!bool(__pred(*__first)))
923           {
924             *__result = *__first;
925             ++__result;
926           }
927       return __result;
928     }
929
930 #ifdef __GXX_EXPERIMENTAL_CXX0X__
931   /**
932    *  @brief Copy the elements of a sequence for which a predicate is true.
933    *  @ingroup mutating_algorithms
934    *  @param  first   An input iterator.
935    *  @param  last    An input iterator.
936    *  @param  result  An output iterator.
937    *  @param  pred    A predicate.
938    *  @return   An iterator designating the end of the resulting sequence.
939    *
940    *  Copies each element in the range @p [first,last) for which
941    *  @p pred returns true to the range beginning at @p result.
942    *
943    *  copy_if() is stable, so the relative order of elements that are
944    *  copied is unchanged.
945   */
946   template<typename _InputIterator, typename _OutputIterator,
947            typename _Predicate>
948     _OutputIterator
949     copy_if(_InputIterator __first, _InputIterator __last,
950             _OutputIterator __result, _Predicate __pred)
951     {
952       // concept requirements
953       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
954       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
955             typename iterator_traits<_InputIterator>::value_type>)
956       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
957             typename iterator_traits<_InputIterator>::value_type>)
958       __glibcxx_requires_valid_range(__first, __last);
959
960       for (; __first != __last; ++__first)
961         if (__pred(*__first))
962           {
963             *__result = *__first;
964             ++__result;
965           }
966       return __result;
967     }
968
969
970   template<typename _InputIterator, typename _Size, typename _OutputIterator>
971     _OutputIterator
972     __copy_n(_InputIterator __first, _Size __n,
973              _OutputIterator __result, input_iterator_tag)
974     {
975       for (; __n > 0; --__n)
976         {
977           *__result = *__first;
978           ++__first;
979           ++__result;
980         }
981       return __result;
982     }
983
984   template<typename _RandomAccessIterator, typename _Size,
985            typename _OutputIterator>
986     inline _OutputIterator
987     __copy_n(_RandomAccessIterator __first, _Size __n,
988              _OutputIterator __result, random_access_iterator_tag)
989     { return std::copy(__first, __first + __n, __result); }
990
991   /**
992    *  @brief Copies the range [first,first+n) into [result,result+n).
993    *  @ingroup mutating_algorithms
994    *  @param  first  An input iterator.
995    *  @param  n      The number of elements to copy.
996    *  @param  result An output iterator.
997    *  @return  result+n.
998    *
999    *  This inline function will boil down to a call to @c memmove whenever
1000    *  possible.  Failing that, if random access iterators are passed, then the
1001    *  loop count will be known (and therefore a candidate for compiler
1002    *  optimizations such as unrolling).
1003   */
1004   template<typename _InputIterator, typename _Size, typename _OutputIterator>
1005     inline _OutputIterator
1006     copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1007     {
1008       // concept requirements
1009       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1010       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1011             typename iterator_traits<_InputIterator>::value_type>)
1012
1013       return std::__copy_n(__first, __n, __result,
1014                            std::__iterator_category(__first));
1015     }
1016
1017   /**
1018    *  @brief Copy the elements of a sequence to separate output sequences
1019    *         depending on the truth value of a predicate.
1020    *  @ingroup mutating_algorithms
1021    *  @param  first   An input iterator.
1022    *  @param  last    An input iterator.
1023    *  @param  out_true   An output iterator.
1024    *  @param  out_false  An output iterator.
1025    *  @param  pred    A predicate.
1026    *  @return   A pair designating the ends of the resulting sequences.
1027    *
1028    *  Copies each element in the range @p [first,last) for which
1029    *  @p pred returns true to the range beginning at @p out_true
1030    *  and each element for which @p pred returns false to @p out_false.
1031   */
1032   template<typename _InputIterator, typename _OutputIterator1,
1033            typename _OutputIterator2, typename _Predicate>
1034     pair<_OutputIterator1, _OutputIterator2>
1035     partition_copy(_InputIterator __first, _InputIterator __last,
1036                    _OutputIterator1 __out_true, _OutputIterator2 __out_false,
1037                    _Predicate __pred)
1038     {
1039       // concept requirements
1040       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1041       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
1042             typename iterator_traits<_InputIterator>::value_type>)
1043       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
1044             typename iterator_traits<_InputIterator>::value_type>)
1045       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1046             typename iterator_traits<_InputIterator>::value_type>)
1047       __glibcxx_requires_valid_range(__first, __last);
1048       
1049       for (; __first != __last; ++__first)
1050         if (__pred(*__first))
1051           {
1052             *__out_true = *__first;
1053             ++__out_true;
1054           }
1055         else
1056           {
1057             *__out_false = *__first;
1058             ++__out_false;
1059           }
1060
1061       return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
1062     }
1063 #endif
1064
1065   /**
1066    *  @brief Remove elements from a sequence.
1067    *  @ingroup mutating_algorithms
1068    *  @param  first  An input iterator.
1069    *  @param  last   An input iterator.
1070    *  @param  value  The value to be removed.
1071    *  @return   An iterator designating the end of the resulting sequence.
1072    *
1073    *  All elements equal to @p value are removed from the range
1074    *  @p [first,last).
1075    *
1076    *  remove() is stable, so the relative order of elements that are
1077    *  not removed is unchanged.
1078    *
1079    *  Elements between the end of the resulting sequence and @p last
1080    *  are still present, but their value is unspecified.
1081   */
1082   template<typename _ForwardIterator, typename _Tp>
1083     _ForwardIterator
1084     remove(_ForwardIterator __first, _ForwardIterator __last,
1085            const _Tp& __value)
1086     {
1087       // concept requirements
1088       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1089                                   _ForwardIterator>)
1090       __glibcxx_function_requires(_EqualOpConcept<
1091             typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1092       __glibcxx_requires_valid_range(__first, __last);
1093
1094       __first = _GLIBCXX_STD_P::find(__first, __last, __value);
1095       if(__first == __last)
1096         return __first;
1097       _ForwardIterator __result = __first;
1098       ++__first;
1099       for(; __first != __last; ++__first)
1100         if(!(*__first == __value))
1101           {
1102             *__result = _GLIBCXX_MOVE(*__first);
1103             ++__result;
1104           }
1105       return __result;
1106     }
1107
1108   /**
1109    *  @brief Remove elements from a sequence using a predicate.
1110    *  @ingroup mutating_algorithms
1111    *  @param  first  A forward iterator.
1112    *  @param  last   A forward iterator.
1113    *  @param  pred   A predicate.
1114    *  @return   An iterator designating the end of the resulting sequence.
1115    *
1116    *  All elements for which @p pred returns true are removed from the range
1117    *  @p [first,last).
1118    *
1119    *  remove_if() is stable, so the relative order of elements that are
1120    *  not removed is unchanged.
1121    *
1122    *  Elements between the end of the resulting sequence and @p last
1123    *  are still present, but their value is unspecified.
1124   */
1125   template<typename _ForwardIterator, typename _Predicate>
1126     _ForwardIterator
1127     remove_if(_ForwardIterator __first, _ForwardIterator __last,
1128               _Predicate __pred)
1129     {
1130       // concept requirements
1131       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1132                                   _ForwardIterator>)
1133       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1134             typename iterator_traits<_ForwardIterator>::value_type>)
1135       __glibcxx_requires_valid_range(__first, __last);
1136
1137       __first = _GLIBCXX_STD_P::find_if(__first, __last, __pred);
1138       if(__first == __last)
1139         return __first;
1140       _ForwardIterator __result = __first;
1141       ++__first;
1142       for(; __first != __last; ++__first)
1143         if(!bool(__pred(*__first)))
1144           {
1145             *__result = _GLIBCXX_MOVE(*__first);
1146             ++__result;
1147           }
1148       return __result;
1149     }
1150
1151   /**
1152    *  @brief Remove consecutive duplicate values from a sequence.
1153    *  @ingroup mutating_algorithms
1154    *  @param  first  A forward iterator.
1155    *  @param  last   A forward iterator.
1156    *  @return  An iterator designating the end of the resulting sequence.
1157    *
1158    *  Removes all but the first element from each group of consecutive
1159    *  values that compare equal.
1160    *  unique() is stable, so the relative order of elements that are
1161    *  not removed is unchanged.
1162    *  Elements between the end of the resulting sequence and @p last
1163    *  are still present, but their value is unspecified.
1164   */
1165   template<typename _ForwardIterator>
1166     _ForwardIterator
1167     unique(_ForwardIterator __first, _ForwardIterator __last)
1168     {
1169       // concept requirements
1170       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1171                                   _ForwardIterator>)
1172       __glibcxx_function_requires(_EqualityComparableConcept<
1173                      typename iterator_traits<_ForwardIterator>::value_type>)
1174       __glibcxx_requires_valid_range(__first, __last);
1175
1176       // Skip the beginning, if already unique.
1177       __first = _GLIBCXX_STD_P::adjacent_find(__first, __last);
1178       if (__first == __last)
1179         return __last;
1180
1181       // Do the real copy work.
1182       _ForwardIterator __dest = __first;
1183       ++__first;
1184       while (++__first != __last)
1185         if (!(*__dest == *__first))
1186           *++__dest = _GLIBCXX_MOVE(*__first);
1187       return ++__dest;
1188     }
1189
1190   /**
1191    *  @brief Remove consecutive values from a sequence using a predicate.
1192    *  @ingroup mutating_algorithms
1193    *  @param  first        A forward iterator.
1194    *  @param  last         A forward iterator.
1195    *  @param  binary_pred  A binary predicate.
1196    *  @return  An iterator designating the end of the resulting sequence.
1197    *
1198    *  Removes all but the first element from each group of consecutive
1199    *  values for which @p binary_pred returns true.
1200    *  unique() is stable, so the relative order of elements that are
1201    *  not removed is unchanged.
1202    *  Elements between the end of the resulting sequence and @p last
1203    *  are still present, but their value is unspecified.
1204   */
1205   template<typename _ForwardIterator, typename _BinaryPredicate>
1206     _ForwardIterator
1207     unique(_ForwardIterator __first, _ForwardIterator __last,
1208            _BinaryPredicate __binary_pred)
1209     {
1210       // concept requirements
1211       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1212                                   _ForwardIterator>)
1213       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1214                 typename iterator_traits<_ForwardIterator>::value_type,
1215                 typename iterator_traits<_ForwardIterator>::value_type>)
1216       __glibcxx_requires_valid_range(__first, __last);
1217
1218       // Skip the beginning, if already unique.
1219       __first = _GLIBCXX_STD_P::adjacent_find(__first, __last, __binary_pred);
1220       if (__first == __last)
1221         return __last;
1222
1223       // Do the real copy work.
1224       _ForwardIterator __dest = __first;
1225       ++__first;
1226       while (++__first != __last)
1227         if (!bool(__binary_pred(*__dest, *__first)))
1228           *++__dest = _GLIBCXX_MOVE(*__first);
1229       return ++__dest;
1230     }
1231
1232   /**
1233    *  This is an uglified unique_copy(_InputIterator, _InputIterator,
1234    *                                  _OutputIterator)
1235    *  overloaded for forward iterators and output iterator as result.
1236   */
1237   template<typename _ForwardIterator, typename _OutputIterator>
1238     _OutputIterator
1239     __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1240                   _OutputIterator __result,
1241                   forward_iterator_tag, output_iterator_tag)
1242     {
1243       // concept requirements -- taken care of in dispatching function
1244       _ForwardIterator __next = __first;
1245       *__result = *__first;
1246       while (++__next != __last)
1247         if (!(*__first == *__next))
1248           {
1249             __first = __next;
1250             *++__result = *__first;
1251           }
1252       return ++__result;
1253     }
1254
1255   /**
1256    *  This is an uglified unique_copy(_InputIterator, _InputIterator,
1257    *                                  _OutputIterator)
1258    *  overloaded for input iterators and output iterator as result.
1259   */
1260   template<typename _InputIterator, typename _OutputIterator>
1261     _OutputIterator
1262     __unique_copy(_InputIterator __first, _InputIterator __last,
1263                   _OutputIterator __result,
1264                   input_iterator_tag, output_iterator_tag)
1265     {
1266       // concept requirements -- taken care of in dispatching function
1267       typename iterator_traits<_InputIterator>::value_type __value = *__first;
1268       *__result = __value;
1269       while (++__first != __last)
1270         if (!(__value == *__first))
1271           {
1272             __value = *__first;
1273             *++__result = __value;
1274           }
1275       return ++__result;
1276     }
1277
1278   /**
1279    *  This is an uglified unique_copy(_InputIterator, _InputIterator,
1280    *                                  _OutputIterator)
1281    *  overloaded for input iterators and forward iterator as result.
1282   */
1283   template<typename _InputIterator, typename _ForwardIterator>
1284     _ForwardIterator
1285     __unique_copy(_InputIterator __first, _InputIterator __last,
1286                   _ForwardIterator __result,
1287                   input_iterator_tag, forward_iterator_tag)
1288     {
1289       // concept requirements -- taken care of in dispatching function
1290       *__result = *__first;
1291       while (++__first != __last)
1292         if (!(*__result == *__first))
1293           *++__result = *__first;
1294       return ++__result;
1295     }
1296
1297   /**
1298    *  This is an uglified
1299    *  unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1300    *              _BinaryPredicate)
1301    *  overloaded for forward iterators and output iterator as result.
1302   */
1303   template<typename _ForwardIterator, typename _OutputIterator,
1304            typename _BinaryPredicate>
1305     _OutputIterator
1306     __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1307                   _OutputIterator __result, _BinaryPredicate __binary_pred,
1308                   forward_iterator_tag, output_iterator_tag)
1309     {
1310       // concept requirements -- iterators already checked
1311       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1312           typename iterator_traits<_ForwardIterator>::value_type,
1313           typename iterator_traits<_ForwardIterator>::value_type>)
1314
1315       _ForwardIterator __next = __first;
1316       *__result = *__first;
1317       while (++__next != __last)
1318         if (!bool(__binary_pred(*__first, *__next)))
1319           {
1320             __first = __next;
1321             *++__result = *__first;
1322           }
1323       return ++__result;
1324     }
1325
1326   /**
1327    *  This is an uglified
1328    *  unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1329    *              _BinaryPredicate)
1330    *  overloaded for input iterators and output iterator as result.
1331   */
1332   template<typename _InputIterator, typename _OutputIterator,
1333            typename _BinaryPredicate>
1334     _OutputIterator
1335     __unique_copy(_InputIterator __first, _InputIterator __last,
1336                   _OutputIterator __result, _BinaryPredicate __binary_pred,
1337                   input_iterator_tag, output_iterator_tag)
1338     {
1339       // concept requirements -- iterators already checked
1340       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1341           typename iterator_traits<_InputIterator>::value_type,
1342           typename iterator_traits<_InputIterator>::value_type>)
1343
1344       typename iterator_traits<_InputIterator>::value_type __value = *__first;
1345       *__result = __value;
1346       while (++__first != __last)
1347         if (!bool(__binary_pred(__value, *__first)))
1348           {
1349             __value = *__first;
1350             *++__result = __value;
1351           }
1352       return ++__result;
1353     }
1354
1355   /**
1356    *  This is an uglified
1357    *  unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1358    *              _BinaryPredicate)
1359    *  overloaded for input iterators and forward iterator as result.
1360   */
1361   template<typename _InputIterator, typename _ForwardIterator,
1362            typename _BinaryPredicate>
1363     _ForwardIterator
1364     __unique_copy(_InputIterator __first, _InputIterator __last,
1365                   _ForwardIterator __result, _BinaryPredicate __binary_pred,
1366                   input_iterator_tag, forward_iterator_tag)
1367     {
1368       // concept requirements -- iterators already checked
1369       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1370           typename iterator_traits<_ForwardIterator>::value_type,
1371           typename iterator_traits<_InputIterator>::value_type>)
1372
1373       *__result = *__first;
1374       while (++__first != __last)
1375         if (!bool(__binary_pred(*__result, *__first)))
1376           *++__result = *__first;
1377       return ++__result;
1378     }
1379
1380   /**
1381    *  This is an uglified reverse(_BidirectionalIterator,
1382    *                              _BidirectionalIterator)
1383    *  overloaded for bidirectional iterators.
1384   */
1385   template<typename _BidirectionalIterator>
1386     void
1387     __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1388               bidirectional_iterator_tag)
1389     {
1390       while (true)
1391         if (__first == __last || __first == --__last)
1392           return;
1393         else
1394           {
1395             std::iter_swap(__first, __last);
1396             ++__first;
1397           }
1398     }
1399
1400   /**
1401    *  This is an uglified reverse(_BidirectionalIterator,
1402    *                              _BidirectionalIterator)
1403    *  overloaded for random access iterators.
1404   */
1405   template<typename _RandomAccessIterator>
1406     void
1407     __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1408               random_access_iterator_tag)
1409     {
1410       if (__first == __last)
1411         return;
1412       --__last;
1413       while (__first < __last)
1414         {
1415           std::iter_swap(__first, __last);
1416           ++__first;
1417           --__last;
1418         }
1419     }
1420
1421   /**
1422    *  @brief Reverse a sequence.
1423    *  @ingroup mutating_algorithms
1424    *  @param  first  A bidirectional iterator.
1425    *  @param  last   A bidirectional iterator.
1426    *  @return   reverse() returns no value.
1427    *
1428    *  Reverses the order of the elements in the range @p [first,last),
1429    *  so that the first element becomes the last etc.
1430    *  For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1431    *  swaps @p *(first+i) and @p *(last-(i+1))
1432   */
1433   template<typename _BidirectionalIterator>
1434     inline void
1435     reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1436     {
1437       // concept requirements
1438       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1439                                   _BidirectionalIterator>)
1440       __glibcxx_requires_valid_range(__first, __last);
1441       std::__reverse(__first, __last, std::__iterator_category(__first));
1442     }
1443
1444   /**
1445    *  @brief Copy a sequence, reversing its elements.
1446    *  @ingroup mutating_algorithms
1447    *  @param  first   A bidirectional iterator.
1448    *  @param  last    A bidirectional iterator.
1449    *  @param  result  An output iterator.
1450    *  @return  An iterator designating the end of the resulting sequence.
1451    *
1452    *  Copies the elements in the range @p [first,last) to the range
1453    *  @p [result,result+(last-first)) such that the order of the
1454    *  elements is reversed.
1455    *  For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1456    *  performs the assignment @p *(result+(last-first)-i) = *(first+i).
1457    *  The ranges @p [first,last) and @p [result,result+(last-first))
1458    *  must not overlap.
1459   */
1460   template<typename _BidirectionalIterator, typename _OutputIterator>
1461     _OutputIterator
1462     reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1463                  _OutputIterator __result)
1464     {
1465       // concept requirements
1466       __glibcxx_function_requires(_BidirectionalIteratorConcept<
1467                                   _BidirectionalIterator>)
1468       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1469                 typename iterator_traits<_BidirectionalIterator>::value_type>)
1470       __glibcxx_requires_valid_range(__first, __last);
1471
1472       while (__first != __last)
1473         {
1474           --__last;
1475           *__result = *__last;
1476           ++__result;
1477         }
1478       return __result;
1479     }
1480
1481   /**
1482    *  This is a helper function for the rotate algorithm specialized on RAIs.
1483    *  It returns the greatest common divisor of two integer values.
1484   */
1485   template<typename _EuclideanRingElement>
1486     _EuclideanRingElement
1487     __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1488     {
1489       while (__n != 0)
1490         {
1491           _EuclideanRingElement __t = __m % __n;
1492           __m = __n;
1493           __n = __t;
1494         }
1495       return __m;
1496     }
1497
1498   /// This is a helper function for the rotate algorithm.
1499   template<typename _ForwardIterator>
1500     void
1501     __rotate(_ForwardIterator __first,
1502              _ForwardIterator __middle,
1503              _ForwardIterator __last,
1504              forward_iterator_tag)
1505     {
1506       if (__first == __middle || __last  == __middle)
1507         return;
1508
1509       _ForwardIterator __first2 = __middle;
1510       do
1511         {
1512           std::iter_swap(__first, __first2);
1513           ++__first;
1514           ++__first2;
1515           if (__first == __middle)
1516             __middle = __first2;
1517         }
1518       while (__first2 != __last);
1519
1520       __first2 = __middle;
1521
1522       while (__first2 != __last)
1523         {
1524           std::iter_swap(__first, __first2);
1525           ++__first;
1526           ++__first2;
1527           if (__first == __middle)
1528             __middle = __first2;
1529           else if (__first2 == __last)
1530             __first2 = __middle;
1531         }
1532     }
1533
1534    /// This is a helper function for the rotate algorithm.
1535   template<typename _BidirectionalIterator>
1536     void
1537     __rotate(_BidirectionalIterator __first,
1538              _BidirectionalIterator __middle,
1539              _BidirectionalIterator __last,
1540               bidirectional_iterator_tag)
1541     {
1542       // concept requirements
1543       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1544                                   _BidirectionalIterator>)
1545
1546       if (__first == __middle || __last  == __middle)
1547         return;
1548
1549       std::__reverse(__first,  __middle, bidirectional_iterator_tag());
1550       std::__reverse(__middle, __last,   bidirectional_iterator_tag());
1551
1552       while (__first != __middle && __middle != __last)
1553         {
1554           std::iter_swap(__first, --__last);
1555           ++__first;
1556         }
1557
1558       if (__first == __middle)
1559         std::__reverse(__middle, __last,   bidirectional_iterator_tag());
1560       else
1561         std::__reverse(__first,  __middle, bidirectional_iterator_tag());
1562     }
1563
1564   /// This is a helper function for the rotate algorithm.
1565   template<typename _RandomAccessIterator>
1566     void
1567     __rotate(_RandomAccessIterator __first,
1568              _RandomAccessIterator __middle,
1569              _RandomAccessIterator __last,
1570              random_access_iterator_tag)
1571     {
1572       // concept requirements
1573       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1574                                   _RandomAccessIterator>)
1575
1576       if (__first == __middle || __last  == __middle)
1577         return;
1578
1579       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1580         _Distance;
1581       typedef typename iterator_traits<_RandomAccessIterator>::value_type
1582         _ValueType;
1583
1584       _Distance __n = __last   - __first;
1585       _Distance __k = __middle - __first;
1586
1587       if (__k == __n - __k)
1588         {
1589           std::swap_ranges(__first, __middle, __middle);
1590           return;
1591         }
1592
1593       _RandomAccessIterator __p = __first;
1594
1595       for (;;)
1596         {
1597           if (__k < __n - __k)
1598             {
1599               if (__is_pod(_ValueType) && __k == 1)
1600                 {
1601                   _ValueType __t = _GLIBCXX_MOVE(*__p);
1602                   _GLIBCXX_MOVE3(__p + 1, __p + __n, __p);
1603                   *(__p + __n - 1) = _GLIBCXX_MOVE(__t);
1604                   return;
1605                 }
1606               _RandomAccessIterator __q = __p + __k;
1607               for (_Distance __i = 0; __i < __n - __k; ++ __i)
1608                 {
1609                   std::iter_swap(__p, __q);
1610                   ++__p;
1611                   ++__q;
1612                 }
1613               __n %= __k;
1614               if (__n == 0)
1615                 return;
1616               std::swap(__n, __k);
1617               __k = __n - __k;
1618             }
1619           else
1620             {
1621               __k = __n - __k;
1622               if (__is_pod(_ValueType) && __k == 1)
1623                 {
1624                   _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
1625                   _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n);
1626                   *__p = _GLIBCXX_MOVE(__t);
1627                   return;
1628                 }
1629               _RandomAccessIterator __q = __p + __n;
1630               __p = __q - __k;
1631               for (_Distance __i = 0; __i < __n - __k; ++ __i)
1632                 {
1633                   --__p;
1634                   --__q;
1635                   std::iter_swap(__p, __q);
1636                 }
1637               __n %= __k;
1638               if (__n == 0)
1639                 return;
1640               std::swap(__n, __k);
1641             }
1642         }
1643     }
1644
1645   /**
1646    *  @brief Rotate the elements of a sequence.
1647    *  @ingroup mutating_algorithms
1648    *  @param  first   A forward iterator.
1649    *  @param  middle  A forward iterator.
1650    *  @param  last    A forward iterator.
1651    *  @return  Nothing.
1652    *
1653    *  Rotates the elements of the range @p [first,last) by @p (middle-first)
1654    *  positions so that the element at @p middle is moved to @p first, the
1655    *  element at @p middle+1 is moved to @first+1 and so on for each element
1656    *  in the range @p [first,last).
1657    *
1658    *  This effectively swaps the ranges @p [first,middle) and
1659    *  @p [middle,last).
1660    *
1661    *  Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1662    *  each @p n in the range @p [0,last-first).
1663   */
1664   template<typename _ForwardIterator>
1665     inline void
1666     rotate(_ForwardIterator __first, _ForwardIterator __middle,
1667            _ForwardIterator __last)
1668     {
1669       // concept requirements
1670       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1671                                   _ForwardIterator>)
1672       __glibcxx_requires_valid_range(__first, __middle);
1673       __glibcxx_requires_valid_range(__middle, __last);
1674
1675       typedef typename iterator_traits<_ForwardIterator>::iterator_category
1676         _IterType;
1677       std::__rotate(__first, __middle, __last, _IterType());
1678     }
1679
1680   /**
1681    *  @brief Copy a sequence, rotating its elements.
1682    *  @ingroup mutating_algorithms
1683    *  @param  first   A forward iterator.
1684    *  @param  middle  A forward iterator.
1685    *  @param  last    A forward iterator.
1686    *  @param  result  An output iterator.
1687    *  @return   An iterator designating the end of the resulting sequence.
1688    *
1689    *  Copies the elements of the range @p [first,last) to the range
1690    *  beginning at @result, rotating the copied elements by @p (middle-first)
1691    *  positions so that the element at @p middle is moved to @p result, the
1692    *  element at @p middle+1 is moved to @result+1 and so on for each element
1693    *  in the range @p [first,last).
1694    *
1695    *  Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1696    *  each @p n in the range @p [0,last-first).
1697   */
1698   template<typename _ForwardIterator, typename _OutputIterator>
1699     _OutputIterator
1700     rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1701                 _ForwardIterator __last, _OutputIterator __result)
1702     {
1703       // concept requirements
1704       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1705       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1706                 typename iterator_traits<_ForwardIterator>::value_type>)
1707       __glibcxx_requires_valid_range(__first, __middle);
1708       __glibcxx_requires_valid_range(__middle, __last);
1709
1710       return std::copy(__first, __middle,
1711                        std::copy(__middle, __last, __result));
1712     }
1713
1714   /// This is a helper function...
1715   template<typename _ForwardIterator, typename _Predicate>
1716     _ForwardIterator
1717     __partition(_ForwardIterator __first, _ForwardIterator __last,
1718                 _Predicate __pred, forward_iterator_tag)
1719     {
1720       if (__first == __last)
1721         return __first;
1722
1723       while (__pred(*__first))
1724         if (++__first == __last)
1725           return __first;
1726
1727       _ForwardIterator __next = __first;
1728
1729       while (++__next != __last)
1730         if (__pred(*__next))
1731           {
1732             std::iter_swap(__first, __next);
1733             ++__first;
1734           }
1735
1736       return __first;
1737     }
1738
1739   /// This is a helper function...
1740   template<typename _BidirectionalIterator, typename _Predicate>
1741     _BidirectionalIterator
1742     __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1743                 _Predicate __pred, bidirectional_iterator_tag)
1744     {
1745       while (true)
1746         {
1747           while (true)
1748             if (__first == __last)
1749               return __first;
1750             else if (__pred(*__first))
1751               ++__first;
1752             else
1753               break;
1754           --__last;
1755           while (true)
1756             if (__first == __last)
1757               return __first;
1758             else if (!bool(__pred(*__last)))
1759               --__last;
1760             else
1761               break;
1762           std::iter_swap(__first, __last);
1763           ++__first;
1764         }
1765     }
1766
1767   // partition
1768
1769   /// This is a helper function...
1770   template<typename _ForwardIterator, typename _Predicate, typename _Distance>
1771     _ForwardIterator
1772     __inplace_stable_partition(_ForwardIterator __first,
1773                                _ForwardIterator __last,
1774                                _Predicate __pred, _Distance __len)
1775     {
1776       if (__len == 1)
1777         return __pred(*__first) ? __last : __first;
1778       _ForwardIterator __middle = __first;
1779       std::advance(__middle, __len / 2);
1780       _ForwardIterator __begin = std::__inplace_stable_partition(__first,
1781                                                                  __middle,
1782                                                                  __pred,
1783                                                                  __len / 2);
1784       _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
1785                                                                __pred,
1786                                                                __len
1787                                                                - __len / 2);
1788       std::rotate(__begin, __middle, __end);
1789       std::advance(__begin, std::distance(__middle, __end));
1790       return __begin;
1791     }
1792
1793   /// This is a helper function...
1794   template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1795            typename _Distance>
1796     _ForwardIterator
1797     __stable_partition_adaptive(_ForwardIterator __first,
1798                                 _ForwardIterator __last,
1799                                 _Predicate __pred, _Distance __len,
1800                                 _Pointer __buffer,
1801                                 _Distance __buffer_size)
1802     {
1803       if (__len <= __buffer_size)
1804         {
1805           _ForwardIterator __result1 = __first;
1806           _Pointer __result2 = __buffer;
1807           for (; __first != __last; ++__first)
1808             if (__pred(*__first))
1809               {
1810                 *__result1 = _GLIBCXX_MOVE(*__first);
1811                 ++__result1;
1812               }
1813             else
1814               {
1815                 *__result2 = _GLIBCXX_MOVE(*__first);
1816                 ++__result2;
1817               }
1818           _GLIBCXX_MOVE3(__buffer, __result2, __result1);
1819           return __result1;
1820         }
1821       else
1822         {
1823           _ForwardIterator __middle = __first;
1824           std::advance(__middle, __len / 2);
1825           _ForwardIterator __begin =
1826             std::__stable_partition_adaptive(__first, __middle, __pred,
1827                                              __len / 2, __buffer,
1828                                              __buffer_size);
1829           _ForwardIterator __end =
1830             std::__stable_partition_adaptive(__middle, __last, __pred,
1831                                              __len - __len / 2,
1832                                              __buffer, __buffer_size);
1833           std::rotate(__begin, __middle, __end);
1834           std::advance(__begin, std::distance(__middle, __end));
1835           return __begin;
1836         }
1837     }
1838
1839   /**
1840    *  @brief Move elements for which a predicate is true to the beginning
1841    *         of a sequence, preserving relative ordering.
1842    *  @ingroup mutating_algorithms
1843    *  @param  first   A forward iterator.
1844    *  @param  last    A forward iterator.
1845    *  @param  pred    A predicate functor.
1846    *  @return  An iterator @p middle such that @p pred(i) is true for each
1847    *  iterator @p i in the range @p [first,middle) and false for each @p i
1848    *  in the range @p [middle,last).
1849    *
1850    *  Performs the same function as @p partition() with the additional
1851    *  guarantee that the relative ordering of elements in each group is
1852    *  preserved, so any two elements @p x and @p y in the range
1853    *  @p [first,last) such that @p pred(x)==pred(y) will have the same
1854    *  relative ordering after calling @p stable_partition().
1855   */
1856   template<typename _ForwardIterator, typename _Predicate>
1857     _ForwardIterator
1858     stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1859                      _Predicate __pred)
1860     {
1861       // concept requirements
1862       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1863                                   _ForwardIterator>)
1864       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1865             typename iterator_traits<_ForwardIterator>::value_type>)
1866       __glibcxx_requires_valid_range(__first, __last);
1867
1868       if (__first == __last)
1869         return __first;
1870       else
1871         {
1872           typedef typename iterator_traits<_ForwardIterator>::value_type
1873             _ValueType;
1874           typedef typename iterator_traits<_ForwardIterator>::difference_type
1875             _DistanceType;
1876
1877           _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
1878                                                                 __last);
1879         if (__buf.size() > 0)
1880           return
1881             std::__stable_partition_adaptive(__first, __last, __pred,
1882                                           _DistanceType(__buf.requested_size()),
1883                                           __buf.begin(),
1884                                           _DistanceType(__buf.size()));
1885         else
1886           return
1887             std::__inplace_stable_partition(__first, __last, __pred,
1888                                          _DistanceType(__buf.requested_size()));
1889         }
1890     }
1891
1892   /// This is a helper function for the sort routines.
1893   template<typename _RandomAccessIterator>
1894     void
1895     __heap_select(_RandomAccessIterator __first,
1896                   _RandomAccessIterator __middle,
1897                   _RandomAccessIterator __last)
1898     {
1899       std::make_heap(__first, __middle);
1900       for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1901         if (*__i < *__first)
1902           std::__pop_heap(__first, __middle, __i);
1903     }
1904
1905   /// This is a helper function for the sort routines.
1906   template<typename _RandomAccessIterator, typename _Compare>
1907     void
1908     __heap_select(_RandomAccessIterator __first,
1909                   _RandomAccessIterator __middle,
1910                   _RandomAccessIterator __last, _Compare __comp)
1911     {
1912       std::make_heap(__first, __middle, __comp);
1913       for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1914         if (__comp(*__i, *__first))
1915           std::__pop_heap(__first, __middle, __i, __comp);
1916     }
1917
1918   // partial_sort
1919
1920   /**
1921    *  @brief Copy the smallest elements of a sequence.
1922    *  @ingroup sorting_algorithms
1923    *  @param  first   An iterator.
1924    *  @param  last    Another iterator.
1925    *  @param  result_first   A random-access iterator.
1926    *  @param  result_last    Another random-access iterator.
1927    *  @return   An iterator indicating the end of the resulting sequence.
1928    *
1929    *  Copies and sorts the smallest N values from the range @p [first,last)
1930    *  to the range beginning at @p result_first, where the number of
1931    *  elements to be copied, @p N, is the smaller of @p (last-first) and
1932    *  @p (result_last-result_first).
1933    *  After the sort if @p i and @j are iterators in the range
1934    *  @p [result_first,result_first+N) such that @i precedes @j then
1935    *  @p *j<*i is false.
1936    *  The value returned is @p result_first+N.
1937   */
1938   template<typename _InputIterator, typename _RandomAccessIterator>
1939     _RandomAccessIterator
1940     partial_sort_copy(_InputIterator __first, _InputIterator __last,
1941                       _RandomAccessIterator __result_first,
1942                       _RandomAccessIterator __result_last)
1943     {
1944       typedef typename iterator_traits<_InputIterator>::value_type
1945         _InputValueType;
1946       typedef typename iterator_traits<_RandomAccessIterator>::value_type
1947         _OutputValueType;
1948       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1949         _DistanceType;
1950
1951       // concept requirements
1952       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1953       __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1954                                   _OutputValueType>)
1955       __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
1956                                                      _OutputValueType>)
1957       __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
1958       __glibcxx_requires_valid_range(__first, __last);
1959       __glibcxx_requires_valid_range(__result_first, __result_last);
1960
1961       if (__result_first == __result_last)
1962         return __result_last;
1963       _RandomAccessIterator __result_real_last = __result_first;
1964       while(__first != __last && __result_real_last != __result_last)
1965         {
1966           *__result_real_last = *__first;
1967           ++__result_real_last;
1968           ++__first;
1969         }
1970       std::make_heap(__result_first, __result_real_last);
1971       while (__first != __last)
1972         {
1973           if (*__first < *__result_first)
1974             std::__adjust_heap(__result_first, _DistanceType(0),
1975                                _DistanceType(__result_real_last
1976                                              - __result_first),
1977                                _InputValueType(*__first));
1978           ++__first;
1979         }
1980       std::sort_heap(__result_first, __result_real_last);
1981       return __result_real_last;
1982     }
1983
1984   /**
1985    *  @brief Copy the smallest elements of a sequence using a predicate for
1986    *         comparison.
1987    *  @ingroup sorting_algorithms
1988    *  @param  first   An input iterator.
1989    *  @param  last    Another input iterator.
1990    *  @param  result_first   A random-access iterator.
1991    *  @param  result_last    Another random-access iterator.
1992    *  @param  comp    A comparison functor.
1993    *  @return   An iterator indicating the end of the resulting sequence.
1994    *
1995    *  Copies and sorts the smallest N values from the range @p [first,last)
1996    *  to the range beginning at @p result_first, where the number of
1997    *  elements to be copied, @p N, is the smaller of @p (last-first) and
1998    *  @p (result_last-result_first).
1999    *  After the sort if @p i and @j are iterators in the range
2000    *  @p [result_first,result_first+N) such that @i precedes @j then
2001    *  @p comp(*j,*i) is false.
2002    *  The value returned is @p result_first+N.
2003   */
2004   template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
2005     _RandomAccessIterator
2006     partial_sort_copy(_InputIterator __first, _InputIterator __last,
2007                       _RandomAccessIterator __result_first,
2008                       _RandomAccessIterator __result_last,
2009                       _Compare __comp)
2010     {
2011       typedef typename iterator_traits<_InputIterator>::value_type
2012         _InputValueType;
2013       typedef typename iterator_traits<_RandomAccessIterator>::value_type
2014         _OutputValueType;
2015       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2016         _DistanceType;
2017
2018       // concept requirements
2019       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2020       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2021                                   _RandomAccessIterator>)
2022       __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2023                                   _OutputValueType>)
2024       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2025                                   _InputValueType, _OutputValueType>)
2026       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2027                                   _OutputValueType, _OutputValueType>)
2028       __glibcxx_requires_valid_range(__first, __last);
2029       __glibcxx_requires_valid_range(__result_first, __result_last);
2030
2031       if (__result_first == __result_last)
2032         return __result_last;
2033       _RandomAccessIterator __result_real_last = __result_first;
2034       while(__first != __last && __result_real_last != __result_last)
2035         {
2036           *__result_real_last = *__first;
2037           ++__result_real_last;
2038           ++__first;
2039         }
2040       std::make_heap(__result_first, __result_real_last, __comp);
2041       while (__first != __last)
2042         {
2043           if (__comp(*__first, *__result_first))
2044             std::__adjust_heap(__result_first, _DistanceType(0),
2045                                _DistanceType(__result_real_last
2046                                              - __result_first),
2047                                _InputValueType(*__first),
2048                                __comp);
2049           ++__first;
2050         }
2051       std::sort_heap(__result_first, __result_real_last, __comp);
2052       return __result_real_last;
2053     }
2054
2055   /// This is a helper function for the sort routine.
2056   template<typename _RandomAccessIterator>
2057     void
2058     __unguarded_linear_insert(_RandomAccessIterator __last)
2059     {
2060       typename iterator_traits<_RandomAccessIterator>::value_type
2061         __val = _GLIBCXX_MOVE(*__last);
2062       _RandomAccessIterator __next = __last;
2063       --__next;
2064       while (__val < *__next)
2065         {
2066           *__last = _GLIBCXX_MOVE(*__next);
2067           __last = __next;
2068           --__next;
2069         }
2070       *__last = _GLIBCXX_MOVE(__val);
2071     }
2072
2073   /// This is a helper function for the sort routine.
2074   template<typename _RandomAccessIterator, typename _Compare>
2075     void
2076     __unguarded_linear_insert(_RandomAccessIterator __last,
2077                               _Compare __comp)
2078     {
2079       typename iterator_traits<_RandomAccessIterator>::value_type
2080         __val = _GLIBCXX_MOVE(*__last);
2081       _RandomAccessIterator __next = __last;
2082       --__next;
2083       while (__comp(__val, *__next))
2084         {
2085           *__last = _GLIBCXX_MOVE(*__next);
2086           __last = __next;
2087           --__next;
2088         }
2089       *__last = _GLIBCXX_MOVE(__val);
2090     }
2091
2092   /// This is a helper function for the sort routine.
2093   template<typename _RandomAccessIterator>
2094     void
2095     __insertion_sort(_RandomAccessIterator __first,
2096                      _RandomAccessIterator __last)
2097     {
2098       if (__first == __last)
2099         return;
2100
2101       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2102         {
2103           if (*__i < *__first)
2104             {
2105               typename iterator_traits<_RandomAccessIterator>::value_type
2106                 __val = _GLIBCXX_MOVE(*__i);
2107               _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
2108               *__first = _GLIBCXX_MOVE(__val);
2109             }
2110           else
2111             std::__unguarded_linear_insert(__i);
2112         }
2113     }
2114
2115   /// This is a helper function for the sort routine.
2116   template<typename _RandomAccessIterator, typename _Compare>
2117     void
2118     __insertion_sort(_RandomAccessIterator __first,
2119                      _RandomAccessIterator __last, _Compare __comp)
2120     {
2121       if (__first == __last) return;
2122
2123       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2124         {
2125           if (__comp(*__i, *__first))
2126             {
2127               typename iterator_traits<_RandomAccessIterator>::value_type
2128                 __val = _GLIBCXX_MOVE(*__i);
2129               _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
2130               *__first = _GLIBCXX_MOVE(__val);
2131             }
2132           else
2133             std::__unguarded_linear_insert(__i, __comp);
2134         }
2135     }
2136
2137   /// This is a helper function for the sort routine.
2138   template<typename _RandomAccessIterator>
2139     inline void
2140     __unguarded_insertion_sort(_RandomAccessIterator __first,
2141                                _RandomAccessIterator __last)
2142     {
2143       typedef typename iterator_traits<_RandomAccessIterator>::value_type
2144         _ValueType;
2145
2146       for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2147         std::__unguarded_linear_insert(__i);
2148     }
2149
2150   /// This is a helper function for the sort routine.
2151   template<typename _RandomAccessIterator, typename _Compare>
2152     inline void
2153     __unguarded_insertion_sort(_RandomAccessIterator __first,
2154                                _RandomAccessIterator __last, _Compare __comp)
2155     {
2156       typedef typename iterator_traits<_RandomAccessIterator>::value_type
2157         _ValueType;
2158
2159       for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2160         std::__unguarded_linear_insert(__i, __comp);
2161     }
2162
2163   /**
2164    *  @doctodo
2165    *  This controls some aspect of the sort routines.
2166   */
2167   enum { _S_threshold = 16 };
2168
2169   /// This is a helper function for the sort routine.
2170   template<typename _RandomAccessIterator>
2171     void
2172     __final_insertion_sort(_RandomAccessIterator __first,
2173                            _RandomAccessIterator __last)
2174     {
2175       if (__last - __first > int(_S_threshold))
2176         {
2177           std::__insertion_sort(__first, __first + int(_S_threshold));
2178           std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
2179         }
2180       else
2181         std::__insertion_sort(__first, __last);
2182     }
2183
2184   /// This is a helper function for the sort routine.
2185   template<typename _RandomAccessIterator, typename _Compare>
2186     void
2187     __final_insertion_sort(_RandomAccessIterator __first,
2188                            _RandomAccessIterator __last, _Compare __comp)
2189     {
2190       if (__last - __first > int(_S_threshold))
2191         {
2192           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
2193           std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
2194                                           __comp);
2195         }
2196       else
2197         std::__insertion_sort(__first, __last, __comp);
2198     }
2199
2200   /// This is a helper function...
2201   template<typename _RandomAccessIterator, typename _Tp>
2202     _RandomAccessIterator
2203     __unguarded_partition(_RandomAccessIterator __first,
2204                           _RandomAccessIterator __last, const _Tp& __pivot)
2205     {
2206       while (true)
2207         {
2208           while (*__first < __pivot)
2209             ++__first;
2210           --__last;
2211           while (__pivot < *__last)
2212             --__last;
2213           if (!(__first < __last))
2214             return __first;
2215           std::iter_swap(__first, __last);
2216           ++__first;
2217         }
2218     }
2219
2220   /// This is a helper function...
2221   template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2222     _RandomAccessIterator
2223     __unguarded_partition(_RandomAccessIterator __first,
2224                           _RandomAccessIterator __last,
2225                           const _Tp& __pivot, _Compare __comp)
2226     {
2227       while (true)
2228         {
2229           while (__comp(*__first, __pivot))
2230             ++__first;
2231           --__last;
2232           while (__comp(__pivot, *__last))
2233             --__last;
2234           if (!(__first < __last))
2235             return __first;
2236           std::iter_swap(__first, __last);
2237           ++__first;
2238         }
2239     }
2240
2241   /// This is a helper function...
2242   template<typename _RandomAccessIterator>
2243     inline _RandomAccessIterator
2244     __unguarded_partition_pivot(_RandomAccessIterator __first,
2245                                 _RandomAccessIterator __last)
2246     {
2247       _RandomAccessIterator __mid = __first + (__last - __first) / 2;
2248       std::__move_median_first(__first, __mid, (__last - 1));
2249       return std::__unguarded_partition(__first + 1, __last, *__first);
2250     }
2251
2252
2253   /// This is a helper function...
2254   template<typename _RandomAccessIterator, typename _Compare>
2255     inline _RandomAccessIterator
2256     __unguarded_partition_pivot(_RandomAccessIterator __first,
2257                                 _RandomAccessIterator __last, _Compare __comp)
2258     {
2259       _RandomAccessIterator __mid = __first + (__last - __first) / 2;
2260       std::__move_median_first(__first, __mid, (__last - 1), __comp);
2261       return std::__unguarded_partition(__first + 1, __last, *__first, __comp);
2262     }
2263
2264   /// This is a helper function for the sort routine.
2265   template<typename _RandomAccessIterator, typename _Size>
2266     void
2267     __introsort_loop(_RandomAccessIterator __first,
2268                      _RandomAccessIterator __last,
2269                      _Size __depth_limit)
2270     {
2271       while (__last - __first > int(_S_threshold))
2272         {
2273           if (__depth_limit == 0)
2274             {
2275               _GLIBCXX_STD_P::partial_sort(__first, __last, __last);
2276               return;
2277             }
2278           --__depth_limit;
2279           _RandomAccessIterator __cut =
2280             std::__unguarded_partition_pivot(__first, __last);
2281           std::__introsort_loop(__cut, __last, __depth_limit);
2282           __last = __cut;
2283         }
2284     }
2285
2286   /// This is a helper function for the sort routine.
2287   template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2288     void
2289     __introsort_loop(_RandomAccessIterator __first,
2290                      _RandomAccessIterator __last,
2291                      _Size __depth_limit, _Compare __comp)
2292     {
2293       while (__last - __first > int(_S_threshold))
2294         {
2295           if (__depth_limit == 0)
2296             {
2297               _GLIBCXX_STD_P::partial_sort(__first, __last, __last, __comp);
2298               return;
2299             }
2300           --__depth_limit;
2301           _RandomAccessIterator __cut =
2302             std::__unguarded_partition_pivot(__first, __last, __comp);
2303           std::__introsort_loop(__cut, __last, __depth_limit, __comp);
2304           __last = __cut;
2305         }
2306     }
2307
2308   // sort
2309
2310   template<typename _RandomAccessIterator, typename _Size>
2311     void
2312     __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2313                   _RandomAccessIterator __last, _Size __depth_limit)
2314     {
2315       typedef typename iterator_traits<_RandomAccessIterator>::value_type
2316         _ValueType;
2317
2318       while (__last - __first > 3)
2319         {
2320           if (__depth_limit == 0)
2321             {
2322               std::__heap_select(__first, __nth + 1, __last);
2323
2324               // Place the nth largest element in its final position.
2325               std::iter_swap(__first, __nth);
2326               return;
2327             }
2328           --__depth_limit;
2329           _RandomAccessIterator __cut =
2330             std::__unguarded_partition_pivot(__first, __last);
2331           if (__cut <= __nth)
2332             __first = __cut;
2333           else
2334             __last = __cut;
2335         }
2336       std::__insertion_sort(__first, __last);
2337     }
2338
2339   template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2340     void
2341     __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
2342                   _RandomAccessIterator __last, _Size __depth_limit,
2343                   _Compare __comp)
2344     {
2345       typedef typename iterator_traits<_RandomAccessIterator>::value_type
2346         _ValueType;
2347
2348       while (__last - __first > 3)
2349         {
2350           if (__depth_limit == 0)
2351             {
2352               std::__heap_select(__first, __nth + 1, __last, __comp);
2353               // Place the nth largest element in its final position.
2354               std::iter_swap(__first, __nth);
2355               return;
2356             }
2357           --__depth_limit;
2358           _RandomAccessIterator __cut =
2359             std::__unguarded_partition_pivot(__first, __last, __comp);
2360           if (__cut <= __nth)
2361             __first = __cut;
2362           else
2363             __last = __cut;
2364         }
2365       std::__insertion_sort(__first, __last, __comp);
2366     }
2367
2368   // nth_element
2369
2370   // lower_bound moved to stl_algobase.h
2371
2372   /**
2373    *  @brief Finds the last position in which @a val could be inserted
2374    *         without changing the ordering.
2375    *  @ingroup binary_search_algorithms
2376    *  @param  first   An iterator.
2377    *  @param  last    Another iterator.
2378    *  @param  val     The search term.
2379    *  @return  An iterator pointing to the first element greater than @a val,
2380    *           or end() if no elements are greater than @a val.
2381    *  @ingroup binary_search_algorithms
2382   */
2383   template<typename _ForwardIterator, typename _Tp>
2384     _ForwardIterator
2385     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2386                 const _Tp& __val)
2387     {
2388       typedef typename iterator_traits<_ForwardIterator>::value_type
2389         _ValueType;
2390       typedef typename iterator_traits<_ForwardIterator>::difference_type
2391         _DistanceType;
2392
2393       // concept requirements
2394       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2395       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2396       __glibcxx_requires_partitioned_upper(__first, __last, __val);
2397
2398       _DistanceType __len = std::distance(__first, __last);
2399       _DistanceType __half;
2400       _ForwardIterator __middle;
2401
2402       while (__len > 0)
2403         {
2404           __half = __len >> 1;
2405           __middle = __first;
2406           std::advance(__middle, __half);
2407           if (__val < *__middle)
2408             __len = __half;
2409           else
2410             {
2411               __first = __middle;
2412               ++__first;
2413               __len = __len - __half - 1;
2414             }
2415         }
2416       return __first;
2417     }
2418
2419   /**
2420    *  @brief Finds the last position in which @a val could be inserted
2421    *         without changing the ordering.
2422    *  @ingroup binary_search_algorithms
2423    *  @param  first   An iterator.
2424    *  @param  last    Another iterator.
2425    *  @param  val     The search term.
2426    *  @param  comp    A functor to use for comparisons.
2427    *  @return  An iterator pointing to the first element greater than @a val,
2428    *           or end() if no elements are greater than @a val.
2429    *  @ingroup binary_search_algorithms
2430    *
2431    *  The comparison function should have the same effects on ordering as
2432    *  the function used for the initial sort.
2433   */
2434   template<typename _ForwardIterator, typename _Tp, typename _Compare>
2435     _ForwardIterator
2436     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2437                 const _Tp& __val, _Compare __comp)
2438     {
2439       typedef typename iterator_traits<_ForwardIterator>::value_type
2440         _ValueType;
2441       typedef typename iterator_traits<_ForwardIterator>::difference_type
2442         _DistanceType;
2443
2444       // concept requirements
2445       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2446       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2447                                   _Tp, _ValueType>)
2448       __glibcxx_requires_partitioned_upper_pred(__first, __last,
2449                                                 __val, __comp);
2450
2451       _DistanceType __len = std::distance(__first, __last);
2452       _DistanceType __half;
2453       _ForwardIterator __middle;
2454
2455       while (__len > 0)
2456         {
2457           __half = __len >> 1;
2458           __middle = __first;
2459           std::advance(__middle, __half);
2460           if (__comp(__val, *__middle))
2461             __len = __half;
2462           else
2463             {
2464               __first = __middle;
2465               ++__first;
2466               __len = __len - __half - 1;
2467             }
2468         }
2469       return __first;
2470     }
2471
2472   /**
2473    *  @brief Finds the largest subrange in which @a val could be inserted
2474    *         at any place in it without changing the ordering.
2475    *  @ingroup binary_search_algorithms
2476    *  @param  first   An iterator.
2477    *  @param  last    Another iterator.
2478    *  @param  val     The search term.
2479    *  @return  An pair of iterators defining the subrange.
2480    *  @ingroup binary_search_algorithms
2481    *
2482    *  This is equivalent to
2483    *  @code
2484    *    std::make_pair(lower_bound(first, last, val),
2485    *                   upper_bound(first, last, val))
2486    *  @endcode
2487    *  but does not actually call those functions.
2488   */
2489   template<typename _ForwardIterator, typename _Tp>
2490     pair<_ForwardIterator, _ForwardIterator>
2491     equal_range(_ForwardIterator __first, _ForwardIterator __last,
2492                 const _Tp& __val)
2493     {
2494       typedef typename iterator_traits<_ForwardIterator>::value_type
2495         _ValueType;
2496       typedef typename iterator_traits<_ForwardIterator>::difference_type
2497         _DistanceType;
2498
2499       // concept requirements
2500       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2501       __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2502       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)  
2503       __glibcxx_requires_partitioned_lower(__first, __last, __val);
2504       __glibcxx_requires_partitioned_upper(__first, __last, __val);      
2505
2506       _DistanceType __len = std::distance(__first, __last);
2507       _DistanceType __half;
2508       _ForwardIterator __middle, __left, __right;
2509
2510       while (__len > 0)
2511         {
2512           __half = __len >> 1;
2513           __middle = __first;
2514           std::advance(__middle, __half);
2515           if (*__middle < __val)
2516             {
2517               __first = __middle;
2518               ++__first;
2519               __len = __len - __half - 1;
2520             }
2521           else if (__val < *__middle)
2522             __len = __half;
2523           else
2524             {
2525               __left = std::lower_bound(__first, __middle, __val);
2526               std::advance(__first, __len);
2527               __right = std::upper_bound(++__middle, __first, __val);
2528               return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2529             }
2530         }
2531       return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2532     }
2533
2534   /**
2535    *  @brief Finds the largest subrange in which @a val could be inserted
2536    *         at any place in it without changing the ordering.
2537    *  @param  first   An iterator.
2538    *  @param  last    Another iterator.
2539    *  @param  val     The search term.
2540    *  @param  comp    A functor to use for comparisons.
2541    *  @return  An pair of iterators defining the subrange.
2542    *  @ingroup binary_search_algorithms
2543    *
2544    *  This is equivalent to
2545    *  @code
2546    *    std::make_pair(lower_bound(first, last, val, comp),
2547    *                   upper_bound(first, last, val, comp))
2548    *  @endcode
2549    *  but does not actually call those functions.
2550   */
2551   template<typename _ForwardIterator, typename _Tp, typename _Compare>
2552     pair<_ForwardIterator, _ForwardIterator>
2553     equal_range(_ForwardIterator __first, _ForwardIterator __last,
2554                 const _Tp& __val,
2555                 _Compare __comp)
2556     {
2557       typedef typename iterator_traits<_ForwardIterator>::value_type
2558         _ValueType;
2559       typedef typename iterator_traits<_ForwardIterator>::difference_type
2560         _DistanceType;
2561
2562       // concept requirements
2563       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2564       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2565                                   _ValueType, _Tp>)
2566       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2567                                   _Tp, _ValueType>)
2568       __glibcxx_requires_partitioned_lower_pred(__first, __last,
2569                                                 __val, __comp);
2570       __glibcxx_requires_partitioned_upper_pred(__first, __last,
2571                                                 __val, __comp);
2572
2573       _DistanceType __len = std::distance(__first, __last);
2574       _DistanceType __half;
2575       _ForwardIterator __middle, __left, __right;
2576
2577       while (__len > 0)
2578         {
2579           __half = __len >> 1;
2580           __middle = __first;
2581           std::advance(__middle, __half);
2582           if (__comp(*__middle, __val))
2583             {
2584               __first = __middle;
2585               ++__first;
2586               __len = __len - __half - 1;
2587             }
2588           else if (__comp(__val, *__middle))
2589             __len = __half;
2590           else
2591             {
2592               __left = std::lower_bound(__first, __middle, __val, __comp);
2593               std::advance(__first, __len);
2594               __right = std::upper_bound(++__middle, __first, __val, __comp);
2595               return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2596             }
2597         }
2598       return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2599     }
2600
2601   /**
2602    *  @brief Determines whether an element exists in a range.
2603    *  @ingroup binary_search_algorithms
2604    *  @param  first   An iterator.
2605    *  @param  last    Another iterator.
2606    *  @param  val     The search term.
2607    *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
2608    *
2609    *  Note that this does not actually return an iterator to @a val.  For
2610    *  that, use std::find or a container's specialized find member functions.
2611   */
2612   template<typename _ForwardIterator, typename _Tp>
2613     bool
2614     binary_search(_ForwardIterator __first, _ForwardIterator __last,
2615                   const _Tp& __val)
2616     {
2617       typedef typename iterator_traits<_ForwardIterator>::value_type
2618         _ValueType;
2619
2620       // concept requirements
2621       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2622       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2623       __glibcxx_requires_partitioned_lower(__first, __last, __val);
2624       __glibcxx_requires_partitioned_upper(__first, __last, __val);
2625
2626       _ForwardIterator __i = std::lower_bound(__first, __last, __val);
2627       return __i != __last && !(__val < *__i);
2628     }
2629
2630   /**
2631    *  @brief Determines whether an element exists in a range.
2632    *  @ingroup binary_search_algorithms
2633    *  @param  first   An iterator.
2634    *  @param  last    Another iterator.
2635    *  @param  val     The search term.
2636    *  @param  comp    A functor to use for comparisons.
2637    *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
2638    *
2639    *  Note that this does not actually return an iterator to @a val.  For
2640    *  that, use std::find or a container's specialized find member functions.
2641    *
2642    *  The comparison function should have the same effects on ordering as
2643    *  the function used for the initial sort.
2644   */
2645   template<typename _ForwardIterator, typename _Tp, typename _Compare>
2646     bool
2647     binary_search(_ForwardIterator __first, _ForwardIterator __last,
2648                   const _Tp& __val, _Compare __comp)
2649     {
2650       typedef typename iterator_traits<_ForwardIterator>::value_type
2651         _ValueType;
2652
2653       // concept requirements
2654       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2655       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2656                                   _Tp, _ValueType>)
2657       __glibcxx_requires_partitioned_lower_pred(__first, __last,
2658                                                 __val, __comp);
2659       __glibcxx_requires_partitioned_upper_pred(__first, __last,
2660                                                 __val, __comp);
2661
2662       _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
2663       return __i != __last && !bool(__comp(__val, *__i));
2664     }
2665
2666   // merge
2667
2668   /// This is a helper function for the merge routines.
2669   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2670            typename _BidirectionalIterator3>
2671     _BidirectionalIterator3
2672     __merge_backward(_BidirectionalIterator1 __first1,
2673                      _BidirectionalIterator1 __last1,
2674                      _BidirectionalIterator2 __first2,
2675                      _BidirectionalIterator2 __last2,
2676                      _BidirectionalIterator3 __result)
2677     {
2678       if (__first1 == __last1)
2679         return std::copy_backward(__first2, __last2, __result);
2680       if (__first2 == __last2)
2681         return std::copy_backward(__first1, __last1, __result);
2682       --__last1;
2683       --__last2;
2684       while (true)
2685         {
2686           if (*__last2 < *__last1)
2687             {
2688               *--__result = *__last1;
2689               if (__first1 == __last1)
2690                 return std::copy_backward(__first2, ++__last2, __result);
2691               --__last1;
2692             }
2693           else
2694             {
2695               *--__result = *__last2;
2696               if (__first2 == __last2)
2697                 return std::copy_backward(__first1, ++__last1, __result);
2698               --__last2;
2699             }
2700         }
2701     }
2702
2703   /// This is a helper function for the merge routines.
2704   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2705            typename _BidirectionalIterator3, typename _Compare>
2706     _BidirectionalIterator3
2707     __merge_backward(_BidirectionalIterator1 __first1,
2708                      _BidirectionalIterator1 __last1,
2709                      _BidirectionalIterator2 __first2,
2710                      _BidirectionalIterator2 __last2,
2711                      _BidirectionalIterator3 __result,
2712                      _Compare __comp)
2713     {
2714       if (__first1 == __last1)
2715         return std::copy_backward(__first2, __last2, __result);
2716       if (__first2 == __last2)
2717         return std::copy_backward(__first1, __last1, __result);
2718       --__last1;
2719       --__last2;
2720       while (true)
2721         {
2722           if (__comp(*__last2, *__last1))
2723             {
2724               *--__result = *__last1;
2725               if (__first1 == __last1)
2726                 return std::copy_backward(__first2, ++__last2, __result);
2727               --__last1;
2728             }
2729           else
2730             {
2731               *--__result = *__last2;
2732               if (__first2 == __last2)
2733                 return std::copy_backward(__first1, ++__last1, __result);
2734               --__last2;
2735             }
2736         }
2737     }
2738
2739   /// This is a helper function for the merge routines.
2740   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2741            typename _Distance>
2742     _BidirectionalIterator1
2743     __rotate_adaptive(_BidirectionalIterator1 __first,
2744                       _BidirectionalIterator1 __middle,
2745                       _BidirectionalIterator1 __last,
2746                       _Distance __len1, _Distance __len2,
2747                       _BidirectionalIterator2 __buffer,
2748                       _Distance __buffer_size)
2749     {
2750       _BidirectionalIterator2 __buffer_end;
2751       if (__len1 > __len2 && __len2 <= __buffer_size)
2752         {
2753           __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2754           _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
2755           return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
2756         }
2757       else if (__len1 <= __buffer_size)
2758         {
2759           __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2760           _GLIBCXX_MOVE3(__middle, __last, __first);
2761           return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
2762         }
2763       else
2764         {
2765           std::rotate(__first, __middle, __last);
2766           std::advance(__first, std::distance(__middle, __last));
2767           return __first;
2768         }
2769     }
2770
2771   /// This is a helper function for the merge routines.
2772   template<typename _BidirectionalIterator, typename _Distance,
2773            typename _Pointer>
2774     void
2775     __merge_adaptive(_BidirectionalIterator __first,
2776                      _BidirectionalIterator __middle,
2777                      _BidirectionalIterator __last,
2778                      _Distance __len1, _Distance __len2,
2779                      _Pointer __buffer, _Distance __buffer_size)
2780     {
2781       if (__len1 <= __len2 && __len1 <= __buffer_size)
2782         {
2783           _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2784           _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2785                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2786                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2787                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
2788                                 __first);
2789         }
2790       else if (__len2 <= __buffer_size)
2791         {
2792           _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2793           std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
2794                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2795                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2796                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2797                                 __last);
2798         }
2799       else
2800         {
2801           _BidirectionalIterator __first_cut = __first;
2802           _BidirectionalIterator __second_cut = __middle;
2803           _Distance __len11 = 0;
2804           _Distance __len22 = 0;
2805           if (__len1 > __len2)
2806             {
2807               __len11 = __len1 / 2;
2808               std::advance(__first_cut, __len11);
2809               __second_cut = std::lower_bound(__middle, __last,
2810                                               *__first_cut);
2811               __len22 = std::distance(__middle, __second_cut);
2812             }
2813           else
2814             {
2815               __len22 = __len2 / 2;
2816               std::advance(__second_cut, __len22);
2817               __first_cut = std::upper_bound(__first, __middle,
2818                                              *__second_cut);
2819               __len11 = std::distance(__first, __first_cut);
2820             }
2821           _BidirectionalIterator __new_middle =
2822             std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2823                                    __len1 - __len11, __len22, __buffer,
2824                                    __buffer_size);
2825           std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2826                                 __len22, __buffer, __buffer_size);
2827           std::__merge_adaptive(__new_middle, __second_cut, __last,
2828                                 __len1 - __len11,
2829                                 __len2 - __len22, __buffer, __buffer_size);
2830         }
2831     }
2832
2833   /// This is a helper function for the merge routines.
2834   template<typename _BidirectionalIterator, typename _Distance, 
2835            typename _Pointer, typename _Compare>
2836     void
2837     __merge_adaptive(_BidirectionalIterator __first,
2838                      _BidirectionalIterator __middle,
2839                      _BidirectionalIterator __last,
2840                      _Distance __len1, _Distance __len2,
2841                      _Pointer __buffer, _Distance __buffer_size,
2842                      _Compare __comp)
2843     {
2844       if (__len1 <= __len2 && __len1 <= __buffer_size)
2845         {
2846           _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2847           _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2848                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2849                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2850                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
2851                                 __first, __comp);
2852         }
2853       else if (__len2 <= __buffer_size)
2854         {
2855           _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2856           std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
2857                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2858                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2859                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2860                                 __last,__comp);
2861         }
2862       else
2863         {
2864           _BidirectionalIterator __first_cut = __first;
2865           _BidirectionalIterator __second_cut = __middle;
2866           _Distance __len11 = 0;
2867           _Distance __len22 = 0;
2868           if (__len1 > __len2)
2869             {
2870               __len11 = __len1 / 2;
2871               std::advance(__first_cut, __len11);
2872               __second_cut = std::lower_bound(__middle, __last, *__first_cut,
2873                                               __comp);
2874               __len22 = std::distance(__middle, __second_cut);
2875             }
2876           else
2877             {
2878               __len22 = __len2 / 2;
2879               std::advance(__second_cut, __len22);
2880               __first_cut = std::upper_bound(__first, __middle, *__second_cut,
2881                                              __comp);
2882               __len11 = std::distance(__first, __first_cut);
2883             }
2884           _BidirectionalIterator __new_middle =
2885             std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2886                                    __len1 - __len11, __len22, __buffer,
2887                                    __buffer_size);
2888           std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2889                                 __len22, __buffer, __buffer_size, __comp);
2890           std::__merge_adaptive(__new_middle, __second_cut, __last,
2891                                 __len1 - __len11,
2892                                 __len2 - __len22, __buffer,
2893                                 __buffer_size, __comp);
2894         }
2895     }
2896
2897   /// This is a helper function for the merge routines.
2898   template<typename _BidirectionalIterator, typename _Distance>
2899     void
2900     __merge_without_buffer(_BidirectionalIterator __first,
2901                            _BidirectionalIterator __middle,
2902                            _BidirectionalIterator __last,
2903                            _Distance __len1, _Distance __len2)
2904     {
2905       if (__len1 == 0 || __len2 == 0)
2906         return;
2907       if (__len1 + __len2 == 2)
2908         {
2909           if (*__middle < *__first)
2910             std::iter_swap(__first, __middle);
2911           return;
2912         }
2913       _BidirectionalIterator __first_cut = __first;
2914       _BidirectionalIterator __second_cut = __middle;
2915       _Distance __len11 = 0;
2916       _Distance __len22 = 0;
2917       if (__len1 > __len2)
2918         {
2919           __len11 = __len1 / 2;
2920           std::advance(__first_cut, __len11);
2921           __second_cut = std::lower_bound(__middle, __last, *__first_cut);
2922           __len22 = std::distance(__middle, __second_cut);
2923         }
2924       else
2925         {
2926           __len22 = __len2 / 2;
2927           std::advance(__second_cut, __len22);
2928           __first_cut = std::upper_bound(__first, __middle, *__second_cut);
2929           __len11 = std::distance(__first, __first_cut);
2930         }
2931       std::rotate(__first_cut, __middle, __second_cut);
2932       _BidirectionalIterator __new_middle = __first_cut;
2933       std::advance(__new_middle, std::distance(__middle, __second_cut));
2934       std::__merge_without_buffer(__first, __first_cut, __new_middle,
2935                                   __len11, __len22);
2936       std::__merge_without_buffer(__new_middle, __second_cut, __last,
2937                                   __len1 - __len11, __len2 - __len22);
2938     }
2939
2940   /// This is a helper function for the merge routines.
2941   template<typename _BidirectionalIterator, typename _Distance,
2942            typename _Compare>
2943     void
2944     __merge_without_buffer(_BidirectionalIterator __first,
2945                            _BidirectionalIterator __middle,
2946                            _BidirectionalIterator __last,
2947                            _Distance __len1, _Distance __len2,
2948                            _Compare __comp)
2949     {
2950       if (__len1 == 0 || __len2 == 0)
2951         return;
2952       if (__len1 + __len2 == 2)
2953         {
2954           if (__comp(*__middle, *__first))
2955             std::iter_swap(__first, __middle);
2956           return;
2957         }
2958       _BidirectionalIterator __first_cut = __first;
2959       _BidirectionalIterator __second_cut = __middle;
2960       _Distance __len11 = 0;
2961       _Distance __len22 = 0;
2962       if (__len1 > __len2)
2963         {
2964           __len11 = __len1 / 2;
2965           std::advance(__first_cut, __len11);
2966           __second_cut = std::lower_bound(__middle, __last, *__first_cut,
2967                                           __comp);
2968           __len22 = std::distance(__middle, __second_cut);
2969         }
2970       else
2971         {
2972           __len22 = __len2 / 2;
2973           std::advance(__second_cut, __len22);
2974           __first_cut = std::upper_bound(__first, __middle, *__second_cut,
2975                                          __comp);
2976           __len11 = std::distance(__first, __first_cut);
2977         }
2978       std::rotate(__first_cut, __middle, __second_cut);
2979       _BidirectionalIterator __new_middle = __first_cut;
2980       std::advance(__new_middle, std::distance(__middle, __second_cut));
2981       std::__merge_without_buffer(__first, __first_cut, __new_middle,
2982                                   __len11, __len22, __comp);
2983       std::__merge_without_buffer(__new_middle, __second_cut, __last,
2984                                   __len1 - __len11, __len2 - __len22, __comp);
2985     }
2986
2987   /**
2988    *  @brief Merges two sorted ranges in place.
2989    *  @ingroup sorting_algorithms
2990    *  @param  first   An iterator.
2991    *  @param  middle  Another iterator.
2992    *  @param  last    Another iterator.
2993    *  @return  Nothing.
2994    *
2995    *  Merges two sorted and consecutive ranges, [first,middle) and
2996    *  [middle,last), and puts the result in [first,last).  The output will
2997    *  be sorted.  The sort is @e stable, that is, for equivalent
2998    *  elements in the two ranges, elements from the first range will always
2999    *  come before elements from the second.
3000    *
3001    *  If enough additional memory is available, this takes (last-first)-1
3002    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
3003    *  distance(first,last).
3004   */
3005   template<typename _BidirectionalIterator>
3006     void
3007     inplace_merge(_BidirectionalIterator __first,
3008                   _BidirectionalIterator __middle,
3009                   _BidirectionalIterator __last)
3010     {
3011       typedef typename iterator_traits<_BidirectionalIterator>::value_type
3012           _ValueType;
3013       typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3014           _DistanceType;
3015
3016       // concept requirements
3017       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3018             _BidirectionalIterator>)
3019       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3020       __glibcxx_requires_sorted(__first, __middle);
3021       __glibcxx_requires_sorted(__middle, __last);
3022
3023       if (__first == __middle || __middle == __last)
3024         return;
3025
3026       _DistanceType __len1 = std::distance(__first, __middle);
3027       _DistanceType __len2 = std::distance(__middle, __last);
3028
3029       _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3030                                                                   __last);
3031       if (__buf.begin() == 0)
3032         std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
3033       else
3034         std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3035                               __buf.begin(), _DistanceType(__buf.size()));
3036     }
3037
3038   /**
3039    *  @brief Merges two sorted ranges in place.
3040    *  @ingroup sorting_algorithms
3041    *  @param  first   An iterator.
3042    *  @param  middle  Another iterator.
3043    *  @param  last    Another iterator.
3044    *  @param  comp    A functor to use for comparisons.
3045    *  @return  Nothing.
3046    *
3047    *  Merges two sorted and consecutive ranges, [first,middle) and
3048    *  [middle,last), and puts the result in [first,last).  The output will
3049    *  be sorted.  The sort is @e stable, that is, for equivalent
3050    *  elements in the two ranges, elements from the first range will always
3051    *  come before elements from the second.
3052    *
3053    *  If enough additional memory is available, this takes (last-first)-1
3054    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
3055    *  distance(first,last).
3056    *
3057    *  The comparison function should have the same effects on ordering as
3058    *  the function used for the initial sort.
3059   */
3060   template<typename _BidirectionalIterator, typename _Compare>
3061     void
3062     inplace_merge(_BidirectionalIterator __first,
3063                   _BidirectionalIterator __middle,
3064                   _BidirectionalIterator __last,
3065                   _Compare __comp)
3066     {
3067       typedef typename iterator_traits<_BidirectionalIterator>::value_type
3068           _ValueType;
3069       typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3070           _DistanceType;
3071
3072       // concept requirements
3073       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3074             _BidirectionalIterator>)
3075       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3076             _ValueType, _ValueType>)
3077       __glibcxx_requires_sorted_pred(__first, __middle, __comp);
3078       __glibcxx_requires_sorted_pred(__middle, __last, __comp);
3079
3080       if (__first == __middle || __middle == __last)
3081         return;
3082
3083       const _DistanceType __len1 = std::distance(__first, __middle);
3084       const _DistanceType __len2 = std::distance(__middle, __last);
3085
3086       _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3087                                                                   __last);
3088       if (__buf.begin() == 0)
3089         std::__merge_without_buffer(__first, __middle, __last, __len1,
3090                                     __len2, __comp);
3091       else
3092         std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3093                               __buf.begin(), _DistanceType(__buf.size()),
3094                               __comp);
3095     }
3096
3097   template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3098            typename _Distance>
3099     void
3100     __merge_sort_loop(_RandomAccessIterator1 __first,
3101                       _RandomAccessIterator1 __last,
3102                       _RandomAccessIterator2 __result,
3103                       _Distance __step_size)
3104     {
3105       const _Distance __two_step = 2 * __step_size;
3106
3107       while (__last - __first >= __two_step)
3108         {
3109           __result = _GLIBCXX_STD_P::merge(
3110                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3111                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3112                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3113                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
3114                         __result);
3115           __first += __two_step;
3116         }
3117
3118       __step_size = std::min(_Distance(__last - __first), __step_size);
3119       _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3120                             _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3121                                                         __step_size),
3122                             _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3123                                                         __step_size),
3124                             _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
3125                             __result);
3126     }
3127
3128   template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3129            typename _Distance, typename _Compare>
3130     void
3131     __merge_sort_loop(_RandomAccessIterator1 __first,
3132                       _RandomAccessIterator1 __last,
3133                       _RandomAccessIterator2 __result, _Distance __step_size,
3134                       _Compare __comp)
3135     {
3136       const _Distance __two_step = 2 * __step_size;
3137
3138       while (__last - __first >= __two_step)
3139         {
3140           __result = _GLIBCXX_STD_P::merge(
3141                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3142                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3143                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3144                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
3145                         __result, __comp);
3146           __first += __two_step;
3147         }
3148       __step_size = std::min(_Distance(__last - __first), __step_size);
3149
3150       _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3151                             _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3152                                                         __step_size),
3153                             _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3154                                                         __step_size),
3155                             _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
3156                             __result, __comp);
3157     }
3158
3159   template<typename _RandomAccessIterator, typename _Distance>
3160     void
3161     __chunk_insertion_sort(_RandomAccessIterator __first,
3162                            _RandomAccessIterator __last,
3163                            _Distance __chunk_size)
3164     {
3165       while (__last - __first >= __chunk_size)
3166         {
3167           std::__insertion_sort(__first, __first + __chunk_size);
3168           __first += __chunk_size;
3169         }
3170       std::__insertion_sort(__first, __last);
3171     }
3172
3173   template<typename _RandomAccessIterator, typename _Distance,
3174            typename _Compare>
3175     void
3176     __chunk_insertion_sort(_RandomAccessIterator __first,
3177                            _RandomAccessIterator __last,
3178                            _Distance __chunk_size, _Compare __comp)
3179     {
3180       while (__last - __first >= __chunk_size)
3181         {
3182           std::__insertion_sort(__first, __first + __chunk_size, __comp);
3183           __first += __chunk_size;
3184         }
3185       std::__insertion_sort(__first, __last, __comp);
3186     }
3187
3188   enum { _S_chunk_size = 7 };
3189
3190   template<typename _RandomAccessIterator, typename _Pointer>
3191     void
3192     __merge_sort_with_buffer(_RandomAccessIterator __first,
3193                              _RandomAccessIterator __last,
3194                              _Pointer __buffer)
3195     {
3196       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3197         _Distance;
3198
3199       const _Distance __len = __last - __first;
3200       const _Pointer __buffer_last = __buffer + __len;
3201
3202       _Distance __step_size = _S_chunk_size;
3203       std::__chunk_insertion_sort(__first, __last, __step_size);
3204
3205       while (__step_size < __len)
3206         {
3207           std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3208           __step_size *= 2;
3209           std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3210           __step_size *= 2;
3211         }
3212     }
3213
3214   template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3215     void
3216     __merge_sort_with_buffer(_RandomAccessIterator __first,
3217                              _RandomAccessIterator __last,
3218                              _Pointer __buffer, _Compare __comp)
3219     {
3220       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3221         _Distance;
3222
3223       const _Distance __len = __last - __first;
3224       const _Pointer __buffer_last = __buffer + __len;
3225
3226       _Distance __step_size = _S_chunk_size;
3227       std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3228
3229       while (__step_size < __len)
3230         {
3231           std::__merge_sort_loop(__first, __last, __buffer,
3232                                  __step_size, __comp);
3233           __step_size *= 2;
3234           std::__merge_sort_loop(__buffer, __buffer_last, __first,
3235                                  __step_size, __comp);
3236           __step_size *= 2;
3237         }
3238     }
3239
3240   template<typename _RandomAccessIterator, typename _Pointer,
3241            typename _Distance>
3242     void
3243     __stable_sort_adaptive(_RandomAccessIterator __first,
3244                            _RandomAccessIterator __last,
3245                            _Pointer __buffer, _Distance __buffer_size)
3246     {
3247       const _Distance __len = (__last - __first + 1) / 2;
3248       const _RandomAccessIterator __middle = __first + __len;
3249       if (__len > __buffer_size)
3250         {
3251           std::__stable_sort_adaptive(__first, __middle,
3252                                       __buffer, __buffer_size);
3253           std::__stable_sort_adaptive(__middle, __last,
3254                                       __buffer, __buffer_size);
3255         }
3256       else
3257         {
3258           std::__merge_sort_with_buffer(__first, __middle, __buffer);
3259           std::__merge_sort_with_buffer(__middle, __last, __buffer);
3260         }
3261       std::__merge_adaptive(__first, __middle, __last,
3262                             _Distance(__middle - __first),
3263                             _Distance(__last - __middle),
3264                             __buffer, __buffer_size);
3265     }
3266
3267   template<typename _RandomAccessIterator, typename _Pointer,
3268            typename _Distance, typename _Compare>
3269     void
3270     __stable_sort_adaptive(_RandomAccessIterator __first,
3271                            _RandomAccessIterator __last,
3272                            _Pointer __buffer, _Distance __buffer_size,
3273                            _Compare __comp)
3274     {
3275       const _Distance __len = (__last - __first + 1) / 2;
3276       const _RandomAccessIterator __middle = __first + __len;
3277       if (__len > __buffer_size)
3278         {
3279           std::__stable_sort_adaptive(__first, __middle, __buffer,
3280                                       __buffer_size, __comp);
3281           std::__stable_sort_adaptive(__middle, __last, __buffer,
3282                                       __buffer_size, __comp);
3283         }
3284       else
3285         {
3286           std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3287           std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3288         }
3289       std::__merge_adaptive(__first, __middle, __last,
3290                             _Distance(__middle - __first),
3291                             _Distance(__last - __middle),
3292                             __buffer, __buffer_size,
3293                             __comp);
3294     }
3295
3296   /// This is a helper function for the stable sorting routines.
3297   template<typename _RandomAccessIterator>
3298     void
3299     __inplace_stable_sort(_RandomAccessIterator __first,
3300                           _RandomAccessIterator __last)
3301     {
3302       if (__last - __first < 15)
3303         {
3304           std::__insertion_sort(__first, __last);
3305           return;
3306         }
3307       _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3308       std::__inplace_stable_sort(__first, __middle);
3309       std::__inplace_stable_sort(__middle, __last);
3310       std::__merge_without_buffer(__first, __middle, __last,
3311                                   __middle - __first,
3312                                   __last - __middle);
3313     }
3314
3315   /// This is a helper function for the stable sorting routines.
3316   template<typename _RandomAccessIterator, typename _Compare>
3317     void
3318     __inplace_stable_sort(_RandomAccessIterator __first,
3319                           _RandomAccessIterator __last, _Compare __comp)
3320     {
3321       if (__last - __first < 15)
3322         {
3323           std::__insertion_sort(__first, __last, __comp);
3324           return;
3325         }
3326       _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3327       std::__inplace_stable_sort(__first, __middle, __comp);
3328       std::__inplace_stable_sort(__middle, __last, __comp);
3329       std::__merge_without_buffer(__first, __middle, __last,
3330                                   __middle - __first,
3331                                   __last - __middle,
3332                                   __comp);
3333     }
3334
3335   // stable_sort
3336
3337   // Set algorithms: includes, set_union, set_intersection, set_difference,
3338   // set_symmetric_difference.  All of these algorithms have the precondition
3339   // that their input ranges are sorted and the postcondition that their output
3340   // ranges are sorted.
3341
3342   /**
3343    *  @brief Determines whether all elements of a sequence exists in a range.
3344    *  @param  first1  Start of search range.
3345    *  @param  last1   End of search range.
3346    *  @param  first2  Start of sequence
3347    *  @param  last2   End of sequence.
3348    *  @return  True if each element in [first2,last2) is contained in order
3349    *  within [first1,last1).  False otherwise.
3350    *  @ingroup set_algorithms
3351    *
3352    *  This operation expects both [first1,last1) and [first2,last2) to be
3353    *  sorted.  Searches for the presence of each element in [first2,last2)
3354    *  within [first1,last1).  The iterators over each range only move forward,
3355    *  so this is a linear algorithm.  If an element in [first2,last2) is not
3356    *  found before the search iterator reaches @a last2, false is returned.
3357   */
3358   template<typename _InputIterator1, typename _InputIterator2>
3359     bool
3360     includes(_InputIterator1 __first1, _InputIterator1 __last1,
3361              _InputIterator2 __first2, _InputIterator2 __last2)
3362     {
3363       typedef typename iterator_traits<_InputIterator1>::value_type
3364         _ValueType1;
3365       typedef typename iterator_traits<_InputIterator2>::value_type
3366         _ValueType2;
3367
3368       // concept requirements
3369       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3370       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3371       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
3372       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
3373       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
3374       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
3375
3376       while (__first1 != __last1 && __first2 != __last2)
3377         if (*__first2 < *__first1)
3378           return false;
3379         else if(*__first1 < *__first2)
3380           ++__first1;
3381         else
3382           ++__first1, ++__first2;
3383
3384       return __first2 == __last2;
3385     }
3386
3387   /**
3388    *  @brief Determines whether all elements of a sequence exists in a range
3389    *  using comparison.
3390    *  @ingroup set_algorithms
3391    *  @param  first1  Start of search range.
3392    *  @param  last1   End of search range.
3393    *  @param  first2  Start of sequence
3394    *  @param  last2   End of sequence.
3395    *  @param  comp    Comparison function to use.
3396    *  @return  True if each element in [first2,last2) is contained in order
3397    *  within [first1,last1) according to comp.  False otherwise.
3398    *  @ingroup set_algorithms
3399    *
3400    *  This operation expects both [first1,last1) and [first2,last2) to be
3401    *  sorted.  Searches for the presence of each element in [first2,last2)
3402    *  within [first1,last1), using comp to decide.  The iterators over each
3403    *  range only move forward, so this is a linear algorithm.  If an element
3404    *  in [first2,last2) is not found before the search iterator reaches @a
3405    *  last2, false is returned.
3406   */
3407   template<typename _InputIterator1, typename _InputIterator2,
3408            typename _Compare>
3409     bool
3410     includes(_InputIterator1 __first1, _InputIterator1 __last1,
3411              _InputIterator2 __first2, _InputIterator2 __last2,
3412              _Compare __comp)
3413     {
3414       typedef typename iterator_traits<_InputIterator1>::value_type
3415         _ValueType1;
3416       typedef typename iterator_traits<_InputIterator2>::value_type
3417         _ValueType2;
3418
3419       // concept requirements
3420       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3421       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3422       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3423                                   _ValueType1, _ValueType2>)
3424       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3425                                   _ValueType2, _ValueType1>)
3426       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
3427       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
3428
3429       while (__first1 != __last1 && __first2 != __last2)
3430         if (__comp(*__first2, *__first1))
3431           return false;
3432         else if(__comp(*__first1, *__first2))
3433           ++__first1;
3434         else
3435           ++__first1, ++__first2;
3436
3437       return __first2 == __last2;
3438     }
3439
3440   // nth_element
3441   // merge
3442   // set_difference
3443   // set_intersection
3444   // set_union
3445   // stable_sort
3446   // set_symmetric_difference
3447   // min_element
3448   // max_element
3449
3450   /**
3451    *  @brief  Permute range into the next @a dictionary ordering.
3452    *  @ingroup sorting_algorithms
3453    *  @param  first  Start of range.
3454    *  @param  last   End of range.
3455    *  @return  False if wrapped to first permutation, true otherwise.
3456    *
3457    *  Treats all permutations of the range as a set of @a dictionary sorted
3458    *  sequences.  Permutes the current sequence into the next one of this set.
3459    *  Returns true if there are more sequences to generate.  If the sequence
3460    *  is the largest of the set, the smallest is generated and false returned.
3461   */
3462   template<typename _BidirectionalIterator>
3463     bool
3464     next_permutation(_BidirectionalIterator __first,
3465                      _BidirectionalIterator __last)
3466     {
3467       // concept requirements
3468       __glibcxx_function_requires(_BidirectionalIteratorConcept<
3469                                   _BidirectionalIterator>)
3470       __glibcxx_function_requires(_LessThanComparableConcept<
3471             typename iterator_traits<_BidirectionalIterator>::value_type>)
3472       __glibcxx_requires_valid_range(__first, __last);
3473
3474       if (__first == __last)
3475         return false;
3476       _BidirectionalIterator __i = __first;
3477       ++__i;
3478       if (__i == __last)
3479         return false;
3480       __i = __last;
3481       --__i;
3482
3483       for(;;)
3484         {
3485           _BidirectionalIterator __ii = __i;
3486           --__i;
3487           if (*__i < *__ii)
3488             {
3489               _BidirectionalIterator __j = __last;
3490               while (!(*__i < *--__j))
3491                 {}
3492               std::iter_swap(__i, __j);
3493               std::reverse(__ii, __last);
3494               return true;
3495             }
3496           if (__i == __first)
3497             {
3498               std::reverse(__first, __last);
3499               return false;
3500             }
3501         }
3502     }
3503
3504   /**
3505    *  @brief  Permute range into the next @a dictionary ordering using
3506    *          comparison functor.
3507    *  @ingroup sorting_algorithms
3508    *  @param  first  Start of range.
3509    *  @param  last   End of range.
3510    *  @param  comp   A comparison functor.
3511    *  @return  False if wrapped to first permutation, true otherwise.
3512    *
3513    *  Treats all permutations of the range [first,last) as a set of
3514    *  @a dictionary sorted sequences ordered by @a comp.  Permutes the current
3515    *  sequence into the next one of this set.  Returns true if there are more
3516    *  sequences to generate.  If the sequence is the largest of the set, the
3517    *  smallest is generated and false returned.
3518   */
3519   template<typename _BidirectionalIterator, typename _Compare>
3520     bool
3521     next_permutation(_BidirectionalIterator __first,
3522                      _BidirectionalIterator __last, _Compare __comp)
3523     {
3524       // concept requirements
3525       __glibcxx_function_requires(_BidirectionalIteratorConcept<
3526                                   _BidirectionalIterator>)
3527       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3528             typename iterator_traits<_BidirectionalIterator>::value_type,
3529             typename iterator_traits<_BidirectionalIterator>::value_type>)
3530       __glibcxx_requires_valid_range(__first, __last);
3531
3532       if (__first == __last)
3533         return false;
3534       _BidirectionalIterator __i = __first;
3535       ++__i;
3536       if (__i == __last)
3537         return false;
3538       __i = __last;
3539       --__i;
3540
3541       for(;;)
3542         {
3543           _BidirectionalIterator __ii = __i;
3544           --__i;
3545           if (__comp(*__i, *__ii))
3546             {
3547               _BidirectionalIterator __j = __last;
3548               while (!bool(__comp(*__i, *--__j)))
3549                 {}
3550               std::iter_swap(__i, __j);
3551               std::reverse(__ii, __last);
3552               return true;
3553             }
3554           if (__i == __first)
3555             {
3556               std::reverse(__first, __last);
3557               return false;
3558             }
3559         }
3560     }
3561
3562   /**
3563    *  @brief  Permute range into the previous @a dictionary ordering.
3564    *  @ingroup sorting_algorithms
3565    *  @param  first  Start of range.
3566    *  @param  last   End of range.
3567    *  @return  False if wrapped to last permutation, true otherwise.
3568    *
3569    *  Treats all permutations of the range as a set of @a dictionary sorted
3570    *  sequences.  Permutes the current sequence into the previous one of this
3571    *  set.  Returns true if there are more sequences to generate.  If the
3572    *  sequence is the smallest of the set, the largest is generated and false
3573    *  returned.
3574   */
3575   template<typename _BidirectionalIterator>
3576     bool
3577     prev_permutation(_BidirectionalIterator __first,
3578                      _BidirectionalIterator __last)
3579     {
3580       // concept requirements
3581       __glibcxx_function_requires(_BidirectionalIteratorConcept<
3582                                   _BidirectionalIterator>)
3583       __glibcxx_function_requires(_LessThanComparableConcept<
3584             typename iterator_traits<_BidirectionalIterator>::value_type>)
3585       __glibcxx_requires_valid_range(__first, __last);
3586
3587       if (__first == __last)
3588         return false;
3589       _BidirectionalIterator __i = __first;
3590       ++__i;
3591       if (__i == __last)
3592         return false;
3593       __i = __last;
3594       --__i;
3595
3596       for(;;)
3597         {
3598           _BidirectionalIterator __ii = __i;
3599           --__i;
3600           if (*__ii < *__i)
3601             {
3602               _BidirectionalIterator __j = __last;
3603               while (!(*--__j < *__i))
3604                 {}
3605               std::iter_swap(__i, __j);
3606               std::reverse(__ii, __last);
3607               return true;
3608             }
3609           if (__i == __first)
3610             {
3611               std::reverse(__first, __last);
3612               return false;
3613             }
3614         }
3615     }
3616
3617   /**
3618    *  @brief  Permute range into the previous @a dictionary ordering using
3619    *          comparison functor.
3620    *  @ingroup sorting_algorithms
3621    *  @param  first  Start of range.
3622    *  @param  last   End of range.
3623    *  @param  comp   A comparison functor.
3624    *  @return  False if wrapped to last permutation, true otherwise.
3625    *
3626    *  Treats all permutations of the range [first,last) as a set of
3627    *  @a dictionary sorted sequences ordered by @a comp.  Permutes the current
3628    *  sequence into the previous one of this set.  Returns true if there are
3629    *  more sequences to generate.  If the sequence is the smallest of the set,
3630    *  the largest is generated and false returned.
3631   */
3632   template<typename _BidirectionalIterator, typename _Compare>
3633     bool
3634     prev_permutation(_BidirectionalIterator __first,
3635                      _BidirectionalIterator __last, _Compare __comp)
3636     {
3637       // concept requirements
3638       __glibcxx_function_requires(_BidirectionalIteratorConcept<
3639                                   _BidirectionalIterator>)
3640       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3641             typename iterator_traits<_BidirectionalIterator>::value_type,
3642             typename iterator_traits<_BidirectionalIterator>::value_type>)
3643       __glibcxx_requires_valid_range(__first, __last);
3644
3645       if (__first == __last)
3646         return false;
3647       _BidirectionalIterator __i = __first;
3648       ++__i;
3649       if (__i == __last)
3650         return false;
3651       __i = __last;
3652       --__i;
3653
3654       for(;;)
3655         {
3656           _BidirectionalIterator __ii = __i;
3657           --__i;
3658           if (__comp(*__ii, *__i))
3659             {
3660               _BidirectionalIterator __j = __last;
3661               while (!bool(__comp(*--__j, *__i)))
3662                 {}
3663               std::iter_swap(__i, __j);
3664               std::reverse(__ii, __last);
3665               return true;
3666             }
3667           if (__i == __first)
3668             {
3669               std::reverse(__first, __last);
3670               return false;
3671             }
3672         }
3673     }
3674
3675   // replace
3676   // replace_if
3677
3678   /**
3679    *  @brief Copy a sequence, replacing each element of one value with another
3680    *         value.
3681    *  @param  first      An input iterator.
3682    *  @param  last       An input iterator.
3683    *  @param  result     An output iterator.
3684    *  @param  old_value  The value to be replaced.
3685    *  @param  new_value  The replacement value.
3686    *  @return   The end of the output sequence, @p result+(last-first).
3687    *
3688    *  Copies each element in the input range @p [first,last) to the
3689    *  output range @p [result,result+(last-first)) replacing elements
3690    *  equal to @p old_value with @p new_value.
3691   */
3692   template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3693     _OutputIterator
3694     replace_copy(_InputIterator __first, _InputIterator __last,
3695                  _OutputIterator __result,
3696                  const _Tp& __old_value, const _Tp& __new_value)
3697     {
3698       // concept requirements
3699       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3700       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3701             typename iterator_traits<_InputIterator>::value_type>)
3702       __glibcxx_function_requires(_EqualOpConcept<
3703             typename iterator_traits<_InputIterator>::value_type, _Tp>)
3704       __glibcxx_requires_valid_range(__first, __last);
3705
3706       for (; __first != __last; ++__first, ++__result)
3707         if (*__first == __old_value)
3708           *__result = __new_value;
3709         else
3710           *__result = *__first;
3711       return __result;
3712     }
3713
3714   /**
3715    *  @brief Copy a sequence, replacing each value for which a predicate
3716    *         returns true with another value.
3717    *  @ingroup mutating_algorithms
3718    *  @param  first      An input iterator.
3719    *  @param  last       An input iterator.
3720    *  @param  result     An output iterator.
3721    *  @param  pred       A predicate.
3722    *  @param  new_value  The replacement value.
3723    *  @return   The end of the output sequence, @p result+(last-first).
3724    *
3725    *  Copies each element in the range @p [first,last) to the range
3726    *  @p [result,result+(last-first)) replacing elements for which
3727    *  @p pred returns true with @p new_value.
3728   */
3729   template<typename _InputIterator, typename _OutputIterator,
3730            typename _Predicate, typename _Tp>
3731     _OutputIterator
3732     replace_copy_if(_InputIterator __first, _InputIterator __last,
3733                     _OutputIterator __result,
3734                     _Predicate __pred, const _Tp& __new_value)
3735     {
3736       // concept requirements
3737       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3738       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3739             typename iterator_traits<_InputIterator>::value_type>)
3740       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3741             typename iterator_traits<_InputIterator>::value_type>)
3742       __glibcxx_requires_valid_range(__first, __last);
3743
3744       for (; __first != __last; ++__first, ++__result)
3745         if (__pred(*__first))
3746           *__result = __new_value;
3747         else
3748           *__result = *__first;
3749       return __result;
3750     }
3751
3752 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3753   /**
3754    *  @brief  Determines whether the elements of a sequence are sorted.
3755    *  @ingroup sorting_algorithms
3756    *  @param  first   An iterator.
3757    *  @param  last    Another iterator.
3758    *  @return  True if the elements are sorted, false otherwise.
3759   */
3760   template<typename _ForwardIterator>
3761     inline bool
3762     is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3763     { return std::is_sorted_until(__first, __last) == __last; }
3764
3765   /**
3766    *  @brief  Determines whether the elements of a sequence are sorted
3767    *          according to a comparison functor.
3768    *  @ingroup sorting_algorithms
3769    *  @param  first   An iterator.
3770    *  @param  last    Another iterator.
3771    *  @param  comp    A comparison functor.
3772    *  @return  True if the elements are sorted, false otherwise.
3773   */
3774   template<typename _ForwardIterator, typename _Compare>
3775     inline bool
3776     is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3777               _Compare __comp)
3778     { return std::is_sorted_until(__first, __last, __comp) == __last; }
3779
3780   /**
3781    *  @brief  Determines the end of a sorted sequence.
3782    *  @ingroup sorting_algorithms
3783    *  @param  first   An iterator.
3784    *  @param  last    Another iterator.
3785    *  @return  An iterator pointing to the last iterator i in [first, last)
3786    *           for which the range [first, i) is sorted.
3787   */
3788   template<typename _ForwardIterator>
3789     _ForwardIterator
3790     is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3791     {
3792       // concept requirements
3793       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3794       __glibcxx_function_requires(_LessThanComparableConcept<
3795             typename iterator_traits<_ForwardIterator>::value_type>)
3796       __glibcxx_requires_valid_range(__first, __last);
3797
3798       if (__first == __last)
3799         return __last;
3800
3801       _ForwardIterator __next = __first;
3802       for (++__next; __next != __last; __first = __next, ++__next)
3803         if (*__next < *__first)
3804           return __next;
3805       return __next;
3806     }
3807
3808   /**
3809    *  @brief  Determines the end of a sorted sequence using comparison functor.
3810    *  @ingroup sorting_algorithms
3811    *  @param  first   An iterator.
3812    *  @param  last    Another iterator.
3813    *  @param  comp    A comparison functor.
3814    *  @return  An iterator pointing to the last iterator i in [first, last)
3815    *           for which the range [first, i) is sorted.
3816   */
3817   template<typename _ForwardIterator, typename _Compare>
3818     _ForwardIterator
3819     is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3820                     _Compare __comp)
3821     {
3822       // concept requirements
3823       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3824       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3825             typename iterator_traits<_ForwardIterator>::value_type,
3826             typename iterator_traits<_ForwardIterator>::value_type>)
3827       __glibcxx_requires_valid_range(__first, __last);
3828
3829       if (__first == __last)
3830         return __last;
3831
3832       _ForwardIterator __next = __first;
3833       for (++__next; __next != __last; __first = __next, ++__next)
3834         if (__comp(*__next, *__first))
3835           return __next;
3836       return __next;
3837     }
3838
3839   /**
3840    *  @brief  Determines min and max at once as an ordered pair.
3841    *  @ingroup sorting_algorithms
3842    *  @param  a  A thing of arbitrary type.
3843    *  @param  b  Another thing of arbitrary type.
3844    *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3845   */
3846   template<typename _Tp>
3847     inline pair<const _Tp&, const _Tp&>
3848     minmax(const _Tp& __a, const _Tp& __b)
3849     {
3850       // concept requirements
3851       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3852
3853       return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
3854                        : pair<const _Tp&, const _Tp&>(__a, __b);
3855     }
3856
3857   /**
3858    *  @brief  Determines min and max at once as an ordered pair.
3859    *  @ingroup sorting_algorithms
3860    *  @param  a  A thing of arbitrary type.
3861    *  @param  b  Another thing of arbitrary type.
3862    *  @param  comp  A @link comparison_functor comparison functor@endlink.
3863    *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3864   */
3865   template<typename _Tp, typename _Compare>
3866     inline pair<const _Tp&, const _Tp&>
3867     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
3868     {
3869       return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
3870                               : pair<const _Tp&, const _Tp&>(__a, __b);
3871     }
3872
3873   /**
3874    *  @brief  Return a pair of iterators pointing to the minimum and maximum
3875    *          elements in a range.
3876    *  @ingroup sorting_algorithms
3877    *  @param  first  Start of range.
3878    *  @param  last   End of range.
3879    *  @return  make_pair(m, M), where m is the first iterator i in 
3880    *           [first, last) such that no other element in the range is
3881    *           smaller, and where M is the last iterator i in [first, last)
3882    *           such that no other element in the range is larger.
3883   */
3884   template<typename _ForwardIterator>
3885     pair<_ForwardIterator, _ForwardIterator>
3886     minmax_element(_ForwardIterator __first, _ForwardIterator __last)
3887     {
3888       // concept requirements
3889       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3890       __glibcxx_function_requires(_LessThanComparableConcept<
3891             typename iterator_traits<_ForwardIterator>::value_type>)
3892       __glibcxx_requires_valid_range(__first, __last);
3893
3894       _ForwardIterator __next = __first;
3895       if (__first == __last
3896           || ++__next == __last)
3897         return std::make_pair(__first, __first);
3898
3899       _ForwardIterator __min, __max;
3900       if (*__next < *__first)
3901         {
3902           __min = __next;
3903           __max = __first;
3904         }
3905       else
3906         {
3907           __min = __first;
3908           __max = __next;
3909         }
3910
3911       __first = __next;
3912       ++__first;
3913
3914       while (__first != __last)
3915         {
3916           __next = __first;
3917           if (++__next == __last)
3918             {
3919               if (*__first < *__min)
3920                 __min = __first;
3921               else if (!(*__first < *__max))
3922                 __max = __first;
3923               break;
3924             }
3925
3926           if (*__next < *__first)
3927             {
3928               if (*__next < *__min)
3929                 __min = __next;
3930               if (!(*__first < *__max))
3931                 __max = __first;
3932             }
3933           else
3934             {
3935               if (*__first < *__min)
3936                 __min = __first;
3937               if (!(*__next < *__max))
3938                 __max = __next;
3939             }
3940
3941           __first = __next;
3942           ++__first;
3943         }
3944
3945       return std::make_pair(__min, __max);
3946     }
3947
3948   /**
3949    *  @brief  Return a pair of iterators pointing to the minimum and maximum
3950    *          elements in a range.
3951    *  @ingroup sorting_algorithms
3952    *  @param  first  Start of range.
3953    *  @param  last   End of range.
3954    *  @param  comp   Comparison functor.
3955    *  @return  make_pair(m, M), where m is the first iterator i in 
3956    *           [first, last) such that no other element in the range is
3957    *           smaller, and where M is the last iterator i in [first, last)
3958    *           such that no other element in the range is larger.
3959   */
3960   template<typename _ForwardIterator, typename _Compare>
3961     pair<_ForwardIterator, _ForwardIterator>
3962     minmax_element(_ForwardIterator __first, _ForwardIterator __last,
3963                    _Compare __comp)
3964     {
3965       // concept requirements
3966       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3967       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3968             typename iterator_traits<_ForwardIterator>::value_type,
3969             typename iterator_traits<_ForwardIterator>::value_type>)
3970       __glibcxx_requires_valid_range(__first, __last);
3971
3972       _ForwardIterator __next = __first;
3973       if (__first == __last
3974           || ++__next == __last)
3975         return std::make_pair(__first, __first);
3976
3977       _ForwardIterator __min, __max;
3978       if (__comp(*__next, *__first))
3979         {
3980           __min = __next;
3981           __max = __first;
3982         }
3983       else
3984         {
3985           __min = __first;
3986           __max = __next;
3987         }
3988
3989       __first = __next;
3990       ++__first;
3991
3992       while (__first != __last)
3993         {
3994           __next = __first;
3995           if (++__next == __last)
3996             {
3997               if (__comp(*__first, *__min))
3998                 __min = __first;
3999               else if (!__comp(*__first, *__max))
4000                 __max = __first;
4001               break;
4002             }
4003
4004           if (__comp(*__next, *__first))
4005             {
4006               if (__comp(*__next, *__min))
4007                 __min = __next;
4008               if (!__comp(*__first, *__max))
4009                 __max = __first;
4010             }
4011           else
4012             {
4013               if (__comp(*__first, *__min))
4014                 __min = __first;
4015               if (!__comp(*__next, *__max))
4016                 __max = __next;
4017             }
4018
4019           __first = __next;
4020           ++__first;
4021         }
4022
4023       return std::make_pair(__min, __max);
4024     }
4025
4026   // N2722 + DR 915.
4027   template<typename _Tp>
4028     inline _Tp
4029     min(initializer_list<_Tp> __l)
4030     { return *std::min_element(__l.begin(), __l.end()); }
4031
4032   template<typename _Tp, typename _Compare>
4033     inline _Tp
4034     min(initializer_list<_Tp> __l, _Compare __comp)
4035     { return *std::min_element(__l.begin(), __l.end(), __comp); }
4036
4037   template<typename _Tp>
4038     inline _Tp
4039     max(initializer_list<_Tp> __l)
4040     { return *std::max_element(__l.begin(), __l.end()); }
4041
4042   template<typename _Tp, typename _Compare>
4043     inline _Tp
4044     max(initializer_list<_Tp> __l, _Compare __comp)
4045     { return *std::max_element(__l.begin(), __l.end(), __comp); }
4046
4047   template<typename _Tp>
4048     inline pair<_Tp, _Tp>
4049     minmax(initializer_list<_Tp> __l)
4050     {
4051       pair<const _Tp*, const _Tp*> __p =
4052         std::minmax_element(__l.begin(), __l.end());
4053       return std::make_pair(*__p.first, *__p.second);
4054     }
4055
4056   template<typename _Tp, typename _Compare>
4057     inline pair<_Tp, _Tp>
4058     minmax(initializer_list<_Tp> __l, _Compare __comp)
4059     {
4060       pair<const _Tp*, const _Tp*> __p =
4061         std::minmax_element(__l.begin(), __l.end(), __comp);
4062       return std::make_pair(*__p.first, *__p.second);
4063     }
4064
4065 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4066   /**
4067    *  @brief Shuffle the elements of a sequence using a uniform random
4068    *         number generator.
4069    *  @ingroup mutating_algorithms
4070    *  @param  first   A forward iterator.
4071    *  @param  last    A forward iterator.
4072    *  @param  g       A UniformRandomNumberGenerator (26.5.1.3).
4073    *  @return  Nothing.
4074    *
4075    *  Reorders the elements in the range @p [first,last) using @p g to
4076    *  provide random numbers.
4077   */
4078   template<typename _RandomAccessIterator,
4079            typename _UniformRandomNumberGenerator>
4080     void
4081     shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4082             _UniformRandomNumberGenerator& __g)
4083     {
4084       // concept requirements
4085       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4086             _RandomAccessIterator>)
4087       __glibcxx_requires_valid_range(__first, __last);
4088
4089       if (__first == __last)
4090         return;
4091
4092       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4093         _DistanceType;
4094
4095       typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
4096       typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
4097       typedef typename __distr_type::param_type __p_type;
4098       __distr_type __d;
4099
4100       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4101         std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
4102     }
4103 #endif
4104
4105 #endif // __GXX_EXPERIMENTAL_CXX0X__
4106
4107 _GLIBCXX_END_NAMESPACE
4108
4109 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
4110
4111   /**
4112    *  @brief Apply a function to every element of a sequence.
4113    *  @ingroup non_mutating_algorithms
4114    *  @param  first  An input iterator.
4115    *  @param  last   An input iterator.
4116    *  @param  f      A unary function object.
4117    *  @return   @p f (std::move(@p f) in C++0x).
4118    *
4119    *  Applies the function object @p f to each element in the range
4120    *  @p [first,last).  @p f must not modify the order of the sequence.
4121    *  If @p f has a return value it is ignored.
4122   */
4123   template<typename _InputIterator, typename _Function>
4124     _Function
4125     for_each(_InputIterator __first, _InputIterator __last, _Function __f)
4126     {
4127       // concept requirements
4128       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4129       __glibcxx_requires_valid_range(__first, __last);
4130       for (; __first != __last; ++__first)
4131         __f(*__first);
4132       return _GLIBCXX_MOVE(__f);
4133     }
4134
4135   /**
4136    *  @brief Find the first occurrence of a value in a sequence.
4137    *  @ingroup non_mutating_algorithms
4138    *  @param  first  An input iterator.
4139    *  @param  last   An input iterator.
4140    *  @param  val    The value to find.
4141    *  @return   The first iterator @c i in the range @p [first,last)
4142    *  such that @c *i == @p val, or @p last if no such iterator exists.
4143   */
4144   template<typename _InputIterator, typename _Tp>
4145     inline _InputIterator
4146     find(_InputIterator __first, _InputIterator __last,
4147          const _Tp& __val)
4148     {
4149       // concept requirements
4150       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4151       __glibcxx_function_requires(_EqualOpConcept<
4152                 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4153       __glibcxx_requires_valid_range(__first, __last);
4154       return std::__find(__first, __last, __val,
4155                          std::__iterator_category(__first));
4156     }
4157
4158   /**
4159    *  @brief Find the first element in a sequence for which a
4160    *         predicate is true.
4161    *  @ingroup non_mutating_algorithms
4162    *  @param  first  An input iterator.
4163    *  @param  last   An input iterator.
4164    *  @param  pred   A predicate.
4165    *  @return   The first iterator @c i in the range @p [first,last)
4166    *  such that @p pred(*i) is true, or @p last if no such iterator exists.
4167   */
4168   template<typename _InputIterator, typename _Predicate>
4169     inline _InputIterator
4170     find_if(_InputIterator __first, _InputIterator __last,
4171             _Predicate __pred)
4172     {
4173       // concept requirements
4174       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4175       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4176               typename iterator_traits<_InputIterator>::value_type>)
4177       __glibcxx_requires_valid_range(__first, __last);
4178       return std::__find_if(__first, __last, __pred,
4179                             std::__iterator_category(__first));
4180     }
4181
4182   /**
4183    *  @brief  Find element from a set in a sequence.
4184    *  @ingroup non_mutating_algorithms
4185    *  @param  first1  Start of range to search.
4186    *  @param  last1   End of range to search.
4187    *  @param  first2  Start of match candidates.
4188    *  @param  last2   End of match candidates.
4189    *  @return   The first iterator @c i in the range
4190    *  @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4191    *  iterator in [first2,last2), or @p last1 if no such iterator exists.
4192    *
4193    *  Searches the range @p [first1,last1) for an element that is equal to
4194    *  some element in the range [first2,last2).  If found, returns an iterator
4195    *  in the range [first1,last1), otherwise returns @p last1.
4196   */
4197   template<typename _InputIterator, typename _ForwardIterator>
4198     _InputIterator
4199     find_first_of(_InputIterator __first1, _InputIterator __last1,
4200                   _ForwardIterator __first2, _ForwardIterator __last2)
4201     {
4202       // concept requirements
4203       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4204       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4205       __glibcxx_function_requires(_EqualOpConcept<
4206             typename iterator_traits<_InputIterator>::value_type,
4207             typename iterator_traits<_ForwardIterator>::value_type>)
4208       __glibcxx_requires_valid_range(__first1, __last1);
4209       __glibcxx_requires_valid_range(__first2, __last2);
4210
4211       for (; __first1 != __last1; ++__first1)
4212         for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4213           if (*__first1 == *__iter)
4214             return __first1;
4215       return __last1;
4216     }
4217
4218   /**
4219    *  @brief  Find element from a set in a sequence using a predicate.
4220    *  @ingroup non_mutating_algorithms
4221    *  @param  first1  Start of range to search.
4222    *  @param  last1   End of range to search.
4223    *  @param  first2  Start of match candidates.
4224    *  @param  last2   End of match candidates.
4225    *  @param  comp    Predicate to use.
4226    *  @return   The first iterator @c i in the range
4227    *  @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4228    *  iterator in [first2,last2), or @p last1 if no such iterator exists.
4229    *
4230
4231    *  Searches the range @p [first1,last1) for an element that is
4232    *  equal to some element in the range [first2,last2).  If found,
4233    *  returns an iterator in the range [first1,last1), otherwise
4234    *  returns @p last1.
4235   */
4236   template<typename _InputIterator, typename _ForwardIterator,
4237            typename _BinaryPredicate>
4238     _InputIterator
4239     find_first_of(_InputIterator __first1, _InputIterator __last1,
4240                   _ForwardIterator __first2, _ForwardIterator __last2,
4241                   _BinaryPredicate __comp)
4242     {
4243       // concept requirements
4244       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4245       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4246       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4247             typename iterator_traits<_InputIterator>::value_type,
4248             typename iterator_traits<_ForwardIterator>::value_type>)
4249       __glibcxx_requires_valid_range(__first1, __last1);
4250       __glibcxx_requires_valid_range(__first2, __last2);
4251
4252       for (; __first1 != __last1; ++__first1)
4253         for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4254           if (__comp(*__first1, *__iter))
4255             return __first1;
4256       return __last1;
4257     }
4258
4259   /**
4260    *  @brief Find two adjacent values in a sequence that are equal.
4261    *  @ingroup non_mutating_algorithms
4262    *  @param  first  A forward iterator.
4263    *  @param  last   A forward iterator.
4264    *  @return   The first iterator @c i such that @c i and @c i+1 are both
4265    *  valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4266    *  or @p last if no such iterator exists.
4267   */
4268   template<typename _ForwardIterator>
4269     _ForwardIterator
4270     adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
4271     {
4272       // concept requirements
4273       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4274       __glibcxx_function_requires(_EqualityComparableConcept<
4275             typename iterator_traits<_ForwardIterator>::value_type>)
4276       __glibcxx_requires_valid_range(__first, __last);
4277       if (__first == __last)
4278         return __last;
4279       _ForwardIterator __next = __first;
4280       while(++__next != __last)
4281         {
4282           if (*__first == *__next)
4283             return __first;
4284           __first = __next;
4285         }
4286       return __last;
4287     }
4288
4289   /**
4290    *  @brief Find two adjacent values in a sequence using a predicate.
4291    *  @ingroup non_mutating_algorithms
4292    *  @param  first         A forward iterator.
4293    *  @param  last          A forward iterator.
4294    *  @param  binary_pred   A binary predicate.
4295    *  @return   The first iterator @c i such that @c i and @c i+1 are both
4296    *  valid iterators in @p [first,last) and such that
4297    *  @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4298    *  exists.
4299   */
4300   template<typename _ForwardIterator, typename _BinaryPredicate>
4301     _ForwardIterator
4302     adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
4303                   _BinaryPredicate __binary_pred)
4304     {
4305       // concept requirements
4306       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4307       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4308             typename iterator_traits<_ForwardIterator>::value_type,
4309             typename iterator_traits<_ForwardIterator>::value_type>)
4310       __glibcxx_requires_valid_range(__first, __last);
4311       if (__first == __last)
4312         return __last;
4313       _ForwardIterator __next = __first;
4314       while(++__next != __last)
4315         {
4316           if (__binary_pred(*__first, *__next))
4317             return __first;
4318           __first = __next;
4319         }
4320       return __last;
4321     }
4322
4323   /**
4324    *  @brief Count the number of copies of a value in a sequence.
4325    *  @ingroup non_mutating_algorithms
4326    *  @param  first  An input iterator.
4327    *  @param  last   An input iterator.
4328    *  @param  value  The value to be counted.
4329    *  @return   The number of iterators @c i in the range @p [first,last)
4330    *  for which @c *i == @p value
4331   */
4332   template<typename _InputIterator, typename _Tp>
4333     typename iterator_traits<_InputIterator>::difference_type
4334     count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
4335     {
4336       // concept requirements
4337       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4338       __glibcxx_function_requires(_EqualOpConcept<
4339         typename iterator_traits<_InputIterator>::value_type, _Tp>)
4340       __glibcxx_requires_valid_range(__first, __last);
4341       typename iterator_traits<_InputIterator>::difference_type __n = 0;
4342       for (; __first != __last; ++__first)
4343         if (*__first == __value)
4344           ++__n;
4345       return __n;
4346     }
4347
4348   /**
4349    *  @brief Count the elements of a sequence for which a predicate is true.
4350    *  @ingroup non_mutating_algorithms
4351    *  @param  first  An input iterator.
4352    *  @param  last   An input iterator.
4353    *  @param  pred   A predicate.
4354    *  @return   The number of iterators @c i in the range @p [first,last)
4355    *  for which @p pred(*i) is true.
4356   */
4357   template<typename _InputIterator, typename _Predicate>
4358     typename iterator_traits<_InputIterator>::difference_type
4359     count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4360     {
4361       // concept requirements
4362       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4363       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4364             typename iterator_traits<_InputIterator>::value_type>)
4365       __glibcxx_requires_valid_range(__first, __last);
4366       typename iterator_traits<_InputIterator>::difference_type __n = 0;
4367       for (; __first != __last; ++__first)
4368         if (__pred(*__first))
4369           ++__n;
4370       return __n;
4371     }
4372
4373   /**
4374    *  @brief Search a sequence for a matching sub-sequence.
4375    *  @ingroup non_mutating_algorithms
4376    *  @param  first1  A forward iterator.
4377    *  @param  last1   A forward iterator.
4378    *  @param  first2  A forward iterator.
4379    *  @param  last2   A forward iterator.
4380    *  @return   The first iterator @c i in the range
4381    *  @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4382    *  for each @c N in the range @p [0,last2-first2), or @p last1 if no
4383    *  such iterator exists.
4384    *
4385    *  Searches the range @p [first1,last1) for a sub-sequence that compares
4386    *  equal value-by-value with the sequence given by @p [first2,last2) and
4387    *  returns an iterator to the first element of the sub-sequence, or
4388    *  @p last1 if the sub-sequence is not found.
4389    *
4390    *  Because the sub-sequence must lie completely within the range
4391    *  @p [first1,last1) it must start at a position less than
4392    *  @p last1-(last2-first2) where @p last2-first2 is the length of the
4393    *  sub-sequence.
4394    *  This means that the returned iterator @c i will be in the range
4395    *  @p [first1,last1-(last2-first2))
4396   */
4397   template<typename _ForwardIterator1, typename _ForwardIterator2>
4398     _ForwardIterator1
4399     search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4400            _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4401     {
4402       // concept requirements
4403       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4404       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4405       __glibcxx_function_requires(_EqualOpConcept<
4406             typename iterator_traits<_ForwardIterator1>::value_type,
4407             typename iterator_traits<_ForwardIterator2>::value_type>)
4408       __glibcxx_requires_valid_range(__first1, __last1);
4409       __glibcxx_requires_valid_range(__first2, __last2);
4410
4411       // Test for empty ranges
4412       if (__first1 == __last1 || __first2 == __last2)
4413         return __first1;
4414
4415       // Test for a pattern of length 1.
4416       _ForwardIterator2 __p1(__first2);
4417       if (++__p1 == __last2)
4418         return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4419
4420       // General case.
4421       _ForwardIterator2 __p;
4422       _ForwardIterator1 __current = __first1;
4423
4424       for (;;)
4425         {
4426           __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4427           if (__first1 == __last1)
4428             return __last1;
4429
4430           __p = __p1;
4431           __current = __first1;
4432           if (++__current == __last1)
4433             return __last1;
4434
4435           while (*__current == *__p)
4436             {
4437               if (++__p == __last2)
4438                 return __first1;
4439               if (++__current == __last1)
4440                 return __last1;
4441             }
4442           ++__first1;
4443         }
4444       return __first1;
4445     }
4446
4447   /**
4448    *  @brief Search a sequence for a matching sub-sequence using a predicate.
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    *  @param  predicate  A binary predicate.
4455    *  @return   The first iterator @c i in the range
4456    *  @p [first1,last1-(last2-first2)) such that
4457    *  @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4458    *  @p [0,last2-first2), or @p last1 if no such iterator exists.
4459    *
4460    *  Searches the range @p [first1,last1) for a sub-sequence that compares
4461    *  equal value-by-value with the sequence given by @p [first2,last2),
4462    *  using @p predicate to determine equality, and returns an iterator
4463    *  to the first element of the sub-sequence, or @p last1 if no such
4464    *  iterator exists.
4465    *
4466    *  @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4467   */
4468   template<typename _ForwardIterator1, typename _ForwardIterator2,
4469            typename _BinaryPredicate>
4470     _ForwardIterator1
4471     search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4472            _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4473            _BinaryPredicate  __predicate)
4474     {
4475       // concept requirements
4476       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4477       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4478       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4479             typename iterator_traits<_ForwardIterator1>::value_type,
4480             typename iterator_traits<_ForwardIterator2>::value_type>)
4481       __glibcxx_requires_valid_range(__first1, __last1);
4482       __glibcxx_requires_valid_range(__first2, __last2);
4483
4484       // Test for empty ranges
4485       if (__first1 == __last1 || __first2 == __last2)
4486         return __first1;
4487
4488       // Test for a pattern of length 1.
4489       _ForwardIterator2 __p1(__first2);
4490       if (++__p1 == __last2)
4491         {
4492           while (__first1 != __last1
4493                  && !bool(__predicate(*__first1, *__first2)))
4494             ++__first1;
4495           return __first1;
4496         }
4497
4498       // General case.
4499       _ForwardIterator2 __p;
4500       _ForwardIterator1 __current = __first1;
4501
4502       for (;;)
4503         {
4504           while (__first1 != __last1
4505                  && !bool(__predicate(*__first1, *__first2)))
4506             ++__first1;
4507           if (__first1 == __last1)
4508             return __last1;
4509
4510           __p = __p1;
4511           __current = __first1;
4512           if (++__current == __last1)
4513             return __last1;
4514
4515           while (__predicate(*__current, *__p))
4516             {
4517               if (++__p == __last2)
4518                 return __first1;
4519               if (++__current == __last1)
4520                 return __last1;
4521             }
4522           ++__first1;
4523         }
4524       return __first1;
4525     }
4526
4527
4528   /**
4529    *  @brief Search a sequence for a number of consecutive values.
4530    *  @ingroup non_mutating_algorithms
4531    *  @param  first  A forward iterator.
4532    *  @param  last   A forward iterator.
4533    *  @param  count  The number of consecutive values.
4534    *  @param  val    The value to find.
4535    *  @return   The first iterator @c i in the range @p [first,last-count)
4536    *  such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4537    *  or @p last if no such iterator exists.
4538    *
4539    *  Searches the range @p [first,last) for @p count consecutive elements
4540    *  equal to @p val.
4541   */
4542   template<typename _ForwardIterator, typename _Integer, typename _Tp>
4543     _ForwardIterator
4544     search_n(_ForwardIterator __first, _ForwardIterator __last,
4545              _Integer __count, const _Tp& __val)
4546     {
4547       // concept requirements
4548       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4549       __glibcxx_function_requires(_EqualOpConcept<
4550         typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4551       __glibcxx_requires_valid_range(__first, __last);
4552
4553       if (__count <= 0)
4554         return __first;
4555       if (__count == 1)
4556         return _GLIBCXX_STD_P::find(__first, __last, __val);
4557       return std::__search_n(__first, __last, __count, __val,
4558                              std::__iterator_category(__first));
4559     }
4560
4561
4562   /**
4563    *  @brief Search a sequence for a number of consecutive values using a
4564    *         predicate.
4565    *  @ingroup non_mutating_algorithms
4566    *  @param  first        A forward iterator.
4567    *  @param  last         A forward iterator.
4568    *  @param  count        The number of consecutive values.
4569    *  @param  val          The value to find.
4570    *  @param  binary_pred  A binary predicate.
4571    *  @return   The first iterator @c i in the range @p [first,last-count)
4572    *  such that @p binary_pred(*(i+N),val) is true for each @c N in the
4573    *  range @p [0,count), or @p last if no such iterator exists.
4574    *
4575    *  Searches the range @p [first,last) for @p count consecutive elements
4576    *  for which the predicate returns true.
4577   */
4578   template<typename _ForwardIterator, typename _Integer, typename _Tp,
4579            typename _BinaryPredicate>
4580     _ForwardIterator
4581     search_n(_ForwardIterator __first, _ForwardIterator __last,
4582              _Integer __count, const _Tp& __val,
4583              _BinaryPredicate __binary_pred)
4584     {
4585       // concept requirements
4586       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4587       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4588             typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4589       __glibcxx_requires_valid_range(__first, __last);
4590
4591       if (__count <= 0)
4592         return __first;
4593       if (__count == 1)
4594         {
4595           while (__first != __last && !bool(__binary_pred(*__first, __val)))
4596             ++__first;
4597           return __first;
4598         }
4599       return std::__search_n(__first, __last, __count, __val, __binary_pred,
4600                              std::__iterator_category(__first));
4601     }
4602
4603
4604   /**
4605    *  @brief Perform an operation on a sequence.
4606    *  @ingroup mutating_algorithms
4607    *  @param  first     An input iterator.
4608    *  @param  last      An input iterator.
4609    *  @param  result    An output iterator.
4610    *  @param  unary_op  A unary operator.
4611    *  @return   An output iterator equal to @p result+(last-first).
4612    *
4613    *  Applies the operator to each element in the input range and assigns
4614    *  the results to successive elements of the output sequence.
4615    *  Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4616    *  range @p [0,last-first).
4617    *
4618    *  @p unary_op must not alter its argument.
4619   */
4620   template<typename _InputIterator, typename _OutputIterator,
4621            typename _UnaryOperation>
4622     _OutputIterator
4623     transform(_InputIterator __first, _InputIterator __last,
4624               _OutputIterator __result, _UnaryOperation __unary_op)
4625     {
4626       // concept requirements
4627       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4628       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4629             // "the type returned by a _UnaryOperation"
4630             __typeof__(__unary_op(*__first))>)
4631       __glibcxx_requires_valid_range(__first, __last);
4632
4633       for (; __first != __last; ++__first, ++__result)
4634         *__result = __unary_op(*__first);
4635       return __result;
4636     }
4637
4638   /**
4639    *  @brief Perform an operation on corresponding elements of two sequences.
4640    *  @ingroup mutating_algorithms
4641    *  @param  first1     An input iterator.
4642    *  @param  last1      An input iterator.
4643    *  @param  first2     An input iterator.
4644    *  @param  result     An output iterator.
4645    *  @param  binary_op  A binary operator.
4646    *  @return   An output iterator equal to @p result+(last-first).
4647    *
4648    *  Applies the operator to the corresponding elements in the two
4649    *  input ranges and assigns the results to successive elements of the
4650    *  output sequence.
4651    *  Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4652    *  @c N in the range @p [0,last1-first1).
4653    *
4654    *  @p binary_op must not alter either of its arguments.
4655   */
4656   template<typename _InputIterator1, typename _InputIterator2,
4657            typename _OutputIterator, typename _BinaryOperation>
4658     _OutputIterator
4659     transform(_InputIterator1 __first1, _InputIterator1 __last1,
4660               _InputIterator2 __first2, _OutputIterator __result,
4661               _BinaryOperation __binary_op)
4662     {
4663       // concept requirements
4664       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4665       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4666       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4667             // "the type returned by a _BinaryOperation"
4668             __typeof__(__binary_op(*__first1,*__first2))>)
4669       __glibcxx_requires_valid_range(__first1, __last1);
4670
4671       for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4672         *__result = __binary_op(*__first1, *__first2);
4673       return __result;
4674     }
4675
4676   /**
4677    *  @brief Replace each occurrence of one value in a sequence with another
4678    *         value.
4679    *  @ingroup mutating_algorithms
4680    *  @param  first      A forward iterator.
4681    *  @param  last       A forward iterator.
4682    *  @param  old_value  The value to be replaced.
4683    *  @param  new_value  The replacement value.
4684    *  @return   replace() returns no value.
4685    *
4686    *  For each iterator @c i in the range @p [first,last) if @c *i ==
4687    *  @p old_value then the assignment @c *i = @p new_value is performed.
4688   */
4689   template<typename _ForwardIterator, typename _Tp>
4690     void
4691     replace(_ForwardIterator __first, _ForwardIterator __last,
4692             const _Tp& __old_value, const _Tp& __new_value)
4693     {
4694       // concept requirements
4695       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4696                                   _ForwardIterator>)
4697       __glibcxx_function_requires(_EqualOpConcept<
4698             typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4699       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4700             typename iterator_traits<_ForwardIterator>::value_type>)
4701       __glibcxx_requires_valid_range(__first, __last);
4702
4703       for (; __first != __last; ++__first)
4704         if (*__first == __old_value)
4705           *__first = __new_value;
4706     }
4707
4708   /**
4709    *  @brief Replace each value in a sequence for which a predicate returns
4710    *         true with another value.
4711    *  @ingroup mutating_algorithms
4712    *  @param  first      A forward iterator.
4713    *  @param  last       A forward iterator.
4714    *  @param  pred       A predicate.
4715    *  @param  new_value  The replacement value.
4716    *  @return   replace_if() returns no value.
4717    *
4718    *  For each iterator @c i in the range @p [first,last) if @p pred(*i)
4719    *  is true then the assignment @c *i = @p new_value is performed.
4720   */
4721   template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4722     void
4723     replace_if(_ForwardIterator __first, _ForwardIterator __last,
4724                _Predicate __pred, const _Tp& __new_value)
4725     {
4726       // concept requirements
4727       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4728                                   _ForwardIterator>)
4729       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4730             typename iterator_traits<_ForwardIterator>::value_type>)
4731       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4732             typename iterator_traits<_ForwardIterator>::value_type>)
4733       __glibcxx_requires_valid_range(__first, __last);
4734
4735       for (; __first != __last; ++__first)
4736         if (__pred(*__first))
4737           *__first = __new_value;
4738     }
4739
4740   /**
4741    *  @brief Assign the result of a function object to each value in a
4742    *         sequence.
4743    *  @ingroup mutating_algorithms
4744    *  @param  first  A forward iterator.
4745    *  @param  last   A forward iterator.
4746    *  @param  gen    A function object taking no arguments and returning
4747    *                 std::iterator_traits<_ForwardIterator>::value_type
4748    *  @return   generate() returns no value.
4749    *
4750    *  Performs the assignment @c *i = @p gen() for each @c i in the range
4751    *  @p [first,last).
4752   */
4753   template<typename _ForwardIterator, typename _Generator>
4754     void
4755     generate(_ForwardIterator __first, _ForwardIterator __last,
4756              _Generator __gen)
4757     {
4758       // concept requirements
4759       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4760       __glibcxx_function_requires(_GeneratorConcept<_Generator,
4761             typename iterator_traits<_ForwardIterator>::value_type>)
4762       __glibcxx_requires_valid_range(__first, __last);
4763
4764       for (; __first != __last; ++__first)
4765         *__first = __gen();
4766     }
4767
4768   /**
4769    *  @brief Assign the result of a function object to each value in a
4770    *         sequence.
4771    *  @ingroup mutating_algorithms
4772    *  @param  first  A forward iterator.
4773    *  @param  n      The length of the sequence.
4774    *  @param  gen    A function object taking no arguments and returning
4775    *                 std::iterator_traits<_ForwardIterator>::value_type
4776    *  @return   The end of the sequence, @p first+n
4777    *
4778    *  Performs the assignment @c *i = @p gen() for each @c i in the range
4779    *  @p [first,first+n).
4780    *
4781    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
4782    *  DR 865. More algorithms that throw away information
4783   */
4784   template<typename _OutputIterator, typename _Size, typename _Generator>
4785     _OutputIterator
4786     generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4787     {
4788       // concept requirements
4789       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4790             // "the type returned by a _Generator"
4791             __typeof__(__gen())>)
4792
4793       for (; __n > 0; --__n, ++__first)
4794         *__first = __gen();
4795       return __first;
4796     }
4797
4798
4799   /**
4800    *  @brief Copy a sequence, removing consecutive duplicate values.
4801    *  @ingroup mutating_algorithms
4802    *  @param  first   An input iterator.
4803    *  @param  last    An input iterator.
4804    *  @param  result  An output iterator.
4805    *  @return   An iterator designating the end of the resulting sequence.
4806    *
4807    *  Copies each element in the range @p [first,last) to the range
4808    *  beginning at @p result, except that only the first element is copied
4809    *  from groups of consecutive elements that compare equal.
4810    *  unique_copy() is stable, so the relative order of elements that are
4811    *  copied is unchanged.
4812    *
4813    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
4814    *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
4815    *  
4816    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
4817    *  DR 538. 241 again: Does unique_copy() require CopyConstructible and 
4818    *  Assignable?
4819   */
4820   template<typename _InputIterator, typename _OutputIterator>
4821     inline _OutputIterator
4822     unique_copy(_InputIterator __first, _InputIterator __last,
4823                 _OutputIterator __result)
4824     {
4825       // concept requirements
4826       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4827       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4828             typename iterator_traits<_InputIterator>::value_type>)
4829       __glibcxx_function_requires(_EqualityComparableConcept<
4830             typename iterator_traits<_InputIterator>::value_type>)
4831       __glibcxx_requires_valid_range(__first, __last);
4832
4833       if (__first == __last)
4834         return __result;
4835       return std::__unique_copy(__first, __last, __result,
4836                                 std::__iterator_category(__first),
4837                                 std::__iterator_category(__result));
4838     }
4839
4840   /**
4841    *  @brief Copy a sequence, removing consecutive values using a predicate.
4842    *  @ingroup mutating_algorithms
4843    *  @param  first        An input iterator.
4844    *  @param  last         An input iterator.
4845    *  @param  result       An output iterator.
4846    *  @param  binary_pred  A binary predicate.
4847    *  @return   An iterator designating the end of the resulting sequence.
4848    *
4849    *  Copies each element in the range @p [first,last) to the range
4850    *  beginning at @p result, except that only the first element is copied
4851    *  from groups of consecutive elements for which @p binary_pred returns
4852    *  true.
4853    *  unique_copy() is stable, so the relative order of elements that are
4854    *  copied is unchanged.
4855    *
4856    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
4857    *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
4858   */
4859   template<typename _InputIterator, typename _OutputIterator,
4860            typename _BinaryPredicate>
4861     inline _OutputIterator
4862     unique_copy(_InputIterator __first, _InputIterator __last,
4863                 _OutputIterator __result,
4864                 _BinaryPredicate __binary_pred)
4865     {
4866       // concept requirements -- predicates checked later
4867       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4868       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4869             typename iterator_traits<_InputIterator>::value_type>)
4870       __glibcxx_requires_valid_range(__first, __last);
4871
4872       if (__first == __last)
4873         return __result;
4874       return std::__unique_copy(__first, __last, __result, __binary_pred,
4875                                 std::__iterator_category(__first),
4876                                 std::__iterator_category(__result));
4877     }
4878
4879
4880   /**
4881    *  @brief Randomly shuffle the elements of a sequence.
4882    *  @ingroup mutating_algorithms
4883    *  @param  first   A forward iterator.
4884    *  @param  last    A forward iterator.
4885    *  @return  Nothing.
4886    *
4887    *  Reorder the elements in the range @p [first,last) using a random
4888    *  distribution, so that every possible ordering of the sequence is
4889    *  equally likely.
4890   */
4891   template<typename _RandomAccessIterator>
4892     inline void
4893     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4894     {
4895       // concept requirements
4896       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4897             _RandomAccessIterator>)
4898       __glibcxx_requires_valid_range(__first, __last);
4899
4900       if (__first != __last)
4901         for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4902           std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
4903     }
4904
4905   /**
4906    *  @brief Shuffle the elements of a sequence using a random number
4907    *         generator.
4908    *  @ingroup mutating_algorithms
4909    *  @param  first   A forward iterator.
4910    *  @param  last    A forward iterator.
4911    *  @param  rand    The RNG functor or function.
4912    *  @return  Nothing.
4913    *
4914    *  Reorders the elements in the range @p [first,last) using @p rand to
4915    *  provide a random distribution. Calling @p rand(N) for a positive
4916    *  integer @p N should return a randomly chosen integer from the
4917    *  range [0,N).
4918   */
4919   template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4920     void
4921     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4922 #ifdef __GXX_EXPERIMENTAL_CXX0X__
4923                    _RandomNumberGenerator&& __rand)
4924 #else
4925                    _RandomNumberGenerator& __rand)
4926 #endif
4927     {
4928       // concept requirements
4929       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4930             _RandomAccessIterator>)
4931       __glibcxx_requires_valid_range(__first, __last);
4932
4933       if (__first == __last)
4934         return;
4935       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4936         std::iter_swap(__i, __first + __rand((__i - __first) + 1));
4937     }
4938
4939
4940   /**
4941    *  @brief Move elements for which a predicate is true to the beginning
4942    *         of a sequence.
4943    *  @ingroup mutating_algorithms
4944    *  @param  first   A forward iterator.
4945    *  @param  last    A forward iterator.
4946    *  @param  pred    A predicate functor.
4947    *  @return  An iterator @p middle such that @p pred(i) is true for each
4948    *  iterator @p i in the range @p [first,middle) and false for each @p i
4949    *  in the range @p [middle,last).
4950    *
4951    *  @p pred must not modify its operand. @p partition() does not preserve
4952    *  the relative ordering of elements in each group, use
4953    *  @p stable_partition() if this is needed.
4954   */
4955   template<typename _ForwardIterator, typename _Predicate>
4956     inline _ForwardIterator
4957     partition(_ForwardIterator __first, _ForwardIterator __last,
4958               _Predicate   __pred)
4959     {
4960       // concept requirements
4961       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4962                                   _ForwardIterator>)
4963       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4964             typename iterator_traits<_ForwardIterator>::value_type>)
4965       __glibcxx_requires_valid_range(__first, __last);
4966
4967       return std::__partition(__first, __last, __pred,
4968                               std::__iterator_category(__first));
4969     }
4970
4971
4972
4973   /**
4974    *  @brief Sort the smallest elements of a sequence.
4975    *  @ingroup sorting_algorithms
4976    *  @param  first   An iterator.
4977    *  @param  middle  Another iterator.
4978    *  @param  last    Another iterator.
4979    *  @return  Nothing.
4980    *
4981    *  Sorts the smallest @p (middle-first) elements in the range
4982    *  @p [first,last) and moves them to the range @p [first,middle). The
4983    *  order of the remaining elements in the range @p [middle,last) is
4984    *  undefined.
4985    *  After the sort if @p i and @j are iterators in the range
4986    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
4987    *  the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4988   */
4989   template<typename _RandomAccessIterator>
4990     inline void
4991     partial_sort(_RandomAccessIterator __first,
4992                  _RandomAccessIterator __middle,
4993                  _RandomAccessIterator __last)
4994     {
4995       typedef typename iterator_traits<_RandomAccessIterator>::value_type
4996         _ValueType;
4997
4998       // concept requirements
4999       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5000             _RandomAccessIterator>)
5001       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5002       __glibcxx_requires_valid_range(__first, __middle);
5003       __glibcxx_requires_valid_range(__middle, __last);
5004
5005       std::__heap_select(__first, __middle, __last);
5006       std::sort_heap(__first, __middle);
5007     }
5008
5009   /**
5010    *  @brief Sort the smallest elements of a sequence using a predicate
5011    *         for comparison.
5012    *  @ingroup sorting_algorithms
5013    *  @param  first   An iterator.
5014    *  @param  middle  Another iterator.
5015    *  @param  last    Another iterator.
5016    *  @param  comp    A comparison functor.
5017    *  @return  Nothing.
5018    *
5019    *  Sorts the smallest @p (middle-first) elements in the range
5020    *  @p [first,last) and moves them to the range @p [first,middle). The
5021    *  order of the remaining elements in the range @p [middle,last) is
5022    *  undefined.
5023    *  After the sort if @p i and @j are iterators in the range
5024    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
5025    *  the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5026    *  are both false.
5027   */
5028   template<typename _RandomAccessIterator, typename _Compare>
5029     inline void
5030     partial_sort(_RandomAccessIterator __first,
5031                  _RandomAccessIterator __middle,
5032                  _RandomAccessIterator __last,
5033                  _Compare __comp)
5034     {
5035       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5036         _ValueType;
5037
5038       // concept requirements
5039       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5040             _RandomAccessIterator>)
5041       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5042                                   _ValueType, _ValueType>)
5043       __glibcxx_requires_valid_range(__first, __middle);
5044       __glibcxx_requires_valid_range(__middle, __last);
5045
5046       std::__heap_select(__first, __middle, __last, __comp);
5047       std::sort_heap(__first, __middle, __comp);
5048     }
5049
5050   /**
5051    *  @brief Sort a sequence just enough to find a particular position.
5052    *  @ingroup sorting_algorithms
5053    *  @param  first   An iterator.
5054    *  @param  nth     Another iterator.
5055    *  @param  last    Another iterator.
5056    *  @return  Nothing.
5057    *
5058    *  Rearranges the elements in the range @p [first,last) so that @p *nth
5059    *  is the same element that would have been in that position had the
5060    *  whole sequence been sorted.
5061    *  whole sequence been sorted. The elements either side of @p *nth are
5062    *  not completely sorted, but for any iterator @i in the range
5063    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
5064    *  holds that @p *j<*i is false.
5065   */
5066   template<typename _RandomAccessIterator>
5067     inline void
5068     nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5069                 _RandomAccessIterator __last)
5070     {
5071       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5072         _ValueType;
5073
5074       // concept requirements
5075       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5076                                   _RandomAccessIterator>)
5077       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5078       __glibcxx_requires_valid_range(__first, __nth);
5079       __glibcxx_requires_valid_range(__nth, __last);
5080
5081       if (__first == __last || __nth == __last)
5082         return;
5083
5084       std::__introselect(__first, __nth, __last,
5085                          std::__lg(__last - __first) * 2);
5086     }
5087
5088   /**
5089    *  @brief Sort a sequence just enough to find a particular position
5090    *         using a predicate for comparison.
5091    *  @ingroup sorting_algorithms
5092    *  @param  first   An iterator.
5093    *  @param  nth     Another iterator.
5094    *  @param  last    Another iterator.
5095    *  @param  comp    A comparison functor.
5096    *  @return  Nothing.
5097    *
5098    *  Rearranges the elements in the range @p [first,last) so that @p *nth
5099    *  is the same element that would have been in that position had the
5100    *  whole sequence been sorted. The elements either side of @p *nth are
5101    *  not completely sorted, but for any iterator @i in the range
5102    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
5103    *  holds that @p comp(*j,*i) is false.
5104   */
5105   template<typename _RandomAccessIterator, typename _Compare>
5106     inline void
5107     nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5108                 _RandomAccessIterator __last, _Compare __comp)
5109     {
5110       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5111         _ValueType;
5112
5113       // concept requirements
5114       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5115                                   _RandomAccessIterator>)
5116       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5117                                   _ValueType, _ValueType>)
5118       __glibcxx_requires_valid_range(__first, __nth);
5119       __glibcxx_requires_valid_range(__nth, __last);
5120
5121       if (__first == __last || __nth == __last)
5122         return;
5123
5124       std::__introselect(__first, __nth, __last,
5125                          std::__lg(__last - __first) * 2, __comp);
5126     }
5127
5128
5129   /**
5130    *  @brief Sort the elements of a sequence.
5131    *  @ingroup sorting_algorithms
5132    *  @param  first   An iterator.
5133    *  @param  last    Another iterator.
5134    *  @return  Nothing.
5135    *
5136    *  Sorts the elements in the range @p [first,last) in ascending order,
5137    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
5138    *  @p [first,last-1).
5139    *
5140    *  The relative ordering of equivalent elements is not preserved, use
5141    *  @p stable_sort() if this is needed.
5142   */
5143   template<typename _RandomAccessIterator>
5144     inline void
5145     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5146     {
5147       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5148         _ValueType;
5149
5150       // concept requirements
5151       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5152             _RandomAccessIterator>)
5153       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5154       __glibcxx_requires_valid_range(__first, __last);
5155
5156       if (__first != __last)
5157         {
5158           std::__introsort_loop(__first, __last,
5159                                 std::__lg(__last - __first) * 2);
5160           std::__final_insertion_sort(__first, __last);
5161         }
5162     }
5163
5164   /**
5165    *  @brief Sort the elements of a sequence using a predicate for comparison.
5166    *  @ingroup sorting_algorithms
5167    *  @param  first   An iterator.
5168    *  @param  last    Another iterator.
5169    *  @param  comp    A comparison functor.
5170    *  @return  Nothing.
5171    *
5172    *  Sorts the elements in the range @p [first,last) in ascending order,
5173    *  such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5174    *  range @p [first,last-1).
5175    *
5176    *  The relative ordering of equivalent elements is not preserved, use
5177    *  @p stable_sort() if this is needed.
5178   */
5179   template<typename _RandomAccessIterator, typename _Compare>
5180     inline void
5181     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5182          _Compare __comp)
5183     {
5184       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5185         _ValueType;
5186
5187       // concept requirements
5188       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5189             _RandomAccessIterator>)
5190       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
5191                                   _ValueType>)
5192       __glibcxx_requires_valid_range(__first, __last);
5193
5194       if (__first != __last)
5195         {
5196           std::__introsort_loop(__first, __last,
5197                                 std::__lg(__last - __first) * 2, __comp);
5198           std::__final_insertion_sort(__first, __last, __comp);
5199         }
5200     }
5201
5202   /**
5203    *  @brief Merges two sorted ranges.
5204    *  @ingroup sorting_algorithms
5205    *  @param  first1  An iterator.
5206    *  @param  first2  Another iterator.
5207    *  @param  last1   Another iterator.
5208    *  @param  last2   Another iterator.
5209    *  @param  result  An iterator pointing to the end of the merged range.
5210    *  @return         An iterator pointing to the first element <em>not less
5211    *                  than</em> @a val.
5212    *
5213    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5214    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
5215    *  must be sorted, and the output range must not overlap with either of
5216    *  the input ranges.  The sort is @e stable, that is, for equivalent
5217    *  elements in the two ranges, elements from the first range will always
5218    *  come before elements from the second.
5219   */
5220   template<typename _InputIterator1, typename _InputIterator2,
5221            typename _OutputIterator>
5222     _OutputIterator
5223     merge(_InputIterator1 __first1, _InputIterator1 __last1,
5224           _InputIterator2 __first2, _InputIterator2 __last2,
5225           _OutputIterator __result)
5226     {
5227       typedef typename iterator_traits<_InputIterator1>::value_type
5228         _ValueType1;
5229       typedef typename iterator_traits<_InputIterator2>::value_type
5230         _ValueType2;
5231
5232       // concept requirements
5233       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5234       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5235       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5236                                   _ValueType1>)
5237       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5238                                   _ValueType2>)
5239       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
5240       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5241       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5242
5243       while (__first1 != __last1 && __first2 != __last2)
5244         {
5245           if (*__first2 < *__first1)
5246             {
5247               *__result = *__first2;
5248               ++__first2;
5249             }
5250           else
5251             {
5252               *__result = *__first1;
5253               ++__first1;
5254             }
5255           ++__result;
5256         }
5257       return std::copy(__first2, __last2, std::copy(__first1, __last1,
5258                                                     __result));
5259     }
5260
5261   /**
5262    *  @brief Merges two sorted ranges.
5263    *  @ingroup sorting_algorithms
5264    *  @param  first1  An iterator.
5265    *  @param  first2  Another iterator.
5266    *  @param  last1   Another iterator.
5267    *  @param  last2   Another iterator.
5268    *  @param  result  An iterator pointing to the end of the merged range.
5269    *  @param  comp    A functor to use for comparisons.
5270    *  @return         An iterator pointing to the first element "not less
5271    *                  than" @a val.
5272    *
5273    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5274    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
5275    *  must be sorted, and the output range must not overlap with either of
5276    *  the input ranges.  The sort is @e stable, that is, for equivalent
5277    *  elements in the two ranges, elements from the first range will always
5278    *  come before elements from the second.
5279    *
5280    *  The comparison function should have the same effects on ordering as
5281    *  the function used for the initial sort.
5282   */
5283   template<typename _InputIterator1, typename _InputIterator2,
5284            typename _OutputIterator, typename _Compare>
5285     _OutputIterator
5286     merge(_InputIterator1 __first1, _InputIterator1 __last1,
5287           _InputIterator2 __first2, _InputIterator2 __last2,
5288           _OutputIterator __result, _Compare __comp)
5289     {
5290       typedef typename iterator_traits<_InputIterator1>::value_type
5291         _ValueType1;
5292       typedef typename iterator_traits<_InputIterator2>::value_type
5293         _ValueType2;
5294
5295       // concept requirements
5296       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5297       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5298       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5299                                   _ValueType1>)
5300       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5301                                   _ValueType2>)
5302       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5303                                   _ValueType2, _ValueType1>)
5304       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5305       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5306
5307       while (__first1 != __last1 && __first2 != __last2)
5308         {
5309           if (__comp(*__first2, *__first1))
5310             {
5311               *__result = *__first2;
5312               ++__first2;
5313             }
5314           else
5315             {
5316               *__result = *__first1;
5317               ++__first1;
5318             }
5319           ++__result;
5320         }
5321       return std::copy(__first2, __last2, std::copy(__first1, __last1,
5322                                                     __result));
5323     }
5324
5325
5326   /**
5327    *  @brief Sort the elements of a sequence, preserving the relative order
5328    *         of equivalent elements.
5329    *  @ingroup sorting_algorithms
5330    *  @param  first   An iterator.
5331    *  @param  last    Another iterator.
5332    *  @return  Nothing.
5333    *
5334    *  Sorts the elements in the range @p [first,last) in ascending order,
5335    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
5336    *  @p [first,last-1).
5337    *
5338    *  The relative ordering of equivalent elements is preserved, so any two
5339    *  elements @p x and @p y in the range @p [first,last) such that
5340    *  @p x<y is false and @p y<x is false will have the same relative
5341    *  ordering after calling @p stable_sort().
5342   */
5343   template<typename _RandomAccessIterator>
5344     inline void
5345     stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5346     {
5347       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5348         _ValueType;
5349       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5350         _DistanceType;
5351
5352       // concept requirements
5353       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5354             _RandomAccessIterator>)
5355       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5356       __glibcxx_requires_valid_range(__first, __last);
5357
5358       _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5359                                                                  __last);
5360       if (__buf.begin() == 0)
5361         std::__inplace_stable_sort(__first, __last);
5362       else
5363         std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5364                                     _DistanceType(__buf.size()));
5365     }
5366
5367   /**
5368    *  @brief Sort the elements of a sequence using a predicate for comparison,
5369    *         preserving the relative order of equivalent elements.
5370    *  @ingroup sorting_algorithms
5371    *  @param  first   An iterator.
5372    *  @param  last    Another iterator.
5373    *  @param  comp    A comparison functor.
5374    *  @return  Nothing.
5375    *
5376    *  Sorts the elements in the range @p [first,last) in ascending order,
5377    *  such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5378    *  range @p [first,last-1).
5379    *
5380    *  The relative ordering of equivalent elements is preserved, so any two
5381    *  elements @p x and @p y in the range @p [first,last) such that
5382    *  @p comp(x,y) is false and @p comp(y,x) is false will have the same
5383    *  relative ordering after calling @p stable_sort().
5384   */
5385   template<typename _RandomAccessIterator, typename _Compare>
5386     inline void
5387     stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5388                 _Compare __comp)
5389     {
5390       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5391         _ValueType;
5392       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5393         _DistanceType;
5394
5395       // concept requirements
5396       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5397             _RandomAccessIterator>)
5398       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5399                                   _ValueType,
5400                                   _ValueType>)
5401       __glibcxx_requires_valid_range(__first, __last);
5402
5403       _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5404                                                                  __last);
5405       if (__buf.begin() == 0)
5406         std::__inplace_stable_sort(__first, __last, __comp);
5407       else
5408         std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5409                                     _DistanceType(__buf.size()), __comp);
5410     }
5411
5412
5413   /**
5414    *  @brief Return the union of two sorted ranges.
5415    *  @ingroup set_algorithms
5416    *  @param  first1  Start of first range.
5417    *  @param  last1   End of first range.
5418    *  @param  first2  Start of second range.
5419    *  @param  last2   End of second range.
5420    *  @return  End of the output range.
5421    *  @ingroup set_algorithms
5422    *
5423    *  This operation iterates over both ranges, copying elements present in
5424    *  each range in order to the output range.  Iterators increment for each
5425    *  range.  When the current element of one range is less than the other,
5426    *  that element is copied and the iterator advanced.  If an element is
5427    *  contained in both ranges, the element from the first range is copied and
5428    *  both ranges advance.  The output range may not overlap either input
5429    *  range.
5430   */
5431   template<typename _InputIterator1, typename _InputIterator2,
5432            typename _OutputIterator>
5433     _OutputIterator
5434     set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5435               _InputIterator2 __first2, _InputIterator2 __last2,
5436               _OutputIterator __result)
5437     {
5438       typedef typename iterator_traits<_InputIterator1>::value_type
5439         _ValueType1;
5440       typedef typename iterator_traits<_InputIterator2>::value_type
5441         _ValueType2;
5442
5443       // concept requirements
5444       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5445       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5446       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5447                                   _ValueType1>)
5448       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5449                                   _ValueType2>)
5450       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5451       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5452       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5453       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5454
5455       while (__first1 != __last1 && __first2 != __last2)
5456         {
5457           if (*__first1 < *__first2)
5458             {
5459               *__result = *__first1;
5460               ++__first1;
5461             }
5462           else if (*__first2 < *__first1)
5463             {
5464               *__result = *__first2;
5465               ++__first2;
5466             }
5467           else
5468             {
5469               *__result = *__first1;
5470               ++__first1;
5471               ++__first2;
5472             }
5473           ++__result;
5474         }
5475       return std::copy(__first2, __last2, std::copy(__first1, __last1,
5476                                                     __result));
5477     }
5478
5479   /**
5480    *  @brief Return the union of two sorted ranges using a comparison functor.
5481    *  @ingroup set_algorithms
5482    *  @param  first1  Start of first range.
5483    *  @param  last1   End of first range.
5484    *  @param  first2  Start of second range.
5485    *  @param  last2   End of second range.
5486    *  @param  comp    The comparison functor.
5487    *  @return  End of the output range.
5488    *  @ingroup set_algorithms
5489    *
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    *  according to @a comp, that element is copied and the iterator advanced.
5494    *  If an equivalent element according to @a comp is contained in both
5495    *  ranges, the element from the first range is copied and both ranges
5496    *  advance.  The output range may not overlap either input range.
5497   */
5498   template<typename _InputIterator1, typename _InputIterator2,
5499            typename _OutputIterator, typename _Compare>
5500     _OutputIterator
5501     set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5502               _InputIterator2 __first2, _InputIterator2 __last2,
5503               _OutputIterator __result, _Compare __comp)
5504     {
5505       typedef typename iterator_traits<_InputIterator1>::value_type
5506         _ValueType1;
5507       typedef typename iterator_traits<_InputIterator2>::value_type
5508         _ValueType2;
5509
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(_BinaryPredicateConcept<_Compare,
5518                                   _ValueType1, _ValueType2>)
5519       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5520                                   _ValueType2, _ValueType1>)
5521       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5522       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5523
5524       while (__first1 != __last1 && __first2 != __last2)
5525         {
5526           if (__comp(*__first1, *__first2))
5527             {
5528               *__result = *__first1;
5529               ++__first1;
5530             }
5531           else if (__comp(*__first2, *__first1))
5532             {
5533               *__result = *__first2;
5534               ++__first2;
5535             }
5536           else
5537             {
5538               *__result = *__first1;
5539               ++__first1;
5540               ++__first2;
5541             }
5542           ++__result;
5543         }
5544       return std::copy(__first2, __last2, std::copy(__first1, __last1,
5545                                                     __result));
5546     }
5547
5548   /**
5549    *  @brief Return the intersection of two sorted ranges.
5550    *  @ingroup set_algorithms
5551    *  @param  first1  Start of first range.
5552    *  @param  last1   End of first range.
5553    *  @param  first2  Start of second range.
5554    *  @param  last2   End of second range.
5555    *  @return  End of the output range.
5556    *  @ingroup set_algorithms
5557    *
5558    *  This operation iterates over both ranges, copying elements present in
5559    *  both ranges in order to the output range.  Iterators increment for each
5560    *  range.  When the current element of one range is less than the other,
5561    *  that iterator advances.  If an element is contained in both ranges, the
5562    *  element from the first range is copied and both ranges advance.  The
5563    *  output range may not overlap either input range.
5564   */
5565   template<typename _InputIterator1, typename _InputIterator2,
5566            typename _OutputIterator>
5567     _OutputIterator
5568     set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5569                      _InputIterator2 __first2, _InputIterator2 __last2,
5570                      _OutputIterator __result)
5571     {
5572       typedef typename iterator_traits<_InputIterator1>::value_type
5573         _ValueType1;
5574       typedef typename iterator_traits<_InputIterator2>::value_type
5575         _ValueType2;
5576
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(_LessThanOpConcept<_ValueType1, _ValueType2>)
5583       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5584       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5585       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5586
5587       while (__first1 != __last1 && __first2 != __last2)
5588         if (*__first1 < *__first2)
5589           ++__first1;
5590         else if (*__first2 < *__first1)
5591           ++__first2;
5592         else
5593           {
5594             *__result = *__first1;
5595             ++__first1;
5596             ++__first2;
5597             ++__result;
5598           }
5599       return __result;
5600     }
5601
5602   /**
5603    *  @brief Return the intersection of two sorted ranges using comparison
5604    *  functor.
5605    *  @ingroup set_algorithms
5606    *  @param  first1  Start of first range.
5607    *  @param  last1   End of first range.
5608    *  @param  first2  Start of second range.
5609    *  @param  last2   End of second range.
5610    *  @param  comp    The comparison functor.
5611    *  @return  End of the output range.
5612    *  @ingroup set_algorithms
5613    *
5614    *  This operation iterates over both ranges, copying elements present in
5615    *  both ranges in order to the output range.  Iterators increment for each
5616    *  range.  When the current element of one range is less than the other
5617    *  according to @a comp, that iterator advances.  If an element is
5618    *  contained in both ranges according to @a comp, the element from the
5619    *  first range is copied and both ranges advance.  The output range may not
5620    *  overlap either input range.
5621   */
5622   template<typename _InputIterator1, typename _InputIterator2,
5623            typename _OutputIterator, typename _Compare>
5624     _OutputIterator
5625     set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5626                      _InputIterator2 __first2, _InputIterator2 __last2,
5627                      _OutputIterator __result, _Compare __comp)
5628     {
5629       typedef typename iterator_traits<_InputIterator1>::value_type
5630         _ValueType1;
5631       typedef typename iterator_traits<_InputIterator2>::value_type
5632         _ValueType2;
5633
5634       // concept requirements
5635       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5636       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5637       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5638                                   _ValueType1>)
5639       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5640                                   _ValueType1, _ValueType2>)
5641       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5642                                   _ValueType2, _ValueType1>)
5643       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5644       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5645
5646       while (__first1 != __last1 && __first2 != __last2)
5647         if (__comp(*__first1, *__first2))
5648           ++__first1;
5649         else if (__comp(*__first2, *__first1))
5650           ++__first2;
5651         else
5652           {
5653             *__result = *__first1;
5654             ++__first1;
5655             ++__first2;
5656             ++__result;
5657           }
5658       return __result;
5659     }
5660
5661   /**
5662    *  @brief Return the difference of two sorted ranges.
5663    *  @ingroup set_algorithms
5664    *  @param  first1  Start of first range.
5665    *  @param  last1   End of first range.
5666    *  @param  first2  Start of second range.
5667    *  @param  last2   End of second range.
5668    *  @return  End of the output range.
5669    *  @ingroup set_algorithms
5670    *
5671    *  This operation iterates over both ranges, copying elements present in
5672    *  the first range but not the second in order to the output range.
5673    *  Iterators increment for each range.  When the current element of the
5674    *  first range is less than the second, that element is copied and the
5675    *  iterator advances.  If the current element of the second range is less,
5676    *  the iterator advances, but no element is copied.  If an element is
5677    *  contained in both ranges, no elements are copied and both ranges
5678    *  advance.  The output range may not overlap either input range.
5679   */
5680   template<typename _InputIterator1, typename _InputIterator2,
5681            typename _OutputIterator>
5682     _OutputIterator
5683     set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5684                    _InputIterator2 __first2, _InputIterator2 __last2,
5685                    _OutputIterator __result)
5686     {
5687       typedef typename iterator_traits<_InputIterator1>::value_type
5688         _ValueType1;
5689       typedef typename iterator_traits<_InputIterator2>::value_type
5690         _ValueType2;
5691
5692       // concept requirements
5693       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5694       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5695       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5696                                   _ValueType1>)
5697       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5698       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
5699       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5700       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5701
5702       while (__first1 != __last1 && __first2 != __last2)
5703         if (*__first1 < *__first2)
5704           {
5705             *__result = *__first1;
5706             ++__first1;
5707             ++__result;
5708           }
5709         else if (*__first2 < *__first1)
5710           ++__first2;
5711         else
5712           {
5713             ++__first1;
5714             ++__first2;
5715           }
5716       return std::copy(__first1, __last1, __result);
5717     }
5718
5719   /**
5720    *  @brief  Return the difference of two sorted ranges using comparison
5721    *  functor.
5722    *  @ingroup set_algorithms
5723    *  @param  first1  Start of first range.
5724    *  @param  last1   End of first range.
5725    *  @param  first2  Start of second range.
5726    *  @param  last2   End of second range.
5727    *  @param  comp    The comparison functor.
5728    *  @return  End of the output range.
5729    *  @ingroup set_algorithms
5730    *
5731    *  This operation iterates over both ranges, copying elements present in
5732    *  the first range but not the second in order to the output range.
5733    *  Iterators increment for each range.  When the current element of the
5734    *  first range is less than the second according to @a comp, that element
5735    *  is copied and the iterator advances.  If the current element of the
5736    *  second range is less, no element is copied and the iterator advances.
5737    *  If an element is contained in both ranges according to @a comp, no
5738    *  elements are copied and both ranges advance.  The output range may not
5739    *  overlap either input range.
5740   */
5741   template<typename _InputIterator1, typename _InputIterator2,
5742            typename _OutputIterator, typename _Compare>
5743     _OutputIterator
5744     set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5745                    _InputIterator2 __first2, _InputIterator2 __last2,
5746                    _OutputIterator __result, _Compare __comp)
5747     {
5748       typedef typename iterator_traits<_InputIterator1>::value_type
5749         _ValueType1;
5750       typedef typename iterator_traits<_InputIterator2>::value_type
5751         _ValueType2;
5752
5753       // concept requirements
5754       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5755       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5756       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5757                                   _ValueType1>)
5758       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5759                                   _ValueType1, _ValueType2>)
5760       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5761                                   _ValueType2, _ValueType1>)
5762       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5763       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5764
5765       while (__first1 != __last1 && __first2 != __last2)
5766         if (__comp(*__first1, *__first2))
5767           {
5768             *__result = *__first1;
5769             ++__first1;
5770             ++__result;
5771           }
5772         else if (__comp(*__first2, *__first1))
5773           ++__first2;
5774         else
5775           {
5776             ++__first1;
5777             ++__first2;
5778           }
5779       return std::copy(__first1, __last1, __result);
5780     }
5781
5782   /**
5783    *  @brief  Return the symmetric difference of two sorted ranges.
5784    *  @ingroup set_algorithms
5785    *  @param  first1  Start of first range.
5786    *  @param  last1   End of first range.
5787    *  @param  first2  Start of second range.
5788    *  @param  last2   End of second range.
5789    *  @return  End of the output range.
5790    *  @ingroup set_algorithms
5791    *
5792    *  This operation iterates over both ranges, copying elements present in
5793    *  one range but not the other in order to the output range.  Iterators
5794    *  increment for each range.  When the current element of one range is less
5795    *  than the other, that element is copied and the iterator advances.  If an
5796    *  element is contained in both ranges, no elements are copied and both
5797    *  ranges advance.  The output range may not overlap either input range.
5798   */
5799   template<typename _InputIterator1, typename _InputIterator2,
5800            typename _OutputIterator>
5801     _OutputIterator
5802     set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5803                              _InputIterator2 __first2, _InputIterator2 __last2,
5804                              _OutputIterator __result)
5805     {
5806       typedef typename iterator_traits<_InputIterator1>::value_type
5807         _ValueType1;
5808       typedef typename iterator_traits<_InputIterator2>::value_type
5809         _ValueType2;
5810
5811       // concept requirements
5812       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5813       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5814       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5815                                   _ValueType1>)
5816       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5817                                   _ValueType2>)
5818       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5819       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
5820       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5821       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5822
5823       while (__first1 != __last1 && __first2 != __last2)
5824         if (*__first1 < *__first2)
5825           {
5826             *__result = *__first1;
5827             ++__first1;
5828             ++__result;
5829           }
5830         else if (*__first2 < *__first1)
5831           {
5832             *__result = *__first2;
5833             ++__first2;
5834             ++__result;
5835           }
5836         else
5837           {
5838             ++__first1;
5839             ++__first2;
5840           }
5841       return std::copy(__first2, __last2, std::copy(__first1,
5842                                                     __last1, __result));
5843     }
5844
5845   /**
5846    *  @brief  Return the symmetric difference of two sorted ranges using
5847    *  comparison functor.
5848    *  @ingroup set_algorithms
5849    *  @param  first1  Start of first range.
5850    *  @param  last1   End of first range.
5851    *  @param  first2  Start of second range.
5852    *  @param  last2   End of second range.
5853    *  @param  comp    The comparison functor.
5854    *  @return  End of the output range.
5855    *  @ingroup set_algorithms
5856    *
5857    *  This operation iterates over both ranges, copying elements present in
5858    *  one range but not the other in order to the output range.  Iterators
5859    *  increment for each range.  When the current element of one range is less
5860    *  than the other according to @a comp, that element is copied and the
5861    *  iterator advances.  If an element is contained in both ranges according
5862    *  to @a comp, no elements are copied and both ranges advance.  The output
5863    *  range may not overlap either input range.
5864   */
5865   template<typename _InputIterator1, typename _InputIterator2,
5866            typename _OutputIterator, typename _Compare>
5867     _OutputIterator
5868     set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5869                              _InputIterator2 __first2, _InputIterator2 __last2,
5870                              _OutputIterator __result,
5871                              _Compare __comp)
5872     {
5873       typedef typename iterator_traits<_InputIterator1>::value_type
5874         _ValueType1;
5875       typedef typename iterator_traits<_InputIterator2>::value_type
5876         _ValueType2;
5877
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(_BinaryPredicateConcept<_Compare,
5886                                   _ValueType1, _ValueType2>)
5887       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5888                                   _ValueType2, _ValueType1>)
5889       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5890       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5891
5892       while (__first1 != __last1 && __first2 != __last2)
5893         if (__comp(*__first1, *__first2))
5894           {
5895             *__result = *__first1;
5896             ++__first1;
5897             ++__result;
5898           }
5899         else if (__comp(*__first2, *__first1))
5900           {
5901             *__result = *__first2;
5902             ++__first2;
5903             ++__result;
5904           }
5905         else
5906           {
5907             ++__first1;
5908             ++__first2;
5909           }
5910       return std::copy(__first2, __last2, 
5911                        std::copy(__first1, __last1, __result));
5912     }
5913
5914
5915   /**
5916    *  @brief  Return the minimum element in a range.
5917    *  @ingroup sorting_algorithms
5918    *  @param  first  Start of range.
5919    *  @param  last   End of range.
5920    *  @return  Iterator referencing the first instance of the smallest value.
5921   */
5922   template<typename _ForwardIterator>
5923     _ForwardIterator
5924     min_element(_ForwardIterator __first, _ForwardIterator __last)
5925     {
5926       // concept requirements
5927       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5928       __glibcxx_function_requires(_LessThanComparableConcept<
5929             typename iterator_traits<_ForwardIterator>::value_type>)
5930       __glibcxx_requires_valid_range(__first, __last);
5931
5932       if (__first == __last)
5933         return __first;
5934       _ForwardIterator __result = __first;
5935       while (++__first != __last)
5936         if (*__first < *__result)
5937           __result = __first;
5938       return __result;
5939     }
5940
5941   /**
5942    *  @brief  Return the minimum element in a range using comparison functor.
5943    *  @ingroup sorting_algorithms
5944    *  @param  first  Start of range.
5945    *  @param  last   End of range.
5946    *  @param  comp   Comparison functor.
5947    *  @return  Iterator referencing the first instance of the smallest value
5948    *  according to comp.
5949   */
5950   template<typename _ForwardIterator, typename _Compare>
5951     _ForwardIterator
5952     min_element(_ForwardIterator __first, _ForwardIterator __last,
5953                 _Compare __comp)
5954     {
5955       // concept requirements
5956       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5957       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5958             typename iterator_traits<_ForwardIterator>::value_type,
5959             typename iterator_traits<_ForwardIterator>::value_type>)
5960       __glibcxx_requires_valid_range(__first, __last);
5961
5962       if (__first == __last)
5963         return __first;
5964       _ForwardIterator __result = __first;
5965       while (++__first != __last)
5966         if (__comp(*__first, *__result))
5967           __result = __first;
5968       return __result;
5969     }
5970
5971   /**
5972    *  @brief  Return the maximum element in a range.
5973    *  @ingroup sorting_algorithms
5974    *  @param  first  Start of range.
5975    *  @param  last   End of range.
5976    *  @return  Iterator referencing the first instance of the largest value.
5977   */
5978   template<typename _ForwardIterator>
5979     _ForwardIterator
5980     max_element(_ForwardIterator __first, _ForwardIterator __last)
5981     {
5982       // concept requirements
5983       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5984       __glibcxx_function_requires(_LessThanComparableConcept<
5985             typename iterator_traits<_ForwardIterator>::value_type>)
5986       __glibcxx_requires_valid_range(__first, __last);
5987
5988       if (__first == __last)
5989         return __first;
5990       _ForwardIterator __result = __first;
5991       while (++__first != __last)
5992         if (*__result < *__first)
5993           __result = __first;
5994       return __result;
5995     }
5996
5997   /**
5998    *  @brief  Return the maximum element in a range using comparison functor.
5999    *  @ingroup sorting_algorithms
6000    *  @param  first  Start of range.
6001    *  @param  last   End of range.
6002    *  @param  comp   Comparison functor.
6003    *  @return  Iterator referencing the first instance of the largest value
6004    *  according to comp.
6005   */
6006   template<typename _ForwardIterator, typename _Compare>
6007     _ForwardIterator
6008     max_element(_ForwardIterator __first, _ForwardIterator __last,
6009                 _Compare __comp)
6010     {
6011       // concept requirements
6012       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6013       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6014             typename iterator_traits<_ForwardIterator>::value_type,
6015             typename iterator_traits<_ForwardIterator>::value_type>)
6016       __glibcxx_requires_valid_range(__first, __last);
6017
6018       if (__first == __last) return __first;
6019       _ForwardIterator __result = __first;
6020       while (++__first != __last)
6021         if (__comp(*__result, *__first))
6022           __result = __first;
6023       return __result;
6024     }
6025
6026 _GLIBCXX_END_NESTED_NAMESPACE
6027
6028 #endif /* _STL_ALGO_H */