OSDN Git Service

2010-11-18 Benjamin Kosnik <bkoz@redhat.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 bits/stl_algo.h
53  *  This is an internal header file, included by other library headers.
54  *  Do not attempt to use it directly. @headername{algorithm}
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 first 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    *  @param  comp    A functor to use for comparisons.
2380    *  @return An iterator pointing to the first element <em>not less
2381    *           than</em> @a val, or end() if every element is less
2382    *           than @a val.
2383    *  @ingroup binary_search_algorithms
2384    *
2385    *  The comparison function should have the same effects on ordering as
2386    *  the function used for the initial sort.
2387   */
2388   template<typename _ForwardIterator, typename _Tp, typename _Compare>
2389     _ForwardIterator
2390     lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2391                 const _Tp& __val, _Compare __comp)
2392     {
2393       typedef typename iterator_traits<_ForwardIterator>::value_type
2394         _ValueType;
2395       typedef typename iterator_traits<_ForwardIterator>::difference_type
2396         _DistanceType;
2397
2398       // concept requirements
2399       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2400       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2401                                   _ValueType, _Tp>)
2402       __glibcxx_requires_partitioned_lower_pred(__first, __last,
2403                                                 __val, __comp);
2404
2405       _DistanceType __len = std::distance(__first, __last);
2406
2407       while (__len > 0)
2408         {
2409           _DistanceType __half = __len >> 1;
2410           _ForwardIterator __middle = __first;
2411           std::advance(__middle, __half);
2412           if (__comp(*__middle, __val))
2413             {
2414               __first = __middle;
2415               ++__first;
2416               __len = __len - __half - 1;
2417             }
2418           else
2419             __len = __half;
2420         }
2421       return __first;
2422     }
2423
2424   /**
2425    *  @brief Finds the last position in which @a val could be inserted
2426    *         without changing the ordering.
2427    *  @ingroup binary_search_algorithms
2428    *  @param  first   An iterator.
2429    *  @param  last    Another iterator.
2430    *  @param  val     The search term.
2431    *  @return  An iterator pointing to the first element greater than @a val,
2432    *           or end() if no elements are greater than @a val.
2433    *  @ingroup binary_search_algorithms
2434   */
2435   template<typename _ForwardIterator, typename _Tp>
2436     _ForwardIterator
2437     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2438                 const _Tp& __val)
2439     {
2440       typedef typename iterator_traits<_ForwardIterator>::value_type
2441         _ValueType;
2442       typedef typename iterator_traits<_ForwardIterator>::difference_type
2443         _DistanceType;
2444
2445       // concept requirements
2446       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2447       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2448       __glibcxx_requires_partitioned_upper(__first, __last, __val);
2449
2450       _DistanceType __len = std::distance(__first, __last);
2451
2452       while (__len > 0)
2453         {
2454           _DistanceType __half = __len >> 1;
2455           _ForwardIterator __middle = __first;
2456           std::advance(__middle, __half);
2457           if (__val < *__middle)
2458             __len = __half;
2459           else
2460             {
2461               __first = __middle;
2462               ++__first;
2463               __len = __len - __half - 1;
2464             }
2465         }
2466       return __first;
2467     }
2468
2469   /**
2470    *  @brief Finds the last position in which @a val could be inserted
2471    *         without changing the ordering.
2472    *  @ingroup binary_search_algorithms
2473    *  @param  first   An iterator.
2474    *  @param  last    Another iterator.
2475    *  @param  val     The search term.
2476    *  @param  comp    A functor to use for comparisons.
2477    *  @return  An iterator pointing to the first element greater than @a val,
2478    *           or end() if no elements are greater than @a val.
2479    *  @ingroup binary_search_algorithms
2480    *
2481    *  The comparison function should have the same effects on ordering as
2482    *  the function used for the initial sort.
2483   */
2484   template<typename _ForwardIterator, typename _Tp, typename _Compare>
2485     _ForwardIterator
2486     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2487                 const _Tp& __val, _Compare __comp)
2488     {
2489       typedef typename iterator_traits<_ForwardIterator>::value_type
2490         _ValueType;
2491       typedef typename iterator_traits<_ForwardIterator>::difference_type
2492         _DistanceType;
2493
2494       // concept requirements
2495       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2496       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2497                                   _Tp, _ValueType>)
2498       __glibcxx_requires_partitioned_upper_pred(__first, __last,
2499                                                 __val, __comp);
2500
2501       _DistanceType __len = std::distance(__first, __last);
2502
2503       while (__len > 0)
2504         {
2505           _DistanceType __half = __len >> 1;
2506           _ForwardIterator __middle = __first;
2507           std::advance(__middle, __half);
2508           if (__comp(__val, *__middle))
2509             __len = __half;
2510           else
2511             {
2512               __first = __middle;
2513               ++__first;
2514               __len = __len - __half - 1;
2515             }
2516         }
2517       return __first;
2518     }
2519
2520   /**
2521    *  @brief Finds the largest subrange in which @a val could be inserted
2522    *         at any place in it without changing the ordering.
2523    *  @ingroup binary_search_algorithms
2524    *  @param  first   An iterator.
2525    *  @param  last    Another iterator.
2526    *  @param  val     The search term.
2527    *  @return  An pair of iterators defining the subrange.
2528    *  @ingroup binary_search_algorithms
2529    *
2530    *  This is equivalent to
2531    *  @code
2532    *    std::make_pair(lower_bound(first, last, val),
2533    *                   upper_bound(first, last, val))
2534    *  @endcode
2535    *  but does not actually call those functions.
2536   */
2537   template<typename _ForwardIterator, typename _Tp>
2538     pair<_ForwardIterator, _ForwardIterator>
2539     equal_range(_ForwardIterator __first, _ForwardIterator __last,
2540                 const _Tp& __val)
2541     {
2542       typedef typename iterator_traits<_ForwardIterator>::value_type
2543         _ValueType;
2544       typedef typename iterator_traits<_ForwardIterator>::difference_type
2545         _DistanceType;
2546
2547       // concept requirements
2548       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2549       __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2550       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)  
2551       __glibcxx_requires_partitioned_lower(__first, __last, __val);
2552       __glibcxx_requires_partitioned_upper(__first, __last, __val);      
2553
2554       _DistanceType __len = std::distance(__first, __last);
2555  
2556       while (__len > 0)
2557         {
2558           _DistanceType __half = __len >> 1;
2559           _ForwardIterator __middle = __first;
2560           std::advance(__middle, __half);
2561           if (*__middle < __val)
2562             {
2563               __first = __middle;
2564               ++__first;
2565               __len = __len - __half - 1;
2566             }
2567           else if (__val < *__middle)
2568             __len = __half;
2569           else
2570             {
2571               _ForwardIterator __left = std::lower_bound(__first, __middle,
2572                                                          __val);
2573               std::advance(__first, __len);
2574               _ForwardIterator __right = std::upper_bound(++__middle, __first,
2575                                                           __val);
2576               return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2577             }
2578         }
2579       return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2580     }
2581
2582   /**
2583    *  @brief Finds the largest subrange in which @a val could be inserted
2584    *         at any place in it without changing the ordering.
2585    *  @param  first   An iterator.
2586    *  @param  last    Another iterator.
2587    *  @param  val     The search term.
2588    *  @param  comp    A functor to use for comparisons.
2589    *  @return  An pair of iterators defining the subrange.
2590    *  @ingroup binary_search_algorithms
2591    *
2592    *  This is equivalent to
2593    *  @code
2594    *    std::make_pair(lower_bound(first, last, val, comp),
2595    *                   upper_bound(first, last, val, comp))
2596    *  @endcode
2597    *  but does not actually call those functions.
2598   */
2599   template<typename _ForwardIterator, typename _Tp, typename _Compare>
2600     pair<_ForwardIterator, _ForwardIterator>
2601     equal_range(_ForwardIterator __first, _ForwardIterator __last,
2602                 const _Tp& __val, _Compare __comp)
2603     {
2604       typedef typename iterator_traits<_ForwardIterator>::value_type
2605         _ValueType;
2606       typedef typename iterator_traits<_ForwardIterator>::difference_type
2607         _DistanceType;
2608
2609       // concept requirements
2610       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2611       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2612                                   _ValueType, _Tp>)
2613       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2614                                   _Tp, _ValueType>)
2615       __glibcxx_requires_partitioned_lower_pred(__first, __last,
2616                                                 __val, __comp);
2617       __glibcxx_requires_partitioned_upper_pred(__first, __last,
2618                                                 __val, __comp);
2619
2620       _DistanceType __len = std::distance(__first, __last);
2621
2622       while (__len > 0)
2623         {
2624           _DistanceType __half = __len >> 1;
2625           _ForwardIterator __middle = __first;
2626           std::advance(__middle, __half);
2627           if (__comp(*__middle, __val))
2628             {
2629               __first = __middle;
2630               ++__first;
2631               __len = __len - __half - 1;
2632             }
2633           else if (__comp(__val, *__middle))
2634             __len = __half;
2635           else
2636             {
2637               _ForwardIterator __left = std::lower_bound(__first, __middle,
2638                                                          __val, __comp);
2639               std::advance(__first, __len);
2640               _ForwardIterator __right = std::upper_bound(++__middle, __first,
2641                                                           __val, __comp);
2642               return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2643             }
2644         }
2645       return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2646     }
2647
2648   /**
2649    *  @brief Determines whether an element exists in a range.
2650    *  @ingroup binary_search_algorithms
2651    *  @param  first   An iterator.
2652    *  @param  last    Another iterator.
2653    *  @param  val     The search term.
2654    *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
2655    *
2656    *  Note that this does not actually return an iterator to @a val.  For
2657    *  that, use std::find or a container's specialized find member functions.
2658   */
2659   template<typename _ForwardIterator, typename _Tp>
2660     bool
2661     binary_search(_ForwardIterator __first, _ForwardIterator __last,
2662                   const _Tp& __val)
2663     {
2664       typedef typename iterator_traits<_ForwardIterator>::value_type
2665         _ValueType;
2666
2667       // concept requirements
2668       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2669       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2670       __glibcxx_requires_partitioned_lower(__first, __last, __val);
2671       __glibcxx_requires_partitioned_upper(__first, __last, __val);
2672
2673       _ForwardIterator __i = std::lower_bound(__first, __last, __val);
2674       return __i != __last && !(__val < *__i);
2675     }
2676
2677   /**
2678    *  @brief Determines whether an element exists in a range.
2679    *  @ingroup binary_search_algorithms
2680    *  @param  first   An iterator.
2681    *  @param  last    Another iterator.
2682    *  @param  val     The search term.
2683    *  @param  comp    A functor to use for comparisons.
2684    *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
2685    *
2686    *  Note that this does not actually return an iterator to @a val.  For
2687    *  that, use std::find or a container's specialized find member functions.
2688    *
2689    *  The comparison function should have the same effects on ordering as
2690    *  the function used for the initial sort.
2691   */
2692   template<typename _ForwardIterator, typename _Tp, typename _Compare>
2693     bool
2694     binary_search(_ForwardIterator __first, _ForwardIterator __last,
2695                   const _Tp& __val, _Compare __comp)
2696     {
2697       typedef typename iterator_traits<_ForwardIterator>::value_type
2698         _ValueType;
2699
2700       // concept requirements
2701       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2702       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2703                                   _Tp, _ValueType>)
2704       __glibcxx_requires_partitioned_lower_pred(__first, __last,
2705                                                 __val, __comp);
2706       __glibcxx_requires_partitioned_upper_pred(__first, __last,
2707                                                 __val, __comp);
2708
2709       _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
2710       return __i != __last && !bool(__comp(__val, *__i));
2711     }
2712
2713   // merge
2714
2715   /// This is a helper function for the merge routines.
2716   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2717            typename _BidirectionalIterator3>
2718     _BidirectionalIterator3
2719     __merge_backward(_BidirectionalIterator1 __first1,
2720                      _BidirectionalIterator1 __last1,
2721                      _BidirectionalIterator2 __first2,
2722                      _BidirectionalIterator2 __last2,
2723                      _BidirectionalIterator3 __result)
2724     {
2725       if (__first1 == __last1)
2726         return std::copy_backward(__first2, __last2, __result);
2727       if (__first2 == __last2)
2728         return std::copy_backward(__first1, __last1, __result);
2729       --__last1;
2730       --__last2;
2731       while (true)
2732         {
2733           if (*__last2 < *__last1)
2734             {
2735               *--__result = *__last1;
2736               if (__first1 == __last1)
2737                 return std::copy_backward(__first2, ++__last2, __result);
2738               --__last1;
2739             }
2740           else
2741             {
2742               *--__result = *__last2;
2743               if (__first2 == __last2)
2744                 return std::copy_backward(__first1, ++__last1, __result);
2745               --__last2;
2746             }
2747         }
2748     }
2749
2750   /// This is a helper function for the merge routines.
2751   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2752            typename _BidirectionalIterator3, typename _Compare>
2753     _BidirectionalIterator3
2754     __merge_backward(_BidirectionalIterator1 __first1,
2755                      _BidirectionalIterator1 __last1,
2756                      _BidirectionalIterator2 __first2,
2757                      _BidirectionalIterator2 __last2,
2758                      _BidirectionalIterator3 __result,
2759                      _Compare __comp)
2760     {
2761       if (__first1 == __last1)
2762         return std::copy_backward(__first2, __last2, __result);
2763       if (__first2 == __last2)
2764         return std::copy_backward(__first1, __last1, __result);
2765       --__last1;
2766       --__last2;
2767       while (true)
2768         {
2769           if (__comp(*__last2, *__last1))
2770             {
2771               *--__result = *__last1;
2772               if (__first1 == __last1)
2773                 return std::copy_backward(__first2, ++__last2, __result);
2774               --__last1;
2775             }
2776           else
2777             {
2778               *--__result = *__last2;
2779               if (__first2 == __last2)
2780                 return std::copy_backward(__first1, ++__last1, __result);
2781               --__last2;
2782             }
2783         }
2784     }
2785
2786   /// This is a helper function for the merge routines.
2787   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2788            typename _Distance>
2789     _BidirectionalIterator1
2790     __rotate_adaptive(_BidirectionalIterator1 __first,
2791                       _BidirectionalIterator1 __middle,
2792                       _BidirectionalIterator1 __last,
2793                       _Distance __len1, _Distance __len2,
2794                       _BidirectionalIterator2 __buffer,
2795                       _Distance __buffer_size)
2796     {
2797       _BidirectionalIterator2 __buffer_end;
2798       if (__len1 > __len2 && __len2 <= __buffer_size)
2799         {
2800           __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2801           _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
2802           return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
2803         }
2804       else if (__len1 <= __buffer_size)
2805         {
2806           __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2807           _GLIBCXX_MOVE3(__middle, __last, __first);
2808           return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
2809         }
2810       else
2811         {
2812           std::rotate(__first, __middle, __last);
2813           std::advance(__first, std::distance(__middle, __last));
2814           return __first;
2815         }
2816     }
2817
2818   /// This is a helper function for the merge routines.
2819   template<typename _BidirectionalIterator, typename _Distance,
2820            typename _Pointer>
2821     void
2822     __merge_adaptive(_BidirectionalIterator __first,
2823                      _BidirectionalIterator __middle,
2824                      _BidirectionalIterator __last,
2825                      _Distance __len1, _Distance __len2,
2826                      _Pointer __buffer, _Distance __buffer_size)
2827     {
2828       if (__len1 <= __len2 && __len1 <= __buffer_size)
2829         {
2830           _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2831           _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2832                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2833                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2834                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
2835                                 __first);
2836         }
2837       else if (__len2 <= __buffer_size)
2838         {
2839           _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2840           std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
2841                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2842                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2843                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2844                                 __last);
2845         }
2846       else
2847         {
2848           _BidirectionalIterator __first_cut = __first;
2849           _BidirectionalIterator __second_cut = __middle;
2850           _Distance __len11 = 0;
2851           _Distance __len22 = 0;
2852           if (__len1 > __len2)
2853             {
2854               __len11 = __len1 / 2;
2855               std::advance(__first_cut, __len11);
2856               __second_cut = std::lower_bound(__middle, __last,
2857                                               *__first_cut);
2858               __len22 = std::distance(__middle, __second_cut);
2859             }
2860           else
2861             {
2862               __len22 = __len2 / 2;
2863               std::advance(__second_cut, __len22);
2864               __first_cut = std::upper_bound(__first, __middle,
2865                                              *__second_cut);
2866               __len11 = std::distance(__first, __first_cut);
2867             }
2868           _BidirectionalIterator __new_middle =
2869             std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2870                                    __len1 - __len11, __len22, __buffer,
2871                                    __buffer_size);
2872           std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2873                                 __len22, __buffer, __buffer_size);
2874           std::__merge_adaptive(__new_middle, __second_cut, __last,
2875                                 __len1 - __len11,
2876                                 __len2 - __len22, __buffer, __buffer_size);
2877         }
2878     }
2879
2880   /// This is a helper function for the merge routines.
2881   template<typename _BidirectionalIterator, typename _Distance, 
2882            typename _Pointer, typename _Compare>
2883     void
2884     __merge_adaptive(_BidirectionalIterator __first,
2885                      _BidirectionalIterator __middle,
2886                      _BidirectionalIterator __last,
2887                      _Distance __len1, _Distance __len2,
2888                      _Pointer __buffer, _Distance __buffer_size,
2889                      _Compare __comp)
2890     {
2891       if (__len1 <= __len2 && __len1 <= __buffer_size)
2892         {
2893           _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2894           _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2895                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2896                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2897                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
2898                                 __first, __comp);
2899         }
2900       else if (__len2 <= __buffer_size)
2901         {
2902           _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2903           std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
2904                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
2905                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
2906                                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
2907                                 __last,__comp);
2908         }
2909       else
2910         {
2911           _BidirectionalIterator __first_cut = __first;
2912           _BidirectionalIterator __second_cut = __middle;
2913           _Distance __len11 = 0;
2914           _Distance __len22 = 0;
2915           if (__len1 > __len2)
2916             {
2917               __len11 = __len1 / 2;
2918               std::advance(__first_cut, __len11);
2919               __second_cut = std::lower_bound(__middle, __last, *__first_cut,
2920                                               __comp);
2921               __len22 = std::distance(__middle, __second_cut);
2922             }
2923           else
2924             {
2925               __len22 = __len2 / 2;
2926               std::advance(__second_cut, __len22);
2927               __first_cut = std::upper_bound(__first, __middle, *__second_cut,
2928                                              __comp);
2929               __len11 = std::distance(__first, __first_cut);
2930             }
2931           _BidirectionalIterator __new_middle =
2932             std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2933                                    __len1 - __len11, __len22, __buffer,
2934                                    __buffer_size);
2935           std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2936                                 __len22, __buffer, __buffer_size, __comp);
2937           std::__merge_adaptive(__new_middle, __second_cut, __last,
2938                                 __len1 - __len11,
2939                                 __len2 - __len22, __buffer,
2940                                 __buffer_size, __comp);
2941         }
2942     }
2943
2944   /// This is a helper function for the merge routines.
2945   template<typename _BidirectionalIterator, typename _Distance>
2946     void
2947     __merge_without_buffer(_BidirectionalIterator __first,
2948                            _BidirectionalIterator __middle,
2949                            _BidirectionalIterator __last,
2950                            _Distance __len1, _Distance __len2)
2951     {
2952       if (__len1 == 0 || __len2 == 0)
2953         return;
2954       if (__len1 + __len2 == 2)
2955         {
2956           if (*__middle < *__first)
2957             std::iter_swap(__first, __middle);
2958           return;
2959         }
2960       _BidirectionalIterator __first_cut = __first;
2961       _BidirectionalIterator __second_cut = __middle;
2962       _Distance __len11 = 0;
2963       _Distance __len22 = 0;
2964       if (__len1 > __len2)
2965         {
2966           __len11 = __len1 / 2;
2967           std::advance(__first_cut, __len11);
2968           __second_cut = std::lower_bound(__middle, __last, *__first_cut);
2969           __len22 = std::distance(__middle, __second_cut);
2970         }
2971       else
2972         {
2973           __len22 = __len2 / 2;
2974           std::advance(__second_cut, __len22);
2975           __first_cut = std::upper_bound(__first, __middle, *__second_cut);
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);
2983       std::__merge_without_buffer(__new_middle, __second_cut, __last,
2984                                   __len1 - __len11, __len2 - __len22);
2985     }
2986
2987   /// This is a helper function for the merge routines.
2988   template<typename _BidirectionalIterator, typename _Distance,
2989            typename _Compare>
2990     void
2991     __merge_without_buffer(_BidirectionalIterator __first,
2992                            _BidirectionalIterator __middle,
2993                            _BidirectionalIterator __last,
2994                            _Distance __len1, _Distance __len2,
2995                            _Compare __comp)
2996     {
2997       if (__len1 == 0 || __len2 == 0)
2998         return;
2999       if (__len1 + __len2 == 2)
3000         {
3001           if (__comp(*__middle, *__first))
3002             std::iter_swap(__first, __middle);
3003           return;
3004         }
3005       _BidirectionalIterator __first_cut = __first;
3006       _BidirectionalIterator __second_cut = __middle;
3007       _Distance __len11 = 0;
3008       _Distance __len22 = 0;
3009       if (__len1 > __len2)
3010         {
3011           __len11 = __len1 / 2;
3012           std::advance(__first_cut, __len11);
3013           __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3014                                           __comp);
3015           __len22 = std::distance(__middle, __second_cut);
3016         }
3017       else
3018         {
3019           __len22 = __len2 / 2;
3020           std::advance(__second_cut, __len22);
3021           __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3022                                          __comp);
3023           __len11 = std::distance(__first, __first_cut);
3024         }
3025       std::rotate(__first_cut, __middle, __second_cut);
3026       _BidirectionalIterator __new_middle = __first_cut;
3027       std::advance(__new_middle, std::distance(__middle, __second_cut));
3028       std::__merge_without_buffer(__first, __first_cut, __new_middle,
3029                                   __len11, __len22, __comp);
3030       std::__merge_without_buffer(__new_middle, __second_cut, __last,
3031                                   __len1 - __len11, __len2 - __len22, __comp);
3032     }
3033
3034   /**
3035    *  @brief Merges two sorted ranges in place.
3036    *  @ingroup sorting_algorithms
3037    *  @param  first   An iterator.
3038    *  @param  middle  Another iterator.
3039    *  @param  last    Another iterator.
3040    *  @return  Nothing.
3041    *
3042    *  Merges two sorted and consecutive ranges, [first,middle) and
3043    *  [middle,last), and puts the result in [first,last).  The output will
3044    *  be sorted.  The sort is @e stable, that is, for equivalent
3045    *  elements in the two ranges, elements from the first range will always
3046    *  come before elements from the second.
3047    *
3048    *  If enough additional memory is available, this takes (last-first)-1
3049    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
3050    *  distance(first,last).
3051   */
3052   template<typename _BidirectionalIterator>
3053     void
3054     inplace_merge(_BidirectionalIterator __first,
3055                   _BidirectionalIterator __middle,
3056                   _BidirectionalIterator __last)
3057     {
3058       typedef typename iterator_traits<_BidirectionalIterator>::value_type
3059           _ValueType;
3060       typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3061           _DistanceType;
3062
3063       // concept requirements
3064       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3065             _BidirectionalIterator>)
3066       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3067       __glibcxx_requires_sorted(__first, __middle);
3068       __glibcxx_requires_sorted(__middle, __last);
3069
3070       if (__first == __middle || __middle == __last)
3071         return;
3072
3073       _DistanceType __len1 = std::distance(__first, __middle);
3074       _DistanceType __len2 = std::distance(__middle, __last);
3075
3076       _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3077                                                                   __last);
3078       if (__buf.begin() == 0)
3079         std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
3080       else
3081         std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3082                               __buf.begin(), _DistanceType(__buf.size()));
3083     }
3084
3085   /**
3086    *  @brief Merges two sorted ranges in place.
3087    *  @ingroup sorting_algorithms
3088    *  @param  first   An iterator.
3089    *  @param  middle  Another iterator.
3090    *  @param  last    Another iterator.
3091    *  @param  comp    A functor to use for comparisons.
3092    *  @return  Nothing.
3093    *
3094    *  Merges two sorted and consecutive ranges, [first,middle) and
3095    *  [middle,last), and puts the result in [first,last).  The output will
3096    *  be sorted.  The sort is @e stable, that is, for equivalent
3097    *  elements in the two ranges, elements from the first range will always
3098    *  come before elements from the second.
3099    *
3100    *  If enough additional memory is available, this takes (last-first)-1
3101    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
3102    *  distance(first,last).
3103    *
3104    *  The comparison function should have the same effects on ordering as
3105    *  the function used for the initial sort.
3106   */
3107   template<typename _BidirectionalIterator, typename _Compare>
3108     void
3109     inplace_merge(_BidirectionalIterator __first,
3110                   _BidirectionalIterator __middle,
3111                   _BidirectionalIterator __last,
3112                   _Compare __comp)
3113     {
3114       typedef typename iterator_traits<_BidirectionalIterator>::value_type
3115           _ValueType;
3116       typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3117           _DistanceType;
3118
3119       // concept requirements
3120       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3121             _BidirectionalIterator>)
3122       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3123             _ValueType, _ValueType>)
3124       __glibcxx_requires_sorted_pred(__first, __middle, __comp);
3125       __glibcxx_requires_sorted_pred(__middle, __last, __comp);
3126
3127       if (__first == __middle || __middle == __last)
3128         return;
3129
3130       const _DistanceType __len1 = std::distance(__first, __middle);
3131       const _DistanceType __len2 = std::distance(__middle, __last);
3132
3133       _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3134                                                                   __last);
3135       if (__buf.begin() == 0)
3136         std::__merge_without_buffer(__first, __middle, __last, __len1,
3137                                     __len2, __comp);
3138       else
3139         std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3140                               __buf.begin(), _DistanceType(__buf.size()),
3141                               __comp);
3142     }
3143
3144   template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3145            typename _Distance>
3146     void
3147     __merge_sort_loop(_RandomAccessIterator1 __first,
3148                       _RandomAccessIterator1 __last,
3149                       _RandomAccessIterator2 __result,
3150                       _Distance __step_size)
3151     {
3152       const _Distance __two_step = 2 * __step_size;
3153
3154       while (__last - __first >= __two_step)
3155         {
3156           __result = _GLIBCXX_STD_P::merge(
3157                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3158                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3159                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3160                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
3161                         __result);
3162           __first += __two_step;
3163         }
3164
3165       __step_size = std::min(_Distance(__last - __first), __step_size);
3166       _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3167                             _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3168                                                         __step_size),
3169                             _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3170                                                         __step_size),
3171                             _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
3172                             __result);
3173     }
3174
3175   template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3176            typename _Distance, typename _Compare>
3177     void
3178     __merge_sort_loop(_RandomAccessIterator1 __first,
3179                       _RandomAccessIterator1 __last,
3180                       _RandomAccessIterator2 __result, _Distance __step_size,
3181                       _Compare __comp)
3182     {
3183       const _Distance __two_step = 2 * __step_size;
3184
3185       while (__last - __first >= __two_step)
3186         {
3187           __result = _GLIBCXX_STD_P::merge(
3188                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3189                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3190                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
3191                         _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
3192                         __result, __comp);
3193           __first += __two_step;
3194         }
3195       __step_size = std::min(_Distance(__last - __first), __step_size);
3196
3197       _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
3198                             _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3199                                                         __step_size),
3200                             _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
3201                                                         __step_size),
3202                             _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
3203                             __result, __comp);
3204     }
3205
3206   template<typename _RandomAccessIterator, typename _Distance>
3207     void
3208     __chunk_insertion_sort(_RandomAccessIterator __first,
3209                            _RandomAccessIterator __last,
3210                            _Distance __chunk_size)
3211     {
3212       while (__last - __first >= __chunk_size)
3213         {
3214           std::__insertion_sort(__first, __first + __chunk_size);
3215           __first += __chunk_size;
3216         }
3217       std::__insertion_sort(__first, __last);
3218     }
3219
3220   template<typename _RandomAccessIterator, typename _Distance,
3221            typename _Compare>
3222     void
3223     __chunk_insertion_sort(_RandomAccessIterator __first,
3224                            _RandomAccessIterator __last,
3225                            _Distance __chunk_size, _Compare __comp)
3226     {
3227       while (__last - __first >= __chunk_size)
3228         {
3229           std::__insertion_sort(__first, __first + __chunk_size, __comp);
3230           __first += __chunk_size;
3231         }
3232       std::__insertion_sort(__first, __last, __comp);
3233     }
3234
3235   enum { _S_chunk_size = 7 };
3236
3237   template<typename _RandomAccessIterator, typename _Pointer>
3238     void
3239     __merge_sort_with_buffer(_RandomAccessIterator __first,
3240                              _RandomAccessIterator __last,
3241                              _Pointer __buffer)
3242     {
3243       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3244         _Distance;
3245
3246       const _Distance __len = __last - __first;
3247       const _Pointer __buffer_last = __buffer + __len;
3248
3249       _Distance __step_size = _S_chunk_size;
3250       std::__chunk_insertion_sort(__first, __last, __step_size);
3251
3252       while (__step_size < __len)
3253         {
3254           std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3255           __step_size *= 2;
3256           std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3257           __step_size *= 2;
3258         }
3259     }
3260
3261   template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3262     void
3263     __merge_sort_with_buffer(_RandomAccessIterator __first,
3264                              _RandomAccessIterator __last,
3265                              _Pointer __buffer, _Compare __comp)
3266     {
3267       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3268         _Distance;
3269
3270       const _Distance __len = __last - __first;
3271       const _Pointer __buffer_last = __buffer + __len;
3272
3273       _Distance __step_size = _S_chunk_size;
3274       std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3275
3276       while (__step_size < __len)
3277         {
3278           std::__merge_sort_loop(__first, __last, __buffer,
3279                                  __step_size, __comp);
3280           __step_size *= 2;
3281           std::__merge_sort_loop(__buffer, __buffer_last, __first,
3282                                  __step_size, __comp);
3283           __step_size *= 2;
3284         }
3285     }
3286
3287   template<typename _RandomAccessIterator, typename _Pointer,
3288            typename _Distance>
3289     void
3290     __stable_sort_adaptive(_RandomAccessIterator __first,
3291                            _RandomAccessIterator __last,
3292                            _Pointer __buffer, _Distance __buffer_size)
3293     {
3294       const _Distance __len = (__last - __first + 1) / 2;
3295       const _RandomAccessIterator __middle = __first + __len;
3296       if (__len > __buffer_size)
3297         {
3298           std::__stable_sort_adaptive(__first, __middle,
3299                                       __buffer, __buffer_size);
3300           std::__stable_sort_adaptive(__middle, __last,
3301                                       __buffer, __buffer_size);
3302         }
3303       else
3304         {
3305           std::__merge_sort_with_buffer(__first, __middle, __buffer);
3306           std::__merge_sort_with_buffer(__middle, __last, __buffer);
3307         }
3308       std::__merge_adaptive(__first, __middle, __last,
3309                             _Distance(__middle - __first),
3310                             _Distance(__last - __middle),
3311                             __buffer, __buffer_size);
3312     }
3313
3314   template<typename _RandomAccessIterator, typename _Pointer,
3315            typename _Distance, typename _Compare>
3316     void
3317     __stable_sort_adaptive(_RandomAccessIterator __first,
3318                            _RandomAccessIterator __last,
3319                            _Pointer __buffer, _Distance __buffer_size,
3320                            _Compare __comp)
3321     {
3322       const _Distance __len = (__last - __first + 1) / 2;
3323       const _RandomAccessIterator __middle = __first + __len;
3324       if (__len > __buffer_size)
3325         {
3326           std::__stable_sort_adaptive(__first, __middle, __buffer,
3327                                       __buffer_size, __comp);
3328           std::__stable_sort_adaptive(__middle, __last, __buffer,
3329                                       __buffer_size, __comp);
3330         }
3331       else
3332         {
3333           std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3334           std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3335         }
3336       std::__merge_adaptive(__first, __middle, __last,
3337                             _Distance(__middle - __first),
3338                             _Distance(__last - __middle),
3339                             __buffer, __buffer_size,
3340                             __comp);
3341     }
3342
3343   /// This is a helper function for the stable sorting routines.
3344   template<typename _RandomAccessIterator>
3345     void
3346     __inplace_stable_sort(_RandomAccessIterator __first,
3347                           _RandomAccessIterator __last)
3348     {
3349       if (__last - __first < 15)
3350         {
3351           std::__insertion_sort(__first, __last);
3352           return;
3353         }
3354       _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3355       std::__inplace_stable_sort(__first, __middle);
3356       std::__inplace_stable_sort(__middle, __last);
3357       std::__merge_without_buffer(__first, __middle, __last,
3358                                   __middle - __first,
3359                                   __last - __middle);
3360     }
3361
3362   /// This is a helper function for the stable sorting routines.
3363   template<typename _RandomAccessIterator, typename _Compare>
3364     void
3365     __inplace_stable_sort(_RandomAccessIterator __first,
3366                           _RandomAccessIterator __last, _Compare __comp)
3367     {
3368       if (__last - __first < 15)
3369         {
3370           std::__insertion_sort(__first, __last, __comp);
3371           return;
3372         }
3373       _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3374       std::__inplace_stable_sort(__first, __middle, __comp);
3375       std::__inplace_stable_sort(__middle, __last, __comp);
3376       std::__merge_without_buffer(__first, __middle, __last,
3377                                   __middle - __first,
3378                                   __last - __middle,
3379                                   __comp);
3380     }
3381
3382   // stable_sort
3383
3384   // Set algorithms: includes, set_union, set_intersection, set_difference,
3385   // set_symmetric_difference.  All of these algorithms have the precondition
3386   // that their input ranges are sorted and the postcondition that their output
3387   // ranges are sorted.
3388
3389   /**
3390    *  @brief Determines whether all elements of a sequence exists in a range.
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    *  @return  True if each element in [first2,last2) is contained in order
3396    *  within [first1,last1).  False otherwise.
3397    *  @ingroup set_algorithms
3398    *
3399    *  This operation expects both [first1,last1) and [first2,last2) to be
3400    *  sorted.  Searches for the presence of each element in [first2,last2)
3401    *  within [first1,last1).  The iterators over each range only move forward,
3402    *  so this is a linear algorithm.  If an element in [first2,last2) is not
3403    *  found before the search iterator reaches @a last2, false is returned.
3404   */
3405   template<typename _InputIterator1, typename _InputIterator2>
3406     bool
3407     includes(_InputIterator1 __first1, _InputIterator1 __last1,
3408              _InputIterator2 __first2, _InputIterator2 __last2)
3409     {
3410       typedef typename iterator_traits<_InputIterator1>::value_type
3411         _ValueType1;
3412       typedef typename iterator_traits<_InputIterator2>::value_type
3413         _ValueType2;
3414
3415       // concept requirements
3416       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3417       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3418       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
3419       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
3420       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
3421       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
3422
3423       while (__first1 != __last1 && __first2 != __last2)
3424         if (*__first2 < *__first1)
3425           return false;
3426         else if(*__first1 < *__first2)
3427           ++__first1;
3428         else
3429           ++__first1, ++__first2;
3430
3431       return __first2 == __last2;
3432     }
3433
3434   /**
3435    *  @brief Determines whether all elements of a sequence exists in a range
3436    *  using comparison.
3437    *  @ingroup set_algorithms
3438    *  @param  first1  Start of search range.
3439    *  @param  last1   End of search range.
3440    *  @param  first2  Start of sequence
3441    *  @param  last2   End of sequence.
3442    *  @param  comp    Comparison function to use.
3443    *  @return  True if each element in [first2,last2) is contained in order
3444    *  within [first1,last1) according to comp.  False otherwise.
3445    *  @ingroup set_algorithms
3446    *
3447    *  This operation expects both [first1,last1) and [first2,last2) to be
3448    *  sorted.  Searches for the presence of each element in [first2,last2)
3449    *  within [first1,last1), using comp to decide.  The iterators over each
3450    *  range only move forward, so this is a linear algorithm.  If an element
3451    *  in [first2,last2) is not found before the search iterator reaches @a
3452    *  last2, false is returned.
3453   */
3454   template<typename _InputIterator1, typename _InputIterator2,
3455            typename _Compare>
3456     bool
3457     includes(_InputIterator1 __first1, _InputIterator1 __last1,
3458              _InputIterator2 __first2, _InputIterator2 __last2,
3459              _Compare __comp)
3460     {
3461       typedef typename iterator_traits<_InputIterator1>::value_type
3462         _ValueType1;
3463       typedef typename iterator_traits<_InputIterator2>::value_type
3464         _ValueType2;
3465
3466       // concept requirements
3467       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3468       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3469       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3470                                   _ValueType1, _ValueType2>)
3471       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3472                                   _ValueType2, _ValueType1>)
3473       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
3474       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
3475
3476       while (__first1 != __last1 && __first2 != __last2)
3477         if (__comp(*__first2, *__first1))
3478           return false;
3479         else if(__comp(*__first1, *__first2))
3480           ++__first1;
3481         else
3482           ++__first1, ++__first2;
3483
3484       return __first2 == __last2;
3485     }
3486
3487   // nth_element
3488   // merge
3489   // set_difference
3490   // set_intersection
3491   // set_union
3492   // stable_sort
3493   // set_symmetric_difference
3494   // min_element
3495   // max_element
3496
3497   /**
3498    *  @brief  Permute range into the next @a dictionary ordering.
3499    *  @ingroup sorting_algorithms
3500    *  @param  first  Start of range.
3501    *  @param  last   End of range.
3502    *  @return  False if wrapped to first permutation, true otherwise.
3503    *
3504    *  Treats all permutations of the range as a set of @a dictionary sorted
3505    *  sequences.  Permutes the current sequence into the next one of this set.
3506    *  Returns true if there are more sequences to generate.  If the sequence
3507    *  is the largest of the set, the smallest is generated and false returned.
3508   */
3509   template<typename _BidirectionalIterator>
3510     bool
3511     next_permutation(_BidirectionalIterator __first,
3512                      _BidirectionalIterator __last)
3513     {
3514       // concept requirements
3515       __glibcxx_function_requires(_BidirectionalIteratorConcept<
3516                                   _BidirectionalIterator>)
3517       __glibcxx_function_requires(_LessThanComparableConcept<
3518             typename iterator_traits<_BidirectionalIterator>::value_type>)
3519       __glibcxx_requires_valid_range(__first, __last);
3520
3521       if (__first == __last)
3522         return false;
3523       _BidirectionalIterator __i = __first;
3524       ++__i;
3525       if (__i == __last)
3526         return false;
3527       __i = __last;
3528       --__i;
3529
3530       for(;;)
3531         {
3532           _BidirectionalIterator __ii = __i;
3533           --__i;
3534           if (*__i < *__ii)
3535             {
3536               _BidirectionalIterator __j = __last;
3537               while (!(*__i < *--__j))
3538                 {}
3539               std::iter_swap(__i, __j);
3540               std::reverse(__ii, __last);
3541               return true;
3542             }
3543           if (__i == __first)
3544             {
3545               std::reverse(__first, __last);
3546               return false;
3547             }
3548         }
3549     }
3550
3551   /**
3552    *  @brief  Permute range into the next @a dictionary ordering using
3553    *          comparison functor.
3554    *  @ingroup sorting_algorithms
3555    *  @param  first  Start of range.
3556    *  @param  last   End of range.
3557    *  @param  comp   A comparison functor.
3558    *  @return  False if wrapped to first permutation, true otherwise.
3559    *
3560    *  Treats all permutations of the range [first,last) as a set of
3561    *  @a dictionary sorted sequences ordered by @a comp.  Permutes the current
3562    *  sequence into the next one of this set.  Returns true if there are more
3563    *  sequences to generate.  If the sequence is the largest of the set, the
3564    *  smallest is generated and false returned.
3565   */
3566   template<typename _BidirectionalIterator, typename _Compare>
3567     bool
3568     next_permutation(_BidirectionalIterator __first,
3569                      _BidirectionalIterator __last, _Compare __comp)
3570     {
3571       // concept requirements
3572       __glibcxx_function_requires(_BidirectionalIteratorConcept<
3573                                   _BidirectionalIterator>)
3574       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3575             typename iterator_traits<_BidirectionalIterator>::value_type,
3576             typename iterator_traits<_BidirectionalIterator>::value_type>)
3577       __glibcxx_requires_valid_range(__first, __last);
3578
3579       if (__first == __last)
3580         return false;
3581       _BidirectionalIterator __i = __first;
3582       ++__i;
3583       if (__i == __last)
3584         return false;
3585       __i = __last;
3586       --__i;
3587
3588       for(;;)
3589         {
3590           _BidirectionalIterator __ii = __i;
3591           --__i;
3592           if (__comp(*__i, *__ii))
3593             {
3594               _BidirectionalIterator __j = __last;
3595               while (!bool(__comp(*__i, *--__j)))
3596                 {}
3597               std::iter_swap(__i, __j);
3598               std::reverse(__ii, __last);
3599               return true;
3600             }
3601           if (__i == __first)
3602             {
3603               std::reverse(__first, __last);
3604               return false;
3605             }
3606         }
3607     }
3608
3609   /**
3610    *  @brief  Permute range into the previous @a dictionary ordering.
3611    *  @ingroup sorting_algorithms
3612    *  @param  first  Start of range.
3613    *  @param  last   End of range.
3614    *  @return  False if wrapped to last permutation, true otherwise.
3615    *
3616    *  Treats all permutations of the range as a set of @a dictionary sorted
3617    *  sequences.  Permutes the current sequence into the previous one of this
3618    *  set.  Returns true if there are more sequences to generate.  If the
3619    *  sequence is the smallest of the set, the largest is generated and false
3620    *  returned.
3621   */
3622   template<typename _BidirectionalIterator>
3623     bool
3624     prev_permutation(_BidirectionalIterator __first,
3625                      _BidirectionalIterator __last)
3626     {
3627       // concept requirements
3628       __glibcxx_function_requires(_BidirectionalIteratorConcept<
3629                                   _BidirectionalIterator>)
3630       __glibcxx_function_requires(_LessThanComparableConcept<
3631             typename iterator_traits<_BidirectionalIterator>::value_type>)
3632       __glibcxx_requires_valid_range(__first, __last);
3633
3634       if (__first == __last)
3635         return false;
3636       _BidirectionalIterator __i = __first;
3637       ++__i;
3638       if (__i == __last)
3639         return false;
3640       __i = __last;
3641       --__i;
3642
3643       for(;;)
3644         {
3645           _BidirectionalIterator __ii = __i;
3646           --__i;
3647           if (*__ii < *__i)
3648             {
3649               _BidirectionalIterator __j = __last;
3650               while (!(*--__j < *__i))
3651                 {}
3652               std::iter_swap(__i, __j);
3653               std::reverse(__ii, __last);
3654               return true;
3655             }
3656           if (__i == __first)
3657             {
3658               std::reverse(__first, __last);
3659               return false;
3660             }
3661         }
3662     }
3663
3664   /**
3665    *  @brief  Permute range into the previous @a dictionary ordering using
3666    *          comparison functor.
3667    *  @ingroup sorting_algorithms
3668    *  @param  first  Start of range.
3669    *  @param  last   End of range.
3670    *  @param  comp   A comparison functor.
3671    *  @return  False if wrapped to last permutation, true otherwise.
3672    *
3673    *  Treats all permutations of the range [first,last) as a set of
3674    *  @a dictionary sorted sequences ordered by @a comp.  Permutes the current
3675    *  sequence into the previous one of this set.  Returns true if there are
3676    *  more sequences to generate.  If the sequence is the smallest of the set,
3677    *  the largest is generated and false returned.
3678   */
3679   template<typename _BidirectionalIterator, typename _Compare>
3680     bool
3681     prev_permutation(_BidirectionalIterator __first,
3682                      _BidirectionalIterator __last, _Compare __comp)
3683     {
3684       // concept requirements
3685       __glibcxx_function_requires(_BidirectionalIteratorConcept<
3686                                   _BidirectionalIterator>)
3687       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3688             typename iterator_traits<_BidirectionalIterator>::value_type,
3689             typename iterator_traits<_BidirectionalIterator>::value_type>)
3690       __glibcxx_requires_valid_range(__first, __last);
3691
3692       if (__first == __last)
3693         return false;
3694       _BidirectionalIterator __i = __first;
3695       ++__i;
3696       if (__i == __last)
3697         return false;
3698       __i = __last;
3699       --__i;
3700
3701       for(;;)
3702         {
3703           _BidirectionalIterator __ii = __i;
3704           --__i;
3705           if (__comp(*__ii, *__i))
3706             {
3707               _BidirectionalIterator __j = __last;
3708               while (!bool(__comp(*--__j, *__i)))
3709                 {}
3710               std::iter_swap(__i, __j);
3711               std::reverse(__ii, __last);
3712               return true;
3713             }
3714           if (__i == __first)
3715             {
3716               std::reverse(__first, __last);
3717               return false;
3718             }
3719         }
3720     }
3721
3722   // replace
3723   // replace_if
3724
3725   /**
3726    *  @brief Copy a sequence, replacing each element of one value with another
3727    *         value.
3728    *  @param  first      An input iterator.
3729    *  @param  last       An input iterator.
3730    *  @param  result     An output iterator.
3731    *  @param  old_value  The value to be replaced.
3732    *  @param  new_value  The replacement value.
3733    *  @return   The end of the output sequence, @p result+(last-first).
3734    *
3735    *  Copies each element in the input range @p [first,last) to the
3736    *  output range @p [result,result+(last-first)) replacing elements
3737    *  equal to @p old_value with @p new_value.
3738   */
3739   template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3740     _OutputIterator
3741     replace_copy(_InputIterator __first, _InputIterator __last,
3742                  _OutputIterator __result,
3743                  const _Tp& __old_value, const _Tp& __new_value)
3744     {
3745       // concept requirements
3746       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3747       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3748             typename iterator_traits<_InputIterator>::value_type>)
3749       __glibcxx_function_requires(_EqualOpConcept<
3750             typename iterator_traits<_InputIterator>::value_type, _Tp>)
3751       __glibcxx_requires_valid_range(__first, __last);
3752
3753       for (; __first != __last; ++__first, ++__result)
3754         if (*__first == __old_value)
3755           *__result = __new_value;
3756         else
3757           *__result = *__first;
3758       return __result;
3759     }
3760
3761   /**
3762    *  @brief Copy a sequence, replacing each value for which a predicate
3763    *         returns true with another value.
3764    *  @ingroup mutating_algorithms
3765    *  @param  first      An input iterator.
3766    *  @param  last       An input iterator.
3767    *  @param  result     An output iterator.
3768    *  @param  pred       A predicate.
3769    *  @param  new_value  The replacement value.
3770    *  @return   The end of the output sequence, @p result+(last-first).
3771    *
3772    *  Copies each element in the range @p [first,last) to the range
3773    *  @p [result,result+(last-first)) replacing elements for which
3774    *  @p pred returns true with @p new_value.
3775   */
3776   template<typename _InputIterator, typename _OutputIterator,
3777            typename _Predicate, typename _Tp>
3778     _OutputIterator
3779     replace_copy_if(_InputIterator __first, _InputIterator __last,
3780                     _OutputIterator __result,
3781                     _Predicate __pred, const _Tp& __new_value)
3782     {
3783       // concept requirements
3784       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3785       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3786             typename iterator_traits<_InputIterator>::value_type>)
3787       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3788             typename iterator_traits<_InputIterator>::value_type>)
3789       __glibcxx_requires_valid_range(__first, __last);
3790
3791       for (; __first != __last; ++__first, ++__result)
3792         if (__pred(*__first))
3793           *__result = __new_value;
3794         else
3795           *__result = *__first;
3796       return __result;
3797     }
3798
3799 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3800   /**
3801    *  @brief  Determines whether the elements of a sequence are sorted.
3802    *  @ingroup sorting_algorithms
3803    *  @param  first   An iterator.
3804    *  @param  last    Another iterator.
3805    *  @return  True if the elements are sorted, false otherwise.
3806   */
3807   template<typename _ForwardIterator>
3808     inline bool
3809     is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3810     { return std::is_sorted_until(__first, __last) == __last; }
3811
3812   /**
3813    *  @brief  Determines whether the elements of a sequence are sorted
3814    *          according to a comparison functor.
3815    *  @ingroup sorting_algorithms
3816    *  @param  first   An iterator.
3817    *  @param  last    Another iterator.
3818    *  @param  comp    A comparison functor.
3819    *  @return  True if the elements are sorted, false otherwise.
3820   */
3821   template<typename _ForwardIterator, typename _Compare>
3822     inline bool
3823     is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3824               _Compare __comp)
3825     { return std::is_sorted_until(__first, __last, __comp) == __last; }
3826
3827   /**
3828    *  @brief  Determines the end of a sorted sequence.
3829    *  @ingroup sorting_algorithms
3830    *  @param  first   An iterator.
3831    *  @param  last    Another iterator.
3832    *  @return  An iterator pointing to the last iterator i in [first, last)
3833    *           for which the range [first, i) is sorted.
3834   */
3835   template<typename _ForwardIterator>
3836     _ForwardIterator
3837     is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3838     {
3839       // concept requirements
3840       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3841       __glibcxx_function_requires(_LessThanComparableConcept<
3842             typename iterator_traits<_ForwardIterator>::value_type>)
3843       __glibcxx_requires_valid_range(__first, __last);
3844
3845       if (__first == __last)
3846         return __last;
3847
3848       _ForwardIterator __next = __first;
3849       for (++__next; __next != __last; __first = __next, ++__next)
3850         if (*__next < *__first)
3851           return __next;
3852       return __next;
3853     }
3854
3855   /**
3856    *  @brief  Determines the end of a sorted sequence using comparison functor.
3857    *  @ingroup sorting_algorithms
3858    *  @param  first   An iterator.
3859    *  @param  last    Another iterator.
3860    *  @param  comp    A comparison functor.
3861    *  @return  An iterator pointing to the last iterator i in [first, last)
3862    *           for which the range [first, i) is sorted.
3863   */
3864   template<typename _ForwardIterator, typename _Compare>
3865     _ForwardIterator
3866     is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3867                     _Compare __comp)
3868     {
3869       // concept requirements
3870       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3871       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3872             typename iterator_traits<_ForwardIterator>::value_type,
3873             typename iterator_traits<_ForwardIterator>::value_type>)
3874       __glibcxx_requires_valid_range(__first, __last);
3875
3876       if (__first == __last)
3877         return __last;
3878
3879       _ForwardIterator __next = __first;
3880       for (++__next; __next != __last; __first = __next, ++__next)
3881         if (__comp(*__next, *__first))
3882           return __next;
3883       return __next;
3884     }
3885
3886   /**
3887    *  @brief  Determines min and max at once as an ordered pair.
3888    *  @ingroup sorting_algorithms
3889    *  @param  a  A thing of arbitrary type.
3890    *  @param  b  Another thing of arbitrary type.
3891    *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3892   */
3893   template<typename _Tp>
3894     inline pair<const _Tp&, const _Tp&>
3895     minmax(const _Tp& __a, const _Tp& __b)
3896     {
3897       // concept requirements
3898       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3899
3900       return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
3901                        : pair<const _Tp&, const _Tp&>(__a, __b);
3902     }
3903
3904   /**
3905    *  @brief  Determines min and max at once as an ordered pair.
3906    *  @ingroup sorting_algorithms
3907    *  @param  a  A thing of arbitrary type.
3908    *  @param  b  Another thing of arbitrary type.
3909    *  @param  comp  A @link comparison_functor comparison functor@endlink.
3910    *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3911   */
3912   template<typename _Tp, typename _Compare>
3913     inline pair<const _Tp&, const _Tp&>
3914     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
3915     {
3916       return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
3917                               : pair<const _Tp&, const _Tp&>(__a, __b);
3918     }
3919
3920   /**
3921    *  @brief  Return a pair of iterators pointing to the minimum and maximum
3922    *          elements in a range.
3923    *  @ingroup sorting_algorithms
3924    *  @param  first  Start of range.
3925    *  @param  last   End of range.
3926    *  @return  make_pair(m, M), where m is the first iterator i in 
3927    *           [first, last) such that no other element in the range is
3928    *           smaller, and where M is the last iterator i in [first, last)
3929    *           such that no other element in the range is larger.
3930   */
3931   template<typename _ForwardIterator>
3932     pair<_ForwardIterator, _ForwardIterator>
3933     minmax_element(_ForwardIterator __first, _ForwardIterator __last)
3934     {
3935       // concept requirements
3936       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3937       __glibcxx_function_requires(_LessThanComparableConcept<
3938             typename iterator_traits<_ForwardIterator>::value_type>)
3939       __glibcxx_requires_valid_range(__first, __last);
3940
3941       _ForwardIterator __next = __first;
3942       if (__first == __last
3943           || ++__next == __last)
3944         return std::make_pair(__first, __first);
3945
3946       _ForwardIterator __min, __max;
3947       if (*__next < *__first)
3948         {
3949           __min = __next;
3950           __max = __first;
3951         }
3952       else
3953         {
3954           __min = __first;
3955           __max = __next;
3956         }
3957
3958       __first = __next;
3959       ++__first;
3960
3961       while (__first != __last)
3962         {
3963           __next = __first;
3964           if (++__next == __last)
3965             {
3966               if (*__first < *__min)
3967                 __min = __first;
3968               else if (!(*__first < *__max))
3969                 __max = __first;
3970               break;
3971             }
3972
3973           if (*__next < *__first)
3974             {
3975               if (*__next < *__min)
3976                 __min = __next;
3977               if (!(*__first < *__max))
3978                 __max = __first;
3979             }
3980           else
3981             {
3982               if (*__first < *__min)
3983                 __min = __first;
3984               if (!(*__next < *__max))
3985                 __max = __next;
3986             }
3987
3988           __first = __next;
3989           ++__first;
3990         }
3991
3992       return std::make_pair(__min, __max);
3993     }
3994
3995   /**
3996    *  @brief  Return a pair of iterators pointing to the minimum and maximum
3997    *          elements in a range.
3998    *  @ingroup sorting_algorithms
3999    *  @param  first  Start of range.
4000    *  @param  last   End of range.
4001    *  @param  comp   Comparison functor.
4002    *  @return  make_pair(m, M), where m is the first iterator i in 
4003    *           [first, last) such that no other element in the range is
4004    *           smaller, and where M is the last iterator i in [first, last)
4005    *           such that no other element in the range is larger.
4006   */
4007   template<typename _ForwardIterator, typename _Compare>
4008     pair<_ForwardIterator, _ForwardIterator>
4009     minmax_element(_ForwardIterator __first, _ForwardIterator __last,
4010                    _Compare __comp)
4011     {
4012       // concept requirements
4013       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4014       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4015             typename iterator_traits<_ForwardIterator>::value_type,
4016             typename iterator_traits<_ForwardIterator>::value_type>)
4017       __glibcxx_requires_valid_range(__first, __last);
4018
4019       _ForwardIterator __next = __first;
4020       if (__first == __last
4021           || ++__next == __last)
4022         return std::make_pair(__first, __first);
4023
4024       _ForwardIterator __min, __max;
4025       if (__comp(*__next, *__first))
4026         {
4027           __min = __next;
4028           __max = __first;
4029         }
4030       else
4031         {
4032           __min = __first;
4033           __max = __next;
4034         }
4035
4036       __first = __next;
4037       ++__first;
4038
4039       while (__first != __last)
4040         {
4041           __next = __first;
4042           if (++__next == __last)
4043             {
4044               if (__comp(*__first, *__min))
4045                 __min = __first;
4046               else if (!__comp(*__first, *__max))
4047                 __max = __first;
4048               break;
4049             }
4050
4051           if (__comp(*__next, *__first))
4052             {
4053               if (__comp(*__next, *__min))
4054                 __min = __next;
4055               if (!__comp(*__first, *__max))
4056                 __max = __first;
4057             }
4058           else
4059             {
4060               if (__comp(*__first, *__min))
4061                 __min = __first;
4062               if (!__comp(*__next, *__max))
4063                 __max = __next;
4064             }
4065
4066           __first = __next;
4067           ++__first;
4068         }
4069
4070       return std::make_pair(__min, __max);
4071     }
4072
4073   // N2722 + DR 915.
4074   template<typename _Tp>
4075     inline _Tp
4076     min(initializer_list<_Tp> __l)
4077     { return *std::min_element(__l.begin(), __l.end()); }
4078
4079   template<typename _Tp, typename _Compare>
4080     inline _Tp
4081     min(initializer_list<_Tp> __l, _Compare __comp)
4082     { return *std::min_element(__l.begin(), __l.end(), __comp); }
4083
4084   template<typename _Tp>
4085     inline _Tp
4086     max(initializer_list<_Tp> __l)
4087     { return *std::max_element(__l.begin(), __l.end()); }
4088
4089   template<typename _Tp, typename _Compare>
4090     inline _Tp
4091     max(initializer_list<_Tp> __l, _Compare __comp)
4092     { return *std::max_element(__l.begin(), __l.end(), __comp); }
4093
4094   template<typename _Tp>
4095     inline pair<_Tp, _Tp>
4096     minmax(initializer_list<_Tp> __l)
4097     {
4098       pair<const _Tp*, const _Tp*> __p =
4099         std::minmax_element(__l.begin(), __l.end());
4100       return std::make_pair(*__p.first, *__p.second);
4101     }
4102
4103   template<typename _Tp, typename _Compare>
4104     inline pair<_Tp, _Tp>
4105     minmax(initializer_list<_Tp> __l, _Compare __comp)
4106     {
4107       pair<const _Tp*, const _Tp*> __p =
4108         std::minmax_element(__l.begin(), __l.end(), __comp);
4109       return std::make_pair(*__p.first, *__p.second);
4110     }
4111
4112 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
4113   /**
4114    *  @brief Shuffle the elements of a sequence using a uniform random
4115    *         number generator.
4116    *  @ingroup mutating_algorithms
4117    *  @param  first   A forward iterator.
4118    *  @param  last    A forward iterator.
4119    *  @param  g       A UniformRandomNumberGenerator (26.5.1.3).
4120    *  @return  Nothing.
4121    *
4122    *  Reorders the elements in the range @p [first,last) using @p g to
4123    *  provide random numbers.
4124   */
4125   template<typename _RandomAccessIterator,
4126            typename _UniformRandomNumberGenerator>
4127     void
4128     shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4129             _UniformRandomNumberGenerator&& __g)
4130     {
4131       // concept requirements
4132       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4133             _RandomAccessIterator>)
4134       __glibcxx_requires_valid_range(__first, __last);
4135
4136       if (__first == __last)
4137         return;
4138
4139       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4140         _DistanceType;
4141
4142       typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
4143       typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
4144       typedef typename __distr_type::param_type __p_type;
4145       __distr_type __d;
4146
4147       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4148         std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
4149     }
4150 #endif
4151
4152 #endif // __GXX_EXPERIMENTAL_CXX0X__
4153
4154 _GLIBCXX_END_NAMESPACE
4155
4156 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
4157
4158   /**
4159    *  @brief Apply a function to every element of a sequence.
4160    *  @ingroup non_mutating_algorithms
4161    *  @param  first  An input iterator.
4162    *  @param  last   An input iterator.
4163    *  @param  f      A unary function object.
4164    *  @return   @p f (std::move(@p f) in C++0x).
4165    *
4166    *  Applies the function object @p f to each element in the range
4167    *  @p [first,last).  @p f must not modify the order of the sequence.
4168    *  If @p f has a return value it is ignored.
4169   */
4170   template<typename _InputIterator, typename _Function>
4171     _Function
4172     for_each(_InputIterator __first, _InputIterator __last, _Function __f)
4173     {
4174       // concept requirements
4175       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4176       __glibcxx_requires_valid_range(__first, __last);
4177       for (; __first != __last; ++__first)
4178         __f(*__first);
4179       return _GLIBCXX_MOVE(__f);
4180     }
4181
4182   /**
4183    *  @brief Find the first occurrence of a value in a sequence.
4184    *  @ingroup non_mutating_algorithms
4185    *  @param  first  An input iterator.
4186    *  @param  last   An input iterator.
4187    *  @param  val    The value to find.
4188    *  @return   The first iterator @c i in the range @p [first,last)
4189    *  such that @c *i == @p val, or @p last if no such iterator exists.
4190   */
4191   template<typename _InputIterator, typename _Tp>
4192     inline _InputIterator
4193     find(_InputIterator __first, _InputIterator __last,
4194          const _Tp& __val)
4195     {
4196       // concept requirements
4197       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4198       __glibcxx_function_requires(_EqualOpConcept<
4199                 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4200       __glibcxx_requires_valid_range(__first, __last);
4201       return std::__find(__first, __last, __val,
4202                          std::__iterator_category(__first));
4203     }
4204
4205   /**
4206    *  @brief Find the first element in a sequence for which a
4207    *         predicate is true.
4208    *  @ingroup non_mutating_algorithms
4209    *  @param  first  An input iterator.
4210    *  @param  last   An input iterator.
4211    *  @param  pred   A predicate.
4212    *  @return   The first iterator @c i in the range @p [first,last)
4213    *  such that @p pred(*i) is true, or @p last if no such iterator exists.
4214   */
4215   template<typename _InputIterator, typename _Predicate>
4216     inline _InputIterator
4217     find_if(_InputIterator __first, _InputIterator __last,
4218             _Predicate __pred)
4219     {
4220       // concept requirements
4221       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4222       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4223               typename iterator_traits<_InputIterator>::value_type>)
4224       __glibcxx_requires_valid_range(__first, __last);
4225       return std::__find_if(__first, __last, __pred,
4226                             std::__iterator_category(__first));
4227     }
4228
4229   /**
4230    *  @brief  Find element from a set in a sequence.
4231    *  @ingroup non_mutating_algorithms
4232    *  @param  first1  Start of range to search.
4233    *  @param  last1   End of range to search.
4234    *  @param  first2  Start of match candidates.
4235    *  @param  last2   End of match candidates.
4236    *  @return   The first iterator @c i in the range
4237    *  @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4238    *  iterator in [first2,last2), or @p last1 if no such iterator exists.
4239    *
4240    *  Searches the range @p [first1,last1) for an element that is equal to
4241    *  some element in the range [first2,last2).  If found, returns an iterator
4242    *  in the range [first1,last1), otherwise returns @p last1.
4243   */
4244   template<typename _InputIterator, typename _ForwardIterator>
4245     _InputIterator
4246     find_first_of(_InputIterator __first1, _InputIterator __last1,
4247                   _ForwardIterator __first2, _ForwardIterator __last2)
4248     {
4249       // concept requirements
4250       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4251       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4252       __glibcxx_function_requires(_EqualOpConcept<
4253             typename iterator_traits<_InputIterator>::value_type,
4254             typename iterator_traits<_ForwardIterator>::value_type>)
4255       __glibcxx_requires_valid_range(__first1, __last1);
4256       __glibcxx_requires_valid_range(__first2, __last2);
4257
4258       for (; __first1 != __last1; ++__first1)
4259         for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4260           if (*__first1 == *__iter)
4261             return __first1;
4262       return __last1;
4263     }
4264
4265   /**
4266    *  @brief  Find element from a set in a sequence using a predicate.
4267    *  @ingroup non_mutating_algorithms
4268    *  @param  first1  Start of range to search.
4269    *  @param  last1   End of range to search.
4270    *  @param  first2  Start of match candidates.
4271    *  @param  last2   End of match candidates.
4272    *  @param  comp    Predicate to use.
4273    *  @return   The first iterator @c i in the range
4274    *  @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4275    *  iterator in [first2,last2), or @p last1 if no such iterator exists.
4276    *
4277
4278    *  Searches the range @p [first1,last1) for an element that is
4279    *  equal to some element in the range [first2,last2).  If found,
4280    *  returns an iterator in the range [first1,last1), otherwise
4281    *  returns @p last1.
4282   */
4283   template<typename _InputIterator, typename _ForwardIterator,
4284            typename _BinaryPredicate>
4285     _InputIterator
4286     find_first_of(_InputIterator __first1, _InputIterator __last1,
4287                   _ForwardIterator __first2, _ForwardIterator __last2,
4288                   _BinaryPredicate __comp)
4289     {
4290       // concept requirements
4291       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4292       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4293       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4294             typename iterator_traits<_InputIterator>::value_type,
4295             typename iterator_traits<_ForwardIterator>::value_type>)
4296       __glibcxx_requires_valid_range(__first1, __last1);
4297       __glibcxx_requires_valid_range(__first2, __last2);
4298
4299       for (; __first1 != __last1; ++__first1)
4300         for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4301           if (__comp(*__first1, *__iter))
4302             return __first1;
4303       return __last1;
4304     }
4305
4306   /**
4307    *  @brief Find two adjacent values in a sequence that are equal.
4308    *  @ingroup non_mutating_algorithms
4309    *  @param  first  A forward iterator.
4310    *  @param  last   A forward iterator.
4311    *  @return   The first iterator @c i such that @c i and @c i+1 are both
4312    *  valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4313    *  or @p last if no such iterator exists.
4314   */
4315   template<typename _ForwardIterator>
4316     _ForwardIterator
4317     adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
4318     {
4319       // concept requirements
4320       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4321       __glibcxx_function_requires(_EqualityComparableConcept<
4322             typename iterator_traits<_ForwardIterator>::value_type>)
4323       __glibcxx_requires_valid_range(__first, __last);
4324       if (__first == __last)
4325         return __last;
4326       _ForwardIterator __next = __first;
4327       while(++__next != __last)
4328         {
4329           if (*__first == *__next)
4330             return __first;
4331           __first = __next;
4332         }
4333       return __last;
4334     }
4335
4336   /**
4337    *  @brief Find two adjacent values in a sequence using a predicate.
4338    *  @ingroup non_mutating_algorithms
4339    *  @param  first         A forward iterator.
4340    *  @param  last          A forward iterator.
4341    *  @param  binary_pred   A binary predicate.
4342    *  @return   The first iterator @c i such that @c i and @c i+1 are both
4343    *  valid iterators in @p [first,last) and such that
4344    *  @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4345    *  exists.
4346   */
4347   template<typename _ForwardIterator, typename _BinaryPredicate>
4348     _ForwardIterator
4349     adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
4350                   _BinaryPredicate __binary_pred)
4351     {
4352       // concept requirements
4353       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4354       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4355             typename iterator_traits<_ForwardIterator>::value_type,
4356             typename iterator_traits<_ForwardIterator>::value_type>)
4357       __glibcxx_requires_valid_range(__first, __last);
4358       if (__first == __last)
4359         return __last;
4360       _ForwardIterator __next = __first;
4361       while(++__next != __last)
4362         {
4363           if (__binary_pred(*__first, *__next))
4364             return __first;
4365           __first = __next;
4366         }
4367       return __last;
4368     }
4369
4370   /**
4371    *  @brief Count the number of copies of a value in a sequence.
4372    *  @ingroup non_mutating_algorithms
4373    *  @param  first  An input iterator.
4374    *  @param  last   An input iterator.
4375    *  @param  value  The value to be counted.
4376    *  @return   The number of iterators @c i in the range @p [first,last)
4377    *  for which @c *i == @p value
4378   */
4379   template<typename _InputIterator, typename _Tp>
4380     typename iterator_traits<_InputIterator>::difference_type
4381     count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
4382     {
4383       // concept requirements
4384       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4385       __glibcxx_function_requires(_EqualOpConcept<
4386         typename iterator_traits<_InputIterator>::value_type, _Tp>)
4387       __glibcxx_requires_valid_range(__first, __last);
4388       typename iterator_traits<_InputIterator>::difference_type __n = 0;
4389       for (; __first != __last; ++__first)
4390         if (*__first == __value)
4391           ++__n;
4392       return __n;
4393     }
4394
4395   /**
4396    *  @brief Count the elements of a sequence for which a predicate is true.
4397    *  @ingroup non_mutating_algorithms
4398    *  @param  first  An input iterator.
4399    *  @param  last   An input iterator.
4400    *  @param  pred   A predicate.
4401    *  @return   The number of iterators @c i in the range @p [first,last)
4402    *  for which @p pred(*i) is true.
4403   */
4404   template<typename _InputIterator, typename _Predicate>
4405     typename iterator_traits<_InputIterator>::difference_type
4406     count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4407     {
4408       // concept requirements
4409       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4410       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4411             typename iterator_traits<_InputIterator>::value_type>)
4412       __glibcxx_requires_valid_range(__first, __last);
4413       typename iterator_traits<_InputIterator>::difference_type __n = 0;
4414       for (; __first != __last; ++__first)
4415         if (__pred(*__first))
4416           ++__n;
4417       return __n;
4418     }
4419
4420   /**
4421    *  @brief Search a sequence for a matching sub-sequence.
4422    *  @ingroup non_mutating_algorithms
4423    *  @param  first1  A forward iterator.
4424    *  @param  last1   A forward iterator.
4425    *  @param  first2  A forward iterator.
4426    *  @param  last2   A forward iterator.
4427    *  @return   The first iterator @c i in the range
4428    *  @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4429    *  for each @c N in the range @p [0,last2-first2), or @p last1 if no
4430    *  such iterator exists.
4431    *
4432    *  Searches the range @p [first1,last1) for a sub-sequence that compares
4433    *  equal value-by-value with the sequence given by @p [first2,last2) and
4434    *  returns an iterator to the first element of the sub-sequence, or
4435    *  @p last1 if the sub-sequence is not found.
4436    *
4437    *  Because the sub-sequence must lie completely within the range
4438    *  @p [first1,last1) it must start at a position less than
4439    *  @p last1-(last2-first2) where @p last2-first2 is the length of the
4440    *  sub-sequence.
4441    *  This means that the returned iterator @c i will be in the range
4442    *  @p [first1,last1-(last2-first2))
4443   */
4444   template<typename _ForwardIterator1, typename _ForwardIterator2>
4445     _ForwardIterator1
4446     search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4447            _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4448     {
4449       // concept requirements
4450       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4451       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4452       __glibcxx_function_requires(_EqualOpConcept<
4453             typename iterator_traits<_ForwardIterator1>::value_type,
4454             typename iterator_traits<_ForwardIterator2>::value_type>)
4455       __glibcxx_requires_valid_range(__first1, __last1);
4456       __glibcxx_requires_valid_range(__first2, __last2);
4457
4458       // Test for empty ranges
4459       if (__first1 == __last1 || __first2 == __last2)
4460         return __first1;
4461
4462       // Test for a pattern of length 1.
4463       _ForwardIterator2 __p1(__first2);
4464       if (++__p1 == __last2)
4465         return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4466
4467       // General case.
4468       _ForwardIterator2 __p;
4469       _ForwardIterator1 __current = __first1;
4470
4471       for (;;)
4472         {
4473           __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4474           if (__first1 == __last1)
4475             return __last1;
4476
4477           __p = __p1;
4478           __current = __first1;
4479           if (++__current == __last1)
4480             return __last1;
4481
4482           while (*__current == *__p)
4483             {
4484               if (++__p == __last2)
4485                 return __first1;
4486               if (++__current == __last1)
4487                 return __last1;
4488             }
4489           ++__first1;
4490         }
4491       return __first1;
4492     }
4493
4494   /**
4495    *  @brief Search a sequence for a matching sub-sequence using a predicate.
4496    *  @ingroup non_mutating_algorithms
4497    *  @param  first1     A forward iterator.
4498    *  @param  last1      A forward iterator.
4499    *  @param  first2     A forward iterator.
4500    *  @param  last2      A forward iterator.
4501    *  @param  predicate  A binary predicate.
4502    *  @return   The first iterator @c i in the range
4503    *  @p [first1,last1-(last2-first2)) such that
4504    *  @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4505    *  @p [0,last2-first2), or @p last1 if no such iterator exists.
4506    *
4507    *  Searches the range @p [first1,last1) for a sub-sequence that compares
4508    *  equal value-by-value with the sequence given by @p [first2,last2),
4509    *  using @p predicate to determine equality, and returns an iterator
4510    *  to the first element of the sub-sequence, or @p last1 if no such
4511    *  iterator exists.
4512    *
4513    *  @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4514   */
4515   template<typename _ForwardIterator1, typename _ForwardIterator2,
4516            typename _BinaryPredicate>
4517     _ForwardIterator1
4518     search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4519            _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4520            _BinaryPredicate  __predicate)
4521     {
4522       // concept requirements
4523       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4524       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4525       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4526             typename iterator_traits<_ForwardIterator1>::value_type,
4527             typename iterator_traits<_ForwardIterator2>::value_type>)
4528       __glibcxx_requires_valid_range(__first1, __last1);
4529       __glibcxx_requires_valid_range(__first2, __last2);
4530
4531       // Test for empty ranges
4532       if (__first1 == __last1 || __first2 == __last2)
4533         return __first1;
4534
4535       // Test for a pattern of length 1.
4536       _ForwardIterator2 __p1(__first2);
4537       if (++__p1 == __last2)
4538         {
4539           while (__first1 != __last1
4540                  && !bool(__predicate(*__first1, *__first2)))
4541             ++__first1;
4542           return __first1;
4543         }
4544
4545       // General case.
4546       _ForwardIterator2 __p;
4547       _ForwardIterator1 __current = __first1;
4548
4549       for (;;)
4550         {
4551           while (__first1 != __last1
4552                  && !bool(__predicate(*__first1, *__first2)))
4553             ++__first1;
4554           if (__first1 == __last1)
4555             return __last1;
4556
4557           __p = __p1;
4558           __current = __first1;
4559           if (++__current == __last1)
4560             return __last1;
4561
4562           while (__predicate(*__current, *__p))
4563             {
4564               if (++__p == __last2)
4565                 return __first1;
4566               if (++__current == __last1)
4567                 return __last1;
4568             }
4569           ++__first1;
4570         }
4571       return __first1;
4572     }
4573
4574
4575   /**
4576    *  @brief Search a sequence for a number of consecutive values.
4577    *  @ingroup non_mutating_algorithms
4578    *  @param  first  A forward iterator.
4579    *  @param  last   A forward iterator.
4580    *  @param  count  The number of consecutive values.
4581    *  @param  val    The value to find.
4582    *  @return   The first iterator @c i in the range @p [first,last-count)
4583    *  such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4584    *  or @p last if no such iterator exists.
4585    *
4586    *  Searches the range @p [first,last) for @p count consecutive elements
4587    *  equal to @p val.
4588   */
4589   template<typename _ForwardIterator, typename _Integer, typename _Tp>
4590     _ForwardIterator
4591     search_n(_ForwardIterator __first, _ForwardIterator __last,
4592              _Integer __count, const _Tp& __val)
4593     {
4594       // concept requirements
4595       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4596       __glibcxx_function_requires(_EqualOpConcept<
4597         typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4598       __glibcxx_requires_valid_range(__first, __last);
4599
4600       if (__count <= 0)
4601         return __first;
4602       if (__count == 1)
4603         return _GLIBCXX_STD_P::find(__first, __last, __val);
4604       return std::__search_n(__first, __last, __count, __val,
4605                              std::__iterator_category(__first));
4606     }
4607
4608
4609   /**
4610    *  @brief Search a sequence for a number of consecutive values using a
4611    *         predicate.
4612    *  @ingroup non_mutating_algorithms
4613    *  @param  first        A forward iterator.
4614    *  @param  last         A forward iterator.
4615    *  @param  count        The number of consecutive values.
4616    *  @param  val          The value to find.
4617    *  @param  binary_pred  A binary predicate.
4618    *  @return   The first iterator @c i in the range @p [first,last-count)
4619    *  such that @p binary_pred(*(i+N),val) is true for each @c N in the
4620    *  range @p [0,count), or @p last if no such iterator exists.
4621    *
4622    *  Searches the range @p [first,last) for @p count consecutive elements
4623    *  for which the predicate returns true.
4624   */
4625   template<typename _ForwardIterator, typename _Integer, typename _Tp,
4626            typename _BinaryPredicate>
4627     _ForwardIterator
4628     search_n(_ForwardIterator __first, _ForwardIterator __last,
4629              _Integer __count, const _Tp& __val,
4630              _BinaryPredicate __binary_pred)
4631     {
4632       // concept requirements
4633       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4634       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4635             typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4636       __glibcxx_requires_valid_range(__first, __last);
4637
4638       if (__count <= 0)
4639         return __first;
4640       if (__count == 1)
4641         {
4642           while (__first != __last && !bool(__binary_pred(*__first, __val)))
4643             ++__first;
4644           return __first;
4645         }
4646       return std::__search_n(__first, __last, __count, __val, __binary_pred,
4647                              std::__iterator_category(__first));
4648     }
4649
4650
4651   /**
4652    *  @brief Perform an operation on a sequence.
4653    *  @ingroup mutating_algorithms
4654    *  @param  first     An input iterator.
4655    *  @param  last      An input iterator.
4656    *  @param  result    An output iterator.
4657    *  @param  unary_op  A unary operator.
4658    *  @return   An output iterator equal to @p result+(last-first).
4659    *
4660    *  Applies the operator to each element in the input range and assigns
4661    *  the results to successive elements of the output sequence.
4662    *  Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4663    *  range @p [0,last-first).
4664    *
4665    *  @p unary_op must not alter its argument.
4666   */
4667   template<typename _InputIterator, typename _OutputIterator,
4668            typename _UnaryOperation>
4669     _OutputIterator
4670     transform(_InputIterator __first, _InputIterator __last,
4671               _OutputIterator __result, _UnaryOperation __unary_op)
4672     {
4673       // concept requirements
4674       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4675       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4676             // "the type returned by a _UnaryOperation"
4677             __typeof__(__unary_op(*__first))>)
4678       __glibcxx_requires_valid_range(__first, __last);
4679
4680       for (; __first != __last; ++__first, ++__result)
4681         *__result = __unary_op(*__first);
4682       return __result;
4683     }
4684
4685   /**
4686    *  @brief Perform an operation on corresponding elements of two sequences.
4687    *  @ingroup mutating_algorithms
4688    *  @param  first1     An input iterator.
4689    *  @param  last1      An input iterator.
4690    *  @param  first2     An input iterator.
4691    *  @param  result     An output iterator.
4692    *  @param  binary_op  A binary operator.
4693    *  @return   An output iterator equal to @p result+(last-first).
4694    *
4695    *  Applies the operator to the corresponding elements in the two
4696    *  input ranges and assigns the results to successive elements of the
4697    *  output sequence.
4698    *  Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4699    *  @c N in the range @p [0,last1-first1).
4700    *
4701    *  @p binary_op must not alter either of its arguments.
4702   */
4703   template<typename _InputIterator1, typename _InputIterator2,
4704            typename _OutputIterator, typename _BinaryOperation>
4705     _OutputIterator
4706     transform(_InputIterator1 __first1, _InputIterator1 __last1,
4707               _InputIterator2 __first2, _OutputIterator __result,
4708               _BinaryOperation __binary_op)
4709     {
4710       // concept requirements
4711       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4712       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4713       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4714             // "the type returned by a _BinaryOperation"
4715             __typeof__(__binary_op(*__first1,*__first2))>)
4716       __glibcxx_requires_valid_range(__first1, __last1);
4717
4718       for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4719         *__result = __binary_op(*__first1, *__first2);
4720       return __result;
4721     }
4722
4723   /**
4724    *  @brief Replace each occurrence of one value in a sequence with another
4725    *         value.
4726    *  @ingroup mutating_algorithms
4727    *  @param  first      A forward iterator.
4728    *  @param  last       A forward iterator.
4729    *  @param  old_value  The value to be replaced.
4730    *  @param  new_value  The replacement value.
4731    *  @return   replace() returns no value.
4732    *
4733    *  For each iterator @c i in the range @p [first,last) if @c *i ==
4734    *  @p old_value then the assignment @c *i = @p new_value is performed.
4735   */
4736   template<typename _ForwardIterator, typename _Tp>
4737     void
4738     replace(_ForwardIterator __first, _ForwardIterator __last,
4739             const _Tp& __old_value, const _Tp& __new_value)
4740     {
4741       // concept requirements
4742       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4743                                   _ForwardIterator>)
4744       __glibcxx_function_requires(_EqualOpConcept<
4745             typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4746       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4747             typename iterator_traits<_ForwardIterator>::value_type>)
4748       __glibcxx_requires_valid_range(__first, __last);
4749
4750       for (; __first != __last; ++__first)
4751         if (*__first == __old_value)
4752           *__first = __new_value;
4753     }
4754
4755   /**
4756    *  @brief Replace each value in a sequence for which a predicate returns
4757    *         true with another value.
4758    *  @ingroup mutating_algorithms
4759    *  @param  first      A forward iterator.
4760    *  @param  last       A forward iterator.
4761    *  @param  pred       A predicate.
4762    *  @param  new_value  The replacement value.
4763    *  @return   replace_if() returns no value.
4764    *
4765    *  For each iterator @c i in the range @p [first,last) if @p pred(*i)
4766    *  is true then the assignment @c *i = @p new_value is performed.
4767   */
4768   template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4769     void
4770     replace_if(_ForwardIterator __first, _ForwardIterator __last,
4771                _Predicate __pred, const _Tp& __new_value)
4772     {
4773       // concept requirements
4774       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4775                                   _ForwardIterator>)
4776       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4777             typename iterator_traits<_ForwardIterator>::value_type>)
4778       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4779             typename iterator_traits<_ForwardIterator>::value_type>)
4780       __glibcxx_requires_valid_range(__first, __last);
4781
4782       for (; __first != __last; ++__first)
4783         if (__pred(*__first))
4784           *__first = __new_value;
4785     }
4786
4787   /**
4788    *  @brief Assign the result of a function object to each value in a
4789    *         sequence.
4790    *  @ingroup mutating_algorithms
4791    *  @param  first  A forward iterator.
4792    *  @param  last   A forward iterator.
4793    *  @param  gen    A function object taking no arguments and returning
4794    *                 std::iterator_traits<_ForwardIterator>::value_type
4795    *  @return   generate() returns no value.
4796    *
4797    *  Performs the assignment @c *i = @p gen() for each @c i in the range
4798    *  @p [first,last).
4799   */
4800   template<typename _ForwardIterator, typename _Generator>
4801     void
4802     generate(_ForwardIterator __first, _ForwardIterator __last,
4803              _Generator __gen)
4804     {
4805       // concept requirements
4806       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4807       __glibcxx_function_requires(_GeneratorConcept<_Generator,
4808             typename iterator_traits<_ForwardIterator>::value_type>)
4809       __glibcxx_requires_valid_range(__first, __last);
4810
4811       for (; __first != __last; ++__first)
4812         *__first = __gen();
4813     }
4814
4815   /**
4816    *  @brief Assign the result of a function object to each value in a
4817    *         sequence.
4818    *  @ingroup mutating_algorithms
4819    *  @param  first  A forward iterator.
4820    *  @param  n      The length of the sequence.
4821    *  @param  gen    A function object taking no arguments and returning
4822    *                 std::iterator_traits<_ForwardIterator>::value_type
4823    *  @return   The end of the sequence, @p first+n
4824    *
4825    *  Performs the assignment @c *i = @p gen() for each @c i in the range
4826    *  @p [first,first+n).
4827    *
4828    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
4829    *  DR 865. More algorithms that throw away information
4830   */
4831   template<typename _OutputIterator, typename _Size, typename _Generator>
4832     _OutputIterator
4833     generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4834     {
4835       // concept requirements
4836       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4837             // "the type returned by a _Generator"
4838             __typeof__(__gen())>)
4839
4840       for (__decltype(__n + 0) __niter = __n;
4841            __niter > 0; --__niter, ++__first)
4842         *__first = __gen();
4843       return __first;
4844     }
4845
4846
4847   /**
4848    *  @brief Copy a sequence, removing consecutive duplicate values.
4849    *  @ingroup mutating_algorithms
4850    *  @param  first   An input iterator.
4851    *  @param  last    An input iterator.
4852    *  @param  result  An output iterator.
4853    *  @return   An iterator designating the end of the resulting sequence.
4854    *
4855    *  Copies each element in the range @p [first,last) to the range
4856    *  beginning at @p result, except that only the first element is copied
4857    *  from groups of consecutive elements that compare equal.
4858    *  unique_copy() is stable, so the relative order of elements that are
4859    *  copied is unchanged.
4860    *
4861    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
4862    *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
4863    *  
4864    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
4865    *  DR 538. 241 again: Does unique_copy() require CopyConstructible and 
4866    *  Assignable?
4867   */
4868   template<typename _InputIterator, typename _OutputIterator>
4869     inline _OutputIterator
4870     unique_copy(_InputIterator __first, _InputIterator __last,
4871                 _OutputIterator __result)
4872     {
4873       // concept requirements
4874       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4875       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4876             typename iterator_traits<_InputIterator>::value_type>)
4877       __glibcxx_function_requires(_EqualityComparableConcept<
4878             typename iterator_traits<_InputIterator>::value_type>)
4879       __glibcxx_requires_valid_range(__first, __last);
4880
4881       if (__first == __last)
4882         return __result;
4883       return std::__unique_copy(__first, __last, __result,
4884                                 std::__iterator_category(__first),
4885                                 std::__iterator_category(__result));
4886     }
4887
4888   /**
4889    *  @brief Copy a sequence, removing consecutive values using a predicate.
4890    *  @ingroup mutating_algorithms
4891    *  @param  first        An input iterator.
4892    *  @param  last         An input iterator.
4893    *  @param  result       An output iterator.
4894    *  @param  binary_pred  A binary predicate.
4895    *  @return   An iterator designating the end of the resulting sequence.
4896    *
4897    *  Copies each element in the range @p [first,last) to the range
4898    *  beginning at @p result, except that only the first element is copied
4899    *  from groups of consecutive elements for which @p binary_pred returns
4900    *  true.
4901    *  unique_copy() is stable, so the relative order of elements that are
4902    *  copied is unchanged.
4903    *
4904    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
4905    *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
4906   */
4907   template<typename _InputIterator, typename _OutputIterator,
4908            typename _BinaryPredicate>
4909     inline _OutputIterator
4910     unique_copy(_InputIterator __first, _InputIterator __last,
4911                 _OutputIterator __result,
4912                 _BinaryPredicate __binary_pred)
4913     {
4914       // concept requirements -- predicates checked later
4915       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4916       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4917             typename iterator_traits<_InputIterator>::value_type>)
4918       __glibcxx_requires_valid_range(__first, __last);
4919
4920       if (__first == __last)
4921         return __result;
4922       return std::__unique_copy(__first, __last, __result, __binary_pred,
4923                                 std::__iterator_category(__first),
4924                                 std::__iterator_category(__result));
4925     }
4926
4927
4928   /**
4929    *  @brief Randomly shuffle the elements of a sequence.
4930    *  @ingroup mutating_algorithms
4931    *  @param  first   A forward iterator.
4932    *  @param  last    A forward iterator.
4933    *  @return  Nothing.
4934    *
4935    *  Reorder the elements in the range @p [first,last) using a random
4936    *  distribution, so that every possible ordering of the sequence is
4937    *  equally likely.
4938   */
4939   template<typename _RandomAccessIterator>
4940     inline void
4941     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4942     {
4943       // concept requirements
4944       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4945             _RandomAccessIterator>)
4946       __glibcxx_requires_valid_range(__first, __last);
4947
4948       if (__first != __last)
4949         for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4950           std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
4951     }
4952
4953   /**
4954    *  @brief Shuffle the elements of a sequence using a random number
4955    *         generator.
4956    *  @ingroup mutating_algorithms
4957    *  @param  first   A forward iterator.
4958    *  @param  last    A forward iterator.
4959    *  @param  rand    The RNG functor or function.
4960    *  @return  Nothing.
4961    *
4962    *  Reorders the elements in the range @p [first,last) using @p rand to
4963    *  provide a random distribution. Calling @p rand(N) for a positive
4964    *  integer @p N should return a randomly chosen integer from the
4965    *  range [0,N).
4966   */
4967   template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4968     void
4969     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4970 #ifdef __GXX_EXPERIMENTAL_CXX0X__
4971                    _RandomNumberGenerator&& __rand)
4972 #else
4973                    _RandomNumberGenerator& __rand)
4974 #endif
4975     {
4976       // concept requirements
4977       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4978             _RandomAccessIterator>)
4979       __glibcxx_requires_valid_range(__first, __last);
4980
4981       if (__first == __last)
4982         return;
4983       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4984         std::iter_swap(__i, __first + __rand((__i - __first) + 1));
4985     }
4986
4987
4988   /**
4989    *  @brief Move elements for which a predicate is true to the beginning
4990    *         of a sequence.
4991    *  @ingroup mutating_algorithms
4992    *  @param  first   A forward iterator.
4993    *  @param  last    A forward iterator.
4994    *  @param  pred    A predicate functor.
4995    *  @return  An iterator @p middle such that @p pred(i) is true for each
4996    *  iterator @p i in the range @p [first,middle) and false for each @p i
4997    *  in the range @p [middle,last).
4998    *
4999    *  @p pred must not modify its operand. @p partition() does not preserve
5000    *  the relative ordering of elements in each group, use
5001    *  @p stable_partition() if this is needed.
5002   */
5003   template<typename _ForwardIterator, typename _Predicate>
5004     inline _ForwardIterator
5005     partition(_ForwardIterator __first, _ForwardIterator __last,
5006               _Predicate   __pred)
5007     {
5008       // concept requirements
5009       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
5010                                   _ForwardIterator>)
5011       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
5012             typename iterator_traits<_ForwardIterator>::value_type>)
5013       __glibcxx_requires_valid_range(__first, __last);
5014
5015       return std::__partition(__first, __last, __pred,
5016                               std::__iterator_category(__first));
5017     }
5018
5019
5020
5021   /**
5022    *  @brief Sort the smallest elements of a sequence.
5023    *  @ingroup sorting_algorithms
5024    *  @param  first   An iterator.
5025    *  @param  middle  Another iterator.
5026    *  @param  last    Another iterator.
5027    *  @return  Nothing.
5028    *
5029    *  Sorts the smallest @p (middle-first) elements in the range
5030    *  @p [first,last) and moves them to the range @p [first,middle). The
5031    *  order of the remaining elements in the range @p [middle,last) is
5032    *  undefined.
5033    *  After the sort if @p i and @j are iterators in the range
5034    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
5035    *  the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
5036   */
5037   template<typename _RandomAccessIterator>
5038     inline void
5039     partial_sort(_RandomAccessIterator __first,
5040                  _RandomAccessIterator __middle,
5041                  _RandomAccessIterator __last)
5042     {
5043       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5044         _ValueType;
5045
5046       // concept requirements
5047       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5048             _RandomAccessIterator>)
5049       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5050       __glibcxx_requires_valid_range(__first, __middle);
5051       __glibcxx_requires_valid_range(__middle, __last);
5052
5053       std::__heap_select(__first, __middle, __last);
5054       std::sort_heap(__first, __middle);
5055     }
5056
5057   /**
5058    *  @brief Sort the smallest elements of a sequence using a predicate
5059    *         for comparison.
5060    *  @ingroup sorting_algorithms
5061    *  @param  first   An iterator.
5062    *  @param  middle  Another iterator.
5063    *  @param  last    Another iterator.
5064    *  @param  comp    A comparison functor.
5065    *  @return  Nothing.
5066    *
5067    *  Sorts the smallest @p (middle-first) elements in the range
5068    *  @p [first,last) and moves them to the range @p [first,middle). The
5069    *  order of the remaining elements in the range @p [middle,last) is
5070    *  undefined.
5071    *  After the sort if @p i and @j are iterators in the range
5072    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
5073    *  the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
5074    *  are both false.
5075   */
5076   template<typename _RandomAccessIterator, typename _Compare>
5077     inline void
5078     partial_sort(_RandomAccessIterator __first,
5079                  _RandomAccessIterator __middle,
5080                  _RandomAccessIterator __last,
5081                  _Compare __comp)
5082     {
5083       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5084         _ValueType;
5085
5086       // concept requirements
5087       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5088             _RandomAccessIterator>)
5089       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5090                                   _ValueType, _ValueType>)
5091       __glibcxx_requires_valid_range(__first, __middle);
5092       __glibcxx_requires_valid_range(__middle, __last);
5093
5094       std::__heap_select(__first, __middle, __last, __comp);
5095       std::sort_heap(__first, __middle, __comp);
5096     }
5097
5098   /**
5099    *  @brief Sort a sequence just enough to find a particular position.
5100    *  @ingroup sorting_algorithms
5101    *  @param  first   An iterator.
5102    *  @param  nth     Another iterator.
5103    *  @param  last    Another iterator.
5104    *  @return  Nothing.
5105    *
5106    *  Rearranges the elements in the range @p [first,last) so that @p *nth
5107    *  is the same element that would have been in that position had the
5108    *  whole sequence been sorted.
5109    *  whole sequence been sorted. The elements either side of @p *nth are
5110    *  not completely sorted, but for any iterator @i in the range
5111    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
5112    *  holds that @p *j<*i is false.
5113   */
5114   template<typename _RandomAccessIterator>
5115     inline void
5116     nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5117                 _RandomAccessIterator __last)
5118     {
5119       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5120         _ValueType;
5121
5122       // concept requirements
5123       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5124                                   _RandomAccessIterator>)
5125       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5126       __glibcxx_requires_valid_range(__first, __nth);
5127       __glibcxx_requires_valid_range(__nth, __last);
5128
5129       if (__first == __last || __nth == __last)
5130         return;
5131
5132       std::__introselect(__first, __nth, __last,
5133                          std::__lg(__last - __first) * 2);
5134     }
5135
5136   /**
5137    *  @brief Sort a sequence just enough to find a particular position
5138    *         using a predicate for comparison.
5139    *  @ingroup sorting_algorithms
5140    *  @param  first   An iterator.
5141    *  @param  nth     Another iterator.
5142    *  @param  last    Another iterator.
5143    *  @param  comp    A comparison functor.
5144    *  @return  Nothing.
5145    *
5146    *  Rearranges the elements in the range @p [first,last) so that @p *nth
5147    *  is the same element that would have been in that position had the
5148    *  whole sequence been sorted. The elements either side of @p *nth are
5149    *  not completely sorted, but for any iterator @i in the range
5150    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
5151    *  holds that @p comp(*j,*i) is false.
5152   */
5153   template<typename _RandomAccessIterator, typename _Compare>
5154     inline void
5155     nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
5156                 _RandomAccessIterator __last, _Compare __comp)
5157     {
5158       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5159         _ValueType;
5160
5161       // concept requirements
5162       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5163                                   _RandomAccessIterator>)
5164       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5165                                   _ValueType, _ValueType>)
5166       __glibcxx_requires_valid_range(__first, __nth);
5167       __glibcxx_requires_valid_range(__nth, __last);
5168
5169       if (__first == __last || __nth == __last)
5170         return;
5171
5172       std::__introselect(__first, __nth, __last,
5173                          std::__lg(__last - __first) * 2, __comp);
5174     }
5175
5176
5177   /**
5178    *  @brief Sort the elements of a sequence.
5179    *  @ingroup sorting_algorithms
5180    *  @param  first   An iterator.
5181    *  @param  last    Another iterator.
5182    *  @return  Nothing.
5183    *
5184    *  Sorts the elements in the range @p [first,last) in ascending order,
5185    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
5186    *  @p [first,last-1).
5187    *
5188    *  The relative ordering of equivalent elements is not preserved, use
5189    *  @p stable_sort() if this is needed.
5190   */
5191   template<typename _RandomAccessIterator>
5192     inline void
5193     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5194     {
5195       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5196         _ValueType;
5197
5198       // concept requirements
5199       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5200             _RandomAccessIterator>)
5201       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5202       __glibcxx_requires_valid_range(__first, __last);
5203
5204       if (__first != __last)
5205         {
5206           std::__introsort_loop(__first, __last,
5207                                 std::__lg(__last - __first) * 2);
5208           std::__final_insertion_sort(__first, __last);
5209         }
5210     }
5211
5212   /**
5213    *  @brief Sort the elements of a sequence using a predicate for comparison.
5214    *  @ingroup sorting_algorithms
5215    *  @param  first   An iterator.
5216    *  @param  last    Another iterator.
5217    *  @param  comp    A comparison functor.
5218    *  @return  Nothing.
5219    *
5220    *  Sorts the elements in the range @p [first,last) in ascending order,
5221    *  such that @p comp(*(i+1),*i) is false for every iterator @p i in the
5222    *  range @p [first,last-1).
5223    *
5224    *  The relative ordering of equivalent elements is not preserved, use
5225    *  @p stable_sort() if this is needed.
5226   */
5227   template<typename _RandomAccessIterator, typename _Compare>
5228     inline void
5229     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5230          _Compare __comp)
5231     {
5232       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5233         _ValueType;
5234
5235       // concept requirements
5236       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5237             _RandomAccessIterator>)
5238       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
5239                                   _ValueType>)
5240       __glibcxx_requires_valid_range(__first, __last);
5241
5242       if (__first != __last)
5243         {
5244           std::__introsort_loop(__first, __last,
5245                                 std::__lg(__last - __first) * 2, __comp);
5246           std::__final_insertion_sort(__first, __last, __comp);
5247         }
5248     }
5249
5250   /**
5251    *  @brief Merges two sorted ranges.
5252    *  @ingroup sorting_algorithms
5253    *  @param  first1  An iterator.
5254    *  @param  first2  Another iterator.
5255    *  @param  last1   Another iterator.
5256    *  @param  last2   Another iterator.
5257    *  @param  result  An iterator pointing to the end of the merged range.
5258    *  @return         An iterator pointing to the first element <em>not less
5259    *                  than</em> @a val.
5260    *
5261    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5262    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
5263    *  must be sorted, and the output range must not overlap with either of
5264    *  the input ranges.  The sort is @e stable, that is, for equivalent
5265    *  elements in the two ranges, elements from the first range will always
5266    *  come before elements from the second.
5267   */
5268   template<typename _InputIterator1, typename _InputIterator2,
5269            typename _OutputIterator>
5270     _OutputIterator
5271     merge(_InputIterator1 __first1, _InputIterator1 __last1,
5272           _InputIterator2 __first2, _InputIterator2 __last2,
5273           _OutputIterator __result)
5274     {
5275       typedef typename iterator_traits<_InputIterator1>::value_type
5276         _ValueType1;
5277       typedef typename iterator_traits<_InputIterator2>::value_type
5278         _ValueType2;
5279
5280       // concept requirements
5281       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5282       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5283       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5284                                   _ValueType1>)
5285       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5286                                   _ValueType2>)
5287       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
5288       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5289       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5290
5291       while (__first1 != __last1 && __first2 != __last2)
5292         {
5293           if (*__first2 < *__first1)
5294             {
5295               *__result = *__first2;
5296               ++__first2;
5297             }
5298           else
5299             {
5300               *__result = *__first1;
5301               ++__first1;
5302             }
5303           ++__result;
5304         }
5305       return std::copy(__first2, __last2, std::copy(__first1, __last1,
5306                                                     __result));
5307     }
5308
5309   /**
5310    *  @brief Merges two sorted ranges.
5311    *  @ingroup sorting_algorithms
5312    *  @param  first1  An iterator.
5313    *  @param  first2  Another iterator.
5314    *  @param  last1   Another iterator.
5315    *  @param  last2   Another iterator.
5316    *  @param  result  An iterator pointing to the end of the merged range.
5317    *  @param  comp    A functor to use for comparisons.
5318    *  @return         An iterator pointing to the first element "not less
5319    *                  than" @a val.
5320    *
5321    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5322    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
5323    *  must be sorted, and the output range must not overlap with either of
5324    *  the input ranges.  The sort is @e stable, that is, for equivalent
5325    *  elements in the two ranges, elements from the first range will always
5326    *  come before elements from the second.
5327    *
5328    *  The comparison function should have the same effects on ordering as
5329    *  the function used for the initial sort.
5330   */
5331   template<typename _InputIterator1, typename _InputIterator2,
5332            typename _OutputIterator, typename _Compare>
5333     _OutputIterator
5334     merge(_InputIterator1 __first1, _InputIterator1 __last1,
5335           _InputIterator2 __first2, _InputIterator2 __last2,
5336           _OutputIterator __result, _Compare __comp)
5337     {
5338       typedef typename iterator_traits<_InputIterator1>::value_type
5339         _ValueType1;
5340       typedef typename iterator_traits<_InputIterator2>::value_type
5341         _ValueType2;
5342
5343       // concept requirements
5344       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5345       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5346       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5347                                   _ValueType1>)
5348       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5349                                   _ValueType2>)
5350       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5351                                   _ValueType2, _ValueType1>)
5352       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5353       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5354
5355       while (__first1 != __last1 && __first2 != __last2)
5356         {
5357           if (__comp(*__first2, *__first1))
5358             {
5359               *__result = *__first2;
5360               ++__first2;
5361             }
5362           else
5363             {
5364               *__result = *__first1;
5365               ++__first1;
5366             }
5367           ++__result;
5368         }
5369       return std::copy(__first2, __last2, std::copy(__first1, __last1,
5370                                                     __result));
5371     }
5372
5373
5374   /**
5375    *  @brief Sort the elements of a sequence, preserving the relative order
5376    *         of equivalent elements.
5377    *  @ingroup sorting_algorithms
5378    *  @param  first   An iterator.
5379    *  @param  last    Another iterator.
5380    *  @return  Nothing.
5381    *
5382    *  Sorts the elements in the range @p [first,last) in ascending order,
5383    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
5384    *  @p [first,last-1).
5385    *
5386    *  The relative ordering of equivalent elements is preserved, so any two
5387    *  elements @p x and @p y in the range @p [first,last) such that
5388    *  @p x<y is false and @p y<x is false will have the same relative
5389    *  ordering after calling @p stable_sort().
5390   */
5391   template<typename _RandomAccessIterator>
5392     inline void
5393     stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5394     {
5395       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5396         _ValueType;
5397       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5398         _DistanceType;
5399
5400       // concept requirements
5401       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5402             _RandomAccessIterator>)
5403       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5404       __glibcxx_requires_valid_range(__first, __last);
5405
5406       _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5407                                                                  __last);
5408       if (__buf.begin() == 0)
5409         std::__inplace_stable_sort(__first, __last);
5410       else
5411         std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5412                                     _DistanceType(__buf.size()));
5413     }
5414
5415   /**
5416    *  @brief Sort the elements of a sequence using a predicate for comparison,
5417    *         preserving the relative order of equivalent elements.
5418    *  @ingroup sorting_algorithms
5419    *  @param  first   An iterator.
5420    *  @param  last    Another iterator.
5421    *  @param  comp    A comparison functor.
5422    *  @return  Nothing.
5423    *
5424    *  Sorts the elements in the range @p [first,last) in ascending order,
5425    *  such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5426    *  range @p [first,last-1).
5427    *
5428    *  The relative ordering of equivalent elements is preserved, so any two
5429    *  elements @p x and @p y in the range @p [first,last) such that
5430    *  @p comp(x,y) is false and @p comp(y,x) is false will have the same
5431    *  relative ordering after calling @p stable_sort().
5432   */
5433   template<typename _RandomAccessIterator, typename _Compare>
5434     inline void
5435     stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5436                 _Compare __comp)
5437     {
5438       typedef typename iterator_traits<_RandomAccessIterator>::value_type
5439         _ValueType;
5440       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5441         _DistanceType;
5442
5443       // concept requirements
5444       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5445             _RandomAccessIterator>)
5446       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5447                                   _ValueType,
5448                                   _ValueType>)
5449       __glibcxx_requires_valid_range(__first, __last);
5450
5451       _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5452                                                                  __last);
5453       if (__buf.begin() == 0)
5454         std::__inplace_stable_sort(__first, __last, __comp);
5455       else
5456         std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5457                                     _DistanceType(__buf.size()), __comp);
5458     }
5459
5460
5461   /**
5462    *  @brief Return the union of two sorted ranges.
5463    *  @ingroup set_algorithms
5464    *  @param  first1  Start of first range.
5465    *  @param  last1   End of first range.
5466    *  @param  first2  Start of second range.
5467    *  @param  last2   End of second range.
5468    *  @return  End of the output range.
5469    *  @ingroup set_algorithms
5470    *
5471    *  This operation iterates over both ranges, copying elements present in
5472    *  each range in order to the output range.  Iterators increment for each
5473    *  range.  When the current element of one range is less than the other,
5474    *  that element is copied and the iterator advanced.  If an element is
5475    *  contained in both ranges, the element from the first range is copied and
5476    *  both ranges advance.  The output range may not overlap either input
5477    *  range.
5478   */
5479   template<typename _InputIterator1, typename _InputIterator2,
5480            typename _OutputIterator>
5481     _OutputIterator
5482     set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5483               _InputIterator2 __first2, _InputIterator2 __last2,
5484               _OutputIterator __result)
5485     {
5486       typedef typename iterator_traits<_InputIterator1>::value_type
5487         _ValueType1;
5488       typedef typename iterator_traits<_InputIterator2>::value_type
5489         _ValueType2;
5490
5491       // concept requirements
5492       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5493       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5494       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5495                                   _ValueType1>)
5496       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5497                                   _ValueType2>)
5498       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5499       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5500       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5501       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5502
5503       while (__first1 != __last1 && __first2 != __last2)
5504         {
5505           if (*__first1 < *__first2)
5506             {
5507               *__result = *__first1;
5508               ++__first1;
5509             }
5510           else if (*__first2 < *__first1)
5511             {
5512               *__result = *__first2;
5513               ++__first2;
5514             }
5515           else
5516             {
5517               *__result = *__first1;
5518               ++__first1;
5519               ++__first2;
5520             }
5521           ++__result;
5522         }
5523       return std::copy(__first2, __last2, std::copy(__first1, __last1,
5524                                                     __result));
5525     }
5526
5527   /**
5528    *  @brief Return the union of two sorted ranges using a comparison functor.
5529    *  @ingroup set_algorithms
5530    *  @param  first1  Start of first range.
5531    *  @param  last1   End of first range.
5532    *  @param  first2  Start of second range.
5533    *  @param  last2   End of second range.
5534    *  @param  comp    The comparison functor.
5535    *  @return  End of the output range.
5536    *  @ingroup set_algorithms
5537    *
5538    *  This operation iterates over both ranges, copying elements present in
5539    *  each range in order to the output range.  Iterators increment for each
5540    *  range.  When the current element of one range is less than the other
5541    *  according to @a comp, that element is copied and the iterator advanced.
5542    *  If an equivalent element according to @a comp is contained in both
5543    *  ranges, the element from the first range is copied and both ranges
5544    *  advance.  The output range may not overlap either input range.
5545   */
5546   template<typename _InputIterator1, typename _InputIterator2,
5547            typename _OutputIterator, typename _Compare>
5548     _OutputIterator
5549     set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5550               _InputIterator2 __first2, _InputIterator2 __last2,
5551               _OutputIterator __result, _Compare __comp)
5552     {
5553       typedef typename iterator_traits<_InputIterator1>::value_type
5554         _ValueType1;
5555       typedef typename iterator_traits<_InputIterator2>::value_type
5556         _ValueType2;
5557
5558       // concept requirements
5559       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5560       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5561       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5562                                   _ValueType1>)
5563       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5564                                   _ValueType2>)
5565       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5566                                   _ValueType1, _ValueType2>)
5567       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5568                                   _ValueType2, _ValueType1>)
5569       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5570       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5571
5572       while (__first1 != __last1 && __first2 != __last2)
5573         {
5574           if (__comp(*__first1, *__first2))
5575             {
5576               *__result = *__first1;
5577               ++__first1;
5578             }
5579           else if (__comp(*__first2, *__first1))
5580             {
5581               *__result = *__first2;
5582               ++__first2;
5583             }
5584           else
5585             {
5586               *__result = *__first1;
5587               ++__first1;
5588               ++__first2;
5589             }
5590           ++__result;
5591         }
5592       return std::copy(__first2, __last2, std::copy(__first1, __last1,
5593                                                     __result));
5594     }
5595
5596   /**
5597    *  @brief Return the intersection of two sorted ranges.
5598    *  @ingroup set_algorithms
5599    *  @param  first1  Start of first range.
5600    *  @param  last1   End of first range.
5601    *  @param  first2  Start of second range.
5602    *  @param  last2   End of second range.
5603    *  @return  End of the output range.
5604    *  @ingroup set_algorithms
5605    *
5606    *  This operation iterates over both ranges, copying elements present in
5607    *  both ranges in order to the output range.  Iterators increment for each
5608    *  range.  When the current element of one range is less than the other,
5609    *  that iterator advances.  If an element is contained in both ranges, the
5610    *  element from the first range is copied and both ranges advance.  The
5611    *  output range may not overlap either input range.
5612   */
5613   template<typename _InputIterator1, typename _InputIterator2,
5614            typename _OutputIterator>
5615     _OutputIterator
5616     set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5617                      _InputIterator2 __first2, _InputIterator2 __last2,
5618                      _OutputIterator __result)
5619     {
5620       typedef typename iterator_traits<_InputIterator1>::value_type
5621         _ValueType1;
5622       typedef typename iterator_traits<_InputIterator2>::value_type
5623         _ValueType2;
5624
5625       // concept requirements
5626       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5627       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5628       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5629                                   _ValueType1>)
5630       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5631       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5632       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5633       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5634
5635       while (__first1 != __last1 && __first2 != __last2)
5636         if (*__first1 < *__first2)
5637           ++__first1;
5638         else if (*__first2 < *__first1)
5639           ++__first2;
5640         else
5641           {
5642             *__result = *__first1;
5643             ++__first1;
5644             ++__first2;
5645             ++__result;
5646           }
5647       return __result;
5648     }
5649
5650   /**
5651    *  @brief Return the intersection of two sorted ranges using comparison
5652    *  functor.
5653    *  @ingroup set_algorithms
5654    *  @param  first1  Start of first range.
5655    *  @param  last1   End of first range.
5656    *  @param  first2  Start of second range.
5657    *  @param  last2   End of second range.
5658    *  @param  comp    The comparison functor.
5659    *  @return  End of the output range.
5660    *  @ingroup set_algorithms
5661    *
5662    *  This operation iterates over both ranges, copying elements present in
5663    *  both ranges in order to the output range.  Iterators increment for each
5664    *  range.  When the current element of one range is less than the other
5665    *  according to @a comp, that iterator advances.  If an element is
5666    *  contained in both ranges according to @a comp, the element from the
5667    *  first range is copied and both ranges advance.  The output range may not
5668    *  overlap either input range.
5669   */
5670   template<typename _InputIterator1, typename _InputIterator2,
5671            typename _OutputIterator, typename _Compare>
5672     _OutputIterator
5673     set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5674                      _InputIterator2 __first2, _InputIterator2 __last2,
5675                      _OutputIterator __result, _Compare __comp)
5676     {
5677       typedef typename iterator_traits<_InputIterator1>::value_type
5678         _ValueType1;
5679       typedef typename iterator_traits<_InputIterator2>::value_type
5680         _ValueType2;
5681
5682       // concept requirements
5683       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5684       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5685       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5686                                   _ValueType1>)
5687       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5688                                   _ValueType1, _ValueType2>)
5689       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5690                                   _ValueType2, _ValueType1>)
5691       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5692       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5693
5694       while (__first1 != __last1 && __first2 != __last2)
5695         if (__comp(*__first1, *__first2))
5696           ++__first1;
5697         else if (__comp(*__first2, *__first1))
5698           ++__first2;
5699         else
5700           {
5701             *__result = *__first1;
5702             ++__first1;
5703             ++__first2;
5704             ++__result;
5705           }
5706       return __result;
5707     }
5708
5709   /**
5710    *  @brief Return the difference of two sorted ranges.
5711    *  @ingroup set_algorithms
5712    *  @param  first1  Start of first range.
5713    *  @param  last1   End of first range.
5714    *  @param  first2  Start of second range.
5715    *  @param  last2   End of second range.
5716    *  @return  End of the output range.
5717    *  @ingroup set_algorithms
5718    *
5719    *  This operation iterates over both ranges, copying elements present in
5720    *  the first range but not the second in order to the output range.
5721    *  Iterators increment for each range.  When the current element of the
5722    *  first range is less than the second, that element is copied and the
5723    *  iterator advances.  If the current element of the second range is less,
5724    *  the iterator advances, but no element is copied.  If an element is
5725    *  contained in both ranges, no elements are copied and both ranges
5726    *  advance.  The output range may not overlap either input range.
5727   */
5728   template<typename _InputIterator1, typename _InputIterator2,
5729            typename _OutputIterator>
5730     _OutputIterator
5731     set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5732                    _InputIterator2 __first2, _InputIterator2 __last2,
5733                    _OutputIterator __result)
5734     {
5735       typedef typename iterator_traits<_InputIterator1>::value_type
5736         _ValueType1;
5737       typedef typename iterator_traits<_InputIterator2>::value_type
5738         _ValueType2;
5739
5740       // concept requirements
5741       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5742       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5743       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5744                                   _ValueType1>)
5745       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5746       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
5747       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5748       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5749
5750       while (__first1 != __last1 && __first2 != __last2)
5751         if (*__first1 < *__first2)
5752           {
5753             *__result = *__first1;
5754             ++__first1;
5755             ++__result;
5756           }
5757         else if (*__first2 < *__first1)
5758           ++__first2;
5759         else
5760           {
5761             ++__first1;
5762             ++__first2;
5763           }
5764       return std::copy(__first1, __last1, __result);
5765     }
5766
5767   /**
5768    *  @brief  Return the difference of two sorted ranges using comparison
5769    *  functor.
5770    *  @ingroup set_algorithms
5771    *  @param  first1  Start of first range.
5772    *  @param  last1   End of first range.
5773    *  @param  first2  Start of second range.
5774    *  @param  last2   End of second range.
5775    *  @param  comp    The comparison functor.
5776    *  @return  End of the output range.
5777    *  @ingroup set_algorithms
5778    *
5779    *  This operation iterates over both ranges, copying elements present in
5780    *  the first range but not the second in order to the output range.
5781    *  Iterators increment for each range.  When the current element of the
5782    *  first range is less than the second according to @a comp, that element
5783    *  is copied and the iterator advances.  If the current element of the
5784    *  second range is less, no element is copied and the iterator advances.
5785    *  If an element is contained in both ranges according to @a comp, no
5786    *  elements are copied and both ranges advance.  The output range may not
5787    *  overlap either input range.
5788   */
5789   template<typename _InputIterator1, typename _InputIterator2,
5790            typename _OutputIterator, typename _Compare>
5791     _OutputIterator
5792     set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5793                    _InputIterator2 __first2, _InputIterator2 __last2,
5794                    _OutputIterator __result, _Compare __comp)
5795     {
5796       typedef typename iterator_traits<_InputIterator1>::value_type
5797         _ValueType1;
5798       typedef typename iterator_traits<_InputIterator2>::value_type
5799         _ValueType2;
5800
5801       // concept requirements
5802       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5803       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5804       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5805                                   _ValueType1>)
5806       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5807                                   _ValueType1, _ValueType2>)
5808       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5809                                   _ValueType2, _ValueType1>)
5810       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5811       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5812
5813       while (__first1 != __last1 && __first2 != __last2)
5814         if (__comp(*__first1, *__first2))
5815           {
5816             *__result = *__first1;
5817             ++__first1;
5818             ++__result;
5819           }
5820         else if (__comp(*__first2, *__first1))
5821           ++__first2;
5822         else
5823           {
5824             ++__first1;
5825             ++__first2;
5826           }
5827       return std::copy(__first1, __last1, __result);
5828     }
5829
5830   /**
5831    *  @brief  Return the symmetric difference of two sorted ranges.
5832    *  @ingroup set_algorithms
5833    *  @param  first1  Start of first range.
5834    *  @param  last1   End of first range.
5835    *  @param  first2  Start of second range.
5836    *  @param  last2   End of second range.
5837    *  @return  End of the output range.
5838    *  @ingroup set_algorithms
5839    *
5840    *  This operation iterates over both ranges, copying elements present in
5841    *  one range but not the other in order to the output range.  Iterators
5842    *  increment for each range.  When the current element of one range is less
5843    *  than the other, that element is copied and the iterator advances.  If an
5844    *  element is contained in both ranges, no elements are copied and both
5845    *  ranges advance.  The output range may not overlap either input range.
5846   */
5847   template<typename _InputIterator1, typename _InputIterator2,
5848            typename _OutputIterator>
5849     _OutputIterator
5850     set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5851                              _InputIterator2 __first2, _InputIterator2 __last2,
5852                              _OutputIterator __result)
5853     {
5854       typedef typename iterator_traits<_InputIterator1>::value_type
5855         _ValueType1;
5856       typedef typename iterator_traits<_InputIterator2>::value_type
5857         _ValueType2;
5858
5859       // concept requirements
5860       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5861       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5862       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5863                                   _ValueType1>)
5864       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5865                                   _ValueType2>)
5866       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5867       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
5868       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5869       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5870
5871       while (__first1 != __last1 && __first2 != __last2)
5872         if (*__first1 < *__first2)
5873           {
5874             *__result = *__first1;
5875             ++__first1;
5876             ++__result;
5877           }
5878         else if (*__first2 < *__first1)
5879           {
5880             *__result = *__first2;
5881             ++__first2;
5882             ++__result;
5883           }
5884         else
5885           {
5886             ++__first1;
5887             ++__first2;
5888           }
5889       return std::copy(__first2, __last2, std::copy(__first1,
5890                                                     __last1, __result));
5891     }
5892
5893   /**
5894    *  @brief  Return the symmetric difference of two sorted ranges using
5895    *  comparison functor.
5896    *  @ingroup set_algorithms
5897    *  @param  first1  Start of first range.
5898    *  @param  last1   End of first range.
5899    *  @param  first2  Start of second range.
5900    *  @param  last2   End of second range.
5901    *  @param  comp    The comparison functor.
5902    *  @return  End of the output range.
5903    *  @ingroup set_algorithms
5904    *
5905    *  This operation iterates over both ranges, copying elements present in
5906    *  one range but not the other in order to the output range.  Iterators
5907    *  increment for each range.  When the current element of one range is less
5908    *  than the other according to @a comp, that element is copied and the
5909    *  iterator advances.  If an element is contained in both ranges according
5910    *  to @a comp, no elements are copied and both ranges advance.  The output
5911    *  range may not overlap either input range.
5912   */
5913   template<typename _InputIterator1, typename _InputIterator2,
5914            typename _OutputIterator, typename _Compare>
5915     _OutputIterator
5916     set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5917                              _InputIterator2 __first2, _InputIterator2 __last2,
5918                              _OutputIterator __result,
5919                              _Compare __comp)
5920     {
5921       typedef typename iterator_traits<_InputIterator1>::value_type
5922         _ValueType1;
5923       typedef typename iterator_traits<_InputIterator2>::value_type
5924         _ValueType2;
5925
5926       // concept requirements
5927       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5928       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5929       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5930                                   _ValueType1>)
5931       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5932                                   _ValueType2>)
5933       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5934                                   _ValueType1, _ValueType2>)
5935       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5936                                   _ValueType2, _ValueType1>)
5937       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5938       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5939
5940       while (__first1 != __last1 && __first2 != __last2)
5941         if (__comp(*__first1, *__first2))
5942           {
5943             *__result = *__first1;
5944             ++__first1;
5945             ++__result;
5946           }
5947         else if (__comp(*__first2, *__first1))
5948           {
5949             *__result = *__first2;
5950             ++__first2;
5951             ++__result;
5952           }
5953         else
5954           {
5955             ++__first1;
5956             ++__first2;
5957           }
5958       return std::copy(__first2, __last2, 
5959                        std::copy(__first1, __last1, __result));
5960     }
5961
5962
5963   /**
5964    *  @brief  Return the minimum element in a range.
5965    *  @ingroup sorting_algorithms
5966    *  @param  first  Start of range.
5967    *  @param  last   End of range.
5968    *  @return  Iterator referencing the first instance of the smallest value.
5969   */
5970   template<typename _ForwardIterator>
5971     _ForwardIterator
5972     min_element(_ForwardIterator __first, _ForwardIterator __last)
5973     {
5974       // concept requirements
5975       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5976       __glibcxx_function_requires(_LessThanComparableConcept<
5977             typename iterator_traits<_ForwardIterator>::value_type>)
5978       __glibcxx_requires_valid_range(__first, __last);
5979
5980       if (__first == __last)
5981         return __first;
5982       _ForwardIterator __result = __first;
5983       while (++__first != __last)
5984         if (*__first < *__result)
5985           __result = __first;
5986       return __result;
5987     }
5988
5989   /**
5990    *  @brief  Return the minimum element in a range using comparison functor.
5991    *  @ingroup sorting_algorithms
5992    *  @param  first  Start of range.
5993    *  @param  last   End of range.
5994    *  @param  comp   Comparison functor.
5995    *  @return  Iterator referencing the first instance of the smallest value
5996    *  according to comp.
5997   */
5998   template<typename _ForwardIterator, typename _Compare>
5999     _ForwardIterator
6000     min_element(_ForwardIterator __first, _ForwardIterator __last,
6001                 _Compare __comp)
6002     {
6003       // concept requirements
6004       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6005       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6006             typename iterator_traits<_ForwardIterator>::value_type,
6007             typename iterator_traits<_ForwardIterator>::value_type>)
6008       __glibcxx_requires_valid_range(__first, __last);
6009
6010       if (__first == __last)
6011         return __first;
6012       _ForwardIterator __result = __first;
6013       while (++__first != __last)
6014         if (__comp(*__first, *__result))
6015           __result = __first;
6016       return __result;
6017     }
6018
6019   /**
6020    *  @brief  Return the maximum element in a range.
6021    *  @ingroup sorting_algorithms
6022    *  @param  first  Start of range.
6023    *  @param  last   End of range.
6024    *  @return  Iterator referencing the first instance of the largest value.
6025   */
6026   template<typename _ForwardIterator>
6027     _ForwardIterator
6028     max_element(_ForwardIterator __first, _ForwardIterator __last)
6029     {
6030       // concept requirements
6031       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6032       __glibcxx_function_requires(_LessThanComparableConcept<
6033             typename iterator_traits<_ForwardIterator>::value_type>)
6034       __glibcxx_requires_valid_range(__first, __last);
6035
6036       if (__first == __last)
6037         return __first;
6038       _ForwardIterator __result = __first;
6039       while (++__first != __last)
6040         if (*__result < *__first)
6041           __result = __first;
6042       return __result;
6043     }
6044
6045   /**
6046    *  @brief  Return the maximum element in a range using comparison functor.
6047    *  @ingroup sorting_algorithms
6048    *  @param  first  Start of range.
6049    *  @param  last   End of range.
6050    *  @param  comp   Comparison functor.
6051    *  @return  Iterator referencing the first instance of the largest value
6052    *  according to comp.
6053   */
6054   template<typename _ForwardIterator, typename _Compare>
6055     _ForwardIterator
6056     max_element(_ForwardIterator __first, _ForwardIterator __last,
6057                 _Compare __comp)
6058     {
6059       // concept requirements
6060       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
6061       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
6062             typename iterator_traits<_ForwardIterator>::value_type,
6063             typename iterator_traits<_ForwardIterator>::value_type>)
6064       __glibcxx_requires_valid_range(__first, __last);
6065
6066       if (__first == __last) return __first;
6067       _ForwardIterator __result = __first;
6068       while (++__first != __last)
6069         if (__comp(*__result, *__first))
6070           __result = __first;
6071       return __result;
6072     }
6073
6074 _GLIBCXX_END_NESTED_NAMESPACE
6075
6076 #endif /* _STL_ALGO_H */