stl_algo.h

Go to the documentation of this file.
00001 // Algorithm implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  *
00032  * Copyright (c) 1994
00033  * Hewlett-Packard Company
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Hewlett-Packard Company makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  *
00043  *
00044  * Copyright (c) 1996
00045  * Silicon Graphics Computer Systems, Inc.
00046  *
00047  * Permission to use, copy, modify, distribute and sell this software
00048  * and its documentation for any purpose is hereby granted without fee,
00049  * provided that the above copyright notice appear in all copies and
00050  * that both that copyright notice and this permission notice appear
00051  * in supporting documentation.  Silicon Graphics makes no
00052  * representations about the suitability of this software for any
00053  * purpose.  It is provided "as is" without express or implied warranty.
00054  */
00055 
00056 /** @file stl_algo.h
00057  *  This is an internal header file, included by other library headers.
00058  *  You should not attempt to use it directly.
00059  */
00060 
00061 #ifndef __GLIBCPP_INTERNAL_ALGO_H
00062 #define __GLIBCPP_INTERNAL_ALGO_H
00063 
00064 #include <bits/stl_heap.h>
00065 #include <bits/stl_tempbuf.h>     // for _Temporary_buffer
00066 
00067 // See concept_check.h for the __glibcpp_*_requires macros.
00068 
00069 namespace std
00070 {
00071 
00072   /**
00073    *  @brief Find the median of three values.
00074    *  @param  a  A value.
00075    *  @param  b  A value.
00076    *  @param  c  A value.
00077    *  @return One of @p a, @p b or @p c.
00078    *
00079    *  If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
00080    *  then the value returned will be @c m.
00081    *  This is an SGI extension.
00082    *  @ingroup SGIextensions
00083   */
00084   template<typename _Tp>
00085   inline const _Tp&
00086     __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
00087     {
00088       // concept requirements
00089       __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
00090       if (__a < __b)
00091     if (__b < __c)
00092       return __b;
00093     else if (__a < __c)
00094       return __c;
00095     else
00096       return __a;
00097       else if (__a < __c)
00098     return __a;
00099       else if (__b < __c)
00100     return __c;
00101       else
00102     return __b;
00103     }
00104 
00105   /**
00106    *  @brief Find the median of three values using a predicate for comparison.
00107    *  @param  a     A value.
00108    *  @param  b     A value.
00109    *  @param  c     A value.
00110    *  @param  comp  A binary predicate.
00111    *  @return One of @p a, @p b or @p c.
00112    *
00113    *  If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
00114    *  and @p comp(m,n) are both true then the value returned will be @c m.
00115    *  This is an SGI extension.
00116    *  @ingroup SGIextensions
00117   */
00118   template<typename _Tp, typename _Compare>
00119     inline const _Tp&
00120     __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
00121     {
00122       // concept requirements
00123       __glibcpp_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>)
00124       if (__comp(__a, __b))
00125     if (__comp(__b, __c))
00126       return __b;
00127     else if (__comp(__a, __c))
00128       return __c;
00129     else
00130       return __a;
00131       else if (__comp(__a, __c))
00132     return __a;
00133       else if (__comp(__b, __c))
00134     return __c;
00135       else
00136     return __b;
00137     }
00138 
00139   /**
00140    *  @brief Apply a function to every element of a sequence.
00141    *  @param  first  An input iterator.
00142    *  @param  last   An input iterator.
00143    *  @param  f      A unary function object.
00144    *  @return   @p f.
00145    *
00146    *  Applies the function object @p f to each element in the range
00147    *  @p [first,last).  @p f must not modify the order of the sequence.
00148    *  If @p f has a return value it is ignored.
00149   */
00150   template<typename _InputIter, typename _Function>
00151     _Function
00152     for_each(_InputIter __first, _InputIter __last, _Function __f)
00153     {
00154       // concept requirements
00155       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00156       for ( ; __first != __last; ++__first)
00157     __f(*__first);
00158       return __f;
00159     }
00160 
00161   /**
00162    *  @if maint
00163    *  This is an overload used by find() for the Input Iterator case.
00164    *  @endif
00165   */
00166   template<typename _InputIter, typename _Tp>
00167     inline _InputIter
00168     find(_InputIter __first, _InputIter __last,
00169      const _Tp& __val,
00170      input_iterator_tag)
00171     {
00172       while (__first != __last && !(*__first == __val))
00173     ++__first;
00174       return __first;
00175     }
00176 
00177   /**
00178    *  @if maint
00179    *  This is an overload used by find_if() for the Input Iterator case.
00180    *  @endif
00181   */
00182   template<typename _InputIter, typename _Predicate>
00183     inline _InputIter
00184     find_if(_InputIter __first, _InputIter __last,
00185         _Predicate __pred,
00186         input_iterator_tag)
00187     {
00188       while (__first != __last && !__pred(*__first))
00189     ++__first;
00190       return __first;
00191     }
00192 
00193   /**
00194    *  @if maint
00195    *  This is an overload used by find() for the RAI case.
00196    *  @endif
00197   */
00198   template<typename _RandomAccessIter, typename _Tp>
00199     _RandomAccessIter
00200     find(_RandomAccessIter __first, _RandomAccessIter __last,
00201      const _Tp& __val,
00202      random_access_iterator_tag)
00203     {
00204       typename iterator_traits<_RandomAccessIter>::difference_type __trip_count
00205     = (__last - __first) >> 2;
00206 
00207       for ( ; __trip_count > 0 ; --__trip_count) {
00208     if (*__first == __val) return __first;
00209     ++__first;
00210 
00211     if (*__first == __val) return __first;
00212     ++__first;
00213 
00214     if (*__first == __val) return __first;
00215     ++__first;
00216 
00217     if (*__first == __val) return __first;
00218     ++__first;
00219       }
00220 
00221       switch(__last - __first) {
00222       case 3:
00223     if (*__first == __val) return __first;
00224     ++__first;
00225       case 2:
00226     if (*__first == __val) return __first;
00227     ++__first;
00228       case 1:
00229     if (*__first == __val) return __first;
00230     ++__first;
00231       case 0:
00232       default:
00233     return __last;
00234       }
00235     }
00236 
00237   /**
00238    *  @if maint
00239    *  This is an overload used by find_if() for the RAI case.
00240    *  @endif
00241   */
00242   template<typename _RandomAccessIter, typename _Predicate>
00243     _RandomAccessIter
00244     find_if(_RandomAccessIter __first, _RandomAccessIter __last,
00245         _Predicate __pred,
00246         random_access_iterator_tag)
00247     {
00248       typename iterator_traits<_RandomAccessIter>::difference_type __trip_count
00249     = (__last - __first) >> 2;
00250 
00251       for ( ; __trip_count > 0 ; --__trip_count) {
00252     if (__pred(*__first)) return __first;
00253     ++__first;
00254 
00255     if (__pred(*__first)) return __first;
00256     ++__first;
00257 
00258     if (__pred(*__first)) return __first;
00259     ++__first;
00260 
00261     if (__pred(*__first)) return __first;
00262     ++__first;
00263       }
00264 
00265       switch(__last - __first) {
00266       case 3:
00267     if (__pred(*__first)) return __first;
00268     ++__first;
00269       case 2:
00270     if (__pred(*__first)) return __first;
00271     ++__first;
00272       case 1:
00273     if (__pred(*__first)) return __first;
00274     ++__first;
00275       case 0:
00276       default:
00277     return __last;
00278       }
00279     }
00280 
00281   /**
00282    *  @brief Find the first occurrence of a value in a sequence.
00283    *  @param  first  An input iterator.
00284    *  @param  last   An input iterator.
00285    *  @param  val    The value to find.
00286    *  @return   The first iterator @c i in the range @p [first,last)
00287    *  such that @c *i == @p val, or @p last if no such iterator exists.
00288   */
00289   template<typename _InputIter, typename _Tp>
00290     inline _InputIter
00291     find(_InputIter __first, _InputIter __last,
00292      const _Tp& __val)
00293     {
00294       // concept requirements
00295       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00296       __glibcpp_function_requires(_EqualOpConcept<
00297         typename iterator_traits<_InputIter>::value_type, _Tp>)
00298       return find(__first, __last, __val, __iterator_category(__first));
00299     }
00300 
00301   /**
00302    *  @brief Find the first element in a sequence for which a predicate is true.
00303    *  @param  first  An input iterator.
00304    *  @param  last   An input iterator.
00305    *  @param  pred   A predicate.
00306    *  @return   The first iterator @c i in the range @p [first,last)
00307    *  such that @p pred(*i) is true, or @p last if no such iterator exists.
00308   */
00309   template<typename _InputIter, typename _Predicate>
00310     inline _InputIter
00311     find_if(_InputIter __first, _InputIter __last,
00312         _Predicate __pred)
00313     {
00314       // concept requirements
00315       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00316       __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
00317           typename iterator_traits<_InputIter>::value_type>)
00318       return find_if(__first, __last, __pred, __iterator_category(__first));
00319     }
00320 
00321   /**
00322    *  @brief Find two adjacent values in a sequence that are equal.
00323    *  @param  first  A forward iterator.
00324    *  @param  last   A forward iterator.
00325    *  @return   The first iterator @c i such that @c i and @c i+1 are both
00326    *  valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
00327    *  or @p last if no such iterator exists.
00328   */
00329   template<typename _ForwardIter>
00330     _ForwardIter
00331     adjacent_find(_ForwardIter __first, _ForwardIter __last)
00332     {
00333       // concept requirements
00334       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
00335       __glibcpp_function_requires(_EqualityComparableConcept<
00336         typename iterator_traits<_ForwardIter>::value_type>)
00337       if (__first == __last)
00338     return __last;
00339       _ForwardIter __next = __first;
00340       while(++__next != __last) {
00341     if (*__first == *__next)
00342       return __first;
00343     __first = __next;
00344       }
00345       return __last;
00346     }
00347 
00348   /**
00349    *  @brief Find two adjacent values in a sequence using a predicate.
00350    *  @param  first         A forward iterator.
00351    *  @param  last          A forward iterator.
00352    *  @param  binary_pred   A binary predicate.
00353    *  @return   The first iterator @c i such that @c i and @c i+1 are both
00354    *  valid iterators in @p [first,last) and such that
00355    *  @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
00356    *  exists.
00357   */
00358   template<typename _ForwardIter, typename _BinaryPredicate>
00359     _ForwardIter
00360     adjacent_find(_ForwardIter __first, _ForwardIter __last,
00361           _BinaryPredicate __binary_pred)
00362     {
00363       // concept requirements
00364       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
00365       __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
00366         typename iterator_traits<_ForwardIter>::value_type,
00367         typename iterator_traits<_ForwardIter>::value_type>)
00368       if (__first == __last)
00369     return __last;
00370       _ForwardIter __next = __first;
00371       while(++__next != __last) {
00372     if (__binary_pred(*__first, *__next))
00373       return __first;
00374     __first = __next;
00375       }
00376       return __last;
00377     }
00378 
00379   /**
00380    *  @brief Count the number of copies of a value in a sequence.
00381    *  @param  first  An input iterator.
00382    *  @param  last   An input iterator.
00383    *  @param  value  The value to be counted.
00384    *  @return   The number of iterators @c i in the range @p [first,last)
00385    *  for which @c *i == @p value
00386   */
00387   template<typename _InputIter, typename _Tp>
00388     typename iterator_traits<_InputIter>::difference_type
00389     count(_InputIter __first, _InputIter __last, const _Tp& __value)
00390     {
00391       // concept requirements
00392       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00393       __glibcpp_function_requires(_EqualityComparableConcept<
00394         typename iterator_traits<_InputIter>::value_type >)
00395       __glibcpp_function_requires(_EqualityComparableConcept<_Tp>)
00396       typename iterator_traits<_InputIter>::difference_type __n = 0;
00397       for ( ; __first != __last; ++__first)
00398     if (*__first == __value)
00399       ++__n;
00400       return __n;
00401     }
00402 
00403   /**
00404    *  @brief Count the elements of a sequence for which a predicate is true.
00405    *  @param  first  An input iterator.
00406    *  @param  last   An input iterator.
00407    *  @param  pred   A predicate.
00408    *  @return   The number of iterators @c i in the range @p [first,last)
00409    *  for which @p pred(*i) is true.
00410   */
00411   template<typename _InputIter, typename _Predicate>
00412     typename iterator_traits<_InputIter>::difference_type
00413     count_if(_InputIter __first, _InputIter __last, _Predicate __pred)
00414     {
00415       // concept requirements
00416       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00417       __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
00418         typename iterator_traits<_InputIter>::value_type>)
00419       typename iterator_traits<_InputIter>::difference_type __n = 0;
00420       for ( ; __first != __last; ++__first)
00421     if (__pred(*__first))
00422       ++__n;
00423       return __n;
00424     }
00425 
00426 
00427   /**
00428    *  @brief Search a sequence for a matching sub-sequence.
00429    *  @param  first1  A forward iterator.
00430    *  @param  last1   A forward iterator.
00431    *  @param  first2  A forward iterator.
00432    *  @param  last2   A forward iterator.
00433    *  @return   The first iterator @c i in the range
00434    *  @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
00435    *  for each @c N in the range @p [0,last2-first2), or @p last1 if no
00436    *  such iterator exists.
00437    *
00438    *  Searches the range @p [first1,last1) for a sub-sequence that compares
00439    *  equal value-by-value with the sequence given by @p [first2,last2) and
00440    *  returns an iterator to the first element of the sub-sequence, or
00441    *  @p last1 if the sub-sequence is not found.
00442    *
00443    *  Because the sub-sequence must lie completely within the range
00444    *  @p [first1,last1) it must start at a position less than
00445    *  @p last1-(last2-first2) where @p last2-first2 is the length of the
00446    *  sub-sequence.
00447    *  This means that the returned iterator @c i will be in the range
00448    *  @p [first1,last1-(last2-first2))
00449   */
00450   template<typename _ForwardIter1, typename _ForwardIter2>
00451     _ForwardIter1
00452     search(_ForwardIter1 __first1, _ForwardIter1 __last1,
00453        _ForwardIter2 __first2, _ForwardIter2 __last2)
00454     {
00455       // concept requirements
00456       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter1>)
00457       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter2>)
00458       __glibcpp_function_requires(_EqualOpConcept<
00459         typename iterator_traits<_ForwardIter1>::value_type,
00460         typename iterator_traits<_ForwardIter2>::value_type>)
00461 
00462       // Test for empty ranges
00463       if (__first1 == __last1 || __first2 == __last2)
00464     return __first1;
00465 
00466       // Test for a pattern of length 1.
00467       _ForwardIter2 __tmp(__first2);
00468       ++__tmp;
00469       if (__tmp == __last2)
00470     return find(__first1, __last1, *__first2);
00471 
00472       // General case.
00473 
00474       _ForwardIter2 __p1, __p;
00475 
00476       __p1 = __first2; ++__p1;
00477 
00478       _ForwardIter1 __current = __first1;
00479 
00480       while (__first1 != __last1) {
00481     __first1 = find(__first1, __last1, *__first2);
00482     if (__first1 == __last1)
00483       return __last1;
00484 
00485     __p = __p1;
00486     __current = __first1;
00487     if (++__current == __last1)
00488       return __last1;
00489 
00490     while (*__current == *__p) {
00491       if (++__p == __last2)
00492         return __first1;
00493       if (++__current == __last1)
00494         return __last1;
00495     }
00496 
00497     ++__first1;
00498       }
00499       return __first1;
00500     }
00501 
00502   /**
00503    *  @brief Search a sequence for a matching sub-sequence using a predicate.
00504    *  @param  first1     A forward iterator.
00505    *  @param  last1      A forward iterator.
00506    *  @param  first2     A forward iterator.
00507    *  @param  last2      A forward iterator.
00508    *  @param  predicate  A binary predicate.
00509    *  @return   The first iterator @c i in the range
00510    *  @p [first1,last1-(last2-first2)) such that
00511    *  @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
00512    *  @p [0,last2-first2), or @p last1 if no such iterator exists.
00513    *
00514    *  Searches the range @p [first1,last1) for a sub-sequence that compares
00515    *  equal value-by-value with the sequence given by @p [first2,last2),
00516    *  using @p predicate to determine equality, and returns an iterator
00517    *  to the first element of the sub-sequence, or @p last1 if no such
00518    *  iterator exists.
00519    *
00520    *  @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
00521   */
00522   template<typename _ForwardIter1, typename _ForwardIter2, typename _BinaryPred>
00523     _ForwardIter1
00524     search(_ForwardIter1 __first1, _ForwardIter1 __last1,
00525        _ForwardIter2 __first2, _ForwardIter2 __last2,
00526        _BinaryPred  __predicate)
00527     {
00528       // concept requirements
00529       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter1>)
00530       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter2>)
00531       __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPred,
00532         typename iterator_traits<_ForwardIter1>::value_type,
00533         typename iterator_traits<_ForwardIter2>::value_type>)
00534 
00535       // Test for empty ranges
00536       if (__first1 == __last1 || __first2 == __last2)
00537     return __first1;
00538 
00539       // Test for a pattern of length 1.
00540       _ForwardIter2 __tmp(__first2);
00541       ++__tmp;
00542       if (__tmp == __last2) {
00543     while (__first1 != __last1 && !__predicate(*__first1, *__first2))
00544       ++__first1;
00545     return __first1;
00546       }
00547 
00548       // General case.
00549 
00550       _ForwardIter2 __p1, __p;
00551 
00552       __p1 = __first2; ++__p1;
00553 
00554       _ForwardIter1 __current = __first1;
00555 
00556       while (__first1 != __last1) {
00557     while (__first1 != __last1) {
00558       if (__predicate(*__first1, *__first2))
00559         break;
00560       ++__first1;
00561     }
00562     while (__first1 != __last1 && !__predicate(*__first1, *__first2))
00563       ++__first1;
00564     if (__first1 == __last1)
00565       return __last1;
00566 
00567     __p = __p1;
00568     __current = __first1;
00569     if (++__current == __last1) return __last1;
00570 
00571     while (__predicate(*__current, *__p)) {
00572       if (++__p == __last2)
00573         return __first1;
00574       if (++__current == __last1)
00575         return __last1;
00576     }
00577 
00578     ++__first1;
00579       }
00580       return __first1;
00581     }
00582 
00583   /**
00584    *  @brief Search a sequence for a number of consecutive values.
00585    *  @param  first  A forward iterator.
00586    *  @param  last   A forward iterator.
00587    *  @param  count  The number of consecutive values.
00588    *  @param  val    The value to find.
00589    *  @return   The first iterator @c i in the range @p [first,last-count)
00590    *  such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
00591    *  or @p last if no such iterator exists.
00592    *
00593    *  Searches the range @p [first,last) for @p count consecutive elements
00594    *  equal to @p val.
00595   */
00596   template<typename _ForwardIter, typename _Integer, typename _Tp>
00597     _ForwardIter
00598     search_n(_ForwardIter __first, _ForwardIter __last,
00599          _Integer __count, const _Tp& __val)
00600     {
00601       // concept requirements
00602       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
00603       __glibcpp_function_requires(_EqualityComparableConcept<
00604         typename iterator_traits<_ForwardIter>::value_type>)
00605       __glibcpp_function_requires(_EqualityComparableConcept<_Tp>)
00606 
00607       if (__count <= 0)
00608     return __first;
00609       else {
00610     __first = find(__first, __last, __val);
00611     while (__first != __last) {
00612       typename iterator_traits<_ForwardIter>::difference_type __n = __count;
00613       --__n;
00614       _ForwardIter __i = __first;
00615       ++__i;
00616       while (__i != __last && __n != 0 && *__i == __val) {
00617         ++__i;
00618         --__n;
00619       }
00620       if (__n == 0)
00621         return __first;
00622       else
00623         __first = find(__i, __last, __val);
00624     }
00625     return __last;
00626       }
00627     }
00628 
00629   /**
00630    *  @brief Search a sequence for a number of consecutive values using a
00631    *         predicate.
00632    *  @param  first        A forward iterator.
00633    *  @param  last         A forward iterator.
00634    *  @param  count        The number of consecutive values.
00635    *  @param  val          The value to find.
00636    *  @param  binary_pred  A binary predicate.
00637    *  @return   The first iterator @c i in the range @p [first,last-count)
00638    *  such that @p binary_pred(*(i+N),val) is true for each @c N in the
00639    *  range @p [0,count), or @p last if no such iterator exists.
00640    *
00641    *  Searches the range @p [first,last) for @p count consecutive elements
00642    *  for which the predicate returns true.
00643   */
00644   template<typename _ForwardIter, typename _Integer, typename _Tp,
00645            typename _BinaryPred>
00646     _ForwardIter
00647     search_n(_ForwardIter __first, _ForwardIter __last,
00648          _Integer __count, const _Tp& __val,
00649          _BinaryPred __binary_pred)
00650     {
00651       // concept requirements
00652       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
00653       __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPred,
00654         typename iterator_traits<_ForwardIter>::value_type, _Tp>)
00655 
00656       if (__count <= 0)
00657     return __first;
00658       else {
00659     while (__first != __last) {
00660       if (__binary_pred(*__first, __val))
00661         break;
00662       ++__first;
00663     }
00664     while (__first != __last) {
00665       typename iterator_traits<_ForwardIter>::difference_type __n = __count;
00666       --__n;
00667       _ForwardIter __i = __first;
00668       ++__i;
00669       while (__i != __last && __n != 0 && __binary_pred(*__i, __val)) {
00670         ++__i;
00671         --__n;
00672       }
00673       if (__n == 0)
00674         return __first;
00675       else {
00676         while (__i != __last) {
00677           if (__binary_pred(*__i, __val))
00678         break;
00679           ++__i;
00680         }
00681         __first = __i;
00682       }
00683     }
00684     return __last;
00685       }
00686     }
00687 
00688   /**
00689    *  @brief Swap the elements of two sequences.
00690    *  @param  first1  A forward iterator.
00691    *  @param  last1   A forward iterator.
00692    *  @param  first2  A forward iterator.
00693    *  @return   An iterator equal to @p first2+(last1-first1).
00694    *
00695    *  Swaps each element in the range @p [first1,last1) with the
00696    *  corresponding element in the range @p [first2,(last1-first1)).
00697    *  The ranges must not overlap.
00698   */
00699   template<typename _ForwardIter1, typename _ForwardIter2>
00700     _ForwardIter2
00701     swap_ranges(_ForwardIter1 __first1, _ForwardIter1 __last1,
00702         _ForwardIter2 __first2)
00703     {
00704       // concept requirements
00705       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter1>)
00706       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter2>)
00707       __glibcpp_function_requires(_ConvertibleConcept<
00708         typename iterator_traits<_ForwardIter1>::value_type,
00709         typename iterator_traits<_ForwardIter2>::value_type>)
00710       __glibcpp_function_requires(_ConvertibleConcept<
00711         typename iterator_traits<_ForwardIter2>::value_type,
00712         typename iterator_traits<_ForwardIter1>::value_type>)
00713 
00714       for ( ; __first1 != __last1; ++__first1, ++__first2)
00715     iter_swap(__first1, __first2);
00716       return __first2;
00717     }
00718 
00719   /**
00720    *  @brief Perform an operation on a sequence.
00721    *  @param  first     An input iterator.
00722    *  @param  last      An input iterator.
00723    *  @param  result    An output iterator.
00724    *  @param  unary_op  A unary operator.
00725    *  @return   An output iterator equal to @p result+(last-first).
00726    *
00727    *  Applies the operator to each element in the input range and assigns
00728    *  the results to successive elements of the output sequence.
00729    *  Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
00730    *  range @p [0,last-first).
00731    *
00732    *  @p unary_op must not alter its argument.
00733   */
00734   template<typename _InputIter, typename _OutputIter, typename _UnaryOperation>
00735     _OutputIter
00736     transform(_InputIter __first, _InputIter __last,
00737           _OutputIter __result, _UnaryOperation __unary_op)
00738     {
00739       // concept requirements
00740       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00741       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
00742             // "the type returned by a _UnaryOperation"
00743             __typeof__(__unary_op(*__first))>)
00744 
00745       for ( ; __first != __last; ++__first, ++__result)
00746     *__result = __unary_op(*__first);
00747       return __result;
00748     }
00749 
00750   /**
00751    *  @brief Perform an operation on corresponding elements of two sequences.
00752    *  @param  first1     An input iterator.
00753    *  @param  last1      An input iterator.
00754    *  @param  first2     An input iterator.
00755    *  @param  result     An output iterator.
00756    *  @param  binary_op  A binary operator.
00757    *  @return   An output iterator equal to @p result+(last-first).
00758    *
00759    *  Applies the operator to the corresponding elements in the two
00760    *  input ranges and assigns the results to successive elements of the
00761    *  output sequence.
00762    *  Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
00763    *  @c N in the range @p [0,last1-first1).
00764    *
00765    *  @p binary_op must not alter either of its arguments.
00766   */
00767   template<typename _InputIter1, typename _InputIter2, typename _OutputIter,
00768        typename _BinaryOperation>
00769     _OutputIter
00770     transform(_InputIter1 __first1, _InputIter1 __last1,
00771           _InputIter2 __first2, _OutputIter __result,
00772           _BinaryOperation __binary_op)
00773     {
00774       // concept requirements
00775       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
00776       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
00777       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
00778             // "the type returned by a _BinaryOperation"
00779             __typeof__(__binary_op(*__first1,*__first2))>)
00780 
00781       for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
00782     *__result = __binary_op(*__first1, *__first2);
00783       return __result;
00784     }
00785 
00786   /**
00787    *  @brief Replace each occurrence of one value in a sequence with another
00788    *         value.
00789    *  @param  first      A forward iterator.
00790    *  @param  last       A forward iterator.
00791    *  @param  old_value  The value to be replaced.
00792    *  @param  new_value  The replacement value.
00793    *  @return   replace() returns no value.
00794    *
00795    *  For each iterator @c i in the range @p [first,last) if @c *i ==
00796    *  @p old_value then the assignment @c *i = @p new_value is performed.
00797   */
00798   template<typename _ForwardIter, typename _Tp>
00799     void
00800     replace(_ForwardIter __first, _ForwardIter __last,
00801         const _Tp& __old_value, const _Tp& __new_value)
00802     {
00803       // concept requirements
00804       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
00805       __glibcpp_function_requires(_EqualOpConcept<
00806         typename iterator_traits<_ForwardIter>::value_type, _Tp>)
00807       __glibcpp_function_requires(_ConvertibleConcept<_Tp,
00808         typename iterator_traits<_ForwardIter>::value_type>)
00809 
00810       for ( ; __first != __last; ++__first)
00811     if (*__first == __old_value)
00812       *__first = __new_value;
00813     }
00814 
00815   /**
00816    *  @brief Replace each value in a sequence for which a predicate returns
00817    *         true with another value.
00818    *  @param  first      A forward iterator.
00819    *  @param  last       A forward iterator.
00820    *  @param  pred       A predicate.
00821    *  @param  new_value  The replacement value.
00822    *  @return   replace_if() returns no value.
00823    *
00824    *  For each iterator @c i in the range @p [first,last) if @p pred(*i)
00825    *  is true then the assignment @c *i = @p new_value is performed.
00826   */
00827   template<typename _ForwardIter, typename _Predicate, typename _Tp>
00828     void
00829     replace_if(_ForwardIter __first, _ForwardIter __last,
00830            _Predicate __pred, const _Tp& __new_value)
00831     {
00832       // concept requirements
00833       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
00834       __glibcpp_function_requires(_ConvertibleConcept<_Tp,
00835         typename iterator_traits<_ForwardIter>::value_type>)
00836       __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
00837         typename iterator_traits<_ForwardIter>::value_type>)
00838 
00839       for ( ; __first != __last; ++__first)
00840     if (__pred(*__first))
00841       *__first = __new_value;
00842     }
00843 
00844   /**
00845    *  @brief Copy a sequence, replacing each element of one value with another
00846    *         value.
00847    *  @param  first      An input iterator.
00848    *  @param  last       An input iterator.
00849    *  @param  result     An output iterator.
00850    *  @param  old_value  The value to be replaced.
00851    *  @param  new_value  The replacement value.
00852    *  @return   The end of the output sequence, @p result+(last-first).
00853    *
00854    *  Copies each element in the input range @p [first,last) to the
00855    *  output range @p [result,result+(last-first)) replacing elements
00856    *  equal to @p old_value with @p new_value.
00857   */
00858   template<typename _InputIter, typename _OutputIter, typename _Tp>
00859     _OutputIter
00860     replace_copy(_InputIter __first, _InputIter __last,
00861          _OutputIter __result,
00862          const _Tp& __old_value, const _Tp& __new_value)
00863     {
00864       // concept requirements
00865       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00866       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
00867         typename iterator_traits<_InputIter>::value_type>)
00868       __glibcpp_function_requires(_EqualOpConcept<
00869         typename iterator_traits<_InputIter>::value_type, _Tp>)
00870 
00871       for ( ; __first != __last; ++__first, ++__result)
00872     *__result = *__first == __old_value ? __new_value : *__first;
00873       return __result;
00874     }
00875 
00876   /**
00877    *  @brief Copy a sequence, replacing each value for which a predicate
00878    *         returns true with another value.
00879    *  @param  first      An input iterator.
00880    *  @param  last       An input iterator.
00881    *  @param  result     An output iterator.
00882    *  @param  pred       A predicate.
00883    *  @param  new_value  The replacement value.
00884    *  @return   The end of the output sequence, @p result+(last-first).
00885    *
00886    *  Copies each element in the range @p [first,last) to the range
00887    *  @p [result,result+(last-first)) replacing elements for which
00888    *  @p pred returns true with @p new_value.
00889   */
00890   template<typename _InputIter, typename _OutputIter, typename _Predicate,
00891            typename _Tp>
00892     _OutputIter
00893     replace_copy_if(_InputIter __first, _InputIter __last,
00894             _OutputIter __result,
00895             _Predicate __pred, const _Tp& __new_value)
00896     {
00897       // concept requirements
00898       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00899       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
00900         typename iterator_traits<_InputIter>::value_type>)
00901       __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
00902         typename iterator_traits<_InputIter>::value_type>)
00903 
00904       for ( ; __first != __last; ++__first, ++__result)
00905     *__result = __pred(*__first) ? __new_value : *__first;
00906       return __result;
00907     }
00908 
00909   /**
00910    *  @brief Assign the result of a function object to each value in a
00911    *         sequence.
00912    *  @param  first  A forward iterator.
00913    *  @param  last   A forward iterator.
00914    *  @param  gen    A function object taking no arguments.
00915    *  @return   generate() returns no value.
00916    *
00917    *  Performs the assignment @c *i = @p gen() for each @c i in the range
00918    *  @p [first,last).
00919   */
00920   template<typename _ForwardIter, typename _Generator>
00921     void
00922     generate(_ForwardIter __first, _ForwardIter __last, _Generator __gen)
00923     {
00924       // concept requirements
00925       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
00926       __glibcpp_function_requires(_GeneratorConcept<_Generator,
00927         typename iterator_traits<_ForwardIter>::value_type>)
00928 
00929       for ( ; __first != __last; ++__first)
00930     *__first = __gen();
00931     }
00932 
00933   /**
00934    *  @brief Assign the result of a function object to each value in a
00935    *         sequence.
00936    *  @param  first  A forward iterator.
00937    *  @param  n      The length of the sequence.
00938    *  @param  gen    A function object taking no arguments.
00939    *  @return   The end of the sequence, @p first+n
00940    *
00941    *  Performs the assignment @c *i = @p gen() for each @c i in the range
00942    *  @p [first,first+n).
00943   */
00944   template<typename _OutputIter, typename _Size, typename _Generator>
00945     _OutputIter
00946     generate_n(_OutputIter __first, _Size __n, _Generator __gen)
00947     {
00948       // concept requirements
00949       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
00950             // "the type returned by a _Generator"
00951             __typeof__(__gen())>)
00952 
00953       for ( ; __n > 0; --__n, ++__first)
00954     *__first = __gen();
00955       return __first;
00956     }
00957 
00958   /**
00959    *  @brief Copy a sequence, removing elements of a given value.
00960    *  @param  first   An input iterator.
00961    *  @param  last    An input iterator.
00962    *  @param  result  An output iterator.
00963    *  @param  value   The value to be removed.
00964    *  @return   An iterator designating the end of the resulting sequence.
00965    *
00966    *  Copies each element in the range @p [first,last) not equal to @p value
00967    *  to the range beginning at @p result.
00968    *  remove_copy() is stable, so the relative order of elements that are
00969    *  copied is unchanged.
00970   */
00971   template<typename _InputIter, typename _OutputIter, typename _Tp>
00972     _OutputIter
00973     remove_copy(_InputIter __first, _InputIter __last,
00974         _OutputIter __result, const _Tp& __value)
00975     {
00976       // concept requirements
00977       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00978       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
00979         typename iterator_traits<_InputIter>::value_type>)
00980       __glibcpp_function_requires(_EqualOpConcept<
00981         typename iterator_traits<_InputIter>::value_type, _Tp>)
00982 
00983       for ( ; __first != __last; ++__first)
00984     if (!(*__first == __value)) {
00985       *__result = *__first;
00986       ++__result;
00987     }
00988       return __result;
00989     }
00990 
00991   /**
00992    *  @brief Copy a sequence, removing elements for which a predicate is true.
00993    *  @param  first   An input iterator.
00994    *  @param  last    An input iterator.
00995    *  @param  result  An output iterator.
00996    *  @param  pred    A predicate.
00997    *  @return   An iterator designating the end of the resulting sequence.
00998    *
00999    *  Copies each element in the range @p [first,last) for which
01000    *  @p pred returns true to the range beginning at @p result.
01001    *
01002    *  remove_copy_if() is stable, so the relative order of elements that are
01003    *  copied is unchanged.
01004   */
01005   template<typename _InputIter, typename _OutputIter, typename _Predicate>
01006     _OutputIter
01007     remove_copy_if(_InputIter __first, _InputIter __last,
01008            _OutputIter __result, _Predicate __pred)
01009     {
01010       // concept requirements
01011       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
01012       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
01013         typename iterator_traits<_InputIter>::value_type>)
01014       __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
01015         typename iterator_traits<_InputIter>::value_type>)
01016 
01017       for ( ; __first != __last; ++__first)
01018     if (!__pred(*__first)) {
01019       *__result = *__first;
01020       ++__result;
01021     }
01022       return __result;
01023     }
01024 
01025   /**
01026    *  @brief Remove elements from a sequence.
01027    *  @param  first  An input iterator.
01028    *  @param  last   An input iterator.
01029    *  @param  value  The value to be removed.
01030    *  @return   An iterator designating the end of the resulting sequence.
01031    *
01032    *  All elements equal to @p value are removed from the range
01033    *  @p [first,last).
01034    *
01035    *  remove() is stable, so the relative order of elements that are
01036    *  not removed is unchanged.
01037    *
01038    *  Elements between the end of the resulting sequence and @p last
01039    *  are still present, but their value is unspecified.
01040   */
01041   template<typename _ForwardIter, typename _Tp>
01042     _ForwardIter
01043     remove(_ForwardIter __first, _ForwardIter __last,
01044        const _Tp& __value)
01045     {
01046       // concept requirements
01047       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
01048       __glibcpp_function_requires(_EqualOpConcept<
01049         typename iterator_traits<_ForwardIter>::value_type, _Tp>)
01050 
01051       __first = find(__first, __last, __value);
01052       _ForwardIter __i = __first;
01053       return __first == __last ? __first
01054                    : remove_copy(++__i, __last, __first, __value);
01055     }
01056 
01057   /**
01058    *  @brief Remove elements from a sequence using a predicate.
01059    *  @param  first  A forward iterator.
01060    *  @param  last   A forward iterator.
01061    *  @param  pred   A predicate.
01062    *  @return   An iterator designating the end of the resulting sequence.
01063    *
01064    *  All elements for which @p pred returns true are removed from the range
01065    *  @p [first,last).
01066    *
01067    *  remove_if() is stable, so the relative order of elements that are
01068    *  not removed is unchanged.
01069    *
01070    *  Elements between the end of the resulting sequence and @p last
01071    *  are still present, but their value is unspecified.
01072   */
01073   template<typename _ForwardIter, typename _Predicate>
01074     _ForwardIter
01075     remove_if(_ForwardIter __first, _ForwardIter __last,
01076           _Predicate __pred)
01077     {
01078       // concept requirements
01079       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
01080       __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
01081         typename iterator_traits<_ForwardIter>::value_type>)
01082 
01083       __first = find_if(__first, __last, __pred);
01084       _ForwardIter __i = __first;
01085       return __first == __last ? __first
01086                    : remove_copy_if(++__i, __last, __first, __pred);
01087     }
01088 
01089   /**
01090    *  @if maint
01091    *  This is an uglified unique_copy(_InputIter, _InputIter, _OutputIter)
01092    *  overloaded for output iterators.
01093    *  @endif
01094   */
01095   template<typename _InputIter, typename _OutputIter>
01096     _OutputIter
01097     __unique_copy(_InputIter __first, _InputIter __last,
01098           _OutputIter __result,
01099           output_iterator_tag)
01100     {
01101       // concept requirements -- taken care of in dispatching function
01102       typename iterator_traits<_InputIter>::value_type __value = *__first;
01103       *__result = __value;
01104       while (++__first != __last)
01105     if (!(__value == *__first)) {
01106       __value = *__first;
01107       *++__result = __value;
01108     }
01109       return ++__result;
01110     }
01111 
01112   /**
01113    *  @if maint
01114    *  This is an uglified unique_copy(_InputIter, _InputIter, _OutputIter)
01115    *  overloaded for forward iterators.
01116    *  @endif
01117   */
01118   template<typename _InputIter, typename _ForwardIter>
01119     _ForwardIter
01120     __unique_copy(_InputIter __first, _InputIter __last,
01121           _ForwardIter __result,
01122           forward_iterator_tag)
01123     {
01124       // concept requirements -- taken care of in dispatching function
01125       *__result = *__first;
01126       while (++__first != __last)
01127     if (!(*__result == *__first))
01128       *++__result = *__first;
01129       return ++__result;
01130     }
01131 
01132   /**
01133    *  @brief Copy a sequence, removing consecutive duplicate values.
01134    *  @param  first   An input iterator.
01135    *  @param  last    An input iterator.
01136    *  @param  result  An output iterator.
01137    *  @return   An iterator designating the end of the resulting sequence.
01138    *
01139    *  Copies each element in the range @p [first,last) to the range
01140    *  beginning at @p result, except that only the first element is copied
01141    *  from groups of consecutive elements that compare equal.
01142    *  unique_copy() is stable, so the relative order of elements that are
01143    *  copied is unchanged.
01144   */
01145   template<typename _InputIter, typename _OutputIter>
01146     inline _OutputIter
01147     unique_copy(_InputIter __first, _InputIter __last,
01148         _OutputIter __result)
01149     {
01150       // concept requirements
01151       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
01152       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
01153         typename iterator_traits<_InputIter>::value_type>)
01154       __glibcpp_function_requires(_EqualityComparableConcept<
01155         typename iterator_traits<_InputIter>::value_type>)
01156 
01157       typedef typename iterator_traits<_OutputIter>::iterator_category _IterType;
01158 
01159       if (__first == __last) return __result;
01160       return __unique_copy(__first, __last, __result, _IterType());
01161     }
01162 
01163   /**
01164    *  @if maint
01165    *  This is an uglified
01166    *  unique_copy(_InputIter, _InputIter, _OutputIter, _BinaryPredicate)
01167    *  overloaded for output iterators.
01168    *  @endif
01169   */
01170   template<typename _InputIter, typename _OutputIter, typename _BinaryPredicate>
01171     _OutputIter
01172     __unique_copy(_InputIter __first, _InputIter __last,
01173           _OutputIter __result,
01174           _BinaryPredicate __binary_pred,
01175           output_iterator_tag)
01176     {
01177       // concept requirements -- iterators already checked
01178       __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
01179       typename iterator_traits<_InputIter>::value_type,
01180       typename iterator_traits<_InputIter>::value_type>)
01181 
01182       typename iterator_traits<_InputIter>::value_type __value = *__first;
01183       *__result = __value;
01184       while (++__first != __last)
01185     if (!__binary_pred(__value, *__first)) {
01186       __value = *__first;
01187       *++__result = __value;
01188     }
01189       return ++__result;
01190     }
01191 
01192   /**
01193    *  @if maint
01194    *  This is an uglified
01195    *  unique_copy(_InputIter, _InputIter, _OutputIter, _BinaryPredicate)
01196    *  overloaded for forward iterators.
01197    *  @endif
01198   */
01199   template<typename _InputIter, typename _ForwardIter, typename _BinaryPredicate>
01200     _ForwardIter
01201     __unique_copy(_InputIter __first, _InputIter __last,
01202           _ForwardIter __result,
01203           _BinaryPredicate __binary_pred,
01204           forward_iterator_tag)
01205     {
01206       // concept requirements -- iterators already checked
01207       __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
01208         typename iterator_traits<_ForwardIter>::value_type,
01209         typename iterator_traits<_InputIter>::value_type>)
01210 
01211       *__result = *__first;
01212       while (++__first != __last)
01213     if (!__binary_pred(*__result, *__first)) *++__result = *__first;
01214       return ++__result;
01215     }
01216 
01217   /**
01218    *  @brief Copy a sequence, removing consecutive values using a predicate.
01219    *  @param  first        An input iterator.
01220    *  @param  last         An input iterator.
01221    *  @param  result       An output iterator.
01222    *  @param  binary_pred  A binary predicate.
01223    *  @return   An iterator designating the end of the resulting sequence.
01224    *
01225    *  Copies each element in the range @p [first,last) to the range
01226    *  beginning at @p result, except that only the first element is copied
01227    *  from groups of consecutive elements for which @p binary_pred returns
01228    *  true.
01229    *  unique_copy() is stable, so the relative order of elements that are
01230    *  copied is unchanged.
01231   */
01232   template<typename _InputIter, typename _OutputIter, typename _BinaryPredicate>
01233     inline _OutputIter
01234     unique_copy(_InputIter __first, _InputIter __last,
01235         _OutputIter __result,
01236         _BinaryPredicate __binary_pred)
01237     {
01238       // concept requirements -- predicates checked later
01239       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
01240       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
01241         typename iterator_traits<_InputIter>::value_type>)
01242 
01243       typedef typename iterator_traits<_OutputIter>::iterator_category _IterType;
01244 
01245       if (__first == __last) return __result;
01246       return __unique_copy(__first, __last,
01247 __result, __binary_pred, _IterType());
01248     }
01249 
01250   /**
01251    *  @brief Remove consecutive duplicate values from a sequence.
01252    *  @param  first  A forward iterator.
01253    *  @param  last   A forward iterator.
01254    *  @return  An iterator designating the end of the resulting sequence.
01255    *
01256    *  Removes all but the first element from each group of consecutive
01257    *  values that compare equal.
01258    *  unique() is stable, so the relative order of elements that are
01259    *  not removed is unchanged.
01260    *  Elements between the end of the resulting sequence and @p last
01261    *  are still present, but their value is unspecified.
01262   */
01263   template<typename _ForwardIter>
01264     _ForwardIter
01265     unique(_ForwardIter __first, _ForwardIter __last)
01266     {
01267       // concept requirements
01268       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
01269       __glibcpp_function_requires(_EqualityComparableConcept<
01270             typename iterator_traits<_ForwardIter>::value_type>)
01271 
01272       __first = adjacent_find(__first, __last);
01273       return unique_copy(__first, __last, __first);
01274     }
01275 
01276   /**
01277    *  @brief Remove consecutive values from a sequence using a predicate.
01278    *  @param  first        A forward iterator.
01279    *  @param  last         A forward iterator.
01280    *  @param  binary_pred  A binary predicate.
01281    *  @return  An iterator designating the end of the resulting sequence.
01282    *
01283    *  Removes all but the first element from each group of consecutive
01284    *  values for which @p binary_pred returns true.
01285    *  unique() is stable, so the relative order of elements that are
01286    *  not removed is unchanged.
01287    *  Elements between the end of the resulting sequence and @p last
01288    *  are still present, but their value is unspecified.
01289   */
01290   template<typename _ForwardIter, typename _BinaryPredicate>
01291     _ForwardIter
01292     unique(_ForwardIter __first, _ForwardIter __last,
01293            _BinaryPredicate __binary_pred)
01294     {
01295       // concept requirements
01296       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
01297       __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
01298         typename iterator_traits<_ForwardIter>::value_type,
01299         typename iterator_traits<_ForwardIter>::value_type>)
01300 
01301       __first = adjacent_find(__first, __last, __binary_pred);
01302       return unique_copy(__first, __last, __first, __binary_pred);
01303     }
01304 
01305   /**
01306    *  @if maint
01307    *  This is an uglified reverse(_BidirectionalIter, _BidirectionalIter)
01308    *  overloaded for bidirectional iterators.
01309    *  @endif
01310   */
01311   template<typename _BidirectionalIter>
01312     void
01313     __reverse(_BidirectionalIter __first, _BidirectionalIter __last,
01314               bidirectional_iterator_tag)
01315     {
01316       while (true)
01317         if (__first == __last || __first == --__last)
01318           return;
01319         else
01320           iter_swap(__first++, __last);
01321     }
01322 
01323   /**
01324    *  @if maint
01325    *  This is an uglified reverse(_BidirectionalIter, _BidirectionalIter)
01326    *  overloaded for bidirectional iterators.
01327    *  @endif
01328   */
01329   template<typename _RandomAccessIter>
01330     void
01331     __reverse(_RandomAccessIter __first, _RandomAccessIter __last,
01332               random_access_iterator_tag)
01333     {
01334       while (__first < __last)
01335         iter_swap(__first++, --__last);
01336     }
01337 
01338   /**
01339    *  @brief Reverse a sequence.
01340    *  @param  first  A bidirectional iterator.
01341    *  @param  last   A bidirectional iterator.
01342    *  @return   reverse() returns no value.
01343    *
01344    *  Reverses the order of the elements in the range @p [first,last),
01345    *  so that the first element becomes the last etc.
01346    *  For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
01347    *  swaps @p *(first+i) and @p *(last-(i+1))
01348   */
01349   template<typename _BidirectionalIter>
01350     inline void
01351     reverse(_BidirectionalIter __first, _BidirectionalIter __last)
01352     {
01353       // concept requirements
01354       __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
01355             _BidirectionalIter>)
01356       __reverse(__first, __last, __iterator_category(__first));
01357     }
01358 
01359   /**
01360    *  @brief Copy a sequence, reversing its elements.
01361    *  @param  first   A bidirectional iterator.
01362    *  @param  last    A bidirectional iterator.
01363    *  @param  result  An output iterator.
01364    *  @return  An iterator designating the end of the resulting sequence.
01365    *
01366    *  Copies the elements in the range @p [first,last) to the range
01367    *  @p [result,result+(last-first)) such that the order of the
01368    *  elements is reversed.
01369    *  For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
01370    *  performs the assignment @p *(result+(last-first)-i) = *(first+i).
01371    *  The ranges @p [first,last) and @p [result,result+(last-first))
01372    *  must not overlap.
01373   */
01374   template<typename _BidirectionalIter, typename _OutputIter>
01375     _OutputIter
01376     reverse_copy(_BidirectionalIter __first, _BidirectionalIter __last,
01377                  _OutputIter __result)
01378     {
01379       // concept requirements
01380       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIter>)
01381       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
01382         typename iterator_traits<_BidirectionalIter>::value_type>)
01383 
01384       while (__first != __last) {
01385     --__last;
01386     *__result = *__last;
01387     ++__result;
01388       }
01389       return __result;
01390     }
01391 
01392 
01393   /**
01394    *  @if maint
01395    *  This is a helper function for the rotate algorithm specialized on RAIs.
01396    *  It returns the greatest common divisor of two integer values.
01397    *  @endif
01398   */
01399   template<typename _EuclideanRingElement>
01400     _EuclideanRingElement
01401     __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
01402     {
01403       while (__n != 0) {
01404     _EuclideanRingElement __t = __m % __n;
01405     __m = __n;
01406     __n = __t;
01407       }
01408       return __m;
01409     }
01410 
01411   /**
01412    *  @if maint
01413    *  This is a helper function for the rotate algorithm.
01414    *  @endif
01415   */
01416   template<typename _ForwardIter>
01417     void
01418     __rotate(_ForwardIter __first,
01419          _ForwardIter __middle,
01420          _ForwardIter __last,
01421           forward_iterator_tag)
01422     {
01423       if ((__first == __middle) || (__last  == __middle))
01424     return;
01425 
01426       _ForwardIter __first2 = __middle;
01427       do {
01428     swap(*__first++, *__first2++);
01429     if (__first == __middle)
01430       __middle = __first2;
01431       } while (__first2 != __last);
01432 
01433       __first2 = __middle;
01434 
01435       while (__first2 != __last) {
01436     swap(*__first++, *__first2++);
01437     if (__first == __middle)
01438       __middle = __first2;
01439     else if (__first2 == __last)
01440       __first2 = __middle;
01441       }
01442     }
01443 
01444   /**
01445    *  @if maint
01446    *  This is a helper function for the rotate algorithm.
01447    *  @endif
01448   */
01449   template<typename _BidirectionalIter>
01450     void
01451     __rotate(_BidirectionalIter __first,
01452          _BidirectionalIter __middle,
01453          _BidirectionalIter __last,
01454           bidirectional_iterator_tag)
01455     {
01456       // concept requirements
01457       __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
01458         _BidirectionalIter>)
01459 
01460       if ((__first == __middle) || (__last  == __middle))
01461     return;
01462 
01463       __reverse(__first,  __middle, bidirectional_iterator_tag());
01464       __reverse(__middle, __last,   bidirectional_iterator_tag());
01465 
01466       while (__first != __middle && __middle != __last)
01467     swap (*__first++, *--__last);
01468 
01469       if (__first == __middle) {
01470     __reverse(__middle, __last,   bidirectional_iterator_tag());
01471       }
01472       else {
01473     __reverse(__first,  __middle, bidirectional_iterator_tag());
01474       }
01475     }
01476 
01477   /**
01478    *  @if maint
01479    *  This is a helper function for the rotate algorithm.
01480    *  @endif
01481   */
01482   template<typename _RandomAccessIter>
01483     void
01484     __rotate(_RandomAccessIter __first,
01485          _RandomAccessIter __middle,
01486          _RandomAccessIter __last,
01487          random_access_iterator_tag)
01488     {
01489       // concept requirements
01490       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
01491         _RandomAccessIter>)
01492 
01493       if ((__first == __middle) || (__last  == __middle))
01494     return;
01495 
01496       typedef typename iterator_traits<_RandomAccessIter>::difference_type _Distance;
01497       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
01498 
01499       _Distance __n = __last   - __first;
01500       _Distance __k = __middle - __first;
01501       _Distance __l = __n - __k;
01502 
01503       if (__k == __l) {
01504     swap_ranges(__first, __middle, __middle);
01505     return;
01506       }
01507 
01508       _Distance __d = __gcd(__n, __k);
01509 
01510       for (_Distance __i = 0; __i < __d; __i++) {
01511     _ValueType __tmp = *__first;
01512     _RandomAccessIter __p = __first;
01513 
01514     if (__k < __l) {
01515       for (_Distance __j = 0; __j < __l/__d; __j++) {
01516         if (__p > __first + __l) {
01517           *__p = *(__p - __l);
01518           __p -= __l;
01519         }
01520 
01521         *__p = *(__p + __k);
01522         __p += __k;
01523       }
01524     }
01525 
01526     else {
01527       for (_Distance __j = 0; __j < __k/__d - 1; __j ++) {
01528         if (__p < __last - __k) {
01529           *__p = *(__p + __k);
01530           __p += __k;
01531         }
01532 
01533         *__p = * (__p - __l);
01534         __p -= __l;
01535       }
01536     }
01537 
01538     *__p = __tmp;
01539     ++__first;
01540       }
01541     }
01542 
01543   /**
01544    *  @brief Rotate the elements of a sequence.
01545    *  @param  first   A forward iterator.
01546    *  @param  middle  A forward iterator.
01547    *  @param  last    A forward iterator.
01548    *  @return  Nothing.
01549    *
01550    *  Rotates the elements of the range @p [first,last) by @p (middle-first)
01551    *  positions so that the element at @p middle is moved to @p first, the
01552    *  element at @p middle+1 is moved to @first+1 and so on for each element
01553    *  in the range @p [first,last).
01554    *
01555    *  This effectively swaps the ranges @p [first,middle) and
01556    *  @p [middle,last).
01557    *
01558    *  Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
01559    *  each @p n in the range @p [0,last-first).
01560   */
01561   template<typename _ForwardIter>
01562     inline void
01563     rotate(_ForwardIter __first, _ForwardIter __middle, _ForwardIter __last)
01564     {
01565       // concept requirements
01566       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
01567 
01568       typedef typename iterator_traits<_ForwardIter>::iterator_category _IterType;
01569       __rotate(__first, __middle, __last, _IterType());
01570     }
01571 
01572   /**
01573    *  @brief Copy a sequence, rotating its elements.
01574    *  @param  first   A forward iterator.
01575    *  @param  middle  A forward iterator.
01576    *  @param  last    A forward iterator.
01577    *  @param  result  An output iterator.
01578    *  @return   An iterator designating the end of the resulting sequence.
01579    *
01580    *  Copies the elements of the range @p [first,last) to the range
01581    *  beginning at @result, rotating the copied elements by @p (middle-first)
01582    *  positions so that the element at @p middle is moved to @p result, the
01583    *  element at @p middle+1 is moved to @result+1 and so on for each element
01584    *  in the range @p [first,last).
01585    *
01586    *  Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
01587    *  each @p n in the range @p [0,last-first).
01588   */
01589   template<typename _ForwardIter, typename _OutputIter>
01590     _OutputIter
01591     rotate_copy(_ForwardIter __first, _ForwardIter __middle,
01592                 _ForwardIter __last, _OutputIter __result)
01593     {
01594       // concept requirements
01595       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
01596       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
01597         typename iterator_traits<_ForwardIter>::value_type>)
01598 
01599       return copy(__first, __middle, copy(__middle, __last, __result));
01600     }
01601 
01602 
01603   /**
01604    *  @if maint
01605    *  Return a random number in the range [0, __n).  This function encapsulates
01606    *  whether we're using rand (part of the standard C library) or lrand48
01607    *  (not standard, but a much better choice whenever it's available).
01608    *
01609    *  XXX There is no corresponding encapsulation fn to seed the generator.
01610    *  @endif
01611   */
01612   template<typename _Distance>
01613     inline _Distance
01614     __random_number(_Distance __n)
01615     {
01616   #ifdef _GLIBCPP_HAVE_DRAND48
01617       return lrand48() % __n;
01618   #else
01619       return rand() % __n;
01620   #endif
01621     }
01622 
01623 
01624   /**
01625    *  @brief Randomly shuffle the elements of a sequence.
01626    *  @param  first   A forward iterator.
01627    *  @param  last    A forward iterator.
01628    *  @return  Nothing.
01629    *
01630    *  Reorder the elements in the range @p [first,last) using a random
01631    *  distribution, so that every possible ordering of the sequence is
01632    *  equally likely.
01633   */
01634   template<typename _RandomAccessIter>
01635     inline void
01636     random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last)
01637     {
01638       // concept requirements
01639       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
01640         _RandomAccessIter>)
01641 
01642       if (__first == __last) return;
01643       for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)
01644     iter_swap(__i, __first + (rand() % ((__i - __first) + 1)));
01645     }
01646 
01647   /**
01648    *  @brief Shuffle the elements of a sequence using a random number
01649    *         generator.
01650    *  @param  first   A forward iterator.
01651    *  @param  last    A forward iterator.
01652    *  @param  rand    The RNG functor or function.
01653    *  @return  Nothing.
01654    *
01655    *  Reorders the elements in the range @p [first,last) using @p rand to
01656    *  provide a random distribution. Calling @p rand(N) for a positive
01657    *  integer @p N should return a randomly chosen integer from the
01658    *  range [0,N).
01659   */
01660   template<typename _RandomAccessIter, typename _RandomNumberGenerator>
01661     void
01662     random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last,
01663            _RandomNumberGenerator& __rand)
01664     {
01665       // concept requirements
01666       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
01667         _RandomAccessIter>)
01668 
01669       if (__first == __last) return;
01670       for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)
01671     iter_swap(__i, __first + __rand((__i - __first) + 1));
01672     }
01673 
01674 
01675   /**
01676    *  @if maint
01677    *  This is a helper function...
01678    *  @endif
01679   */
01680   template<typename _ForwardIter, typename _Predicate>
01681     _ForwardIter
01682     __partition(_ForwardIter __first, _ForwardIter __last,
01683         _Predicate   __pred,
01684         forward_iterator_tag)
01685     {
01686       if (__first == __last) return __first;
01687 
01688       while (__pred(*__first))
01689     if (++__first == __last) return __first;
01690 
01691       _ForwardIter __next = __first;
01692 
01693       while (++__next != __last)
01694     if (__pred(*__next)) {
01695       swap(*__first, *__next);
01696       ++__first;
01697     }
01698 
01699       return __first;
01700     }
01701 
01702   /**
01703    *  @if maint
01704    *  This is a helper function...
01705    *  @endif
01706   */
01707   template<typename _BidirectionalIter, typename _Predicate>
01708     _BidirectionalIter
01709     __partition(_BidirectionalIter __first, _BidirectionalIter __last,
01710         _Predicate __pred,
01711         bidirectional_iterator_tag)
01712     {
01713       while (true) {
01714     while (true)
01715       if (__first == __last)
01716         return __first;
01717       else if (__pred(*__first))
01718         ++__first;
01719       else
01720         break;
01721     --__last;
01722     while (true)
01723       if (__first == __last)
01724         return __first;
01725       else if (!__pred(*__last))
01726         --__last;
01727       else
01728         break;
01729     iter_swap(__first, __last);
01730     ++__first;
01731       }
01732     }
01733 
01734   /**
01735    *  @brief Move elements for which a predicate is true to the beginning
01736    *         of a sequence.
01737    *  @param  first   A forward iterator.
01738    *  @param  last    A forward iterator.
01739    *  @param  pred    A predicate functor.
01740    *  @return  An iterator @p middle such that @p pred(i) is true for each
01741    *  iterator @p i in the range @p [first,middle) and false for each @p i
01742    *  in the range @p [middle,last).
01743    *  
01744    *  @p pred must not modify its operand. @p partition() does not preserve
01745    *  the relative ordering of elements in each group, use
01746    *  @p stable_partition() if this is needed.
01747   */
01748   template<typename _ForwardIter, typename _Predicate>
01749     inline _ForwardIter
01750     partition(_ForwardIter __first, _ForwardIter __last,
01751           _Predicate   __pred)
01752     {
01753       // concept requirements
01754       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
01755       __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
01756         typename iterator_traits<_ForwardIter>::value_type>)
01757 
01758       return __partition(__first, __last, __pred, __iterator_category(__first));
01759     }
01760 
01761 
01762   /**
01763    *  @if maint
01764    *  This is a helper function...
01765    *  @endif
01766   */
01767   template<typename _ForwardIter, typename _Predicate, typename _Distance>
01768     _ForwardIter
01769     __inplace_stable_partition(_ForwardIter __first, _ForwardIter __last,
01770                    _Predicate __pred, _Distance __len)
01771     {
01772       if (__len == 1)
01773     return __pred(*__first) ? __last : __first;
01774       _ForwardIter __middle = __first;
01775       advance(__middle, __len / 2);
01776       _ForwardIter __begin = __inplace_stable_partition(__first, __middle,
01777                             __pred,
01778                             __len / 2);
01779       _ForwardIter __end = __inplace_stable_partition(__middle, __last,
01780                               __pred,
01781                               __len - __len / 2);
01782       rotate(__begin, __middle, __end);
01783       advance(__begin, distance(__middle, __end));
01784       return __begin;
01785     }
01786 
01787   /**
01788    *  @if maint
01789    *  This is a helper function...
01790    *  @endif
01791   */
01792   template<typename _ForwardIter, typename _Pointer, typename _Predicate,
01793        typename _Distance>
01794     _ForwardIter
01795     __stable_partition_adaptive(_ForwardIter __first, _ForwardIter __last,
01796                 _Predicate __pred, _Distance __len,
01797                 _Pointer __buffer,
01798                 _Distance __buffer_size)
01799     {
01800       if (__len <= __buffer_size) {
01801     _ForwardIter __result1 = __first;
01802     _Pointer __result2 = __buffer;
01803     for ( ; __first != __last ; ++__first)
01804       if (__pred(*__first)) {
01805         *__result1 = *__first;
01806         ++__result1;
01807       }
01808       else {
01809         *__result2 = *__first;
01810         ++__result2;
01811       }
01812     copy(__buffer, __result2, __result1);
01813     return __result1;
01814       }
01815       else {
01816     _ForwardIter __middle = __first;
01817     advance(__middle, __len / 2);
01818     _ForwardIter __begin = __stable_partition_adaptive(__first, __middle,
01819                                __pred,
01820                                __len / 2,
01821                                __buffer, __buffer_size);
01822     _ForwardIter __end = __stable_partition_adaptive( __middle, __last,
01823                               __pred,
01824                               __len - __len / 2,
01825                               __buffer, __buffer_size);
01826     rotate(__begin, __middle, __end);
01827     advance(__begin, distance(__middle, __end));
01828     return __begin;
01829       }
01830     }
01831 
01832   /**
01833    *  @brief Move elements for which a predicate is true to the beginning
01834    *         of a sequence, preserving relative ordering.
01835    *  @param  first   A forward iterator.
01836    *  @param  last    A forward iterator.
01837    *  @param  pred    A predicate functor.
01838    *  @return  An iterator @p middle such that @p pred(i) is true for each
01839    *  iterator @p i in the range @p [first,middle) and false for each @p i
01840    *  in the range @p [middle,last).
01841    *  
01842    *  Performs the same function as @p partition() with the additional
01843    *  guarantee that the relative ordering of elements in each group is
01844    *  preserved, so any two elements @p x and @p y in the range
01845    *  @p [first,last) such that @p pred(x)==pred(y) will have the same
01846    *  relative ordering after calling @p stable_partition().
01847   */
01848   template<typename _ForwardIter, typename _Predicate>
01849     _ForwardIter
01850     stable_partition(_ForwardIter __first, _ForwardIter __last,
01851              _Predicate __pred)
01852     {
01853       // concept requirements
01854       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
01855       __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
01856         typename iterator_traits<_ForwardIter>::value_type>)
01857 
01858       if (__first == __last)
01859     return __first;
01860       else
01861       {
01862     typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
01863     typedef typename iterator_traits<_ForwardIter>::difference_type _DistanceType;
01864 
01865     _Temporary_buffer<_ForwardIter, _ValueType> __buf(__first, __last);
01866     if (__buf.size() > 0)
01867       return __stable_partition_adaptive(__first, __last, __pred,
01868                          _DistanceType(__buf.requested_size()),
01869                          __buf.begin(), __buf.size());
01870     else
01871       return __inplace_stable_partition(__first, __last, __pred,
01872                         _DistanceType(__buf.requested_size()));
01873       }
01874     }
01875 
01876   /**
01877    *  @if maint
01878    *  This is a helper function...
01879    *  @endif
01880   */
01881   template<typename _RandomAccessIter, typename _Tp>
01882     _RandomAccessIter
01883     __unguarded_partition(_RandomAccessIter __first, _RandomAccessIter __last,
01884               _Tp __pivot)
01885     {
01886       while (true) {
01887     while (*__first < __pivot)
01888       ++__first;
01889     --__last;
01890     while (__pivot < *__last)
01891       --__last;
01892     if (!(__first < __last))
01893       return __first;
01894     iter_swap(__first, __last);
01895     ++__first;
01896       }
01897     }
01898 
01899   /**
01900    *  @if maint
01901    *  This is a helper function...
01902    *  @endif
01903   */
01904   template<typename _RandomAccessIter, typename _Tp, typename _Compare>
01905     _RandomAccessIter
01906     __unguarded_partition(_RandomAccessIter __first, _RandomAccessIter __last,
01907               _Tp __pivot, _Compare __comp)
01908     {
01909       while (true) {
01910     while (__comp(*__first, __pivot))
01911       ++__first;
01912     --__last;
01913     while (__comp(__pivot, *__last))
01914       --__last;
01915     if (!(__first < __last))
01916       return __first;
01917     iter_swap(__first, __last);
01918     ++__first;
01919       }
01920     }
01921 
01922 
01923   /**
01924    *  @if maint
01925    *  @doctodo
01926    *  This controls some aspect of the sort routines.
01927    *  @endif
01928   */
01929   enum { _M_threshold = 16 };
01930 
01931   /**
01932    *  @if maint
01933    *  This is a helper function for the sort routine.
01934    *  @endif
01935   */
01936   template<typename _RandomAccessIter, typename _Tp>
01937     void
01938     __unguarded_linear_insert(_RandomAccessIter __last, _Tp __val)
01939     {
01940       _RandomAccessIter __next = __last;
01941       --__next;
01942       while (__val < *__next) {
01943     *__last = *__next;
01944     __last = __next;
01945     --__next;
01946       }
01947       *__last = __val;
01948     }
01949 
01950   /**
01951    *  @if maint
01952    *  This is a helper function for the sort routine.
01953    *  @endif
01954   */
01955   template<typename _RandomAccessIter, typename _Tp, typename _Compare>
01956     void
01957     __unguarded_linear_insert(_RandomAccessIter __last, _Tp __val, _Compare __comp)
01958     {
01959       _RandomAccessIter __next = __last;
01960       --__next;
01961       while (__comp(__val, *__next)) {
01962     *__last = *__next;
01963     __last = __next;
01964     --__next;
01965       }
01966       *__last = __val;
01967     }
01968 
01969   /**
01970    *  @if maint
01971    *  This is a helper function for the sort routine.
01972    *  @endif
01973   */
01974   template<typename _RandomAccessIter>
01975     void
01976     __insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last)
01977     {
01978       if (__first == __last) return;
01979 
01980       for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)
01981       {
01982     typename iterator_traits<_RandomAccessIter>::value_type __val = *__i;
01983     if (__val < *__first) {
01984       copy_backward(__first, __i, __i + 1);
01985       *__first = __val;
01986     }
01987     else
01988       __unguarded_linear_insert(__i, __val);
01989       }
01990     }
01991 
01992   /**
01993    *  @if maint
01994    *  This is a helper function for the sort routine.
01995    *  @endif
01996   */
01997   template<typename _RandomAccessIter, typename _Compare>
01998     void
01999     __insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last,
02000              _Compare __comp)
02001     {
02002       if (__first == __last) return;
02003 
02004       for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)
02005       {
02006     typename iterator_traits<_RandomAccessIter>::value_type __val = *__i;
02007     if (__comp(__val, *__first)) {
02008       copy_backward(__first, __i, __i + 1);
02009       *__first = __val;
02010     }
02011     else
02012       __unguarded_linear_insert(__i, __val, __comp);
02013       }
02014     }
02015 
02016   /**
02017    *  @if maint
02018    *  This is a helper function for the sort routine.
02019    *  @endif
02020   */
02021   template<typename _RandomAccessIter>
02022     inline void
02023     __unguarded_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last)
02024     {
02025       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02026 
02027       for (_RandomAccessIter __i = __first; __i != __last; ++__i)
02028     __unguarded_linear_insert(__i, _ValueType(*__i));
02029     }
02030 
02031   /**
02032    *  @if maint
02033    *  This is a helper function for the sort routine.
02034    *  @endif
02035   */
02036   template<typename _RandomAccessIter, typename _Compare>
02037     inline void
02038     __unguarded_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last,
02039                    _Compare __comp)
02040     {
02041       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02042 
02043       for (_RandomAccessIter __i = __first; __i != __last; ++__i)
02044     __unguarded_linear_insert(__i, _ValueType(*__i), __comp);
02045     }
02046 
02047   /**
02048    *  @if maint
02049    *  This is a helper function for the sort routine.
02050    *  @endif
02051   */
02052   template<typename _RandomAccessIter>
02053     void
02054     __final_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last)
02055     {
02056       if (__last - __first > _M_threshold) {
02057     __insertion_sort(__first, __first + _M_threshold);
02058     __unguarded_insertion_sort(__first + _M_threshold, __last);
02059       }
02060       else
02061     __insertion_sort(__first, __last);
02062     }
02063 
02064   /**
02065    *  @if maint
02066    *  This is a helper function for the sort routine.
02067    *  @endif
02068   */
02069   template<typename _RandomAccessIter, typename _Compare>
02070     void
02071     __final_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last,
02072                _Compare __comp)
02073     {
02074       if (__last - __first > _M_threshold) {
02075     __insertion_sort(__first, __first + _M_threshold, __comp);
02076     __unguarded_insertion_sort(__first + _M_threshold, __last, __comp);
02077       }
02078       else
02079     __insertion_sort(__first, __last, __comp);
02080     }
02081 
02082   /**
02083    *  @if maint
02084    *  This is a helper function for the sort routine.
02085    *  @endif
02086   */
02087   template<typename _Size>
02088     inline _Size
02089     __lg(_Size __n)
02090     {
02091       _Size __k;
02092       for (__k = 0; __n != 1; __n >>= 1) ++__k;
02093       return __k;
02094     }
02095 
02096   /**
02097    *  @if maint
02098    *  This is a helper function for the sort routine.
02099    *  @endif
02100   */
02101   template<typename _RandomAccessIter, typename _Size>
02102     void
02103     __introsort_loop(_RandomAccessIter __first, _RandomAccessIter __last,
02104              _Size __depth_limit)
02105     {
02106       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02107 
02108       while (__last - __first > _M_threshold) {
02109     if (__depth_limit == 0) {
02110       partial_sort(__first, __last, __last);
02111       return;
02112     }
02113     --__depth_limit;
02114     _RandomAccessIter __cut =
02115       __unguarded_partition(__first, __last,
02116                 _ValueType(__median(*__first,
02117                             *(__first + (__last - __first)/2),
02118                             *(__last - 1))));
02119     __introsort_loop(__cut, __last, __depth_limit);
02120     __last = __cut;
02121       }
02122     }
02123 
02124   /**
02125    *  @if maint
02126    *  This is a helper function for the sort routine.
02127    *  @endif
02128   */
02129   template<typename _RandomAccessIter, typename _Size, typename _Compare>
02130     void
02131     __introsort_loop(_RandomAccessIter __first, _RandomAccessIter __last,
02132              _Size __depth_limit, _Compare __comp)
02133     {
02134       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02135 
02136       while (__last - __first > _M_threshold) {
02137     if (__depth_limit == 0) {
02138       partial_sort(__first, __last, __last, __comp);
02139       return;
02140     }
02141     --__depth_limit;
02142     _RandomAccessIter __cut =
02143       __unguarded_partition(__first, __last,
02144                 _ValueType(__median(*__first,
02145                             *(__first + (__last - __first)/2),
02146                             *(__last - 1), __comp)),
02147        __comp);
02148     __introsort_loop(__cut, __last, __depth_limit, __comp);
02149     __last = __cut;
02150       }
02151     }
02152 
02153   /**
02154    *  @brief Sort the elements of a sequence.
02155    *  @param  first   An iterator.
02156    *  @param  last    Another iterator.
02157    *  @return  Nothing.
02158    *
02159    *  Sorts the elements in the range @p [first,last) in ascending order,
02160    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
02161    *  @p [first,last-1).
02162    *
02163    *  The relative ordering of equivalent elements is not preserved, use
02164    *  @p stable_sort() if this is needed.
02165   */
02166   template<typename _RandomAccessIter>
02167     inline void
02168     sort(_RandomAccessIter __first, _RandomAccessIter __last)
02169     {
02170       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02171 
02172       // concept requirements
02173       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
02174         _RandomAccessIter>)
02175       __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
02176 
02177       if (__first != __last) {
02178     __introsort_loop(__first, __last, __lg(__last - __first) * 2);
02179     __final_insertion_sort(__first, __last);
02180       }
02181     }
02182 
02183   /**
02184    *  @brief Sort the elements of a sequence using a predicate for comparison.
02185    *  @param  first   An iterator.
02186    *  @param  last    Another iterator.
02187    *  @param  comp    A comparison functor.
02188    *  @return  Nothing.
02189    *
02190    *  Sorts the elements in the range @p [first,last) in ascending order,
02191    *  such that @p comp(*(i+1),*i) is false for every iterator @p i in the
02192    *  range @p [first,last-1).
02193    *
02194    *  The relative ordering of equivalent elements is not preserved, use
02195    *  @p stable_sort() if this is needed.
02196   */
02197   template<typename _RandomAccessIter, typename _Compare>
02198     inline void
02199     sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp)
02200     {
02201       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02202 
02203       // concept requirements
02204       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
02205         _RandomAccessIter>)
02206       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _ValueType>)
02207 
02208       if (__first != __last) {
02209     __introsort_loop(__first, __last, __lg(__last - __first) * 2, __comp);
02210     __final_insertion_sort(__first, __last, __comp);
02211       }
02212     }
02213 
02214 
02215   /**
02216    *  @if maint
02217    *  This is a helper function for the stable sorting routines.
02218    *  @endif
02219   */
02220   template<typename _RandomAccessIter>
02221     void
02222     __inplace_stable_sort(_RandomAccessIter __first, _RandomAccessIter __last)
02223     {
02224       if (__last - __first < 15) {
02225     __insertion_sort(__first, __last);
02226     return;
02227       }
02228       _RandomAccessIter __middle = __first + (__last - __first) / 2;
02229       __inplace_stable_sort(__first, __middle);
02230       __inplace_stable_sort(__middle, __last);
02231       __merge_without_buffer(__first, __middle, __last,
02232                  __middle - __first,
02233                  __last - __middle);
02234     }
02235 
02236   /**
02237    *  @if maint
02238    *  This is a helper function for the stable sorting routines.
02239    *  @endif
02240   */
02241   template<typename _RandomAccessIter, typename _Compare>
02242     void
02243     __inplace_stable_sort(_RandomAccessIter __first, _RandomAccessIter __last,
02244               _Compare __comp)
02245     {
02246       if (__last - __first < 15) {
02247     __insertion_sort(__first, __last, __comp);
02248     return;
02249       }
02250       _RandomAccessIter __middle = __first + (__last - __first) / 2;
02251       __inplace_stable_sort(__first, __middle, __comp);
02252       __inplace_stable_sort(__middle, __last, __comp);
02253       __merge_without_buffer(__first, __middle, __last,
02254                  __middle - __first,
02255                  __last - __middle,
02256                  __comp);
02257     }
02258 
02259   template<typename _RandomAccessIter1, typename _RandomAccessIter2,
02260        typename _Distance>
02261     void
02262     __merge_sort_loop(_RandomAccessIter1 __first, _RandomAccessIter1 __last,
02263               _RandomAccessIter2 __result, _Distance __step_size)
02264     {
02265       _Distance __two_step = 2 * __step_size;
02266 
02267       while (__last - __first >= __two_step) {
02268     __result = merge(__first, __first + __step_size,
02269              __first + __step_size, __first + __two_step,
02270              __result);
02271     __first += __two_step;
02272       }
02273 
02274       __step_size = min(_Distance(__last - __first), __step_size);
02275       merge(__first, __first + __step_size, __first + __step_size, __last,
02276         __result);
02277     }
02278 
02279   template<typename _RandomAccessIter1, typename _RandomAccessIter2,
02280        typename _Distance, typename _Compare>
02281     void
02282     __merge_sort_loop(_RandomAccessIter1 __first, _RandomAccessIter1 __last,
02283               _RandomAccessIter2 __result, _Distance __step_size,
02284               _Compare __comp)
02285     {
02286       _Distance __two_step = 2 * __step_size;
02287 
02288       while (__last - __first >= __two_step) {
02289     __result = merge(__first, __first + __step_size,
02290              __first + __step_size, __first + __two_step,
02291              __result,
02292              __comp);
02293     __first += __two_step;
02294       }
02295       __step_size = min(_Distance(__last - __first), __step_size);
02296 
02297       merge(__first, __first + __step_size,
02298         __first + __step_size, __last,
02299         __result,
02300         __comp);
02301     }
02302 
02303   enum { _M_chunk_size = 7 };
02304 
02305   template<typename _RandomAccessIter, typename _Distance>
02306     void
02307     __chunk_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last,
02308                _Distance __chunk_size)
02309     {
02310       while (__last - __first >= __chunk_size) {
02311     __insertion_sort(__first, __first + __chunk_size);
02312     __first += __chunk_size;
02313       }
02314       __insertion_sort(__first, __last);
02315     }
02316 
02317   template<typename _RandomAccessIter, typename _Distance, typename _Compare>
02318     void
02319     __chunk_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last,
02320                _Distance __chunk_size, _Compare __comp)
02321     {
02322       while (__last - __first >= __chunk_size) {
02323     __insertion_sort(__first, __first + __chunk_size, __comp);
02324     __first += __chunk_size;
02325       }
02326       __insertion_sort(__first, __last, __comp);
02327     }
02328 
02329   template<typename _RandomAccessIter, typename _Pointer>
02330     void
02331     __merge_sort_with_buffer(_RandomAccessIter __first, _RandomAccessIter __last,
02332                              _Pointer __buffer)
02333     {
02334       typedef typename iterator_traits<_RandomAccessIter>::difference_type _Distance;
02335 
02336       _Distance __len = __last - __first;
02337       _Pointer __buffer_last = __buffer + __len;
02338 
02339       _Distance __step_size = _M_chunk_size;
02340       __chunk_insertion_sort(__first, __last, __step_size);
02341 
02342       while (__step_size < __len) {
02343     __merge_sort_loop(__first, __last, __buffer, __step_size);
02344     __step_size *= 2;
02345     __merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
02346     __step_size *= 2;
02347       }
02348     }
02349 
02350   template<typename _RandomAccessIter, typename _Pointer, typename _Compare>
02351     void
02352     __merge_sort_with_buffer(_RandomAccessIter __first, _RandomAccessIter __last,
02353                              _Pointer __buffer, _Compare __comp)
02354     {
02355       typedef typename iterator_traits<_RandomAccessIter>::difference_type _Distance;
02356 
02357       _Distance __len = __last - __first;
02358       _Pointer __buffer_last = __buffer + __len;
02359 
02360       _Distance __step_size = _M_chunk_size;
02361       __chunk_insertion_sort(__first, __last, __step_size, __comp);
02362 
02363       while (__step_size < __len) {
02364     __merge_sort_loop(__first, __last, __buffer, __step_size, __comp);
02365     __step_size *= 2;
02366     __merge_sort_loop(__buffer, __buffer_last, __first, __step_size, __comp);
02367     __step_size *= 2;
02368       }
02369     }
02370 
02371   template<typename _RandomAccessIter, typename _Pointer, typename _Distance>
02372     void
02373     __stable_sort_adaptive(_RandomAccessIter __first, _RandomAccessIter __last,
02374                            _Pointer __buffer, _Distance __buffer_size)
02375     {
02376       _Distance __len = (__last - __first + 1) / 2;
02377       _RandomAccessIter __middle = __first + __len;
02378       if (__len > __buffer_size) {
02379     __stable_sort_adaptive(__first, __middle, __buffer, __buffer_size);
02380     __stable_sort_adaptive(__middle, __last, __buffer, __buffer_size);
02381       }
02382       else {
02383     __merge_sort_with_buffer(__first, __middle, __buffer);
02384     __merge_sort_with_buffer(__middle, __last, __buffer);
02385       }
02386       __merge_adaptive(__first, __middle, __last, _Distance(__middle - __first),
02387                        _Distance(__last - __middle), __buffer, __buffer_size);
02388     }
02389 
02390   template<typename _RandomAccessIter, typename _Pointer, typename _Distance,
02391            typename _Compare>
02392     void
02393     __stable_sort_adaptive(_RandomAccessIter __first, _RandomAccessIter __last,
02394                            _Pointer __buffer, _Distance __buffer_size,
02395                            _Compare __comp)
02396     {
02397       _Distance __len = (__last - __first + 1) / 2;
02398       _RandomAccessIter __middle = __first + __len;
02399       if (__len > __buffer_size) {
02400     __stable_sort_adaptive(__first, __middle, __buffer, __buffer_size,
02401                                __comp);
02402     __stable_sort_adaptive(__middle, __last, __buffer, __buffer_size,
02403                                __comp);
02404       }
02405       else {
02406     __merge_sort_with_buffer(__first, __middle, __buffer, __comp);
02407     __merge_sort_with_buffer(__middle, __last, __buffer, __comp);
02408       }
02409       __merge_adaptive(__first, __middle, __last, _Distance(__middle - __first),
02410                        _Distance(__last - __middle), __buffer, __buffer_size,
02411                        __comp);
02412     }
02413 
02414   /**
02415    *  @brief Sort the elements of a sequence, preserving the relative order
02416    *         of equivalent elements.
02417    *  @param  first   An iterator.
02418    *  @param  last    Another iterator.
02419    *  @return  Nothing.
02420    *
02421    *  Sorts the elements in the range @p [first,last) in ascending order,
02422    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
02423    *  @p [first,last-1).
02424    *
02425    *  The relative ordering of equivalent elements is preserved, so any two
02426    *  elements @p x and @p y in the range @p [first,last) such that
02427    *  @p x<y is false and @p y<x is false will have the same relative
02428    *  ordering after calling @p stable_sort().
02429   */
02430   template<typename _RandomAccessIter>
02431     inline void
02432     stable_sort(_RandomAccessIter __first, _RandomAccessIter __last)
02433     {
02434       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02435       typedef typename iterator_traits<_RandomAccessIter>::difference_type _DistanceType;
02436 
02437       // concept requirements
02438       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
02439         _RandomAccessIter>)
02440       __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
02441 
02442       _Temporary_buffer<_RandomAccessIter, _ValueType> buf(__first, __last);
02443       if (buf.begin() == 0)
02444     __inplace_stable_sort(__first, __last);
02445       else
02446     __stable_sort_adaptive(__first, __last, buf.begin(), _DistanceType(buf.size()));
02447     }
02448 
02449   /**
02450    *  @brief Sort the elements of a sequence using a predicate for comparison,
02451    *         preserving the relative order of equivalent elements.
02452    *  @param  first   An iterator.
02453    *  @param  last    Another iterator.
02454    *  @param  comp    A comparison functor.
02455    *  @return  Nothing.
02456    *
02457    *  Sorts the elements in the range @p [first,last) in ascending order,
02458    *  such that @p comp(*(i+1),*i) is false for each iterator @p i in the
02459    *  range @p [first,last-1).
02460    *
02461    *  The relative ordering of equivalent elements is preserved, so any two
02462    *  elements @p x and @p y in the range @p [first,last) such that
02463    *  @p comp(x,y) is false and @p comp(y,x) is false will have the same
02464    *  relative ordering after calling @p stable_sort().
02465   */
02466   template<typename _RandomAccessIter, typename _Compare>
02467     inline void
02468     stable_sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp)
02469     {
02470       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02471       typedef typename iterator_traits<_RandomAccessIter>::difference_type _DistanceType;
02472 
02473       // concept requirements
02474       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
02475         _RandomAccessIter>)
02476       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
02477                               _ValueType, _ValueType>)
02478 
02479       _Temporary_buffer<_RandomAccessIter, _ValueType> buf(__first, __last);
02480       if (buf.begin() == 0)
02481     __inplace_stable_sort(__first, __last, __comp);
02482       else
02483     __stable_sort_adaptive(__first, __last, buf.begin(), _DistanceType(buf.size()),
02484                    __comp);
02485     }
02486 
02487   /**
02488    *  @brief Sort the smallest elements of a sequence.
02489    *  @param  first   An iterator.
02490    *  @param  middle  Another iterator.
02491    *  @param  last    Another iterator.
02492    *  @return  Nothing.
02493    *
02494    *  Sorts the smallest @p (middle-first) elements in the range
02495    *  @p [first,last) and moves them to the range @p [first,middle). The
02496    *  order of the remaining elements in the range @p [middle,last) is
02497    *  undefined.
02498    *  After the sort if @p i and @j are iterators in the range
02499    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
02500    *  the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
02501   */
02502   template<typename _RandomAccessIter>
02503     void
02504     partial_sort(_RandomAccessIter __first,
02505          _RandomAccessIter __middle,
02506          _RandomAccessIter __last)
02507     {
02508       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02509 
02510       // concept requirements
02511       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
02512         _RandomAccessIter>)
02513       __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
02514 
02515       make_heap(__first, __middle);
02516       for (_RandomAccessIter __i = __middle; __i < __last; ++__i)
02517     if (*__i < *__first)
02518       __pop_heap(__first, __middle, __i, _ValueType(*__i));
02519       sort_heap(__first, __middle);
02520     }
02521 
02522   /**
02523    *  @brief Sort the smallest elements of a sequence using a predicate
02524    *         for comparison.
02525    *  @param  first   An iterator.
02526    *  @param  middle  Another iterator.
02527    *  @param  last    Another iterator.
02528    *  @param  comp    A comparison functor.
02529    *  @return  Nothing.
02530    *
02531    *  Sorts the smallest @p (middle-first) elements in the range
02532    *  @p [first,last) and moves them to the range @p [first,middle). The
02533    *  order of the remaining elements in the range @p [middle,last) is
02534    *  undefined.
02535    *  After the sort if @p i and @j are iterators in the range
02536    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
02537    *  the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
02538    *  are both false.
02539   */
02540   template<typename _RandomAccessIter, typename _Compare>
02541     void
02542     partial_sort(_RandomAccessIter __first,
02543          _RandomAccessIter __middle,
02544          _RandomAccessIter __last,
02545          _Compare __comp)
02546     {
02547       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02548 
02549       // concept requirements
02550       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
02551         _RandomAccessIter>)
02552       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
02553                               _ValueType, _ValueType>)
02554 
02555       make_heap(__first, __middle, __comp);
02556       for (_RandomAccessIter __i = __middle; __i < __last; ++__i)
02557     if (__comp(*__i, *__first))
02558       __pop_heap(__first, __middle, __i, _ValueType(*__i), __comp);
02559       sort_heap(__first, __middle, __comp);
02560     }
02561 
02562   /**
02563    *  @brief Copy the smallest elements of a sequence.
02564    *  @param  first   An iterator.
02565    *  @param  last    Another iterator.
02566    *  @param  result_first   A random-access iterator.
02567    *  @param  result_last    Another random-access iterator.
02568    *  @return   An iterator indicating the end of the resulting sequence.
02569    *
02570    *  Copies and sorts the smallest N values from the range @p [first,last)
02571    *  to the range beginning at @p result_first, where the number of
02572    *  elements to be copied, @p N, is the smaller of @p (last-first) and
02573    *  @p (result_last-result_first).
02574    *  After the sort if @p i and @j are iterators in the range
02575    *  @p [result_first,result_first+N) such that @i precedes @j then
02576    *  @p *j<*i is false.
02577    *  The value returned is @p result_first+N.
02578   */
02579   template<typename _InputIter, typename _RandomAccessIter>
02580     _RandomAccessIter
02581     partial_sort_copy(_InputIter __first, _InputIter __last,
02582               _RandomAccessIter __result_first,
02583               _RandomAccessIter __result_last)
02584     {
02585       typedef typename iterator_traits<_InputIter>::value_type _InputValueType;
02586       typedef typename iterator_traits<_RandomAccessIter>::value_type _OutputValueType;
02587       typedef typename iterator_traits<_RandomAccessIter>::difference_type _DistanceType;
02588 
02589       // concept requirements
02590       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
02591       __glibcpp_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
02592       __glibcpp_function_requires(_LessThanComparableConcept<_OutputValueType>)
02593       __glibcpp_function_requires(_LessThanComparableConcept<_InputValueType>)
02594 
02595       if (__result_first == __result_last) return __result_last;
02596       _RandomAccessIter __result_real_last = __result_first;
02597       while(__first != __last && __result_real_last != __result_last) {
02598     *__result_real_last = *__first;
02599     ++__result_real_last;
02600     ++__first;
02601       }
02602       make_heap(__result_first, __result_real_last);
02603       while (__first != __last) {
02604     if (*__first < *__result_first)
02605       __adjust_heap(__result_first, _DistanceType(0),
02606             _DistanceType(__result_real_last - __result_first),
02607             _InputValueType(*__first));
02608     ++__first;
02609       }
02610       sort_heap(__result_first, __result_real_last);
02611       return __result_real_last;
02612     }
02613 
02614   /**
02615    *  @brief Copy the smallest elements of a sequence using a predicate for
02616    *         comparison.
02617    *  @param  first   An input iterator.
02618    *  @param  last    Another input iterator.
02619    *  @param  result_first   A random-access iterator.
02620    *  @param  result_last    Another random-access iterator.
02621    *  @param  comp    A comparison functor.
02622    *  @return   An iterator indicating the end of the resulting sequence.
02623    *
02624    *  Copies and sorts the smallest N values from the range @p [first,last)
02625    *  to the range beginning at @p result_first, where the number of
02626    *  elements to be copied, @p N, is the smaller of @p (last-first) and
02627    *  @p (result_last-result_first).
02628    *  After the sort if @p i and @j are iterators in the range
02629    *  @p [result_first,result_first+N) such that @i precedes @j then
02630    *  @p comp(*j,*i) is false.
02631    *  The value returned is @p result_first+N.
02632   */
02633   template<typename _InputIter, typename _RandomAccessIter, typename _Compare>
02634     _RandomAccessIter
02635     partial_sort_copy(_InputIter __first, _InputIter __last,
02636               _RandomAccessIter __result_first,
02637               _RandomAccessIter __result_last,
02638               _Compare __comp)
02639     {
02640       typedef typename iterator_traits<_InputIter>::value_type _InputValueType;
02641       typedef typename iterator_traits<_RandomAccessIter>::value_type _OutputValueType;
02642       typedef typename iterator_traits<_RandomAccessIter>::difference_type _DistanceType;
02643 
02644       // concept requirements
02645       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
02646       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIter>)
02647       __glibcpp_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
02648       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
02649                   _OutputValueType, _OutputValueType>)
02650 
02651       if (__result_first == __result_last) return __result_last;
02652       _RandomAccessIter __result_real_last = __result_first;
02653       while(__first != __last && __result_real_last != __result_last) {
02654     *__result_real_last = *__first;
02655     ++__result_real_last;
02656     ++__first;
02657       }
02658       make_heap(__result_first, __result_real_last, __comp);
02659       while (__first != __last) {
02660     if (__comp(*__first, *__result_first))
02661       __adjust_heap(__result_first, _DistanceType(0),
02662             _DistanceType(__result_real_last - __result_first),
02663             _InputValueType(*__first),
02664             __comp);
02665     ++__first;
02666       }
02667       sort_heap(__result_first, __result_real_last, __comp);
02668       return __result_real_last;
02669     }
02670 
02671   /**
02672    *  @brief Sort a sequence just enough to find a particular position.
02673    *  @param  first   An iterator.
02674    *  @param  nth     Another iterator.
02675    *  @param  last    Another iterator.
02676    *  @return  Nothing.
02677    *
02678    *  Rearranges the elements in the range @p [first,last) so that @p *nth
02679    *  is the same element that would have been in that position had the
02680    *  whole sequence been sorted. 
02681    *  whole sequence been sorted. The elements either side of @p *nth are
02682    *  not completely sorted, but for any iterator @i in the range
02683    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
02684    *  holds that @p *j<*i is false.
02685   */
02686   template<typename _RandomAccessIter>
02687     void
02688     nth_element(_RandomAccessIter __first,
02689         _RandomAccessIter __nth,
02690         _RandomAccessIter __last)
02691     {
02692       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02693 
02694       // concept requirements
02695       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIter>)
02696       __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
02697 
02698       while (__last - __first > 3) {
02699     _RandomAccessIter __cut =
02700       __unguarded_partition(__first, __last,
02701                 _ValueType(__median(*__first,
02702                             *(__first + (__last - __first)/2),
02703                             *(__last - 1))));
02704     if (__cut <= __nth)
02705       __first = __cut;
02706     else
02707       __last = __cut;
02708       }
02709       __insertion_sort(__first, __last);
02710     }
02711 
02712   /**
02713    *  @brief Sort a sequence just enough to find a particular position
02714    *         using a predicate for comparison.
02715    *  @param  first   An iterator.
02716    *  @param  nth     Another iterator.
02717    *  @param  last    Another iterator.
02718    *  @param  comp    A comparison functor.
02719    *  @return  Nothing.
02720    *
02721    *  Rearranges the elements in the range @p [first,last) so that @p *nth
02722    *  is the same element that would have been in that position had the
02723    *  whole sequence been sorted. The elements either side of @p *nth are
02724    *  not completely sorted, but for any iterator @i in the range
02725    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
02726    *  holds that @p comp(*j,*i) is false.
02727   */
02728   template<typename _RandomAccessIter, typename _Compare>
02729     void
02730     nth_element(_RandomAccessIter __first,
02731         _RandomAccessIter __nth,
02732         _RandomAccessIter __last,
02733                 _Compare __comp)
02734     {
02735       typedef typename iterator_traits<_RandomAccessIter>::value_type _ValueType;
02736 
02737       // concept requirements
02738       __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIter>)
02739       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
02740                   _ValueType, _ValueType>)
02741 
02742       while (__last - __first > 3) {
02743     _RandomAccessIter __cut =
02744       __unguarded_partition(__first, __last,
02745                 _ValueType(__median(*__first,
02746                             *(__first + (__last - __first)/2),
02747                             *(__last - 1),
02748                             __comp)),
02749                 __comp);
02750     if (__cut <= __nth)
02751       __first = __cut;
02752     else
02753       __last = __cut;
02754       }
02755       __insertion_sort(__first, __last, __comp);
02756     }
02757 
02758 
02759   /**
02760    *  @brief Finds the first position in which @a val could be inserted
02761    *         without changing the ordering.
02762    *  @param  first   An iterator.
02763    *  @param  last    Another iterator.
02764    *  @param  val     The search term.
02765    *  @return  An iterator pointing to the first element "not less than" @a val.
02766    *  @ingroup binarysearch
02767   */
02768   template<typename _ForwardIter, typename _Tp>
02769     _ForwardIter
02770     lower_bound(_ForwardIter __first, _ForwardIter __last, const _Tp& __val)
02771     {
02772       typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
02773       typedef typename iterator_traits<_ForwardIter>::difference_type _DistanceType;
02774 
02775       // concept requirements
02776       // Note that these are slightly stricter than those of the 4-argument
02777       // version, defined next.  The difference is in the strictness of the
02778       // comparison operations... so for looser checking, define your own
02779       // comparison function, as was intended.
02780       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
02781       __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
02782       __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
02783 
02784       _DistanceType __len = distance(__first, __last);
02785       _DistanceType __half;
02786       _ForwardIter __middle;
02787 
02788       while (__len > 0) {
02789     __half = __len >> 1;
02790     __middle = __first;
02791     advance(__middle, __half);
02792     if (*__middle < __val) {
02793       __first = __middle;
02794       ++__first;
02795       __len = __len - __half - 1;
02796     }
02797     else
02798       __len = __half;
02799       }
02800       return __first;
02801     }
02802 
02803   /**
02804    *  @brief Finds the first position in which @a val could be inserted
02805    *         without changing the ordering.
02806    *  @param  first   An iterator.
02807    *  @param  last    Another iterator.
02808    *  @param  val     The search term.
02809    *  @param  comp    A functor to use for comparisons.
02810    *  @return  An iterator pointing to the first element "not less than" @a val.
02811    *  @ingroup binarysearch
02812    *
02813    *  The comparison function should have the same effects on ordering as
02814    *  the function used for the initial sort.
02815   */
02816   template<typename _ForwardIter, typename _Tp, typename _Compare>
02817     _ForwardIter
02818     lower_bound(_ForwardIter __first, _ForwardIter __last,
02819         const _Tp& __val, _Compare __comp)
02820     {
02821       typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
02822       typedef typename iterator_traits<_ForwardIter>::difference_type _DistanceType;
02823 
02824       // concept requirements
02825       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
02826       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>)
02827 
02828       _DistanceType __len = distance(__first, __last);
02829       _DistanceType __half;
02830       _ForwardIter __middle;
02831 
02832       while (__len > 0) {
02833     __half = __len >> 1;
02834     __middle = __first;
02835     advance(__middle, __half);
02836     if (__comp(*__middle, __val)) {
02837       __first = __middle;
02838       ++__first;
02839       __len = __len - __half - 1;
02840     }
02841     else
02842       __len = __half;
02843       }
02844       return __first;
02845     }
02846 
02847   /**
02848    *  @brief Finds the last position in which @a val could be inserted
02849    *         without changing the ordering.
02850    *  @param  first   An iterator.
02851    *  @param  last    Another iterator.
02852    *  @param  val     The search term.
02853    *  @return  An iterator pointing to the first element greater than @a val.
02854    *  @ingroup binarysearch
02855   */
02856   template<typename _ForwardIter, typename _Tp>
02857     _ForwardIter
02858     upper_bound(_ForwardIter __first, _ForwardIter __last, const _Tp& __val)
02859     {
02860       typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
02861       typedef typename iterator_traits<_ForwardIter>::difference_type _DistanceType;
02862 
02863       // concept requirements
02864       // See comments on lower_bound.
02865       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
02866       __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
02867       __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
02868 
02869       _DistanceType __len = distance(__first, __last);
02870       _DistanceType __half;
02871       _ForwardIter __middle;
02872 
02873       while (__len > 0) {
02874     __half = __len >> 1;
02875     __middle = __first;
02876     advance(__middle, __half);
02877     if (__val < *__middle)
02878       __len = __half;
02879     else {
02880       __first = __middle;
02881       ++__first;
02882       __len = __len - __half - 1;
02883     }
02884       }
02885       return __first;
02886     }
02887 
02888   /**
02889    *  @brief Finds the last position in which @a val could be inserted
02890    *         without changing the ordering.
02891    *  @param  first   An iterator.
02892    *  @param  last    Another iterator.
02893    *  @param  val     The search term.
02894    *  @param  comp    A functor to use for comparisons.
02895    *  @return  An iterator pointing to the first element greater than @a val.
02896    *  @ingroup binarysearch
02897    *
02898    *  The comparison function should have the same effects on ordering as
02899    *  the function used for the initial sort.
02900   */
02901   template<typename _ForwardIter, typename _Tp, typename _Compare>
02902     _ForwardIter
02903     upper_bound(_ForwardIter __first, _ForwardIter __last,
02904         const _Tp& __val, _Compare __comp)
02905     {
02906       typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
02907       typedef typename iterator_traits<_ForwardIter>::difference_type _DistanceType;
02908 
02909       // concept requirements
02910       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
02911       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>)
02912 
02913       _DistanceType __len = distance(__first, __last);
02914       _DistanceType __half;
02915       _ForwardIter __middle;
02916 
02917       while (__len > 0) {
02918     __half = __len >> 1;
02919     __middle = __first;
02920     advance(__middle, __half);
02921     if (__comp(__val, *__middle))
02922       __len = __half;
02923     else {
02924       __first = __middle;
02925       ++__first;
02926       __len = __len - __half - 1;
02927     }
02928       }
02929       return __first;
02930     }
02931 
02932   /**
02933    *  @brief Finds the largest subrange in which @a val could be inserted
02934    *         at any place in it without changing the ordering.
02935    *  @param  first   An iterator.
02936    *  @param  last    Another iterator.
02937    *  @param  val     The search term.
02938    *  @return  An pair of iterators defining the subrange.
02939    *  @ingroup binarysearch
02940    *
02941    *  This is equivalent to
02942    *  @code
02943    *    std::make_pair(lower_bound(first, last, val),
02944    *                   upper_bound(first, last, val))
02945    *  @endcode
02946    *  but does not actually call those functions.
02947   */
02948   template<typename _ForwardIter, typename _Tp>
02949     pair<_ForwardIter, _ForwardIter>
02950     equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val)
02951     {
02952       typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
02953       typedef typename iterator_traits<_ForwardIter>::difference_type _DistanceType;
02954 
02955       // concept requirements
02956       // See comments on lower_bound.
02957       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
02958       __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
02959       __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
02960 
02961       _DistanceType __len = distance(__first, __last);
02962       _DistanceType __half;
02963       _ForwardIter __middle, __left, __right;
02964 
02965       while (__len > 0) {
02966     __half = __len >> 1;
02967     __middle = __first;
02968     advance(__middle, __half);
02969     if (*__middle < __val) {
02970       __first = __middle;
02971       ++__first;
02972       __len = __len - __half - 1;
02973     }
02974     else if (__val < *__middle)
02975       __len = __half;
02976     else {
02977       __left = lower_bound(__first, __middle, __val);
02978       advance(__first, __len);
02979       __right = upper_bound(++__middle, __first, __val);
02980       return pair<_ForwardIter, _ForwardIter>(__left, __right);
02981     }
02982       }
02983       return pair<_ForwardIter, _ForwardIter>(__first, __first);
02984     }
02985 
02986   /**
02987    *  @brief Finds the largest subrange in which @a val could be inserted
02988    *         at any place in it without changing the ordering.
02989    *  @param  first   An iterator.
02990    *  @param  last    Another iterator.
02991    *  @param  val     The search term.
02992    *  @param  comp    A functor to use for comparisons.
02993    *  @return  An pair of iterators defining the subrange.
02994    *  @ingroup binarysearch
02995    *
02996    *  This is equivalent to
02997    *  @code
02998    *    std::make_pair(lower_bound(first, last, val, comp),
02999    *                   upper_bound(first, last, val, comp))
03000    *  @endcode
03001    *  but does not actually call those functions.
03002   */
03003   template<typename _ForwardIter, typename _Tp, typename _Compare>
03004     pair<_ForwardIter, _ForwardIter>
03005     equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
03006         _Compare __comp)
03007     {
03008       typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
03009       typedef typename iterator_traits<_ForwardIter>::difference_type _DistanceType;
03010 
03011       // concept requirements
03012       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
03013       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>)
03014       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>)
03015 
03016       _DistanceType __len = distance(__first, __last);
03017       _DistanceType __half;
03018       _ForwardIter __middle, __left, __right;
03019 
03020       while (__len > 0) {
03021     __half = __len >> 1;
03022     __middle = __first;
03023     advance(__middle, __half);
03024     if (__comp(*__middle, __val)) {
03025       __first = __middle;
03026       ++__first;
03027       __len = __len - __half - 1;
03028     }
03029     else if (__comp(__val, *__middle))
03030       __len = __half;
03031     else {
03032       __left = lower_bound(__first, __middle, __val, __comp);
03033       advance(__first, __len);
03034       __right = upper_bound(++__middle, __first, __val, __comp);
03035       return pair<_ForwardIter, _ForwardIter>(__left, __right);
03036     }
03037       }
03038       return pair<_ForwardIter, _ForwardIter>(__first, __first);
03039     }
03040 
03041   /**
03042    *  @brief Determines whether an element exists in a range.
03043    *  @param  first   An iterator.
03044    *  @param  last    Another iterator.
03045    *  @param  val     The search term.
03046    *  @return  True if @a val (or its equivelent) is in [@a first,@a last ].
03047    *  @ingroup binarysearch
03048    *
03049    *  Note that this does not actually return an iterator to @a val.  For
03050    *  that, use std::find or a container's specialized find member functions.
03051   */
03052   template<typename _ForwardIter, typename _Tp>
03053     bool
03054     binary_search(_ForwardIter __first, _ForwardIter __last,
03055                   const _Tp& __val)
03056     {
03057       // concept requirements
03058       // See comments on lower_bound.
03059       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
03060       __glibcpp_function_requires(_SameTypeConcept<_Tp,
03061         typename iterator_traits<_ForwardIter>::value_type>)
03062       __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
03063 
03064       _ForwardIter __i = lower_bound(__first, __last, __val);
03065       return __i != __last && !(__val < *__i);
03066     }
03067 
03068   /**
03069    *  @brief Determines whether an element exists in a range.
03070    *  @param  first   An iterator.
03071    *  @param  last    Another iterator.
03072    *  @param  val     The search term.
03073    *  @param  comp    A functor to use for comparisons.
03074    *  @return  True if @a val (or its equivelent) is in [@a first,@a last ].
03075    *  @ingroup binarysearch
03076    *
03077    *  Note that this does not actually return an iterator to @a val.  For
03078    *  that, use std::find or a container's specialized find member functions.
03079    *
03080    *  The comparison function should have the same effects on ordering as
03081    *  the function used for the initial sort.
03082   */
03083   template<typename _ForwardIter, typename _Tp, typename _Compare>
03084     bool
03085     binary_search(_ForwardIter __first, _ForwardIter __last,
03086                   const _Tp& __val, _Compare __comp)
03087     {
03088       // concept requirements
03089       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
03090       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03091         typename iterator_traits<_ForwardIter>::value_type, _Tp>)
03092       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp,
03093         typename iterator_traits<_ForwardIter>::value_type>)
03094 
03095       _ForwardIter __i = lower_bound(__first, __last, __val, __comp);
03096       return __i != __last && !__comp(__val, *__i);
03097     }
03098 
03099   /**
03100    *  @brief Merges two sorted ranges.
03101    *  @param  first1  An iterator.
03102    *  @param  first2  Another iterator.
03103    *  @param  last1   Another iterator.
03104    *  @param  last2   Another iterator.
03105    *  @param  result  An iterator pointing to the end of the merged range.
03106    *  @return  An iterator pointing to the first element "not less than" @a val.
03107    *
03108    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
03109    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
03110    *  must be sorted, and the output range must not overlap with either of
03111    *  the input ranges.  The sort is @e stable, that is, for equivalent
03112    *  elements in the two ranges, elements from the first range will always
03113    *  come before elements from the second.
03114   */
03115   template<typename _InputIter1, typename _InputIter2, typename _OutputIter>
03116     _OutputIter
03117     merge(_InputIter1 __first1, _InputIter1 __last1,
03118       _InputIter2 __first2, _InputIter2 __last2,
03119       _OutputIter __result)
03120     {
03121       // concept requirements
03122       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03123       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03124       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03125         typename iterator_traits<_InputIter1>::value_type>)
03126       __glibcpp_function_requires(_SameTypeConcept<
03127         typename iterator_traits<_InputIter1>::value_type,
03128         typename iterator_traits<_InputIter2>::value_type>)
03129       __glibcpp_function_requires(_LessThanComparableConcept<
03130         typename iterator_traits<_InputIter1>::value_type>)
03131 
03132       while (__first1 != __last1 && __first2 != __last2) {
03133     if (*__first2 < *__first1) {
03134       *__result = *__first2;
03135       ++__first2;
03136     }
03137     else {
03138       *__result = *__first1;
03139       ++__first1;
03140     }
03141     ++__result;
03142       }
03143       return copy(__first2, __last2, copy(__first1, __last1, __result));
03144     }
03145 
03146   /**
03147    *  @brief Merges two sorted ranges.
03148    *  @param  first1  An iterator.
03149    *  @param  first2  Another iterator.
03150    *  @param  last1   Another iterator.
03151    *  @param  last2   Another iterator.
03152    *  @param  result  An iterator pointing to the end of the merged range.
03153    *  @param  comp    A functor to use for comparisons.
03154    *  @return  An iterator pointing to the first element "not less than" @a val.
03155    *
03156    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
03157    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
03158    *  must be sorted, and the output range must not overlap with either of
03159    *  the input ranges.  The sort is @e stable, that is, for equivalent
03160    *  elements in the two ranges, elements from the first range will always
03161    *  come before elements from the second.
03162    *
03163    *  The comparison function should have the same effects on ordering as
03164    *  the function used for the initial sort.
03165   */
03166   template<typename _InputIter1, typename _InputIter2, typename _OutputIter,
03167        typename _Compare>
03168     _OutputIter
03169     merge(_InputIter1 __first1, _InputIter1 __last1,
03170       _InputIter2 __first2, _InputIter2 __last2,
03171       _OutputIter __result, _Compare __comp)
03172     {
03173       // concept requirements
03174       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03175       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03176       __glibcpp_function_requires(_SameTypeConcept<
03177         typename iterator_traits<_InputIter1>::value_type,
03178         typename iterator_traits<_InputIter2>::value_type>)
03179       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03180         typename iterator_traits<_InputIter1>::value_type>)
03181       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03182         typename iterator_traits<_InputIter1>::value_type,
03183         typename iterator_traits<_InputIter2>::value_type>)
03184 
03185       while (__first1 != __last1 && __first2 != __last2) {
03186     if (__comp(*__first2, *__first1)) {
03187       *__result = *__first2;
03188       ++__first2;
03189     }
03190     else {
03191       *__result = *__first1;
03192       ++__first1;
03193     }
03194     ++__result;
03195       }
03196       return copy(__first2, __last2, copy(__first1, __last1, __result));
03197     }
03198 
03199   /**
03200    *  @if maint
03201    *  This is a helper function for the merge routines.
03202    *  @endif
03203   */
03204   template<typename _BidirectionalIter, typename _Distance>
03205     void
03206     __merge_without_buffer(_BidirectionalIter __first,
03207                _BidirectionalIter __middle,
03208                _BidirectionalIter __last,
03209                _Distance __len1, _Distance __len2)
03210     {
03211       if (__len1 == 0 || __len2 == 0)
03212     return;
03213       if (__len1 + __len2 == 2) {
03214     if (*__middle < *__first)
03215           iter_swap(__first, __middle);
03216     return;
03217       }
03218       _BidirectionalIter __first_cut = __first;
03219       _BidirectionalIter __second_cut = __middle;
03220       _Distance __len11 = 0;
03221       _Distance __len22 = 0;
03222       if (__len1 > __len2) {
03223     __len11 = __len1 / 2;
03224     advance(__first_cut, __len11);
03225     __second_cut = lower_bound(__middle, __last, *__first_cut);
03226     __len22 = distance(__middle, __second_cut);
03227       }
03228       else {
03229     __len22 = __len2 / 2;
03230     advance(__second_cut, __len22);
03231     __first_cut = upper_bound(__first, __middle, *__second_cut);
03232     __len11 = distance(__first, __first_cut);
03233       }
03234       rotate(__first_cut, __middle, __second_cut);
03235       _BidirectionalIter __new_middle = __first_cut;
03236       advance(__new_middle, distance(__middle, __second_cut));
03237       __merge_without_buffer(__first, __first_cut, __new_middle,
03238                  __len11, __len22);
03239       __merge_without_buffer(__new_middle, __second_cut, __last,
03240                  __len1 - __len11, __len2 - __len22);
03241     }
03242 
03243   /**
03244    *  @if maint
03245    *  This is a helper function for the merge routines.
03246    *  @endif
03247   */
03248   template<typename _BidirectionalIter, typename _Distance, typename _Compare>
03249     void
03250     __merge_without_buffer(_BidirectionalIter __first,
03251                            _BidirectionalIter __middle,
03252                _BidirectionalIter __last,
03253                _Distance __len1, _Distance __len2,
03254                _Compare __comp)
03255     {
03256       if (__len1 == 0 || __len2 == 0)
03257     return;
03258       if (__len1 + __len2 == 2) {
03259     if (__comp(*__middle, *__first))
03260           iter_swap(__first, __middle);
03261     return;
03262       }
03263       _BidirectionalIter __first_cut = __first;
03264       _BidirectionalIter __second_cut = __middle;
03265       _Distance __len11 = 0;
03266       _Distance __len22 = 0;
03267       if (__len1 > __len2) {
03268     __len11 = __len1 / 2;
03269     advance(__first_cut, __len11);
03270     __second_cut = lower_bound(__middle, __last, *__first_cut, __comp);
03271     __len22 = distance(__middle, __second_cut);
03272       }
03273       else {
03274     __len22 = __len2 / 2;
03275     advance(__second_cut, __len22);
03276     __first_cut = upper_bound(__first, __middle, *__second_cut, __comp);
03277     __len11 = distance(__first, __first_cut);
03278       }
03279       rotate(__first_cut, __middle, __second_cut);
03280       _BidirectionalIter __new_middle = __first_cut;
03281       advance(__new_middle, distance(__middle, __second_cut));
03282       __merge_without_buffer(__first, __first_cut, __new_middle,
03283                  __len11, __len22, __comp);
03284       __merge_without_buffer(__new_middle, __second_cut, __last,
03285                  __len1 - __len11, __len2 - __len22, __comp);
03286     }
03287 
03288   /**
03289    *  @if maint
03290    *  This is a helper function for the merge routines.
03291    *  @endif
03292   */
03293   template<typename _BidirectionalIter1, typename _BidirectionalIter2,
03294        typename _Distance>
03295     _BidirectionalIter1
03296     __rotate_adaptive(_BidirectionalIter1 __first,
03297               _BidirectionalIter1 __middle,
03298               _BidirectionalIter1 __last,
03299               _Distance __len1, _Distance __len2,
03300               _BidirectionalIter2 __buffer,
03301               _Distance __buffer_size)
03302     {
03303       _BidirectionalIter2 __buffer_end;
03304       if (__len1 > __len2 && __len2 <= __buffer_size) {
03305     __buffer_end = copy(__middle, __last, __buffer);
03306     copy_backward(__first, __middle, __last);
03307     return copy(__buffer, __buffer_end, __first);
03308       }
03309       else if (__len1 <= __buffer_size) {
03310     __buffer_end = copy(__first, __middle, __buffer);
03311     copy(__middle, __last, __first);
03312     return copy_backward(__buffer, __buffer_end, __last);
03313       }
03314       else {
03315     rotate(__first, __middle, __last);
03316     advance(__first, distance(__middle, __last));
03317     return __first;
03318       }
03319     }
03320 
03321   /**
03322    *  @if maint
03323    *  This is a helper function for the merge routines.
03324    *  @endif
03325   */
03326   template<typename _BidirectionalIter1, typename _BidirectionalIter2,
03327        typename _BidirectionalIter3>
03328     _BidirectionalIter3
03329     __merge_backward(_BidirectionalIter1 __first1, _BidirectionalIter1 __last1,
03330              _BidirectionalIter2 __first2, _BidirectionalIter2 __last2,
03331              _BidirectionalIter3 __result)
03332     {
03333       if (__first1 == __last1)
03334     return copy_backward(__first2, __last2, __result);
03335       if (__first2 == __last2)
03336     return copy_backward(__first1, __last1, __result);
03337       --__last1;
03338       --__last2;
03339       while (true) {
03340     if (*__last2 < *__last1) {
03341       *--__result = *__last1;
03342       if (__first1 == __last1)
03343         return copy_backward(__first2, ++__last2, __result);
03344       --__last1;
03345     }
03346     else {
03347       *--__result = *__last2;
03348       if (__first2 == __last2)
03349         return copy_backward(__first1, ++__last1, __result);
03350       --__last2;
03351     }
03352       }
03353     }
03354 
03355   /**
03356    *  @if maint
03357    *  This is a helper function for the merge routines.
03358    *  @endif
03359   */
03360   template<typename _BidirectionalIter1, typename _BidirectionalIter2,
03361        typename _BidirectionalIter3, typename _Compare>
03362     _BidirectionalIter3
03363     __merge_backward(_BidirectionalIter1 __first1, _BidirectionalIter1 __last1,
03364              _BidirectionalIter2 __first2, _BidirectionalIter2 __last2,
03365              _BidirectionalIter3 __result,
03366              _Compare __comp)
03367     {
03368       if (__first1 == __last1)
03369     return copy_backward(__first2, __last2, __result);
03370       if (__first2 == __last2)
03371     return copy_backward(__first1, __last1, __result);
03372       --__last1;
03373       --__last2;
03374       while (true) {
03375     if (__comp(*__last2, *__last1)) {
03376       *--__result = *__last1;
03377       if (__first1 == __last1)
03378         return copy_backward(__first2, ++__last2, __result);
03379       --__last1;
03380     }
03381     else {
03382       *--__result = *__last2;
03383       if (__first2 == __last2)
03384         return copy_backward(__first1, ++__last1, __result);
03385       --__last2;
03386     }
03387       }
03388     }
03389 
03390   /**
03391    *  @if maint
03392    *  This is a helper function for the merge routines.
03393    *  @endif
03394   */
03395   template<typename _BidirectionalIter, typename _Distance, typename _Pointer>
03396     void
03397     __merge_adaptive(_BidirectionalIter __first,
03398                      _BidirectionalIter __middle,
03399              _BidirectionalIter __last,
03400              _Distance __len1, _Distance __len2,
03401              _Pointer __buffer, _Distance __buffer_size)
03402     {
03403       if (__len1 <= __len2 && __len1 <= __buffer_size) {
03404         _Pointer __buffer_end = copy(__first, __middle, __buffer);
03405         merge(__buffer, __buffer_end, __middle, __last, __first);
03406       }
03407       else if (__len2 <= __buffer_size) {
03408         _Pointer __buffer_end = copy(__middle, __last, __buffer);
03409         __merge_backward(__first, __middle, __buffer, __buffer_end, __last);
03410       }
03411       else {
03412         _BidirectionalIter __first_cut = __first;
03413         _BidirectionalIter __second_cut = __middle;
03414         _Distance __len11 = 0;
03415         _Distance __len22 = 0;
03416         if (__len1 > __len2) {
03417           __len11 = __len1 / 2;
03418           advance(__first_cut, __len11);
03419           __second_cut = lower_bound(__middle, __last, *__first_cut);
03420           __len22 = distance(__middle, __second_cut);
03421         }
03422         else {
03423           __len22 = __len2 / 2;
03424           advance(__second_cut, __len22);
03425           __first_cut = upper_bound(__first, __middle, *__second_cut);
03426           __len11 = distance(__first, __first_cut);
03427         }
03428         _BidirectionalIter __new_middle =
03429           __rotate_adaptive(__first_cut, __middle, __second_cut,
03430                     __len1 - __len11, __len22, __buffer,
03431                     __buffer_size);
03432         __merge_adaptive(__first, __first_cut, __new_middle, __len11,
03433                  __len22, __buffer, __buffer_size);
03434         __merge_adaptive(__new_middle, __second_cut, __last, __len1 - __len11,
03435                  __len2 - __len22, __buffer, __buffer_size);
03436       }
03437     }
03438 
03439   /**
03440    *  @if maint
03441    *  This is a helper function for the merge routines.
03442    *  @endif
03443   */
03444   template<typename _BidirectionalIter, typename _Distance, typename _Pointer,
03445        typename _Compare>
03446     void
03447     __merge_adaptive(_BidirectionalIter __first,
03448                      _BidirectionalIter __middle,
03449              _BidirectionalIter __last,
03450              _Distance __len1, _Distance __len2,
03451              _Pointer __buffer, _Distance __buffer_size,
03452              _Compare __comp)
03453     {
03454       if (__len1 <= __len2 && __len1 <= __buffer_size) {
03455         _Pointer __buffer_end = copy(__first, __middle, __buffer);
03456         merge(__buffer, __buffer_end, __middle, __last, __first, __comp);
03457       }
03458       else if (__len2 <= __buffer_size) {
03459         _Pointer __buffer_end = copy(__middle, __last, __buffer);
03460         __merge_backward(__first, __middle, __buffer, __buffer_end, __last,
03461                          __comp);
03462       }
03463       else {
03464         _BidirectionalIter __first_cut = __first;
03465         _BidirectionalIter __second_cut = __middle;
03466         _Distance __len11 = 0;
03467         _Distance __len22 = 0;
03468         if (__len1 > __len2) {
03469           __len11 = __len1 / 2;
03470           advance(__first_cut, __len11);
03471           __second_cut = lower_bound(__middle, __last, *__first_cut, __comp);
03472           __len22 = distance(__middle, __second_cut);
03473         }
03474         else {
03475           __len22 = __len2 / 2;
03476           advance(__second_cut, __len22);
03477           __first_cut = upper_bound(__first, __middle, *__second_cut, __comp);
03478           __len11 = distance(__first, __first_cut);
03479         }
03480         _BidirectionalIter __new_middle =
03481           __rotate_adaptive(__first_cut, __middle, __second_cut,
03482                     __len1 - __len11, __len22, __buffer,
03483                     __buffer_size);
03484         __merge_adaptive(__first, __first_cut, __new_middle, __len11,
03485                  __len22, __buffer, __buffer_size, __comp);
03486         __merge_adaptive(__new_middle, __second_cut, __last, __len1 - __len11,
03487                  __len2 - __len22, __buffer, __buffer_size, __comp);
03488       }
03489     }
03490 
03491   /**
03492    *  @brief Merges two sorted ranges in place.
03493    *  @param  first   An iterator.
03494    *  @param  middle  Another iterator.
03495    *  @param  last    Another iterator.
03496    *  @return  Nothing.
03497    *
03498    *  Merges two sorted and consecutive ranges, [first,middle) and
03499    *  [middle,last), and puts the result in [first,last).  The output will
03500    *  be sorted.  The sort is @e stable, that is, for equivalent
03501    *  elements in the two ranges, elements from the first range will always
03502    *  come before elements from the second.
03503    *
03504    *  If enough additional memory is available, this takes (last-first)-1
03505    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
03506    *  distance(first,last).
03507   */
03508   template<typename _BidirectionalIter>
03509     void
03510     inplace_merge(_BidirectionalIter __first,
03511           _BidirectionalIter __middle,
03512           _BidirectionalIter __last)
03513     {
03514       typedef typename iterator_traits<_BidirectionalIter>::value_type
03515           _ValueType;
03516       typedef typename iterator_traits<_BidirectionalIter>::difference_type
03517           _DistanceType;
03518 
03519       // concept requirements
03520       __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
03521         _BidirectionalIter>)
03522       __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
03523 
03524       if (__first == __middle || __middle == __last)
03525     return;
03526 
03527       _DistanceType __len1 = distance(__first, __middle);
03528       _DistanceType __len2 = distance(__middle, __last);
03529 
03530       _Temporary_buffer<_BidirectionalIter, _ValueType> __buf(__first, __last);
03531       if (__buf.begin() == 0)
03532     __merge_without_buffer(__first, __middle, __last, __len1, __len2);
03533       else
03534     __merge_adaptive(__first, __middle, __last, __len1, __len2,
03535              __buf.begin(), _DistanceType(__buf.size()));
03536     }
03537 
03538   /**
03539    *  @brief Merges two sorted ranges in place.
03540    *  @param  first   An iterator.
03541    *  @param  middle  Another iterator.
03542    *  @param  last    Another iterator.
03543    *  @param  comp    A functor to use for comparisons.
03544    *  @return  Nothing.
03545    *
03546    *  Merges two sorted and consecutive ranges, [first,middle) and
03547    *  [middle,last), and puts the result in [first,last).  The output will
03548    *  be sorted.  The sort is @e stable, that is, for equivalent
03549    *  elements in the two ranges, elements from the first range will always
03550    *  come before elements from the second.
03551    *
03552    *  If enough additional memory is available, this takes (last-first)-1
03553    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
03554    *  distance(first,last).
03555    *
03556    *  The comparison function should have the same effects on ordering as
03557    *  the function used for the initial sort.
03558   */
03559   template<typename _BidirectionalIter, typename _Compare>
03560     void
03561     inplace_merge(_BidirectionalIter __first,
03562           _BidirectionalIter __middle,
03563           _BidirectionalIter __last,
03564           _Compare __comp)
03565     {
03566       typedef typename iterator_traits<_BidirectionalIter>::value_type
03567           _ValueType;
03568       typedef typename iterator_traits<_BidirectionalIter>::difference_type
03569           _DistanceType;
03570 
03571       // concept requirements
03572       __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
03573         _BidirectionalIter>)
03574       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03575         _ValueType, _ValueType>)
03576 
03577       if (__first == __middle || __middle == __last)
03578     return;
03579 
03580       _DistanceType __len1 = distance(__first, __middle);
03581       _DistanceType __len2 = distance(__middle, __last);
03582 
03583       _Temporary_buffer<_BidirectionalIter, _ValueType> __buf(__first, __last);
03584       if (__buf.begin() == 0)
03585     __merge_without_buffer(__first, __middle, __last, __len1, __len2, __comp);
03586       else
03587     __merge_adaptive(__first, __middle, __last, __len1, __len2,
03588              __buf.begin(), _DistanceType(__buf.size()),
03589              __comp);
03590     }
03591 
03592   // Set algorithms: includes, set_union, set_intersection, set_difference,
03593   // set_symmetric_difference.  All of these algorithms have the precondition
03594   // that their input ranges are sorted and the postcondition that their output
03595   // ranges are sorted.
03596 
03597   template<typename _InputIter1, typename _InputIter2>
03598     bool
03599     includes(_InputIter1 __first1, _InputIter1 __last1,
03600          _InputIter2 __first2, _InputIter2 __last2)
03601     {
03602       // concept requirements
03603       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03604       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03605       __glibcpp_function_requires(_SameTypeConcept<
03606         typename iterator_traits<_InputIter1>::value_type,
03607         typename iterator_traits<_InputIter2>::value_type>)
03608       __glibcpp_function_requires(_LessThanComparableConcept<
03609         typename iterator_traits<_InputIter1>::value_type>)
03610 
03611       while (__first1 != __last1 && __first2 != __last2)
03612     if (*__first2 < *__first1)
03613       return false;
03614     else if(*__first1 < *__first2)
03615       ++__first1;
03616     else
03617       ++__first1, ++__first2;
03618 
03619       return __first2 == __last2;
03620     }
03621 
03622   template<typename _InputIter1, typename _InputIter2, typename _Compare>
03623     bool
03624     includes(_InputIter1 __first1, _InputIter1 __last1,
03625          _InputIter2 __first2, _InputIter2 __last2, _Compare __comp)
03626     {
03627       // concept requirements
03628       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03629       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03630       __glibcpp_function_requires(_SameTypeConcept<
03631         typename iterator_traits<_InputIter1>::value_type,
03632         typename iterator_traits<_InputIter2>::value_type>)
03633       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03634         typename iterator_traits<_InputIter1>::value_type,
03635         typename iterator_traits<_InputIter2>::value_type>)
03636 
03637       while (__first1 != __last1 && __first2 != __last2)
03638     if (__comp(*__first2, *__first1))
03639       return false;
03640     else if(__comp(*__first1, *__first2))
03641       ++__first1;
03642     else
03643       ++__first1, ++__first2;
03644 
03645       return __first2 == __last2;
03646     }
03647 
03648   template<typename _InputIter1, typename _InputIter2, typename _OutputIter>
03649     _OutputIter
03650     set_union(_InputIter1 __first1, _InputIter1 __last1,
03651           _InputIter2 __first2, _InputIter2 __last2,
03652           _OutputIter __result)
03653     {
03654       // concept requirements
03655       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03656       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03657       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03658         typename iterator_traits<_InputIter1>::value_type>)
03659       __glibcpp_function_requires(_SameTypeConcept<
03660         typename iterator_traits<_InputIter1>::value_type,
03661         typename iterator_traits<_InputIter2>::value_type>)
03662       __glibcpp_function_requires(_LessThanComparableConcept<
03663         typename iterator_traits<_InputIter1>::value_type>)
03664 
03665       while (__first1 != __last1 && __first2 != __last2) {
03666     if (*__first1 < *__first2) {
03667       *__result = *__first1;
03668       ++__first1;
03669     }
03670     else if (*__first2 < *__first1) {
03671       *__result = *__first2;
03672       ++__first2;
03673     }
03674     else {
03675       *__result = *__first1;
03676       ++__first1;
03677       ++__first2;
03678     }
03679     ++__result;
03680       }
03681       return copy(__first2, __last2, copy(__first1, __last1, __result));
03682     }
03683 
03684   template<typename _InputIter1, typename _InputIter2, typename _OutputIter,
03685        typename _Compare>
03686     _OutputIter
03687     set_union(_InputIter1 __first1, _InputIter1 __last1,
03688           _InputIter2 __first2, _InputIter2 __last2,
03689           _OutputIter __result, _Compare __comp)
03690     {
03691       // concept requirements
03692       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03693       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03694       __glibcpp_function_requires(_SameTypeConcept<
03695         typename iterator_traits<_InputIter1>::value_type,
03696         typename iterator_traits<_InputIter2>::value_type>)
03697       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03698         typename iterator_traits<_InputIter1>::value_type>)
03699       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03700         typename iterator_traits<_InputIter1>::value_type,
03701         typename iterator_traits<_InputIter2>::value_type>)
03702 
03703       while (__first1 != __last1 && __first2 != __last2) {
03704     if (__comp(*__first1, *__first2)) {
03705       *__result = *__first1;
03706       ++__first1;
03707     }
03708     else if (__comp(*__first2, *__first1)) {
03709       *__result = *__first2;
03710       ++__first2;
03711     }
03712     else {
03713       *__result = *__first1;
03714       ++__first1;
03715       ++__first2;
03716     }
03717     ++__result;
03718       }
03719       return copy(__first2, __last2, copy(__first1, __last1, __result));
03720     }
03721 
03722   template<typename _InputIter1, typename _InputIter2, typename _OutputIter>
03723     _OutputIter
03724     set_intersection(_InputIter1 __first1, _InputIter1 __last1,
03725              _InputIter2 __first2, _InputIter2 __last2,
03726              _OutputIter __result)
03727     {
03728       // concept requirements
03729       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03730       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03731       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03732         typename iterator_traits<_InputIter1>::value_type>)
03733       __glibcpp_function_requires(_SameTypeConcept<
03734         typename iterator_traits<_InputIter1>::value_type,
03735         typename iterator_traits<_InputIter2>::value_type>)
03736       __glibcpp_function_requires(_LessThanComparableConcept<
03737         typename iterator_traits<_InputIter1>::value_type>)
03738 
03739       while (__first1 != __last1 && __first2 != __last2)
03740     if (*__first1 < *__first2)
03741       ++__first1;
03742     else if (*__first2 < *__first1)
03743       ++__first2;
03744     else {
03745       *__result = *__first1;
03746       ++__first1;
03747       ++__first2;
03748       ++__result;
03749     }
03750       return __result;
03751     }
03752 
03753   template<typename _InputIter1, typename _InputIter2, typename _OutputIter,
03754        typename _Compare>
03755     _OutputIter
03756     set_intersection(_InputIter1 __first1, _InputIter1 __last1,
03757              _InputIter2 __first2, _InputIter2 __last2,
03758              _OutputIter __result, _Compare __comp)
03759     {
03760       // concept requirements
03761       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03762       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03763       __glibcpp_function_requires(_SameTypeConcept<
03764         typename iterator_traits<_InputIter1>::value_type,
03765         typename iterator_traits<_InputIter2>::value_type>)
03766       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03767         typename iterator_traits<_InputIter1>::value_type>)
03768       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03769         typename iterator_traits<_InputIter1>::value_type,
03770         typename iterator_traits<_InputIter2>::value_type>)
03771 
03772       while (__first1 != __last1 && __first2 != __last2)
03773     if (__comp(*__first1, *__first2))
03774       ++__first1;
03775     else if (__comp(*__first2, *__first1))
03776       ++__first2;
03777     else {
03778       *__result = *__first1;
03779       ++__first1;
03780       ++__first2;
03781       ++__result;
03782     }
03783       return __result;
03784     }
03785 
03786   template<typename _InputIter1, typename _InputIter2, typename _OutputIter>
03787     _OutputIter
03788     set_difference(_InputIter1 __first1, _InputIter1 __last1,
03789            _InputIter2 __first2, _InputIter2 __last2,
03790            _OutputIter __result)
03791     {
03792       // concept requirements
03793       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03794       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03795       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03796         typename iterator_traits<_InputIter1>::value_type>)
03797       __glibcpp_function_requires(_SameTypeConcept<
03798         typename iterator_traits<_InputIter1>::value_type,
03799         typename iterator_traits<_InputIter2>::value_type>)
03800       __glibcpp_function_requires(_LessThanComparableConcept<
03801         typename iterator_traits<_InputIter1>::value_type>)
03802 
03803       while (__first1 != __last1 && __first2 != __last2)
03804     if (*__first1 < *__first2) {
03805       *__result = *__first1;
03806       ++__first1;
03807       ++__result;
03808     }
03809     else if (*__first2 < *__first1)
03810       ++__first2;
03811     else {
03812       ++__first1;
03813       ++__first2;
03814     }
03815       return copy(__first1, __last1, __result);
03816     }
03817 
03818   template<typename _InputIter1, typename _InputIter2, typename _OutputIter,
03819        typename _Compare>
03820     _OutputIter
03821     set_difference(_InputIter1 __first1, _InputIter1 __last1,
03822            _InputIter2 __first2, _InputIter2 __last2,
03823            _OutputIter __result, _Compare __comp)
03824     {
03825       // concept requirements
03826       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03827       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03828       __glibcpp_function_requires(_SameTypeConcept<
03829         typename iterator_traits<_InputIter1>::value_type,
03830         typename iterator_traits<_InputIter2>::value_type>)
03831       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03832         typename iterator_traits<_InputIter1>::value_type>)
03833       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03834         typename iterator_traits<_InputIter1>::value_type,
03835         typename iterator_traits<_InputIter2>::value_type>)
03836 
03837       while (__first1 != __last1 && __first2 != __last2)
03838     if (__comp(*__first1, *__first2)) {
03839       *__result = *__first1;
03840       ++__first1;
03841       ++__result;
03842     }
03843     else if (__comp(*__first2, *__first1))
03844       ++__first2;
03845     else {
03846       ++__first1;
03847       ++__first2;
03848     }
03849       return copy(__first1, __last1, __result);
03850     }
03851 
03852   template<typename _InputIter1, typename _InputIter2, typename _OutputIter>
03853     _OutputIter
03854     set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1,
03855                  _InputIter2 __first2, _InputIter2 __last2,
03856                  _OutputIter __result)
03857     {
03858       // concept requirements
03859       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03860       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03861       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03862         typename iterator_traits<_InputIter1>::value_type>)
03863       __glibcpp_function_requires(_SameTypeConcept<
03864         typename iterator_traits<_InputIter1>::value_type,
03865         typename iterator_traits<_InputIter2>::value_type>)
03866       __glibcpp_function_requires(_LessThanComparableConcept<
03867         typename iterator_traits<_InputIter1>::value_type>)
03868 
03869       while (__first1 != __last1 && __first2 != __last2)
03870     if (*__first1 < *__first2) {
03871       *__result = *__first1;
03872       ++__first1;
03873       ++__result;
03874     }
03875     else if (*__first2 < *__first1) {
03876       *__result = *__first2;
03877       ++__first2;
03878       ++__result;
03879     }
03880     else {
03881       ++__first1;
03882       ++__first2;
03883     }
03884       return copy(__first2, __last2, copy(__first1, __last1, __result));
03885     }
03886 
03887   template<typename _InputIter1, typename _InputIter2, typename _OutputIter,
03888        typename _Compare>
03889     _OutputIter
03890     set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1,
03891                  _InputIter2 __first2, _InputIter2 __last2,
03892                  _OutputIter __result,
03893                  _Compare __comp)
03894     {
03895       // concept requirements
03896       __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
03897       __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
03898       __glibcpp_function_requires(_SameTypeConcept<
03899         typename iterator_traits<_InputIter1>::value_type,
03900         typename iterator_traits<_InputIter2>::value_type>)
03901       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
03902         typename iterator_traits<_InputIter1>::value_type>)
03903       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03904         typename iterator_traits<_InputIter1>::value_type,
03905         typename iterator_traits<_InputIter2>::value_type>)
03906 
03907       while (__first1 != __last1 && __first2 != __last2)
03908     if (__comp(*__first1, *__first2)) {
03909       *__result = *__first1;
03910       ++__first1;
03911       ++__result;
03912     }
03913     else if (__comp(*__first2, *__first1)) {
03914       *__result = *__first2;
03915       ++__first2;
03916       ++__result;
03917     }
03918     else {
03919       ++__first1;
03920       ++__first2;
03921     }
03922       return copy(__first2, __last2, copy(__first1, __last1, __result));
03923     }
03924 
03925   // min_element and max_element, with and without an explicitly supplied
03926   // comparison function.
03927 
03928   template<typename _ForwardIter>
03929     _ForwardIter
03930     max_element(_ForwardIter __first, _ForwardIter __last)
03931     {
03932       // concept requirements
03933       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
03934       __glibcpp_function_requires(_LessThanComparableConcept<
03935         typename iterator_traits<_ForwardIter>::value_type>)
03936 
03937       if (__first == __last) return __first;
03938       _ForwardIter __result = __first;
03939       while (++__first != __last)
03940     if (*__result < *__first)
03941       __result = __first;
03942       return __result;
03943     }
03944 
03945   template<typename _ForwardIter, typename _Compare>
03946     _ForwardIter
03947     max_element(_ForwardIter __first, _ForwardIter __last,
03948         _Compare __comp)
03949     {
03950       // concept requirements
03951       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
03952       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03953         typename iterator_traits<_ForwardIter>::value_type,
03954         typename iterator_traits<_ForwardIter>::value_type>)
03955 
03956       if (__first == __last) return __first;
03957       _ForwardIter __result = __first;
03958       while (++__first != __last)
03959     if (__comp(*__result, *__first)) __result = __first;
03960       return __result;
03961     }
03962 
03963   template<typename _ForwardIter>
03964     _ForwardIter
03965     min_element(_ForwardIter __first, _ForwardIter __last)
03966     {
03967       // concept requirements
03968       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
03969       __glibcpp_function_requires(_LessThanComparableConcept<
03970         typename iterator_traits<_ForwardIter>::value_type>)
03971 
03972       if (__first == __last) return __first;
03973       _ForwardIter __result = __first;
03974       while (++__first != __last)
03975     if (*__first < *__result)
03976       __result = __first;
03977       return __result;
03978     }
03979 
03980   template<typename _ForwardIter, typename _Compare>
03981     _ForwardIter
03982     min_element(_ForwardIter __first, _ForwardIter __last,
03983         _Compare __comp)
03984     {
03985       // concept requirements
03986       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
03987       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
03988         typename iterator_traits<_ForwardIter>::value_type,
03989         typename iterator_traits<_ForwardIter>::value_type>)
03990 
03991       if (__first == __last) return __first;
03992       _ForwardIter __result = __first;
03993       while (++__first != __last)
03994     if (__comp(*__first, *__result))
03995       __result = __first;
03996       return __result;
03997     }
03998 
03999   // next_permutation and prev_permutation, with and without an explicitly
04000   // supplied comparison function.
04001 
04002   template<typename _BidirectionalIter>
04003     bool
04004     next_permutation(_BidirectionalIter __first, _BidirectionalIter __last)
04005     {
04006       // concept requirements
04007       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIter>)
04008       __glibcpp_function_requires(_LessThanComparableConcept<
04009         typename iterator_traits<_BidirectionalIter>::value_type>)
04010 
04011       if (__first == __last)
04012     return false;
04013       _BidirectionalIter __i = __first;
04014       ++__i;
04015       if (__i == __last)
04016     return false;
04017       __i = __last;
04018       --__i;
04019 
04020       for(;;) {
04021     _BidirectionalIter __ii = __i;
04022     --__i;
04023     if (*__i < *__ii) {
04024       _BidirectionalIter __j = __last;
04025       while (!(*__i < *--__j))
04026         {}
04027       iter_swap(__i, __j);
04028       reverse(__ii, __last);
04029       return true;
04030     }
04031     if (__i == __first) {
04032       reverse(__first, __last);
04033       return false;
04034     }
04035       }
04036     }
04037 
04038   template<typename _BidirectionalIter, typename _Compare>
04039     bool
04040     next_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
04041              _Compare __comp)
04042     {
04043       // concept requirements
04044       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIter>)
04045       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
04046         typename iterator_traits<_BidirectionalIter>::value_type,
04047         typename iterator_traits<_BidirectionalIter>::value_type>)
04048 
04049       if (__first == __last)
04050     return false;
04051       _BidirectionalIter __i = __first;
04052       ++__i;
04053       if (__i == __last)
04054     return false;
04055       __i = __last;
04056       --__i;
04057 
04058       for(;;) {
04059     _BidirectionalIter __ii = __i;
04060     --__i;
04061     if (__comp(*__i, *__ii)) {
04062       _BidirectionalIter __j = __last;
04063       while (!__comp(*__i, *--__j))
04064         {}
04065       iter_swap(__i, __j);
04066       reverse(__ii, __last);
04067       return true;
04068     }
04069     if (__i == __first) {
04070       reverse(__first, __last);
04071       return false;
04072     }
04073       }
04074     }
04075 
04076   template<typename _BidirectionalIter>
04077     bool
04078     prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last)
04079     {
04080       // concept requirements
04081       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIter>)
04082       __glibcpp_function_requires(_LessThanComparableConcept<
04083         typename iterator_traits<_BidirectionalIter>::value_type>)
04084 
04085       if (__first == __last)
04086     return false;
04087       _BidirectionalIter __i = __first;
04088       ++__i;
04089       if (__i == __last)
04090     return false;
04091       __i = __last;
04092       --__i;
04093 
04094       for(;;) {
04095     _BidirectionalIter __ii = __i;
04096     --__i;
04097     if (*__ii < *__i) {
04098       _BidirectionalIter __j = __last;
04099       while (!(*--__j < *__i))
04100         {}
04101       iter_swap(__i, __j);
04102       reverse(__ii, __last);
04103       return true;
04104     }
04105     if (__i == __first) {
04106       reverse(__first, __last);
04107       return false;
04108     }
04109       }
04110     }
04111 
04112   template<typename _BidirectionalIter, typename _Compare>
04113     bool
04114     prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
04115              _Compare __comp)
04116     {
04117       // concept requirements
04118       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIter>)
04119       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
04120         typename iterator_traits<_BidirectionalIter>::value_type,
04121         typename iterator_traits<_BidirectionalIter>::value_type>)
04122 
04123       if (__first == __last)
04124     return false;
04125       _BidirectionalIter __i = __first;
04126       ++__i;
04127       if (__i == __last)
04128     return false;
04129       __i = __last;
04130       --__i;
04131 
04132       for(;;) {
04133     _BidirectionalIter __ii = __i;
04134     --__i;
04135     if (__comp(*__ii, *__i)) {
04136       _BidirectionalIter __j = __last;
04137       while (!__comp(*--__j, *__i))
04138         {}
04139       iter_swap(__i, __j);
04140       reverse(__ii, __last);
04141       return true;
04142     }
04143     if (__i == __first) {
04144       reverse(__first, __last);
04145       return false;
04146     }
04147       }
04148     }
04149 
04150   // find_first_of, with and without an explicitly supplied comparison function.
04151 
04152   template<typename _InputIter, typename _ForwardIter>
04153     _InputIter
04154     find_first_of(_InputIter __first1, _InputIter __last1,
04155           _ForwardIter __first2, _ForwardIter __last2)
04156     {
04157       // concept requirements
04158       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
04159       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
04160       __glibcpp_function_requires(_EqualOpConcept<
04161         typename iterator_traits<_InputIter>::value_type,
04162         typename iterator_traits<_ForwardIter>::value_type>)
04163 
04164       for ( ; __first1 != __last1; ++__first1)
04165     for (_ForwardIter __iter = __first2; __iter != __last2; ++__iter)
04166       if (*__first1 == *__iter)
04167         return __first1;
04168       return __last1;
04169     }
04170 
04171   template<typename _InputIter, typename _ForwardIter, typename _BinaryPredicate>
04172     _InputIter
04173     find_first_of(_InputIter __first1, _InputIter __last1,
04174           _ForwardIter __first2, _ForwardIter __last2,
04175           _BinaryPredicate __comp)
04176     {
04177       // concept requirements
04178       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
04179       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
04180       __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04181         typename iterator_traits<_InputIter>::value_type,
04182         typename iterator_traits<_ForwardIter>::value_type>)
04183 
04184       for ( ; __first1 != __last1; ++__first1)
04185     for (_ForwardIter __iter = __first2; __iter != __last2; ++__iter)
04186       if (__comp(*__first1, *__iter))
04187         return __first1;
04188       return __last1;
04189     }
04190 
04191 
04192   // find_end, with and without an explicitly supplied comparison function.
04193   // Search [first2, last2) as a subsequence in [first1, last1), and return
04194   // the *last* possible match.  Note that find_end for bidirectional iterators
04195   // is much faster than for forward iterators.
04196 
04197   // find_end for forward iterators.
04198   template<typename _ForwardIter1, typename _ForwardIter2>
04199     _ForwardIter1
04200     __find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
04201            _ForwardIter2 __first2, _ForwardIter2 __last2,
04202            forward_iterator_tag, forward_iterator_tag)
04203     {
04204       if (__first2 == __last2)
04205     return __last1;
04206       else {
04207     _ForwardIter1 __result = __last1;
04208     while (1) {
04209       _ForwardIter1 __new_result
04210         = search(__first1, __last1, __first2, __last2);
04211       if (__new_result == __last1)
04212         return __result;
04213       else {
04214         __result = __new_result;
04215         __first1 = __new_result;
04216         ++__first1;
04217       }
04218     }
04219       }
04220     }
04221 
04222   template<typename _ForwardIter1, typename _ForwardIter2,
04223        typename _BinaryPredicate>
04224     _ForwardIter1
04225     __find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
04226            _ForwardIter2 __first2, _ForwardIter2 __last2,
04227            forward_iterator_tag, forward_iterator_tag,
04228            _BinaryPredicate __comp)
04229     {
04230       if (__first2 == __last2)
04231     return __last1;
04232       else {
04233     _ForwardIter1 __result = __last1;
04234     while (1) {
04235       _ForwardIter1 __new_result
04236         = search(__first1, __last1, __first2, __last2, __comp);
04237       if (__new_result == __last1)
04238         return __result;
04239       else {
04240         __result = __new_result;
04241         __first1 = __new_result;
04242         ++__first1;
04243       }
04244     }
04245       }
04246     }
04247 
04248   // find_end for bidirectional iterators.  Requires partial specialization.
04249   template<typename _BidirectionalIter1, typename _BidirectionalIter2>
04250     _BidirectionalIter1
04251     __find_end(_BidirectionalIter1 __first1, _BidirectionalIter1 __last1,
04252            _BidirectionalIter2 __first2, _BidirectionalIter2 __last2,
04253            bidirectional_iterator_tag, bidirectional_iterator_tag)
04254     {
04255       // concept requirements
04256       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIter1>)
04257       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIter2>)
04258 
04259       typedef reverse_iterator<_BidirectionalIter1> _RevIter1;
04260       typedef reverse_iterator<_BidirectionalIter2> _RevIter2;
04261 
04262       _RevIter1 __rlast1(__first1);
04263       _RevIter2 __rlast2(__first2);
04264       _RevIter1 __rresult = search(_RevIter1(__last1), __rlast1,
04265                    _RevIter2(__last2), __rlast2);
04266 
04267       if (__rresult == __rlast1)
04268     return __last1;
04269       else {
04270     _BidirectionalIter1 __result = __rresult.base();
04271     advance(__result, -distance(__first2, __last2));
04272     return __result;
04273       }
04274     }
04275 
04276   template<typename _BidirectionalIter1, typename _BidirectionalIter2,
04277        typename _BinaryPredicate>
04278     _BidirectionalIter1
04279     __find_end(_BidirectionalIter1 __first1, _BidirectionalIter1 __last1,
04280            _BidirectionalIter2 __first2, _BidirectionalIter2 __last2,
04281            bidirectional_iterator_tag, bidirectional_iterator_tag,
04282            _BinaryPredicate __comp)
04283     {
04284       // concept requirements
04285       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIter1>)
04286       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIter2>)
04287 
04288       typedef reverse_iterator<_BidirectionalIter1> _RevIter1;
04289       typedef reverse_iterator<_BidirectionalIter2> _RevIter2;
04290 
04291       _RevIter1 __rlast1(__first1);
04292       _RevIter2 __rlast2(__first2);
04293       _RevIter1 __rresult = search(_RevIter1(__last1), __rlast1,
04294                    _RevIter2(__last2), __rlast2,
04295                    __comp);
04296 
04297       if (__rresult == __rlast1)
04298     return __last1;
04299       else {
04300     _BidirectionalIter1 __result = __rresult.base();
04301     advance(__result, -distance(__first2, __last2));
04302     return __result;
04303       }
04304     }
04305 
04306   // Dispatching functions for find_end.
04307 
04308   template<typename _ForwardIter1, typename _ForwardIter2>
04309     inline _ForwardIter1
04310     find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
04311          _ForwardIter2 __first2, _ForwardIter2 __last2)
04312     {
04313       // concept requirements
04314       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter1>)
04315       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter2>)
04316       __glibcpp_function_requires(_EqualOpConcept<
04317         typename iterator_traits<_ForwardIter1>::value_type,
04318         typename iterator_traits<_ForwardIter2>::value_type>)
04319 
04320       return __find_end(__first1, __last1, __first2, __last2,
04321             __iterator_category(__first1),
04322             __iterator_category(__first2));
04323     }
04324 
04325   template<typename _ForwardIter1, typename _ForwardIter2,
04326        typename _BinaryPredicate>
04327     inline _ForwardIter1
04328     find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
04329          _ForwardIter2 __first2, _ForwardIter2 __last2,
04330          _BinaryPredicate __comp)
04331     {
04332       // concept requirements
04333       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter1>)
04334       __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter2>)
04335       __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04336         typename iterator_traits<_ForwardIter1>::value_type,
04337         typename iterator_traits<_ForwardIter2>::value_type>)
04338 
04339       return __find_end(__first1, __last1, __first2, __last2,
04340             __iterator_category(__first1),
04341             __iterator_category(__first2),
04342             __comp);
04343     }
04344 
04345 } // namespace std
04346 
04347 #endif /* __GLIBCPP_INTERNAL_ALGO_H */
04348 

Generated on Sat Mar 19 18:32:21 2005 for libstdc++-v3 Source by  doxygen 1.4.1