OSDN Git Service

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