OSDN Git Service

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