stl_algo.h

Go to the documentation of this file.
00001 // Algorithm implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /*
00032  *
00033  * Copyright (c) 1994
00034  * Hewlett-Packard Company
00035  *
00036  * Permission to use, copy, modify, distribute and sell this software
00037  * and its documentation for any purpose is hereby granted without fee,
00038  * provided that the above copyright notice appear in all copies and
00039  * that both that copyright notice and this permission notice appear
00040  * in supporting documentation.  Hewlett-Packard Company makes no
00041  * representations about the suitability of this software for any
00042  * purpose.  It is provided "as is" without express or implied warranty.
00043  *
00044  *
00045  * Copyright (c) 1996
00046  * Silicon Graphics Computer Systems, Inc.
00047  *
00048  * Permission to use, copy, modify, distribute and sell this software
00049  * and its documentation for any purpose is hereby granted without fee,
00050  * provided that the above copyright notice appear in all copies and
00051  * that both that copyright notice and this permission notice appear
00052  * in supporting documentation.  Silicon Graphics makes no
00053  * representations about the suitability of this software for any
00054  * purpose.  It is provided "as is" without express or implied warranty.
00055  */
00056 
00057 /** @file stl_algo.h
00058  *  This is an internal header file, included by other library headers.
00059  *  You should not attempt to use it directly.
00060  */
00061 
00062 #ifndef _STL_ALGO_H
00063 #define _STL_ALGO_H 1
00064 
00065 #include <cstdlib>             // for rand
00066 #include <bits/algorithmfwd.h>
00067 #include <bits/stl_heap.h>
00068 #include <bits/stl_tempbuf.h>  // for _Temporary_buffer
00069 #include <debug/debug.h>
00070 
00071 // See concept_check.h for the __glibcxx_*_requires macros.
00072 
00073 _GLIBCXX_BEGIN_NAMESPACE(std)
00074 
00075   /**
00076    *  @brief Find the median of three values.
00077    *  @param  a  A value.
00078    *  @param  b  A value.
00079    *  @param  c  A value.
00080    *  @return One of @p a, @p b or @p c.
00081    *
00082    *  If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
00083    *  then the value returned will be @c m.
00084    *  This is an SGI extension.
00085    *  @ingroup SGIextensions
00086   */
00087   template<typename _Tp>
00088     inline const _Tp&
00089     __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
00090     {
00091       // concept requirements
00092       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
00093       if (__a < __b)
00094     if (__b < __c)
00095       return __b;
00096     else if (__a < __c)
00097       return __c;
00098     else
00099       return __a;
00100       else if (__a < __c)
00101     return __a;
00102       else if (__b < __c)
00103     return __c;
00104       else
00105     return __b;
00106     }
00107 
00108   /**
00109    *  @brief Find the median of three values using a predicate for comparison.
00110    *  @param  a     A value.
00111    *  @param  b     A value.
00112    *  @param  c     A value.
00113    *  @param  comp  A binary predicate.
00114    *  @return One of @p a, @p b or @p c.
00115    *
00116    *  If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
00117    *  and @p comp(m,n) are both true then the value returned will be @c m.
00118    *  This is an SGI extension.
00119    *  @ingroup SGIextensions
00120   */
00121   template<typename _Tp, typename _Compare>
00122     inline const _Tp&
00123     __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
00124     {
00125       // concept requirements
00126       __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
00127                                          _Tp, _Tp>)
00128       if (__comp(__a, __b))
00129     if (__comp(__b, __c))
00130       return __b;
00131     else if (__comp(__a, __c))
00132       return __c;
00133     else
00134       return __a;
00135       else if (__comp(__a, __c))
00136     return __a;
00137       else if (__comp(__b, __c))
00138     return __c;
00139       else
00140     return __b;
00141     }
00142 
00143   // for_each
00144 
00145   /// This is an overload used by find() for the Input Iterator case.
00146   template<typename _InputIterator, typename _Tp>
00147     inline _InputIterator
00148     __find(_InputIterator __first, _InputIterator __last,
00149        const _Tp& __val, input_iterator_tag)
00150     {
00151       while (__first != __last && !(*__first == __val))
00152     ++__first;
00153       return __first;
00154     }
00155 
00156   /// This is an overload used by find_if() for the Input Iterator case.
00157   template<typename _InputIterator, typename _Predicate>
00158     inline _InputIterator
00159     __find_if(_InputIterator __first, _InputIterator __last,
00160           _Predicate __pred, input_iterator_tag)
00161     {
00162       while (__first != __last && !bool(__pred(*__first)))
00163     ++__first;
00164       return __first;
00165     }
00166 
00167   /// This is an overload used by find() for the RAI case.
00168   template<typename _RandomAccessIterator, typename _Tp>
00169     _RandomAccessIterator
00170     __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
00171        const _Tp& __val, random_access_iterator_tag)
00172     {
00173       typename iterator_traits<_RandomAccessIterator>::difference_type
00174     __trip_count = (__last - __first) >> 2;
00175 
00176       for (; __trip_count > 0; --__trip_count)
00177     {
00178       if (*__first == __val)
00179         return __first;
00180       ++__first;
00181 
00182       if (*__first == __val)
00183         return __first;
00184       ++__first;
00185 
00186       if (*__first == __val)
00187         return __first;
00188       ++__first;
00189 
00190       if (*__first == __val)
00191         return __first;
00192       ++__first;
00193     }
00194 
00195       switch (__last - __first)
00196     {
00197     case 3:
00198       if (*__first == __val)
00199         return __first;
00200       ++__first;
00201     case 2:
00202       if (*__first == __val)
00203         return __first;
00204       ++__first;
00205     case 1:
00206       if (*__first == __val)
00207         return __first;
00208       ++__first;
00209     case 0:
00210     default:
00211       return __last;
00212     }
00213     }
00214 
00215   /// This is an overload used by find_if() for the RAI case.
00216   template<typename _RandomAccessIterator, typename _Predicate>
00217     _RandomAccessIterator
00218     __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
00219           _Predicate __pred, random_access_iterator_tag)
00220     {
00221       typename iterator_traits<_RandomAccessIterator>::difference_type
00222     __trip_count = (__last - __first) >> 2;
00223 
00224       for (; __trip_count > 0; --__trip_count)
00225     {
00226       if (__pred(*__first))
00227         return __first;
00228       ++__first;
00229 
00230       if (__pred(*__first))
00231         return __first;
00232       ++__first;
00233 
00234       if (__pred(*__first))
00235         return __first;
00236       ++__first;
00237 
00238       if (__pred(*__first))
00239         return __first;
00240       ++__first;
00241     }
00242 
00243       switch (__last - __first)
00244     {
00245     case 3:
00246       if (__pred(*__first))
00247         return __first;
00248       ++__first;
00249     case 2:
00250       if (__pred(*__first))
00251         return __first;
00252       ++__first;
00253     case 1:
00254       if (__pred(*__first))
00255         return __first;
00256       ++__first;
00257     case 0:
00258     default:
00259       return __last;
00260     }
00261     }
00262 
00263   // set_difference
00264   // set_intersection
00265   // set_symmetric_difference
00266   // set_union
00267   // for_each
00268   // find
00269   // find_if
00270   // find_first_of
00271   // adjacent_find
00272   // count
00273   // count_if
00274   // search
00275 
00276   /**
00277    *  This is an uglified
00278    *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
00279    *  overloaded for forward iterators.
00280   */
00281   template<typename _ForwardIterator, typename _Integer, typename _Tp>
00282     _ForwardIterator
00283     __search_n(_ForwardIterator __first, _ForwardIterator __last,
00284            _Integer __count, const _Tp& __val,
00285            std::forward_iterator_tag)
00286     {
00287       __first = _GLIBCXX_STD_P::find(__first, __last, __val);
00288       while (__first != __last)
00289     {
00290       typename iterator_traits<_ForwardIterator>::difference_type
00291         __n = __count;
00292       _ForwardIterator __i = __first;
00293       ++__i;
00294       while (__i != __last && __n != 1 && *__i == __val)
00295         {
00296           ++__i;
00297           --__n;
00298         }
00299       if (__n == 1)
00300         return __first;
00301       if (__i == __last)
00302         return __last;
00303       __first = _GLIBCXX_STD_P::find(++__i, __last, __val);
00304     }
00305       return __last;
00306     }
00307 
00308   /**
00309    *  This is an uglified
00310    *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
00311    *  overloaded for random access iterators.
00312   */
00313   template<typename _RandomAccessIter, typename _Integer, typename _Tp>
00314     _RandomAccessIter
00315     __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
00316            _Integer __count, const _Tp& __val, 
00317            std::random_access_iterator_tag)
00318     {
00319       
00320       typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
00321     _DistanceType;
00322 
00323       _DistanceType __tailSize = __last - __first;
00324       const _DistanceType __pattSize = __count;
00325 
00326       if (__tailSize < __pattSize)
00327         return __last;
00328 
00329       const _DistanceType __skipOffset = __pattSize - 1;
00330       _RandomAccessIter __lookAhead = __first + __skipOffset;
00331       __tailSize -= __pattSize;
00332 
00333       while (1) // the main loop...
00334     {
00335       // __lookAhead here is always pointing to the last element of next 
00336       // possible match.
00337       while (!(*__lookAhead == __val)) // the skip loop...
00338         {
00339           if (__tailSize < __pattSize)
00340         return __last;  // Failure
00341           __lookAhead += __pattSize;
00342           __tailSize -= __pattSize;
00343         }
00344       _DistanceType __remainder = __skipOffset;
00345       for (_RandomAccessIter __backTrack = __lookAhead - 1; 
00346            *__backTrack == __val; --__backTrack)
00347         {
00348           if (--__remainder == 0)
00349         return (__lookAhead - __skipOffset); // Success
00350         }
00351       if (__remainder > __tailSize)
00352         return __last; // Failure
00353       __lookAhead += __remainder;
00354       __tailSize -= __remainder;
00355     }
00356     }
00357 
00358   // search_n
00359 
00360   /**
00361    *  This is an uglified
00362    *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
00363    *           _BinaryPredicate)
00364    *  overloaded for forward iterators.
00365   */
00366   template<typename _ForwardIterator, typename _Integer, typename _Tp,
00367            typename _BinaryPredicate>
00368     _ForwardIterator
00369     __search_n(_ForwardIterator __first, _ForwardIterator __last,
00370            _Integer __count, const _Tp& __val,
00371            _BinaryPredicate __binary_pred, std::forward_iterator_tag)
00372     {
00373       while (__first != __last && !bool(__binary_pred(*__first, __val)))
00374         ++__first;
00375 
00376       while (__first != __last)
00377     {
00378       typename iterator_traits<_ForwardIterator>::difference_type
00379         __n = __count;
00380       _ForwardIterator __i = __first;
00381       ++__i;
00382       while (__i != __last && __n != 1 && bool(__binary_pred(*__i, __val)))
00383         {
00384           ++__i;
00385           --__n;
00386         }
00387       if (__n == 1)
00388         return __first;
00389       if (__i == __last)
00390         return __last;
00391       __first = ++__i;
00392       while (__first != __last
00393          && !bool(__binary_pred(*__first, __val)))
00394         ++__first;
00395     }
00396       return __last;
00397     }
00398 
00399   /**
00400    *  This is an uglified
00401    *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
00402    *           _BinaryPredicate)
00403    *  overloaded for random access iterators.
00404   */
00405   template<typename _RandomAccessIter, typename _Integer, typename _Tp,
00406        typename _BinaryPredicate>
00407     _RandomAccessIter
00408     __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
00409            _Integer __count, const _Tp& __val,
00410            _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
00411     {
00412       
00413       typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
00414     _DistanceType;
00415 
00416       _DistanceType __tailSize = __last - __first;
00417       const _DistanceType __pattSize = __count;
00418 
00419       if (__tailSize < __pattSize)
00420         return __last;
00421 
00422       const _DistanceType __skipOffset = __pattSize - 1;
00423       _RandomAccessIter __lookAhead = __first + __skipOffset;
00424       __tailSize -= __pattSize;
00425 
00426       while (1) // the main loop...
00427     {
00428       // __lookAhead here is always pointing to the last element of next 
00429       // possible match.
00430       while (!bool(__binary_pred(*__lookAhead, __val))) // the skip loop...
00431         {
00432           if (__tailSize < __pattSize)
00433         return __last;  // Failure
00434           __lookAhead += __pattSize;
00435           __tailSize -= __pattSize;
00436         }
00437       _DistanceType __remainder = __skipOffset;
00438       for (_RandomAccessIter __backTrack = __lookAhead - 1; 
00439            __binary_pred(*__backTrack, __val); --__backTrack)
00440         {
00441           if (--__remainder == 0)
00442         return (__lookAhead - __skipOffset); // Success
00443         }
00444       if (__remainder > __tailSize)
00445         return __last; // Failure
00446       __lookAhead += __remainder;
00447       __tailSize -= __remainder;
00448     }
00449     }
00450 
00451   // find_end for forward iterators.
00452   template<typename _ForwardIterator1, typename _ForwardIterator2>
00453     _ForwardIterator1
00454     __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
00455            _ForwardIterator2 __first2, _ForwardIterator2 __last2,
00456            forward_iterator_tag, forward_iterator_tag)
00457     {
00458       if (__first2 == __last2)
00459     return __last1;
00460       else
00461     {
00462       _ForwardIterator1 __result = __last1;
00463       while (1)
00464         {
00465           _ForwardIterator1 __new_result
00466         = _GLIBCXX_STD_P::search(__first1, __last1, __first2, __last2);
00467           if (__new_result == __last1)
00468         return __result;
00469           else
00470         {
00471           __result = __new_result;
00472           __first1 = __new_result;
00473           ++__first1;
00474         }
00475         }
00476     }
00477     }
00478 
00479   template<typename _ForwardIterator1, typename _ForwardIterator2,
00480        typename _BinaryPredicate>
00481     _ForwardIterator1
00482     __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
00483            _ForwardIterator2 __first2, _ForwardIterator2 __last2,
00484            forward_iterator_tag, forward_iterator_tag,
00485            _BinaryPredicate __comp)
00486     {
00487       if (__first2 == __last2)
00488     return __last1;
00489       else
00490     {
00491       _ForwardIterator1 __result = __last1;
00492       while (1)
00493         {
00494           _ForwardIterator1 __new_result
00495         = _GLIBCXX_STD_P::search(__first1, __last1, __first2,
00496                      __last2, __comp);
00497           if (__new_result == __last1)
00498         return __result;
00499           else
00500         {
00501           __result = __new_result;
00502           __first1 = __new_result;
00503           ++__first1;
00504         }
00505         }
00506     }
00507     }
00508 
00509   // find_end for bidirectional iterators (much faster).
00510   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
00511     _BidirectionalIterator1
00512     __find_end(_BidirectionalIterator1 __first1,
00513            _BidirectionalIterator1 __last1,
00514            _BidirectionalIterator2 __first2,
00515            _BidirectionalIterator2 __last2,
00516            bidirectional_iterator_tag, bidirectional_iterator_tag)
00517     {
00518       // concept requirements
00519       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00520                   _BidirectionalIterator1>)
00521       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00522                   _BidirectionalIterator2>)
00523 
00524       typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
00525       typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
00526 
00527       _RevIterator1 __rlast1(__first1);
00528       _RevIterator2 __rlast2(__first2);
00529       _RevIterator1 __rresult = _GLIBCXX_STD_P::search(_RevIterator1(__last1),
00530                                __rlast1,
00531                                _RevIterator2(__last2),
00532                                __rlast2);
00533 
00534       if (__rresult == __rlast1)
00535     return __last1;
00536       else
00537     {
00538       _BidirectionalIterator1 __result = __rresult.base();
00539       std::advance(__result, -std::distance(__first2, __last2));
00540       return __result;
00541     }
00542     }
00543 
00544   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
00545        typename _BinaryPredicate>
00546     _BidirectionalIterator1
00547     __find_end(_BidirectionalIterator1 __first1,
00548            _BidirectionalIterator1 __last1,
00549            _BidirectionalIterator2 __first2,
00550            _BidirectionalIterator2 __last2,
00551            bidirectional_iterator_tag, bidirectional_iterator_tag,
00552            _BinaryPredicate __comp)
00553     {
00554       // concept requirements
00555       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00556                   _BidirectionalIterator1>)
00557       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00558                   _BidirectionalIterator2>)
00559 
00560       typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
00561       typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
00562 
00563       _RevIterator1 __rlast1(__first1);
00564       _RevIterator2 __rlast2(__first2);
00565       _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
00566                         _RevIterator2(__last2), __rlast2,
00567                         __comp);
00568 
00569       if (__rresult == __rlast1)
00570     return __last1;
00571       else
00572     {
00573       _BidirectionalIterator1 __result = __rresult.base();
00574       std::advance(__result, -std::distance(__first2, __last2));
00575       return __result;
00576     }
00577     }
00578 
00579   /**
00580    *  @brief  Find last matching subsequence in a sequence.
00581    *  @param  first1  Start of range to search.
00582    *  @param  last1   End of range to search.
00583    *  @param  first2  Start of sequence to match.
00584    *  @param  last2   End of sequence to match.
00585    *  @return   The last iterator @c i in the range
00586    *  @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
00587    *  for each @c N in the range @p [0,last2-first2), or @p last1 if no
00588    *  such iterator exists.
00589    *
00590    *  Searches the range @p [first1,last1) for a sub-sequence that compares
00591    *  equal value-by-value with the sequence given by @p [first2,last2) and
00592    *  returns an iterator to the first element of the sub-sequence, or
00593    *  @p last1 if the sub-sequence is not found.  The sub-sequence will be the
00594    *  last such subsequence contained in [first,last1).
00595    *
00596    *  Because the sub-sequence must lie completely within the range
00597    *  @p [first1,last1) it must start at a position less than
00598    *  @p last1-(last2-first2) where @p last2-first2 is the length of the
00599    *  sub-sequence.
00600    *  This means that the returned iterator @c i will be in the range
00601    *  @p [first1,last1-(last2-first2))
00602   */
00603   template<typename _ForwardIterator1, typename _ForwardIterator2>
00604     inline _ForwardIterator1
00605     find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
00606          _ForwardIterator2 __first2, _ForwardIterator2 __last2)
00607     {
00608       // concept requirements
00609       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
00610       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
00611       __glibcxx_function_requires(_EqualOpConcept<
00612         typename iterator_traits<_ForwardIterator1>::value_type,
00613         typename iterator_traits<_ForwardIterator2>::value_type>)
00614       __glibcxx_requires_valid_range(__first1, __last1);
00615       __glibcxx_requires_valid_range(__first2, __last2);
00616 
00617       return std::__find_end(__first1, __last1, __first2, __last2,
00618                  std::__iterator_category(__first1),
00619                  std::__iterator_category(__first2));
00620     }
00621 
00622   /**
00623    *  @brief  Find last matching subsequence in a sequence using a predicate.
00624    *  @param  first1  Start of range to search.
00625    *  @param  last1   End of range to search.
00626    *  @param  first2  Start of sequence to match.
00627    *  @param  last2   End of sequence to match.
00628    *  @param  comp    The predicate to use.
00629    *  @return   The last iterator @c i in the range
00630    *  @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
00631    *  (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
00632    *  @p last1 if no such iterator exists.
00633    *
00634    *  Searches the range @p [first1,last1) for a sub-sequence that compares
00635    *  equal value-by-value with the sequence given by @p [first2,last2) using
00636    *  comp as a predicate and returns an iterator to the first element of the
00637    *  sub-sequence, or @p last1 if the sub-sequence is not found.  The
00638    *  sub-sequence will be the last such subsequence contained in
00639    *  [first,last1).
00640    *
00641    *  Because the sub-sequence must lie completely within the range
00642    *  @p [first1,last1) it must start at a position less than
00643    *  @p last1-(last2-first2) where @p last2-first2 is the length of the
00644    *  sub-sequence.
00645    *  This means that the returned iterator @c i will be in the range
00646    *  @p [first1,last1-(last2-first2))
00647   */
00648   template<typename _ForwardIterator1, typename _ForwardIterator2,
00649        typename _BinaryPredicate>
00650     inline _ForwardIterator1
00651     find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
00652          _ForwardIterator2 __first2, _ForwardIterator2 __last2,
00653          _BinaryPredicate __comp)
00654     {
00655       // concept requirements
00656       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
00657       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
00658       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
00659         typename iterator_traits<_ForwardIterator1>::value_type,
00660         typename iterator_traits<_ForwardIterator2>::value_type>)
00661       __glibcxx_requires_valid_range(__first1, __last1);
00662       __glibcxx_requires_valid_range(__first2, __last2);
00663 
00664       return std::__find_end(__first1, __last1, __first2, __last2,
00665                  std::__iterator_category(__first1),
00666                  std::__iterator_category(__first2),
00667                  __comp);
00668     }
00669 
00670 
00671   /**
00672    *  @brief Copy a sequence, removing elements of a given value.
00673    *  @param  first   An input iterator.
00674    *  @param  last    An input iterator.
00675    *  @param  result  An output iterator.
00676    *  @param  value   The value to be removed.
00677    *  @return   An iterator designating the end of the resulting sequence.
00678    *
00679    *  Copies each element in the range @p [first,last) not equal to @p value
00680    *  to the range beginning at @p result.
00681    *  remove_copy() is stable, so the relative order of elements that are
00682    *  copied is unchanged.
00683   */
00684   template<typename _InputIterator, typename _OutputIterator, typename _Tp>
00685     _OutputIterator
00686     remove_copy(_InputIterator __first, _InputIterator __last,
00687         _OutputIterator __result, const _Tp& __value)
00688     {
00689       // concept requirements
00690       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00691       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00692         typename iterator_traits<_InputIterator>::value_type>)
00693       __glibcxx_function_requires(_EqualOpConcept<
00694         typename iterator_traits<_InputIterator>::value_type, _Tp>)
00695       __glibcxx_requires_valid_range(__first, __last);
00696 
00697       for (; __first != __last; ++__first)
00698     if (!(*__first == __value))
00699       {
00700         *__result = *__first;
00701         ++__result;
00702       }
00703       return __result;
00704     }
00705 
00706   /**
00707    *  @brief Copy a sequence, removing elements for which a predicate is true.
00708    *  @param  first   An input iterator.
00709    *  @param  last    An input iterator.
00710    *  @param  result  An output iterator.
00711    *  @param  pred    A predicate.
00712    *  @return   An iterator designating the end of the resulting sequence.
00713    *
00714    *  Copies each element in the range @p [first,last) for which
00715    *  @p pred returns true to the range beginning at @p result.
00716    *
00717    *  remove_copy_if() is stable, so the relative order of elements that are
00718    *  copied is unchanged.
00719   */
00720   template<typename _InputIterator, typename _OutputIterator,
00721        typename _Predicate>
00722     _OutputIterator
00723     remove_copy_if(_InputIterator __first, _InputIterator __last,
00724            _OutputIterator __result, _Predicate __pred)
00725     {
00726       // concept requirements
00727       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00728       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00729         typename iterator_traits<_InputIterator>::value_type>)
00730       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
00731         typename iterator_traits<_InputIterator>::value_type>)
00732       __glibcxx_requires_valid_range(__first, __last);
00733 
00734       for (; __first != __last; ++__first)
00735     if (!bool(__pred(*__first)))
00736       {
00737         *__result = *__first;
00738         ++__result;
00739       }
00740       return __result;
00741     }
00742 
00743   /**
00744    *  @brief Remove elements from a sequence.
00745    *  @param  first  An input iterator.
00746    *  @param  last   An input iterator.
00747    *  @param  value  The value to be removed.
00748    *  @return   An iterator designating the end of the resulting sequence.
00749    *
00750    *  All elements equal to @p value are removed from the range
00751    *  @p [first,last).
00752    *
00753    *  remove() is stable, so the relative order of elements that are
00754    *  not removed is unchanged.
00755    *
00756    *  Elements between the end of the resulting sequence and @p last
00757    *  are still present, but their value is unspecified.
00758   */
00759   template<typename _ForwardIterator, typename _Tp>
00760     _ForwardIterator
00761     remove(_ForwardIterator __first, _ForwardIterator __last,
00762        const _Tp& __value)
00763     {
00764       // concept requirements
00765       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
00766                   _ForwardIterator>)
00767       __glibcxx_function_requires(_EqualOpConcept<
00768         typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
00769       __glibcxx_requires_valid_range(__first, __last);
00770 
00771       __first = _GLIBCXX_STD_P::find(__first, __last, __value);
00772       if(__first == __last)
00773         return __first;
00774       _ForwardIterator __result = __first;
00775       ++__first;
00776       for(; __first != __last; ++__first)
00777         if(!(*__first == __value))
00778           {
00779             *__result = _GLIBCXX_MOVE(*__first);
00780             ++__result;
00781           }
00782       return __result;
00783     }
00784 
00785   /**
00786    *  @brief Remove elements from a sequence using a predicate.
00787    *  @param  first  A forward iterator.
00788    *  @param  last   A forward iterator.
00789    *  @param  pred   A predicate.
00790    *  @return   An iterator designating the end of the resulting sequence.
00791    *
00792    *  All elements for which @p pred returns true are removed from the range
00793    *  @p [first,last).
00794    *
00795    *  remove_if() is stable, so the relative order of elements that are
00796    *  not removed is unchanged.
00797    *
00798    *  Elements between the end of the resulting sequence and @p last
00799    *  are still present, but their value is unspecified.
00800   */
00801   template<typename _ForwardIterator, typename _Predicate>
00802     _ForwardIterator
00803     remove_if(_ForwardIterator __first, _ForwardIterator __last,
00804           _Predicate __pred)
00805     {
00806       // concept requirements
00807       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
00808                   _ForwardIterator>)
00809       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
00810         typename iterator_traits<_ForwardIterator>::value_type>)
00811       __glibcxx_requires_valid_range(__first, __last);
00812 
00813       __first = _GLIBCXX_STD_P::find_if(__first, __last, __pred);
00814       if(__first == __last)
00815         return __first;
00816       _ForwardIterator __result = __first;
00817       ++__first;
00818       for(; __first != __last; ++__first)
00819         if(!__pred(*__first))
00820           {
00821             *__result = _GLIBCXX_MOVE(*__first);
00822             ++__result;
00823           }
00824       return __result;
00825     }
00826 
00827   /**
00828    *  @brief Remove consecutive duplicate values from a sequence.
00829    *  @param  first  A forward iterator.
00830    *  @param  last   A forward iterator.
00831    *  @return  An iterator designating the end of the resulting sequence.
00832    *
00833    *  Removes all but the first element from each group of consecutive
00834    *  values that compare equal.
00835    *  unique() is stable, so the relative order of elements that are
00836    *  not removed is unchanged.
00837    *  Elements between the end of the resulting sequence and @p last
00838    *  are still present, but their value is unspecified.
00839   */
00840   template<typename _ForwardIterator>
00841     _ForwardIterator
00842     unique(_ForwardIterator __first, _ForwardIterator __last)
00843     {
00844       // concept requirements
00845       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
00846                   _ForwardIterator>)
00847       __glibcxx_function_requires(_EqualityComparableConcept<
00848              typename iterator_traits<_ForwardIterator>::value_type>)
00849       __glibcxx_requires_valid_range(__first, __last);
00850 
00851       // Skip the beginning, if already unique.
00852       __first = _GLIBCXX_STD_P::adjacent_find(__first, __last);
00853       if (__first == __last)
00854     return __last;
00855 
00856       // Do the real copy work.
00857       _ForwardIterator __dest = __first;
00858       ++__first;
00859       while (++__first != __last)
00860     if (!(*__dest == *__first))
00861       *++__dest = _GLIBCXX_MOVE(*__first);
00862       return ++__dest;
00863     }
00864 
00865   /**
00866    *  @brief Remove consecutive values from a sequence using a predicate.
00867    *  @param  first        A forward iterator.
00868    *  @param  last         A forward iterator.
00869    *  @param  binary_pred  A binary predicate.
00870    *  @return  An iterator designating the end of the resulting sequence.
00871    *
00872    *  Removes all but the first element from each group of consecutive
00873    *  values for which @p binary_pred returns true.
00874    *  unique() is stable, so the relative order of elements that are
00875    *  not removed is unchanged.
00876    *  Elements between the end of the resulting sequence and @p last
00877    *  are still present, but their value is unspecified.
00878   */
00879   template<typename _ForwardIterator, typename _BinaryPredicate>
00880     _ForwardIterator
00881     unique(_ForwardIterator __first, _ForwardIterator __last,
00882            _BinaryPredicate __binary_pred)
00883     {
00884       // concept requirements
00885       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
00886                   _ForwardIterator>)
00887       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
00888         typename iterator_traits<_ForwardIterator>::value_type,
00889         typename iterator_traits<_ForwardIterator>::value_type>)
00890       __glibcxx_requires_valid_range(__first, __last);
00891 
00892       // Skip the beginning, if already unique.
00893       __first = _GLIBCXX_STD_P::adjacent_find(__first, __last, __binary_pred);
00894       if (__first == __last)
00895     return __last;
00896 
00897       // Do the real copy work.
00898       _ForwardIterator __dest = __first;
00899       ++__first;
00900       while (++__first != __last)
00901     if (!bool(__binary_pred(*__dest, *__first)))
00902       *++__dest = _GLIBCXX_MOVE(*__first);
00903       return ++__dest;
00904     }
00905 
00906   /**
00907    *  This is an uglified unique_copy(_InputIterator, _InputIterator,
00908    *                                  _OutputIterator)
00909    *  overloaded for forward iterators and output iterator as result.
00910   */
00911   template<typename _ForwardIterator, typename _OutputIterator>
00912     _OutputIterator
00913     __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
00914           _OutputIterator __result,
00915           forward_iterator_tag, output_iterator_tag)
00916     {
00917       // concept requirements -- taken care of in dispatching function
00918       _ForwardIterator __next = __first;
00919       *__result = *__first;
00920       while (++__next != __last)
00921     if (!(*__first == *__next))
00922       {
00923         __first = __next;
00924         *++__result = *__first;
00925       }
00926       return ++__result;
00927     }
00928 
00929   /**
00930    *  This is an uglified unique_copy(_InputIterator, _InputIterator,
00931    *                                  _OutputIterator)
00932    *  overloaded for input iterators and output iterator as result.
00933   */
00934   template<typename _InputIterator, typename _OutputIterator>
00935     _OutputIterator
00936     __unique_copy(_InputIterator __first, _InputIterator __last,
00937           _OutputIterator __result,
00938           input_iterator_tag, output_iterator_tag)
00939     {
00940       // concept requirements -- taken care of in dispatching function
00941       typename iterator_traits<_InputIterator>::value_type __value = *__first;
00942       *__result = __value;
00943       while (++__first != __last)
00944     if (!(__value == *__first))
00945       {
00946         __value = *__first;
00947         *++__result = __value;
00948       }
00949       return ++__result;
00950     }
00951 
00952   /**
00953    *  This is an uglified unique_copy(_InputIterator, _InputIterator,
00954    *                                  _OutputIterator)
00955    *  overloaded for input iterators and forward iterator as result.
00956   */
00957   template<typename _InputIterator, typename _ForwardIterator>
00958     _ForwardIterator
00959     __unique_copy(_InputIterator __first, _InputIterator __last,
00960           _ForwardIterator __result,
00961           input_iterator_tag, forward_iterator_tag)
00962     {
00963       // concept requirements -- taken care of in dispatching function
00964       *__result = *__first;
00965       while (++__first != __last)
00966     if (!(*__result == *__first))
00967       *++__result = *__first;
00968       return ++__result;
00969     }
00970 
00971   /**
00972    *  This is an uglified
00973    *  unique_copy(_InputIterator, _InputIterator, _OutputIterator,
00974    *              _BinaryPredicate)
00975    *  overloaded for forward iterators and output iterator as result.
00976   */
00977   template<typename _ForwardIterator, typename _OutputIterator,
00978        typename _BinaryPredicate>
00979     _OutputIterator
00980     __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
00981           _OutputIterator __result, _BinaryPredicate __binary_pred,
00982           forward_iterator_tag, output_iterator_tag)
00983     {
00984       // concept requirements -- iterators already checked
00985       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
00986       typename iterator_traits<_ForwardIterator>::value_type,
00987       typename iterator_traits<_ForwardIterator>::value_type>)
00988 
00989       _ForwardIterator __next = __first;
00990       *__result = *__first;
00991       while (++__next != __last)
00992     if (!bool(__binary_pred(*__first, *__next)))
00993       {
00994         __first = __next;
00995         *++__result = *__first;
00996       }
00997       return ++__result;
00998     }
00999 
01000   /**
01001    *  This is an uglified
01002    *  unique_copy(_InputIterator, _InputIterator, _OutputIterator,
01003    *              _BinaryPredicate)
01004    *  overloaded for input iterators and output iterator as result.
01005   */
01006   template<typename _InputIterator, typename _OutputIterator,
01007        typename _BinaryPredicate>
01008     _OutputIterator
01009     __unique_copy(_InputIterator __first, _InputIterator __last,
01010           _OutputIterator __result, _BinaryPredicate __binary_pred,
01011           input_iterator_tag, output_iterator_tag)
01012     {
01013       // concept requirements -- iterators already checked
01014       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
01015       typename iterator_traits<_InputIterator>::value_type,
01016       typename iterator_traits<_InputIterator>::value_type>)
01017 
01018       typename iterator_traits<_InputIterator>::value_type __value = *__first;
01019       *__result = __value;
01020       while (++__first != __last)
01021     if (!bool(__binary_pred(__value, *__first)))
01022       {
01023         __value = *__first;
01024         *++__result = __value;
01025       }
01026       return ++__result;
01027     }
01028 
01029   /**
01030    *  This is an uglified
01031    *  unique_copy(_InputIterator, _InputIterator, _OutputIterator,
01032    *              _BinaryPredicate)
01033    *  overloaded for input iterators and forward iterator as result.
01034   */
01035   template<typename _InputIterator, typename _ForwardIterator,
01036        typename _BinaryPredicate>
01037     _ForwardIterator
01038     __unique_copy(_InputIterator __first, _InputIterator __last,
01039           _ForwardIterator __result, _BinaryPredicate __binary_pred,
01040           input_iterator_tag, forward_iterator_tag)
01041     {
01042       // concept requirements -- iterators already checked
01043       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
01044       typename iterator_traits<_ForwardIterator>::value_type,
01045       typename iterator_traits<_InputIterator>::value_type>)
01046 
01047       *__result = *__first;
01048       while (++__first != __last)
01049     if (!bool(__binary_pred(*__result, *__first)))
01050       *++__result = *__first;
01051       return ++__result;
01052     }
01053 
01054   /**
01055    *  This is an uglified reverse(_BidirectionalIterator,
01056    *                              _BidirectionalIterator)
01057    *  overloaded for bidirectional iterators.
01058   */
01059   template<typename _BidirectionalIterator>
01060     void
01061     __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
01062           bidirectional_iterator_tag)
01063     {
01064       while (true)
01065     if (__first == __last || __first == --__last)
01066       return;
01067     else
01068       {
01069         std::iter_swap(__first, __last);
01070         ++__first;
01071       }
01072     }
01073 
01074   /**
01075    *  This is an uglified reverse(_BidirectionalIterator,
01076    *                              _BidirectionalIterator)
01077    *  overloaded for random access iterators.
01078   */
01079   template<typename _RandomAccessIterator>
01080     void
01081     __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
01082           random_access_iterator_tag)
01083     {
01084       if (__first == __last)
01085     return;
01086       --__last;
01087       while (__first < __last)
01088     {
01089       std::iter_swap(__first, __last);
01090       ++__first;
01091       --__last;
01092     }
01093     }
01094 
01095   /**
01096    *  @brief Reverse a sequence.
01097    *  @param  first  A bidirectional iterator.
01098    *  @param  last   A bidirectional iterator.
01099    *  @return   reverse() returns no value.
01100    *
01101    *  Reverses the order of the elements in the range @p [first,last),
01102    *  so that the first element becomes the last etc.
01103    *  For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
01104    *  swaps @p *(first+i) and @p *(last-(i+1))
01105   */
01106   template<typename _BidirectionalIterator>
01107     inline void
01108     reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
01109     {
01110       // concept requirements
01111       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
01112                   _BidirectionalIterator>)
01113       __glibcxx_requires_valid_range(__first, __last);
01114       std::__reverse(__first, __last, std::__iterator_category(__first));
01115     }
01116 
01117   /**
01118    *  @brief Copy a sequence, reversing its elements.
01119    *  @param  first   A bidirectional iterator.
01120    *  @param  last    A bidirectional iterator.
01121    *  @param  result  An output iterator.
01122    *  @return  An iterator designating the end of the resulting sequence.
01123    *
01124    *  Copies the elements in the range @p [first,last) to the range
01125    *  @p [result,result+(last-first)) such that the order of the
01126    *  elements is reversed.
01127    *  For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
01128    *  performs the assignment @p *(result+(last-first)-i) = *(first+i).
01129    *  The ranges @p [first,last) and @p [result,result+(last-first))
01130    *  must not overlap.
01131   */
01132   template<typename _BidirectionalIterator, typename _OutputIterator>
01133     _OutputIterator
01134     reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
01135                  _OutputIterator __result)
01136     {
01137       // concept requirements
01138       __glibcxx_function_requires(_BidirectionalIteratorConcept<
01139                   _BidirectionalIterator>)
01140       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
01141         typename iterator_traits<_BidirectionalIterator>::value_type>)
01142       __glibcxx_requires_valid_range(__first, __last);
01143 
01144       while (__first != __last)
01145     {
01146       --__last;
01147       *__result = *__last;
01148       ++__result;
01149     }
01150       return __result;
01151     }
01152 
01153   /**
01154    *  This is a helper function for the rotate algorithm specialized on RAIs.
01155    *  It returns the greatest common divisor of two integer values.
01156   */
01157   template<typename _EuclideanRingElement>
01158     _EuclideanRingElement
01159     __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
01160     {
01161       while (__n != 0)
01162     {
01163       _EuclideanRingElement __t = __m % __n;
01164       __m = __n;
01165       __n = __t;
01166     }
01167       return __m;
01168     }
01169 
01170   /// This is a helper function for the rotate algorithm.
01171   template<typename _ForwardIterator>
01172     void
01173     __rotate(_ForwardIterator __first,
01174          _ForwardIterator __middle,
01175          _ForwardIterator __last,
01176          forward_iterator_tag)
01177     {
01178       if (__first == __middle || __last  == __middle)
01179     return;
01180 
01181       _ForwardIterator __first2 = __middle;
01182       do
01183     {
01184       std::iter_swap(__first, __first2);
01185       ++__first;
01186       ++__first2;
01187       if (__first == __middle)
01188         __middle = __first2;
01189     }
01190       while (__first2 != __last);
01191 
01192       __first2 = __middle;
01193 
01194       while (__first2 != __last)
01195     {
01196       std::iter_swap(__first, __first2);
01197       ++__first;
01198       ++__first2;
01199       if (__first == __middle)
01200         __middle = __first2;
01201       else if (__first2 == __last)
01202         __first2 = __middle;
01203     }
01204     }
01205 
01206    /// This is a helper function for the rotate algorithm.
01207   template<typename _BidirectionalIterator>
01208     void
01209     __rotate(_BidirectionalIterator __first,
01210          _BidirectionalIterator __middle,
01211          _BidirectionalIterator __last,
01212           bidirectional_iterator_tag)
01213     {
01214       // concept requirements
01215       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
01216                   _BidirectionalIterator>)
01217 
01218       if (__first == __middle || __last  == __middle)
01219     return;
01220 
01221       std::__reverse(__first,  __middle, bidirectional_iterator_tag());
01222       std::__reverse(__middle, __last,   bidirectional_iterator_tag());
01223 
01224       while (__first != __middle && __middle != __last)
01225     {
01226       std::iter_swap(__first, --__last);
01227       ++__first;
01228     }
01229 
01230       if (__first == __middle)
01231     std::__reverse(__middle, __last,   bidirectional_iterator_tag());
01232       else
01233     std::__reverse(__first,  __middle, bidirectional_iterator_tag());
01234     }
01235 
01236   /// This is a helper function for the rotate algorithm.
01237   template<typename _RandomAccessIterator>
01238     void
01239     __rotate(_RandomAccessIterator __first,
01240          _RandomAccessIterator __middle,
01241          _RandomAccessIterator __last,
01242          random_access_iterator_tag)
01243     {
01244       // concept requirements
01245       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
01246                   _RandomAccessIterator>)
01247 
01248       if (__first == __middle || __last  == __middle)
01249     return;
01250 
01251       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
01252     _Distance;
01253       typedef typename iterator_traits<_RandomAccessIterator>::value_type
01254     _ValueType;
01255 
01256       const _Distance __n = __last   - __first;
01257       const _Distance __k = __middle - __first;
01258       const _Distance __l = __n - __k;
01259 
01260       if (__k == __l)
01261     {
01262       std::swap_ranges(__first, __middle, __middle);
01263       return;
01264     }
01265 
01266       const _Distance __d = std::__gcd(__n, __k);
01267 
01268       for (_Distance __i = 0; __i < __d; __i++)
01269     {
01270       _ValueType __tmp = _GLIBCXX_MOVE(*__first);
01271       _RandomAccessIterator __p = __first;
01272 
01273       if (__k < __l)
01274         {
01275           for (_Distance __j = 0; __j < __l / __d; __j++)
01276         {
01277           if (__p > __first + __l)
01278             {
01279               *__p = _GLIBCXX_MOVE(*(__p - __l));
01280               __p -= __l;
01281             }
01282 
01283           *__p = _GLIBCXX_MOVE(*(__p + __k));
01284           __p += __k;
01285         }
01286         }
01287       else
01288         {
01289           for (_Distance __j = 0; __j < __k / __d - 1; __j ++)
01290         {
01291           if (__p < __last - __k)
01292             {
01293               *__p = _GLIBCXX_MOVE(*(__p + __k));
01294               __p += __k;
01295             }
01296           *__p = _GLIBCXX_MOVE(*(__p - __l));
01297           __p -= __l;
01298         }
01299         }
01300 
01301       *__p = _GLIBCXX_MOVE(__tmp);
01302       ++__first;
01303     }
01304     }
01305 
01306   /**
01307    *  @brief Rotate the elements of a sequence.
01308    *  @param  first   A forward iterator.
01309    *  @param  middle  A forward iterator.
01310    *  @param  last    A forward iterator.
01311    *  @return  Nothing.
01312    *
01313    *  Rotates the elements of the range @p [first,last) by @p (middle-first)
01314    *  positions so that the element at @p middle is moved to @p first, the
01315    *  element at @p middle+1 is moved to @first+1 and so on for each element
01316    *  in the range @p [first,last).
01317    *
01318    *  This effectively swaps the ranges @p [first,middle) and
01319    *  @p [middle,last).
01320    *
01321    *  Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
01322    *  each @p n in the range @p [0,last-first).
01323   */
01324   template<typename _ForwardIterator>
01325     inline void
01326     rotate(_ForwardIterator __first, _ForwardIterator __middle,
01327        _ForwardIterator __last)
01328     {
01329       // concept requirements
01330       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
01331                   _ForwardIterator>)
01332       __glibcxx_requires_valid_range(__first, __middle);
01333       __glibcxx_requires_valid_range(__middle, __last);
01334 
01335       typedef typename iterator_traits<_ForwardIterator>::iterator_category
01336     _IterType;
01337       std::__rotate(__first, __middle, __last, _IterType());
01338     }
01339 
01340   /**
01341    *  @brief Copy a sequence, rotating its elements.
01342    *  @param  first   A forward iterator.
01343    *  @param  middle  A forward iterator.
01344    *  @param  last    A forward iterator.
01345    *  @param  result  An output iterator.
01346    *  @return   An iterator designating the end of the resulting sequence.
01347    *
01348    *  Copies the elements of the range @p [first,last) to the range
01349    *  beginning at @result, rotating the copied elements by @p (middle-first)
01350    *  positions so that the element at @p middle is moved to @p result, the
01351    *  element at @p middle+1 is moved to @result+1 and so on for each element
01352    *  in the range @p [first,last).
01353    *
01354    *  Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
01355    *  each @p n in the range @p [0,last-first).
01356   */
01357   template<typename _ForwardIterator, typename _OutputIterator>
01358     _OutputIterator
01359     rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
01360                 _ForwardIterator __last, _OutputIterator __result)
01361     {
01362       // concept requirements
01363       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
01364       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
01365         typename iterator_traits<_ForwardIterator>::value_type>)
01366       __glibcxx_requires_valid_range(__first, __middle);
01367       __glibcxx_requires_valid_range(__middle, __last);
01368 
01369       return std::copy(__first, __middle,
01370                        std::copy(__middle, __last, __result));
01371     }
01372 
01373   /// This is a helper function...
01374   template<typename _ForwardIterator, typename _Predicate>
01375     _ForwardIterator
01376     __partition(_ForwardIterator __first, _ForwardIterator __last,
01377         _Predicate __pred, forward_iterator_tag)
01378     {
01379       if (__first == __last)
01380     return __first;
01381 
01382       while (__pred(*__first))
01383     if (++__first == __last)
01384       return __first;
01385 
01386       _ForwardIterator __next = __first;
01387 
01388       while (++__next != __last)
01389     if (__pred(*__next))
01390       {
01391         std::iter_swap(__first, __next);
01392         ++__first;
01393       }
01394 
01395       return __first;
01396     }
01397 
01398   /// This is a helper function...
01399   template<typename _BidirectionalIterator, typename _Predicate>
01400     _BidirectionalIterator
01401     __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
01402         _Predicate __pred, bidirectional_iterator_tag)
01403     {
01404       while (true)
01405     {
01406       while (true)
01407         if (__first == __last)
01408           return __first;
01409         else if (__pred(*__first))
01410           ++__first;
01411         else
01412           break;
01413       --__last;
01414       while (true)
01415         if (__first == __last)
01416           return __first;
01417         else if (!bool(__pred(*__last)))
01418           --__last;
01419         else
01420           break;
01421       std::iter_swap(__first, __last);
01422       ++__first;
01423     }
01424     }
01425 
01426   // partition
01427 
01428   /// This is a helper function...
01429   template<typename _ForwardIterator, typename _Predicate, typename _Distance>
01430     _ForwardIterator
01431     __inplace_stable_partition(_ForwardIterator __first,
01432                    _ForwardIterator __last,
01433                    _Predicate __pred, _Distance __len)
01434     {
01435       if (__len == 1)
01436     return __pred(*__first) ? __last : __first;
01437       _ForwardIterator __middle = __first;
01438       std::advance(__middle, __len / 2);
01439       _ForwardIterator __begin = std::__inplace_stable_partition(__first,
01440                                  __middle,
01441                                  __pred,
01442                                  __len / 2);
01443       _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
01444                                    __pred,
01445                                    __len
01446                                    - __len / 2);
01447       std::rotate(__begin, __middle, __end);
01448       std::advance(__begin, std::distance(__middle, __end));
01449       return __begin;
01450     }
01451 
01452   /// This is a helper function...
01453   template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
01454        typename _Distance>
01455     _ForwardIterator
01456     __stable_partition_adaptive(_ForwardIterator __first,
01457                 _ForwardIterator __last,
01458                 _Predicate __pred, _Distance __len,
01459                 _Pointer __buffer,
01460                 _Distance __buffer_size)
01461     {
01462       if (__len <= __buffer_size)
01463     {
01464       _ForwardIterator __result1 = __first;
01465       _Pointer __result2 = __buffer;
01466       for (; __first != __last; ++__first)
01467         if (__pred(*__first))
01468           {
01469         *__result1 = *__first;
01470         ++__result1;
01471           }
01472         else
01473           {
01474         *__result2 = *__first;
01475         ++__result2;
01476           }
01477       std::copy(__buffer, __result2, __result1);
01478       return __result1;
01479     }
01480       else
01481     {
01482       _ForwardIterator __middle = __first;
01483       std::advance(__middle, __len / 2);
01484       _ForwardIterator __begin =
01485         std::__stable_partition_adaptive(__first, __middle, __pred,
01486                          __len / 2, __buffer,
01487                          __buffer_size);
01488       _ForwardIterator __end =
01489         std::__stable_partition_adaptive(__middle, __last, __pred,
01490                          __len - __len / 2,
01491                          __buffer, __buffer_size);
01492       std::rotate(__begin, __middle, __end);
01493       std::advance(__begin, std::distance(__middle, __end));
01494       return __begin;
01495     }
01496     }
01497 
01498   /**
01499    *  @brief Move elements for which a predicate is true to the beginning
01500    *         of a sequence, preserving relative ordering.
01501    *  @param  first   A forward iterator.
01502    *  @param  last    A forward iterator.
01503    *  @param  pred    A predicate functor.
01504    *  @return  An iterator @p middle such that @p pred(i) is true for each
01505    *  iterator @p i in the range @p [first,middle) and false for each @p i
01506    *  in the range @p [middle,last).
01507    *
01508    *  Performs the same function as @p partition() with the additional
01509    *  guarantee that the relative ordering of elements in each group is
01510    *  preserved, so any two elements @p x and @p y in the range
01511    *  @p [first,last) such that @p pred(x)==pred(y) will have the same
01512    *  relative ordering after calling @p stable_partition().
01513   */
01514   template<typename _ForwardIterator, typename _Predicate>
01515     _ForwardIterator
01516     stable_partition(_ForwardIterator __first, _ForwardIterator __last,
01517              _Predicate __pred)
01518     {
01519       // concept requirements
01520       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
01521                   _ForwardIterator>)
01522       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
01523         typename iterator_traits<_ForwardIterator>::value_type>)
01524       __glibcxx_requires_valid_range(__first, __last);
01525 
01526       if (__first == __last)
01527     return __first;
01528       else
01529     {
01530       typedef typename iterator_traits<_ForwardIterator>::value_type
01531         _ValueType;
01532       typedef typename iterator_traits<_ForwardIterator>::difference_type
01533         _DistanceType;
01534 
01535       _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
01536                                 __last);
01537     if (__buf.size() > 0)
01538       return
01539         std::__stable_partition_adaptive(__first, __last, __pred,
01540                       _DistanceType(__buf.requested_size()),
01541                       __buf.begin(),
01542                       _DistanceType(__buf.size()));
01543     else
01544       return
01545         std::__inplace_stable_partition(__first, __last, __pred,
01546                      _DistanceType(__buf.requested_size()));
01547     }
01548     }
01549 
01550   /// This is a helper function for the sort routines.
01551   template<typename _RandomAccessIterator>
01552     void
01553     __heap_select(_RandomAccessIterator __first,
01554           _RandomAccessIterator __middle,
01555           _RandomAccessIterator __last)
01556     {
01557       std::make_heap(__first, __middle);
01558       for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
01559     if (*__i < *__first)
01560       std::__pop_heap(__first, __middle, __i);
01561     }
01562 
01563   /// This is a helper function for the sort routines.
01564   template<typename _RandomAccessIterator, typename _Compare>
01565     void
01566     __heap_select(_RandomAccessIterator __first,
01567           _RandomAccessIterator __middle,
01568           _RandomAccessIterator __last, _Compare __comp)
01569     {
01570       std::make_heap(__first, __middle, __comp);
01571       for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
01572     if (__comp(*__i, *__first))
01573       std::__pop_heap(__first, __middle, __i, __comp);
01574     }
01575 
01576   // partial_sort
01577 
01578   /**
01579    *  @brief Copy the smallest elements of a sequence.
01580    *  @param  first   An iterator.
01581    *  @param  last    Another iterator.
01582    *  @param  result_first   A random-access iterator.
01583    *  @param  result_last    Another random-access iterator.
01584    *  @return   An iterator indicating the end of the resulting sequence.
01585    *
01586    *  Copies and sorts the smallest N values from the range @p [first,last)
01587    *  to the range beginning at @p result_first, where the number of
01588    *  elements to be copied, @p N, is the smaller of @p (last-first) and
01589    *  @p (result_last-result_first).
01590    *  After the sort if @p i and @j are iterators in the range
01591    *  @p [result_first,result_first+N) such that @i precedes @j then
01592    *  @p *j<*i is false.
01593    *  The value returned is @p result_first+N.
01594   */
01595   template<typename _InputIterator, typename _RandomAccessIterator>
01596     _RandomAccessIterator
01597     partial_sort_copy(_InputIterator __first, _InputIterator __last,
01598               _RandomAccessIterator __result_first,
01599               _RandomAccessIterator __result_last)
01600     {
01601       typedef typename iterator_traits<_InputIterator>::value_type
01602     _InputValueType;
01603       typedef typename iterator_traits<_RandomAccessIterator>::value_type
01604     _OutputValueType;
01605       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
01606     _DistanceType;
01607 
01608       // concept requirements
01609       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
01610       __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
01611                   _OutputValueType>)
01612       __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
01613                                      _OutputValueType>)
01614       __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
01615       __glibcxx_requires_valid_range(__first, __last);
01616       __glibcxx_requires_valid_range(__result_first, __result_last);
01617 
01618       if (__result_first == __result_last)
01619     return __result_last;
01620       _RandomAccessIterator __result_real_last = __result_first;
01621       while(__first != __last && __result_real_last != __result_last)
01622     {
01623       *__result_real_last = *__first;
01624       ++__result_real_last;
01625       ++__first;
01626     }
01627       std::make_heap(__result_first, __result_real_last);
01628       while (__first != __last)
01629     {
01630       if (*__first < *__result_first)
01631         std::__adjust_heap(__result_first, _DistanceType(0),
01632                    _DistanceType(__result_real_last
01633                          - __result_first),
01634                    _InputValueType(*__first));
01635       ++__first;
01636     }
01637       std::sort_heap(__result_first, __result_real_last);
01638       return __result_real_last;
01639     }
01640 
01641   /**
01642    *  @brief Copy the smallest elements of a sequence using a predicate for
01643    *         comparison.
01644    *  @param  first   An input iterator.
01645    *  @param  last    Another input iterator.
01646    *  @param  result_first   A random-access iterator.
01647    *  @param  result_last    Another random-access iterator.
01648    *  @param  comp    A comparison functor.
01649    *  @return   An iterator indicating the end of the resulting sequence.
01650    *
01651    *  Copies and sorts the smallest N values from the range @p [first,last)
01652    *  to the range beginning at @p result_first, where the number of
01653    *  elements to be copied, @p N, is the smaller of @p (last-first) and
01654    *  @p (result_last-result_first).
01655    *  After the sort if @p i and @j are iterators in the range
01656    *  @p [result_first,result_first+N) such that @i precedes @j then
01657    *  @p comp(*j,*i) is false.
01658    *  The value returned is @p result_first+N.
01659   */
01660   template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
01661     _RandomAccessIterator
01662     partial_sort_copy(_InputIterator __first, _InputIterator __last,
01663               _RandomAccessIterator __result_first,
01664               _RandomAccessIterator __result_last,
01665               _Compare __comp)
01666     {
01667       typedef typename iterator_traits<_InputIterator>::value_type
01668     _InputValueType;
01669       typedef typename iterator_traits<_RandomAccessIterator>::value_type
01670     _OutputValueType;
01671       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
01672     _DistanceType;
01673 
01674       // concept requirements
01675       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
01676       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
01677                   _RandomAccessIterator>)
01678       __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
01679                   _OutputValueType>)
01680       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
01681                   _InputValueType, _OutputValueType>)
01682       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
01683                   _OutputValueType, _OutputValueType>)
01684       __glibcxx_requires_valid_range(__first, __last);
01685       __glibcxx_requires_valid_range(__result_first, __result_last);
01686 
01687       if (__result_first == __result_last)
01688     return __result_last;
01689       _RandomAccessIterator __result_real_last = __result_first;
01690       while(__first != __last && __result_real_last != __result_last)
01691     {
01692       *__result_real_last = *__first;
01693       ++__result_real_last;
01694       ++__first;
01695     }
01696       std::make_heap(__result_first, __result_real_last, __comp);
01697       while (__first != __last)
01698     {
01699       if (__comp(*__first, *__result_first))
01700         std::__adjust_heap(__result_first, _DistanceType(0),
01701                    _DistanceType(__result_real_last
01702                          - __result_first),
01703                    _InputValueType(*__first),
01704                    __comp);
01705       ++__first;
01706     }
01707       std::sort_heap(__result_first, __result_real_last, __comp);
01708       return __result_real_last;
01709     }
01710 
01711   /// This is a helper function for the sort routine.
01712   template<typename _RandomAccessIterator, typename _Tp>
01713     void
01714     __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
01715     {
01716       _RandomAccessIterator __next = __last;
01717       --__next;
01718       while (__val < *__next)
01719     {
01720       *__last = *__next;
01721       __last = __next;
01722       --__next;
01723     }
01724       *__last = __val;
01725     }
01726 
01727   /// This is a helper function for the sort routine.
01728   template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
01729     void
01730     __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
01731                   _Compare __comp)
01732     {
01733       _RandomAccessIterator __next = __last;
01734       --__next;
01735       while (__comp(__val, *__next))
01736     {
01737       *__last = *__next;
01738       __last = __next;
01739       --__next;
01740     }
01741       *__last = __val;
01742     }
01743 
01744   /// This is a helper function for the sort routine.
01745   template<typename _RandomAccessIterator>
01746     void
01747     __insertion_sort(_RandomAccessIterator __first,
01748              _RandomAccessIterator __last)
01749     {
01750       if (__first == __last)
01751     return;
01752 
01753       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
01754     {
01755       typename iterator_traits<_RandomAccessIterator>::value_type
01756         __val = *__i;
01757       if (__val < *__first)
01758         {
01759           std::copy_backward(__first, __i, __i + 1);
01760           *__first = __val;
01761         }
01762       else
01763         std::__unguarded_linear_insert(__i, __val);
01764     }
01765     }
01766 
01767   /// This is a helper function for the sort routine.
01768   template<typename _RandomAccessIterator, typename _Compare>
01769     void
01770     __insertion_sort(_RandomAccessIterator __first,
01771              _RandomAccessIterator __last, _Compare __comp)
01772     {
01773       if (__first == __last) return;
01774 
01775       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
01776     {
01777       typename iterator_traits<_RandomAccessIterator>::value_type
01778         __val = *__i;
01779       if (__comp(__val, *__first))
01780         {
01781           std::copy_backward(__first, __i, __i + 1);
01782           *__first = __val;
01783         }
01784       else
01785         std::__unguarded_linear_insert(__i, __val, __comp);
01786     }
01787     }
01788 
01789   /// This is a helper function for the sort routine.
01790   template<typename _RandomAccessIterator>
01791     inline void
01792     __unguarded_insertion_sort(_RandomAccessIterator __first,
01793                    _RandomAccessIterator __last)
01794     {
01795       typedef typename iterator_traits<_RandomAccessIterator>::value_type
01796     _ValueType;
01797 
01798       for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
01799     std::__unguarded_linear_insert(__i, _ValueType(*__i));
01800     }
01801 
01802   /// This is a helper function for the sort routine.
01803   template<typename _RandomAccessIterator, typename _Compare>
01804     inline void
01805     __unguarded_insertion_sort(_RandomAccessIterator __first,
01806                    _RandomAccessIterator __last, _Compare __comp)
01807     {
01808       typedef typename iterator_traits<_RandomAccessIterator>::value_type
01809     _ValueType;
01810 
01811       for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
01812     std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
01813     }
01814 
01815   /**
01816    *  @doctodo
01817    *  This controls some aspect of the sort routines.
01818   */
01819   enum { _S_threshold = 16 };
01820 
01821   /// This is a helper function for the sort routine.
01822   template<typename _RandomAccessIterator>
01823     void
01824     __final_insertion_sort(_RandomAccessIterator __first,
01825                _RandomAccessIterator __last)
01826     {
01827       if (__last - __first > int(_S_threshold))
01828     {
01829       std::__insertion_sort(__first, __first + int(_S_threshold));
01830       std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
01831     }
01832       else
01833     std::__insertion_sort(__first, __last);
01834     }
01835 
01836   /// This is a helper function for the sort routine.
01837   template<typename _RandomAccessIterator, typename _Compare>
01838     void
01839     __final_insertion_sort(_RandomAccessIterator __first,
01840                _RandomAccessIterator __last, _Compare __comp)
01841     {
01842       if (__last - __first > int(_S_threshold))
01843     {
01844       std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
01845       std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
01846                       __comp);
01847     }
01848       else
01849     std::__insertion_sort(__first, __last, __comp);
01850     }
01851 
01852   /// This is a helper function...
01853   template<typename _RandomAccessIterator, typename _Tp>
01854     _RandomAccessIterator
01855     __unguarded_partition(_RandomAccessIterator __first,
01856               _RandomAccessIterator __last, _Tp __pivot)
01857     {
01858       while (true)
01859     {
01860       while (*__first < __pivot)
01861         ++__first;
01862       --__last;
01863       while (__pivot < *__last)
01864         --__last;
01865       if (!(__first < __last))
01866         return __first;
01867       std::iter_swap(__first, __last);
01868       ++__first;
01869     }
01870     }
01871 
01872   /// This is a helper function...
01873   template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
01874     _RandomAccessIterator
01875     __unguarded_partition(_RandomAccessIterator __first,
01876               _RandomAccessIterator __last,
01877               _Tp __pivot, _Compare __comp)
01878     {
01879       while (true)
01880     {
01881       while (__comp(*__first, __pivot))
01882         ++__first;
01883       --__last;
01884       while (__comp(__pivot, *__last))
01885         --__last;
01886       if (!(__first < __last))
01887         return __first;
01888       std::iter_swap(__first, __last);
01889       ++__first;
01890     }
01891     }
01892 
01893   /// This is a helper function for the sort routine.
01894   template<typename _RandomAccessIterator, typename _Size>
01895     void
01896     __introsort_loop(_RandomAccessIterator __first,
01897              _RandomAccessIterator __last,
01898              _Size __depth_limit)
01899     {
01900       typedef typename iterator_traits<_RandomAccessIterator>::value_type
01901     _ValueType;
01902 
01903       while (__last - __first > int(_S_threshold))
01904     {
01905       if (__depth_limit == 0)
01906         {
01907           _GLIBCXX_STD_P::partial_sort(__first, __last, __last);
01908           return;
01909         }
01910       --__depth_limit;
01911       _RandomAccessIterator __cut =
01912         std::__unguarded_partition(__first, __last,
01913                        _ValueType(std::__median(*__first,
01914                                 *(__first
01915                                   + (__last
01916                                      - __first)
01917                                   / 2),
01918                                 *(__last
01919                                   - 1))));
01920       std::__introsort_loop(__cut, __last, __depth_limit);
01921       __last = __cut;
01922     }
01923     }
01924 
01925   /// This is a helper function for the sort routine.
01926   template<typename _RandomAccessIterator, typename _Size, typename _Compare>
01927     void
01928     __introsort_loop(_RandomAccessIterator __first,
01929              _RandomAccessIterator __last,
01930              _Size __depth_limit, _Compare __comp)
01931     {
01932       typedef typename iterator_traits<_RandomAccessIterator>::value_type
01933     _ValueType;
01934 
01935       while (__last - __first > int(_S_threshold))
01936     {
01937       if (__depth_limit == 0)
01938         {
01939           _GLIBCXX_STD_P::partial_sort(__first, __last, __last, __comp);
01940           return;
01941         }
01942       --__depth_limit;
01943       _RandomAccessIterator __cut =
01944         std::__unguarded_partition(__first, __last,
01945                        _ValueType(std::__median(*__first,
01946                                 *(__first
01947                                   + (__last
01948                                      - __first)
01949                                   / 2),
01950                                 *(__last - 1),
01951                                 __comp)),
01952                        __comp);
01953       std::__introsort_loop(__cut, __last, __depth_limit, __comp);
01954       __last = __cut;
01955     }
01956     }
01957 
01958   /// This is a helper function for the sort routines.  Precondition: __n > 0.
01959   template<typename _Size>
01960     inline _Size
01961     __lg(_Size __n)
01962     {
01963       _Size __k;
01964       for (__k = 0; __n != 0; __n >>= 1)
01965     ++__k;
01966       return __k - 1;
01967     }
01968 
01969   inline int
01970   __lg(int __n)
01971   { return sizeof(int) * __CHAR_BIT__  - 1 - __builtin_clz(__n); }
01972 
01973   inline long
01974   __lg(long __n)
01975   { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
01976 
01977   inline long long
01978   __lg(long long __n)
01979   { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
01980 
01981   // sort
01982 
01983   template<typename _RandomAccessIterator, typename _Size>
01984     void
01985     __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
01986           _RandomAccessIterator __last, _Size __depth_limit)
01987     {
01988       typedef typename iterator_traits<_RandomAccessIterator>::value_type
01989     _ValueType;
01990 
01991       while (__last - __first > 3)
01992     {
01993       if (__depth_limit == 0)
01994         {
01995           std::__heap_select(__first, __nth + 1, __last);
01996 
01997           // Place the nth largest element in its final position.
01998           std::iter_swap(__first, __nth);
01999           return;
02000         }
02001       --__depth_limit;
02002       _RandomAccessIterator __cut =
02003         std::__unguarded_partition(__first, __last,
02004                        _ValueType(std::__median(*__first,
02005                                 *(__first
02006                                   + (__last
02007                                      - __first)
02008                                   / 2),
02009                                 *(__last
02010                                   - 1))));
02011       if (__cut <= __nth)
02012         __first = __cut;
02013       else
02014         __last = __cut;
02015     }
02016       std::__insertion_sort(__first, __last);
02017     }
02018 
02019   template<typename _RandomAccessIterator, typename _Size, typename _Compare>
02020     void
02021     __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
02022           _RandomAccessIterator __last, _Size __depth_limit,
02023           _Compare __comp)
02024     {
02025       typedef typename iterator_traits<_RandomAccessIterator>::value_type
02026     _ValueType;
02027 
02028       while (__last - __first > 3)
02029     {
02030       if (__depth_limit == 0)
02031         {
02032           std::__heap_select(__first, __nth + 1, __last, __comp);
02033           // Place the nth largest element in its final position.
02034           std::iter_swap(__first, __nth);
02035           return;
02036         }
02037       --__depth_limit;
02038       _RandomAccessIterator __cut =
02039         std::__unguarded_partition(__first, __last,
02040                        _ValueType(std::__median(*__first,
02041                                 *(__first
02042                                   + (__last
02043                                      - __first)
02044                                   / 2),
02045                                 *(__last - 1),
02046                                 __comp)),
02047                        __comp);
02048       if (__cut <= __nth)
02049         __first = __cut;
02050       else
02051         __last = __cut;
02052     }
02053       std::__insertion_sort(__first, __last, __comp);
02054     }
02055 
02056   // nth_element
02057 
02058   /**
02059    *  @brief Finds the first position in which @a val could be inserted
02060    *         without changing the ordering.
02061    *  @param  first   An iterator.
02062    *  @param  last    Another iterator.
02063    *  @param  val     The search term.
02064    *  @return         An iterator pointing to the first element "not less
02065    *                  than" @a val, or end() if every element is less than 
02066    *                  @a val.
02067    *  @ingroup binarysearch
02068   */
02069   template<typename _ForwardIterator, typename _Tp>
02070     _ForwardIterator
02071     lower_bound(_ForwardIterator __first, _ForwardIterator __last,
02072         const _Tp& __val)
02073     {
02074       typedef typename iterator_traits<_ForwardIterator>::value_type
02075     _ValueType;
02076       typedef typename iterator_traits<_ForwardIterator>::difference_type
02077     _DistanceType;
02078 
02079       // concept requirements
02080       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02081       __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
02082       __glibcxx_requires_partitioned_lower(__first, __last, __val);
02083 
02084       _DistanceType __len = std::distance(__first, __last);
02085       _DistanceType __half;
02086       _ForwardIterator __middle;
02087 
02088       while (__len > 0)
02089     {
02090       __half = __len >> 1;
02091       __middle = __first;
02092       std::advance(__middle, __half);
02093       if (*__middle < __val)
02094         {
02095           __first = __middle;
02096           ++__first;
02097           __len = __len - __half - 1;
02098         }
02099       else
02100         __len = __half;
02101     }
02102       return __first;
02103     }
02104 
02105   /**
02106    *  @brief Finds the first position in which @a val could be inserted
02107    *         without changing the ordering.
02108    *  @param  first   An iterator.
02109    *  @param  last    Another iterator.
02110    *  @param  val     The search term.
02111    *  @param  comp    A functor to use for comparisons.
02112    *  @return  An iterator pointing to the first element "not less than" @a val,
02113    *           or end() if every element is less than @a val.
02114    *  @ingroup binarysearch
02115    *
02116    *  The comparison function should have the same effects on ordering as
02117    *  the function used for the initial sort.
02118   */
02119   template<typename _ForwardIterator, typename _Tp, typename _Compare>
02120     _ForwardIterator
02121     lower_bound(_ForwardIterator __first, _ForwardIterator __last,
02122         const _Tp& __val, _Compare __comp)
02123     {
02124       typedef typename iterator_traits<_ForwardIterator>::value_type
02125     _ValueType;
02126       typedef typename iterator_traits<_ForwardIterator>::difference_type
02127     _DistanceType;
02128 
02129       // concept requirements
02130       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02131       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02132                   _ValueType, _Tp>)
02133       __glibcxx_requires_partitioned_lower_pred(__first, __last,
02134                         __val, __comp);
02135 
02136       _DistanceType __len = std::distance(__first, __last);
02137       _DistanceType __half;
02138       _ForwardIterator __middle;
02139 
02140       while (__len > 0)
02141     {
02142       __half = __len >> 1;
02143       __middle = __first;
02144       std::advance(__middle, __half);
02145       if (__comp(*__middle, __val))
02146         {
02147           __first = __middle;
02148           ++__first;
02149           __len = __len - __half - 1;
02150         }
02151       else
02152         __len = __half;
02153     }
02154       return __first;
02155     }
02156 
02157   /**
02158    *  @brief Finds the last position in which @a val could be inserted
02159    *         without changing the ordering.
02160    *  @param  first   An iterator.
02161    *  @param  last    Another iterator.
02162    *  @param  val     The search term.
02163    *  @return  An iterator pointing to the first element greater than @a val,
02164    *           or end() if no elements are greater than @a val.
02165    *  @ingroup binarysearch
02166   */
02167   template<typename _ForwardIterator, typename _Tp>
02168     _ForwardIterator
02169     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
02170         const _Tp& __val)
02171     {
02172       typedef typename iterator_traits<_ForwardIterator>::value_type
02173     _ValueType;
02174       typedef typename iterator_traits<_ForwardIterator>::difference_type
02175     _DistanceType;
02176 
02177       // concept requirements
02178       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02179       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
02180       __glibcxx_requires_partitioned_upper(__first, __last, __val);
02181 
02182       _DistanceType __len = std::distance(__first, __last);
02183       _DistanceType __half;
02184       _ForwardIterator __middle;
02185 
02186       while (__len > 0)
02187     {
02188       __half = __len >> 1;
02189       __middle = __first;
02190       std::advance(__middle, __half);
02191       if (__val < *__middle)
02192         __len = __half;
02193       else
02194         {
02195           __first = __middle;
02196           ++__first;
02197           __len = __len - __half - 1;
02198         }
02199     }
02200       return __first;
02201     }
02202 
02203   /**
02204    *  @brief Finds the last position in which @a val could be inserted
02205    *         without changing the ordering.
02206    *  @param  first   An iterator.
02207    *  @param  last    Another iterator.
02208    *  @param  val     The search term.
02209    *  @param  comp    A functor to use for comparisons.
02210    *  @return  An iterator pointing to the first element greater than @a val,
02211    *           or end() if no elements are greater than @a val.
02212    *  @ingroup binarysearch
02213    *
02214    *  The comparison function should have the same effects on ordering as
02215    *  the function used for the initial sort.
02216   */
02217   template<typename _ForwardIterator, typename _Tp, typename _Compare>
02218     _ForwardIterator
02219     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
02220         const _Tp& __val, _Compare __comp)
02221     {
02222       typedef typename iterator_traits<_ForwardIterator>::value_type
02223     _ValueType;
02224       typedef typename iterator_traits<_ForwardIterator>::difference_type
02225     _DistanceType;
02226 
02227       // concept requirements
02228       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02229       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02230                   _Tp, _ValueType>)
02231       __glibcxx_requires_partitioned_upper_pred(__first, __last,
02232                         __val, __comp);
02233 
02234       _DistanceType __len = std::distance(__first, __last);
02235       _DistanceType __half;
02236       _ForwardIterator __middle;
02237 
02238       while (__len > 0)
02239     {
02240       __half = __len >> 1;
02241       __middle = __first;
02242       std::advance(__middle, __half);
02243       if (__comp(__val, *__middle))
02244         __len = __half;
02245       else
02246         {
02247           __first = __middle;
02248           ++__first;
02249           __len = __len - __half - 1;
02250         }
02251     }
02252       return __first;
02253     }
02254 
02255   /**
02256    *  @brief Finds the largest subrange in which @a val could be inserted
02257    *         at any place in it without changing the ordering.
02258    *  @param  first   An iterator.
02259    *  @param  last    Another iterator.
02260    *  @param  val     The search term.
02261    *  @return  An pair of iterators defining the subrange.
02262    *  @ingroup binarysearch
02263    *
02264    *  This is equivalent to
02265    *  @code
02266    *    std::make_pair(lower_bound(first, last, val),
02267    *                   upper_bound(first, last, val))
02268    *  @endcode
02269    *  but does not actually call those functions.
02270   */
02271   template<typename _ForwardIterator, typename _Tp>
02272     pair<_ForwardIterator, _ForwardIterator>
02273     equal_range(_ForwardIterator __first, _ForwardIterator __last,
02274         const _Tp& __val)
02275     {
02276       typedef typename iterator_traits<_ForwardIterator>::value_type
02277     _ValueType;
02278       typedef typename iterator_traits<_ForwardIterator>::difference_type
02279     _DistanceType;
02280 
02281       // concept requirements
02282       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02283       __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
02284       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)  
02285       __glibcxx_requires_partitioned_lower(__first, __last, __val);
02286       __glibcxx_requires_partitioned_upper(__first, __last, __val);      
02287 
02288       _DistanceType __len = std::distance(__first, __last);
02289       _DistanceType __half;
02290       _ForwardIterator __middle, __left, __right;
02291 
02292       while (__len > 0)
02293     {
02294       __half = __len >> 1;
02295       __middle = __first;
02296       std::advance(__middle, __half);
02297       if (*__middle < __val)
02298         {
02299           __first = __middle;
02300           ++__first;
02301           __len = __len - __half - 1;
02302         }
02303       else if (__val < *__middle)
02304         __len = __half;
02305       else
02306         {
02307           __left = std::lower_bound(__first, __middle, __val);
02308           std::advance(__first, __len);
02309           __right = std::upper_bound(++__middle, __first, __val);
02310           return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
02311         }
02312     }
02313       return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
02314     }
02315 
02316   /**
02317    *  @brief Finds the largest subrange in which @a val could be inserted
02318    *         at any place in it without changing the ordering.
02319    *  @param  first   An iterator.
02320    *  @param  last    Another iterator.
02321    *  @param  val     The search term.
02322    *  @param  comp    A functor to use for comparisons.
02323    *  @return  An pair of iterators defining the subrange.
02324    *  @ingroup binarysearch
02325    *
02326    *  This is equivalent to
02327    *  @code
02328    *    std::make_pair(lower_bound(first, last, val, comp),
02329    *                   upper_bound(first, last, val, comp))
02330    *  @endcode
02331    *  but does not actually call those functions.
02332   */
02333   template<typename _ForwardIterator, typename _Tp, typename _Compare>
02334     pair<_ForwardIterator, _ForwardIterator>
02335     equal_range(_ForwardIterator __first, _ForwardIterator __last,
02336         const _Tp& __val,
02337         _Compare __comp)
02338     {
02339       typedef typename iterator_traits<_ForwardIterator>::value_type
02340     _ValueType;
02341       typedef typename iterator_traits<_ForwardIterator>::difference_type
02342     _DistanceType;
02343 
02344       // concept requirements
02345       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02346       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02347                   _ValueType, _Tp>)
02348       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02349                   _Tp, _ValueType>)
02350       __glibcxx_requires_partitioned_lower_pred(__first, __last,
02351                         __val, __comp);
02352       __glibcxx_requires_partitioned_upper_pred(__first, __last,
02353                         __val, __comp);
02354 
02355       _DistanceType __len = std::distance(__first, __last);
02356       _DistanceType __half;
02357       _ForwardIterator __middle, __left, __right;
02358 
02359       while (__len > 0)
02360     {
02361       __half = __len >> 1;
02362       __middle = __first;
02363       std::advance(__middle, __half);
02364       if (__comp(*__middle, __val))
02365         {
02366           __first = __middle;
02367           ++__first;
02368           __len = __len - __half - 1;
02369         }
02370       else if (__comp(__val, *__middle))
02371         __len = __half;
02372       else
02373         {
02374           __left = std::lower_bound(__first, __middle, __val, __comp);
02375           std::advance(__first, __len);
02376           __right = std::upper_bound(++__middle, __first, __val, __comp);
02377           return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
02378         }
02379     }
02380       return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
02381     }
02382 
02383   /**
02384    *  @brief Determines whether an element exists in a range.
02385    *  @param  first   An iterator.
02386    *  @param  last    Another iterator.
02387    *  @param  val     The search term.
02388    *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
02389    *  @ingroup binarysearch
02390    *
02391    *  Note that this does not actually return an iterator to @a val.  For
02392    *  that, use std::find or a container's specialized find member functions.
02393   */
02394   template<typename _ForwardIterator, typename _Tp>
02395     bool
02396     binary_search(_ForwardIterator __first, _ForwardIterator __last,
02397                   const _Tp& __val)
02398     {
02399       typedef typename iterator_traits<_ForwardIterator>::value_type
02400     _ValueType;
02401 
02402       // concept requirements
02403       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02404       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
02405       __glibcxx_requires_partitioned_lower(__first, __last, __val);
02406       __glibcxx_requires_partitioned_upper(__first, __last, __val);
02407 
02408       _ForwardIterator __i = std::lower_bound(__first, __last, __val);
02409       return __i != __last && !(__val < *__i);
02410     }
02411 
02412   /**
02413    *  @brief Determines whether an element exists in a range.
02414    *  @param  first   An iterator.
02415    *  @param  last    Another iterator.
02416    *  @param  val     The search term.
02417    *  @param  comp    A functor to use for comparisons.
02418    *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
02419    *  @ingroup binarysearch
02420    *
02421    *  Note that this does not actually return an iterator to @a val.  For
02422    *  that, use std::find or a container's specialized find member functions.
02423    *
02424    *  The comparison function should have the same effects on ordering as
02425    *  the function used for the initial sort.
02426   */
02427   template<typename _ForwardIterator, typename _Tp, typename _Compare>
02428     bool
02429     binary_search(_ForwardIterator __first, _ForwardIterator __last,
02430                   const _Tp& __val, _Compare __comp)
02431     {
02432       typedef typename iterator_traits<_ForwardIterator>::value_type
02433     _ValueType;
02434 
02435       // concept requirements
02436       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02437       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02438                   _Tp, _ValueType>)
02439       __glibcxx_requires_partitioned_lower_pred(__first, __last,
02440                         __val, __comp);
02441       __glibcxx_requires_partitioned_upper_pred(__first, __last,
02442                         __val, __comp);
02443 
02444       _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
02445       return __i != __last && !bool(__comp(__val, *__i));
02446     }
02447 
02448   // merge
02449 
02450   /// This is a helper function for the merge routines.
02451   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
02452        typename _BidirectionalIterator3>
02453     _BidirectionalIterator3
02454     __merge_backward(_BidirectionalIterator1 __first1,
02455              _BidirectionalIterator1 __last1,
02456              _BidirectionalIterator2 __first2,
02457              _BidirectionalIterator2 __last2,
02458              _BidirectionalIterator3 __result)
02459     {
02460       if (__first1 == __last1)
02461     return std::copy_backward(__first2, __last2, __result);
02462       if (__first2 == __last2)
02463     return std::copy_backward(__first1, __last1, __result);
02464       --__last1;
02465       --__last2;
02466       while (true)
02467     {
02468       if (*__last2 < *__last1)
02469         {
02470           *--__result = *__last1;
02471           if (__first1 == __last1)
02472         return std::copy_backward(__first2, ++__last2, __result);
02473           --__last1;
02474         }
02475       else
02476         {
02477           *--__result = *__last2;
02478           if (__first2 == __last2)
02479         return std::copy_backward(__first1, ++__last1, __result);
02480           --__last2;
02481         }
02482     }
02483     }
02484 
02485   /// This is a helper function for the merge routines.
02486   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
02487        typename _BidirectionalIterator3, typename _Compare>
02488     _BidirectionalIterator3
02489     __merge_backward(_BidirectionalIterator1 __first1,
02490              _BidirectionalIterator1 __last1,
02491              _BidirectionalIterator2 __first2,
02492              _BidirectionalIterator2 __last2,
02493              _BidirectionalIterator3 __result,
02494              _Compare __comp)
02495     {
02496       if (__first1 == __last1)
02497     return std::copy_backward(__first2, __last2, __result);
02498       if (__first2 == __last2)
02499     return std::copy_backward(__first1, __last1, __result);
02500       --__last1;
02501       --__last2;
02502       while (true)
02503     {
02504       if (__comp(*__last2, *__last1))
02505         {
02506           *--__result = *__last1;
02507           if (__first1 == __last1)
02508         return std::copy_backward(__first2, ++__last2, __result);
02509           --__last1;
02510         }
02511       else
02512         {
02513           *--__result = *__last2;
02514           if (__first2 == __last2)
02515         return std::copy_backward(__first1, ++__last1, __result);
02516           --__last2;
02517         }
02518     }
02519     }
02520 
02521   /// This is a helper function for the merge routines.
02522   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
02523        typename _Distance>
02524     _BidirectionalIterator1
02525     __rotate_adaptive(_BidirectionalIterator1 __first,
02526               _BidirectionalIterator1 __middle,
02527               _BidirectionalIterator1 __last,
02528               _Distance __len1, _Distance __len2,
02529               _BidirectionalIterator2 __buffer,
02530               _Distance __buffer_size)
02531     {
02532       _BidirectionalIterator2 __buffer_end;
02533       if (__len1 > __len2 && __len2 <= __buffer_size)
02534     {
02535       __buffer_end = std::copy(__middle, __last, __buffer);
02536       std::copy_backward(__first, __middle, __last);
02537       return std::copy(__buffer, __buffer_end, __first);
02538     }
02539       else if (__len1 <= __buffer_size)
02540     {
02541       __buffer_end = std::copy(__first, __middle, __buffer);
02542       std::copy(__middle, __last, __first);
02543       return std::copy_backward(__buffer, __buffer_end, __last);
02544     }
02545       else
02546     {
02547       std::rotate(__first, __middle, __last);
02548       std::advance(__first, std::distance(__middle, __last));
02549       return __first;
02550     }
02551     }
02552 
02553   /// This is a helper function for the merge routines.
02554   template<typename _BidirectionalIterator, typename _Distance,
02555        typename _Pointer>
02556     void
02557     __merge_adaptive(_BidirectionalIterator __first,
02558                      _BidirectionalIterator __middle,
02559              _BidirectionalIterator __last,
02560              _Distance __len1, _Distance __len2,
02561              _Pointer __buffer, _Distance __buffer_size)
02562     {
02563       if (__len1 <= __len2 && __len1 <= __buffer_size)
02564     {
02565       _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
02566       _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last, 
02567                 __first);
02568     }
02569       else if (__len2 <= __buffer_size)
02570     {
02571       _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
02572       std::__merge_backward(__first, __middle, __buffer,
02573                 __buffer_end, __last);
02574     }
02575       else
02576     {
02577       _BidirectionalIterator __first_cut = __first;
02578       _BidirectionalIterator __second_cut = __middle;
02579       _Distance __len11 = 0;
02580       _Distance __len22 = 0;
02581       if (__len1 > __len2)
02582         {
02583           __len11 = __len1 / 2;
02584           std::advance(__first_cut, __len11);
02585           __second_cut = std::lower_bound(__middle, __last,
02586                           *__first_cut);
02587           __len22 = std::distance(__middle, __second_cut);
02588         }
02589       else
02590         {
02591           __len22 = __len2 / 2;
02592           std::advance(__second_cut, __len22);
02593           __first_cut = std::upper_bound(__first, __middle,
02594                          *__second_cut);
02595           __len11 = std::distance(__first, __first_cut);
02596         }
02597       _BidirectionalIterator __new_middle =
02598         std::__rotate_adaptive(__first_cut, __middle, __second_cut,
02599                    __len1 - __len11, __len22, __buffer,
02600                    __buffer_size);
02601       std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
02602                 __len22, __buffer, __buffer_size);
02603       std::__merge_adaptive(__new_middle, __second_cut, __last,
02604                 __len1 - __len11,
02605                 __len2 - __len22, __buffer, __buffer_size);
02606     }
02607     }
02608 
02609   /// This is a helper function for the merge routines.
02610   template<typename _BidirectionalIterator, typename _Distance, 
02611        typename _Pointer, typename _Compare>
02612     void
02613     __merge_adaptive(_BidirectionalIterator __first,
02614                      _BidirectionalIterator __middle,
02615              _BidirectionalIterator __last,
02616              _Distance __len1, _Distance __len2,
02617              _Pointer __buffer, _Distance __buffer_size,
02618              _Compare __comp)
02619     {
02620       if (__len1 <= __len2 && __len1 <= __buffer_size)
02621     {
02622       _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
02623       _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last,
02624                 __first, __comp);
02625     }
02626       else if (__len2 <= __buffer_size)
02627     {
02628       _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
02629       std::__merge_backward(__first, __middle, __buffer, __buffer_end,
02630                 __last, __comp);
02631     }
02632       else
02633     {
02634       _BidirectionalIterator __first_cut = __first;
02635       _BidirectionalIterator __second_cut = __middle;
02636       _Distance __len11 = 0;
02637       _Distance __len22 = 0;
02638       if (__len1 > __len2)
02639         {
02640           __len11 = __len1 / 2;
02641           std::advance(__first_cut, __len11);
02642           __second_cut = std::lower_bound(__middle, __last, *__first_cut,
02643                           __comp);
02644           __len22 = std::distance(__middle, __second_cut);
02645         }
02646       else
02647         {
02648           __len22 = __len2 / 2;
02649           std::advance(__second_cut, __len22);
02650           __first_cut = std::upper_bound(__first, __middle, *__second_cut,
02651                          __comp);
02652           __len11 = std::distance(__first, __first_cut);
02653         }
02654       _BidirectionalIterator __new_middle =
02655         std::__rotate_adaptive(__first_cut, __middle, __second_cut,
02656                    __len1 - __len11, __len22, __buffer,
02657                    __buffer_size);
02658       std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
02659                 __len22, __buffer, __buffer_size, __comp);
02660       std::__merge_adaptive(__new_middle, __second_cut, __last,
02661                 __len1 - __len11,
02662                 __len2 - __len22, __buffer,
02663                 __buffer_size, __comp);
02664     }
02665     }
02666 
02667   /// This is a helper function for the merge routines.
02668   template<typename _BidirectionalIterator, typename _Distance>
02669     void
02670     __merge_without_buffer(_BidirectionalIterator __first,
02671                _BidirectionalIterator __middle,
02672                _BidirectionalIterator __last,
02673                _Distance __len1, _Distance __len2)
02674     {
02675       if (__len1 == 0 || __len2 == 0)
02676     return;
02677       if (__len1 + __len2 == 2)
02678     {
02679       if (*__middle < *__first)
02680         std::iter_swap(__first, __middle);
02681       return;
02682     }
02683       _BidirectionalIterator __first_cut = __first;
02684       _BidirectionalIterator __second_cut = __middle;
02685       _Distance __len11 = 0;
02686       _Distance __len22 = 0;
02687       if (__len1 > __len2)
02688     {
02689       __len11 = __len1 / 2;
02690       std::advance(__first_cut, __len11);
02691       __second_cut = std::lower_bound(__middle, __last, *__first_cut);
02692       __len22 = std::distance(__middle, __second_cut);
02693     }
02694       else
02695     {
02696       __len22 = __len2 / 2;
02697       std::advance(__second_cut, __len22);
02698       __first_cut = std::upper_bound(__first, __middle, *__second_cut);
02699       __len11 = std::distance(__first, __first_cut);
02700     }
02701       std::rotate(__first_cut, __middle, __second_cut);
02702       _BidirectionalIterator __new_middle = __first_cut;
02703       std::advance(__new_middle, std::distance(__middle, __second_cut));
02704       std::__merge_without_buffer(__first, __first_cut, __new_middle,
02705                   __len11, __len22);
02706       std::__merge_without_buffer(__new_middle, __second_cut, __last,
02707                   __len1 - __len11, __len2 - __len22);
02708     }
02709 
02710   /// This is a helper function for the merge routines.
02711   template<typename _BidirectionalIterator, typename _Distance,
02712        typename _Compare>
02713     void
02714     __merge_without_buffer(_BidirectionalIterator __first,
02715                            _BidirectionalIterator __middle,
02716                _BidirectionalIterator __last,
02717                _Distance __len1, _Distance __len2,
02718                _Compare __comp)
02719     {
02720       if (__len1 == 0 || __len2 == 0)
02721     return;
02722       if (__len1 + __len2 == 2)
02723     {
02724       if (__comp(*__middle, *__first))
02725         std::iter_swap(__first, __middle);
02726       return;
02727     }
02728       _BidirectionalIterator __first_cut = __first;
02729       _BidirectionalIterator __second_cut = __middle;
02730       _Distance __len11 = 0;
02731       _Distance __len22 = 0;
02732       if (__len1 > __len2)
02733     {
02734       __len11 = __len1 / 2;
02735       std::advance(__first_cut, __len11);
02736       __second_cut = std::lower_bound(__middle, __last, *__first_cut,
02737                       __comp);
02738       __len22 = std::distance(__middle, __second_cut);
02739     }
02740       else
02741     {
02742       __len22 = __len2 / 2;
02743       std::advance(__second_cut, __len22);
02744       __first_cut = std::upper_bound(__first, __middle, *__second_cut,
02745                      __comp);
02746       __len11 = std::distance(__first, __first_cut);
02747     }
02748       std::rotate(__first_cut, __middle, __second_cut);
02749       _BidirectionalIterator __new_middle = __first_cut;
02750       std::advance(__new_middle, std::distance(__middle, __second_cut));
02751       std::__merge_without_buffer(__first, __first_cut, __new_middle,
02752                   __len11, __len22, __comp);
02753       std::__merge_without_buffer(__new_middle, __second_cut, __last,
02754                   __len1 - __len11, __len2 - __len22, __comp);
02755     }
02756 
02757   /**
02758    *  @brief Merges two sorted ranges in place.
02759    *  @param  first   An iterator.
02760    *  @param  middle  Another iterator.
02761    *  @param  last    Another iterator.
02762    *  @return  Nothing.
02763    *
02764    *  Merges two sorted and consecutive ranges, [first,middle) and
02765    *  [middle,last), and puts the result in [first,last).  The output will
02766    *  be sorted.  The sort is @e stable, that is, for equivalent
02767    *  elements in the two ranges, elements from the first range will always
02768    *  come before elements from the second.
02769    *
02770    *  If enough additional memory is available, this takes (last-first)-1
02771    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
02772    *  distance(first,last).
02773   */
02774   template<typename _BidirectionalIterator>
02775     void
02776     inplace_merge(_BidirectionalIterator __first,
02777           _BidirectionalIterator __middle,
02778           _BidirectionalIterator __last)
02779     {
02780       typedef typename iterator_traits<_BidirectionalIterator>::value_type
02781           _ValueType;
02782       typedef typename iterator_traits<_BidirectionalIterator>::difference_type
02783           _DistanceType;
02784 
02785       // concept requirements
02786       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
02787         _BidirectionalIterator>)
02788       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
02789       __glibcxx_requires_sorted(__first, __middle);
02790       __glibcxx_requires_sorted(__middle, __last);
02791 
02792       if (__first == __middle || __middle == __last)
02793     return;
02794 
02795       _DistanceType __len1 = std::distance(__first, __middle);
02796       _DistanceType __len2 = std::distance(__middle, __last);
02797 
02798       _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
02799                                   __last);
02800       if (__buf.begin() == 0)
02801     std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
02802       else
02803     std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
02804                   __buf.begin(), _DistanceType(__buf.size()));
02805     }
02806 
02807   /**
02808    *  @brief Merges two sorted ranges in place.
02809    *  @param  first   An iterator.
02810    *  @param  middle  Another iterator.
02811    *  @param  last    Another iterator.
02812    *  @param  comp    A functor to use for comparisons.
02813    *  @return  Nothing.
02814    *
02815    *  Merges two sorted and consecutive ranges, [first,middle) and
02816    *  [middle,last), and puts the result in [first,last).  The output will
02817    *  be sorted.  The sort is @e stable, that is, for equivalent
02818    *  elements in the two ranges, elements from the first range will always
02819    *  come before elements from the second.
02820    *
02821    *  If enough additional memory is available, this takes (last-first)-1
02822    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
02823    *  distance(first,last).
02824    *
02825    *  The comparison function should have the same effects on ordering as
02826    *  the function used for the initial sort.
02827   */
02828   template<typename _BidirectionalIterator, typename _Compare>
02829     void
02830     inplace_merge(_BidirectionalIterator __first,
02831           _BidirectionalIterator __middle,
02832           _BidirectionalIterator __last,
02833           _Compare __comp)
02834     {
02835       typedef typename iterator_traits<_BidirectionalIterator>::value_type
02836           _ValueType;
02837       typedef typename iterator_traits<_BidirectionalIterator>::difference_type
02838           _DistanceType;
02839 
02840       // concept requirements
02841       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
02842         _BidirectionalIterator>)
02843       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02844         _ValueType, _ValueType>)
02845       __glibcxx_requires_sorted_pred(__first, __middle, __comp);
02846       __glibcxx_requires_sorted_pred(__middle, __last, __comp);
02847 
02848       if (__first == __middle || __middle == __last)
02849     return;
02850 
02851       const _DistanceType __len1 = std::distance(__first, __middle);
02852       const _DistanceType __len2 = std::distance(__middle, __last);
02853 
02854       _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
02855                                   __last);
02856       if (__buf.begin() == 0)
02857     std::__merge_without_buffer(__first, __middle, __last, __len1,
02858                     __len2, __comp);
02859       else
02860     std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
02861                   __buf.begin(), _DistanceType(__buf.size()),
02862                   __comp);
02863     }
02864 
02865   template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
02866        typename _Distance>
02867     void
02868     __merge_sort_loop(_RandomAccessIterator1 __first,
02869               _RandomAccessIterator1 __last,
02870               _RandomAccessIterator2 __result,
02871               _Distance __step_size)
02872     {
02873       const _Distance __two_step = 2 * __step_size;
02874 
02875       while (__last - __first >= __two_step)
02876     {
02877       __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
02878                        __first + __step_size,
02879                        __first + __two_step,
02880                        __result);
02881       __first += __two_step;
02882     }
02883 
02884       __step_size = std::min(_Distance(__last - __first), __step_size);
02885       _GLIBCXX_STD_P::merge(__first, __first + __step_size, 
02886                 __first + __step_size, __last,
02887                 __result);
02888     }
02889 
02890   template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
02891        typename _Distance, typename _Compare>
02892     void
02893     __merge_sort_loop(_RandomAccessIterator1 __first,
02894               _RandomAccessIterator1 __last,
02895               _RandomAccessIterator2 __result, _Distance __step_size,
02896               _Compare __comp)
02897     {
02898       const _Distance __two_step = 2 * __step_size;
02899 
02900       while (__last - __first >= __two_step)
02901     {
02902       __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
02903                 __first + __step_size, __first + __two_step,
02904                 __result,
02905                 __comp);
02906       __first += __two_step;
02907     }
02908       __step_size = std::min(_Distance(__last - __first), __step_size);
02909 
02910       _GLIBCXX_STD_P::merge(__first, __first + __step_size,
02911                 __first + __step_size, __last, __result, __comp);
02912     }
02913 
02914   template<typename _RandomAccessIterator, typename _Distance>
02915     void
02916     __chunk_insertion_sort(_RandomAccessIterator __first,
02917                _RandomAccessIterator __last,
02918                _Distance __chunk_size)
02919     {
02920       while (__last - __first >= __chunk_size)
02921     {
02922       std::__insertion_sort(__first, __first + __chunk_size);
02923       __first += __chunk_size;
02924     }
02925       std::__insertion_sort(__first, __last);
02926     }
02927 
02928   template<typename _RandomAccessIterator, typename _Distance,
02929        typename _Compare>
02930     void
02931     __chunk_insertion_sort(_RandomAccessIterator __first,
02932                _RandomAccessIterator __last,
02933                _Distance __chunk_size, _Compare __comp)
02934     {
02935       while (__last - __first >= __chunk_size)
02936     {
02937       std::__insertion_sort(__first, __first + __chunk_size, __comp);
02938       __first += __chunk_size;
02939     }
02940       std::__insertion_sort(__first, __last, __comp);
02941     }
02942 
02943   enum { _S_chunk_size = 7 };
02944 
02945   template<typename _RandomAccessIterator, typename _Pointer>
02946     void
02947     __merge_sort_with_buffer(_RandomAccessIterator __first,
02948                  _RandomAccessIterator __last,
02949                              _Pointer __buffer)
02950     {
02951       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
02952     _Distance;
02953 
02954       const _Distance __len = __last - __first;
02955       const _Pointer __buffer_last = __buffer + __len;
02956 
02957       _Distance __step_size = _S_chunk_size;
02958       std::__chunk_insertion_sort(__first, __last, __step_size);
02959 
02960       while (__step_size < __len)
02961     {
02962       std::__merge_sort_loop(__first, __last, __buffer, __step_size);
02963       __step_size *= 2;
02964       std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
02965       __step_size *= 2;
02966     }
02967     }
02968 
02969   template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
02970     void
02971     __merge_sort_with_buffer(_RandomAccessIterator __first,
02972                  _RandomAccessIterator __last,
02973                              _Pointer __buffer, _Compare __comp)
02974     {
02975       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
02976     _Distance;
02977 
02978       const _Distance __len = __last - __first;
02979       const _Pointer __buffer_last = __buffer + __len;
02980 
02981       _Distance __step_size = _S_chunk_size;
02982       std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
02983 
02984       while (__step_size < __len)
02985     {
02986       std::__merge_sort_loop(__first, __last, __buffer,
02987                  __step_size, __comp);
02988       __step_size *= 2;
02989       std::__merge_sort_loop(__buffer, __buffer_last, __first,
02990                  __step_size, __comp);
02991       __step_size *= 2;
02992     }
02993     }
02994 
02995   template<typename _RandomAccessIterator, typename _Pointer,
02996        typename _Distance>
02997     void
02998     __stable_sort_adaptive(_RandomAccessIterator __first,
02999                _RandomAccessIterator __last,
03000                            _Pointer __buffer, _Distance __buffer_size)
03001     {
03002       const _Distance __len = (__last - __first + 1) / 2;
03003       const _RandomAccessIterator __middle = __first + __len;
03004       if (__len > __buffer_size)
03005     {
03006       std::__stable_sort_adaptive(__first, __middle,
03007                       __buffer, __buffer_size);
03008       std::__stable_sort_adaptive(__middle, __last,
03009                       __buffer, __buffer_size);
03010     }
03011       else
03012     {
03013       std::__merge_sort_with_buffer(__first, __middle, __buffer);
03014       std::__merge_sort_with_buffer(__middle, __last, __buffer);
03015     }
03016       std::__merge_adaptive(__first, __middle, __last,
03017                 _Distance(__middle - __first),
03018                 _Distance(__last - __middle),
03019                 __buffer, __buffer_size);
03020     }
03021 
03022   template<typename _RandomAccessIterator, typename _Pointer,
03023        typename _Distance, typename _Compare>
03024     void
03025     __stable_sort_adaptive(_RandomAccessIterator __first,
03026                _RandomAccessIterator __last,
03027                            _Pointer __buffer, _Distance __buffer_size,
03028                            _Compare __comp)
03029     {
03030       const _Distance __len = (__last - __first + 1) / 2;
03031       const _RandomAccessIterator __middle = __first + __len;
03032       if (__len > __buffer_size)
03033     {
03034       std::__stable_sort_adaptive(__first, __middle, __buffer,
03035                       __buffer_size, __comp);
03036       std::__stable_sort_adaptive(__middle, __last, __buffer,
03037                       __buffer_size, __comp);
03038     }
03039       else
03040     {
03041       std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
03042       std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
03043     }
03044       std::__merge_adaptive(__first, __middle, __last,
03045                 _Distance(__middle - __first),
03046                 _Distance(__last - __middle),
03047                 __buffer, __buffer_size,
03048                 __comp);
03049     }
03050 
03051   /// This is a helper function for the stable sorting routines.
03052   template<typename _RandomAccessIterator>
03053     void
03054     __inplace_stable_sort(_RandomAccessIterator __first,
03055               _RandomAccessIterator __last)
03056     {
03057       if (__last - __first < 15)
03058     {
03059       std::__insertion_sort(__first, __last);
03060       return;
03061     }
03062       _RandomAccessIterator __middle = __first + (__last - __first) / 2;
03063       std::__inplace_stable_sort(__first, __middle);
03064       std::__inplace_stable_sort(__middle, __last);
03065       std::__merge_without_buffer(__first, __middle, __last,
03066                   __middle - __first,
03067                   __last - __middle);
03068     }
03069 
03070   /// This is a helper function for the stable sorting routines.
03071   template<typename _RandomAccessIterator, typename _Compare>
03072     void
03073     __inplace_stable_sort(_RandomAccessIterator __first,
03074               _RandomAccessIterator __last, _Compare __comp)
03075     {
03076       if (__last - __first < 15)
03077     {
03078       std::__insertion_sort(__first, __last, __comp);
03079       return;
03080     }
03081       _RandomAccessIterator __middle = __first + (__last - __first) / 2;
03082       std::__inplace_stable_sort(__first, __middle, __comp);
03083       std::__inplace_stable_sort(__middle, __last, __comp);
03084       std::__merge_without_buffer(__first, __middle, __last,
03085                   __middle - __first,
03086                   __last - __middle,
03087                   __comp);
03088     }
03089 
03090   // stable_sort
03091 
03092   // Set algorithms: includes, set_union, set_intersection, set_difference,
03093   // set_symmetric_difference.  All of these algorithms have the precondition
03094   // that their input ranges are sorted and the postcondition that their output
03095   // ranges are sorted.
03096 
03097   /**
03098    *  @brief Determines whether all elements of a sequence exists in a range.
03099    *  @param  first1  Start of search range.
03100    *  @param  last1   End of search range.
03101    *  @param  first2  Start of sequence
03102    *  @param  last2   End of sequence.
03103    *  @return  True if each element in [first2,last2) is contained in order
03104    *  within [first1,last1).  False otherwise.
03105    *  @ingroup setoperations
03106    *
03107    *  This operation expects both [first1,last1) and [first2,last2) to be
03108    *  sorted.  Searches for the presence of each element in [first2,last2)
03109    *  within [first1,last1).  The iterators over each range only move forward,
03110    *  so this is a linear algorithm.  If an element in [first2,last2) is not
03111    *  found before the search iterator reaches @a last2, false is returned.
03112   */
03113   template<typename _InputIterator1, typename _InputIterator2>
03114     bool
03115     includes(_InputIterator1 __first1, _InputIterator1 __last1,
03116          _InputIterator2 __first2, _InputIterator2 __last2)
03117     {
03118       typedef typename iterator_traits<_InputIterator1>::value_type
03119     _ValueType1;
03120       typedef typename iterator_traits<_InputIterator2>::value_type
03121     _ValueType2;
03122 
03123       // concept requirements
03124       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
03125       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
03126       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
03127       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
03128       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
03129       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
03130 
03131       while (__first1 != __last1 && __first2 != __last2)
03132     if (*__first2 < *__first1)
03133       return false;
03134     else if(*__first1 < *__first2)
03135       ++__first1;
03136     else
03137       ++__first1, ++__first2;
03138 
03139       return __first2 == __last2;
03140     }
03141 
03142   /**
03143    *  @brief Determines whether all elements of a sequence exists in a range
03144    *  using comparison.
03145    *  @param  first1  Start of search range.
03146    *  @param  last1   End of search range.
03147    *  @param  first2  Start of sequence
03148    *  @param  last2   End of sequence.
03149    *  @param  comp    Comparison function to use.
03150    *  @return  True if each element in [first2,last2) is contained in order
03151    *  within [first1,last1) according to comp.  False otherwise.
03152    *  @ingroup setoperations
03153    *
03154    *  This operation expects both [first1,last1) and [first2,last2) to be
03155    *  sorted.  Searches for the presence of each element in [first2,last2)
03156    *  within [first1,last1), using comp to decide.  The iterators over each
03157    *  range only move forward, so this is a linear algorithm.  If an element
03158    *  in [first2,last2) is not found before the search iterator reaches @a
03159    *  last2, false is returned.
03160   */
03161   template<typename _InputIterator1, typename _InputIterator2,
03162        typename _Compare>
03163     bool
03164     includes(_InputIterator1 __first1, _InputIterator1 __last1,
03165          _InputIterator2 __first2, _InputIterator2 __last2,
03166          _Compare __comp)
03167     {
03168       typedef typename iterator_traits<_InputIterator1>::value_type
03169     _ValueType1;
03170       typedef typename iterator_traits<_InputIterator2>::value_type
03171     _ValueType2;
03172 
03173       // concept requirements
03174       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
03175       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
03176       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03177                   _ValueType1, _ValueType2>)
03178       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03179                   _ValueType2, _ValueType1>)
03180       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
03181       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
03182 
03183       while (__first1 != __last1 && __first2 != __last2)
03184     if (__comp(*__first2, *__first1))
03185       return false;
03186     else if(__comp(*__first1, *__first2))
03187       ++__first1;
03188     else
03189       ++__first1, ++__first2;
03190 
03191       return __first2 == __last2;
03192     }
03193 
03194   // nth_element
03195   // merge
03196   // set_difference
03197   // set_intersection
03198   // set_union
03199   // stable_sort
03200   // set_symmetric_difference
03201   // min_element
03202   // max_element
03203 
03204   /**
03205    *  @brief  Permute range into the next "dictionary" ordering.
03206    *  @param  first  Start of range.
03207    *  @param  last   End of range.
03208    *  @return  False if wrapped to first permutation, true otherwise.
03209    *
03210    *  Treats all permutations of the range as a set of "dictionary" sorted
03211    *  sequences.  Permutes the current sequence into the next one of this set.
03212    *  Returns true if there are more sequences to generate.  If the sequence
03213    *  is the largest of the set, the smallest is generated and false returned.
03214   */
03215   template<typename _BidirectionalIterator>
03216     bool
03217     next_permutation(_BidirectionalIterator __first,
03218              _BidirectionalIterator __last)
03219     {
03220       // concept requirements
03221       __glibcxx_function_requires(_BidirectionalIteratorConcept<
03222                   _BidirectionalIterator>)
03223       __glibcxx_function_requires(_LessThanComparableConcept<
03224         typename iterator_traits<_BidirectionalIterator>::value_type>)
03225       __glibcxx_requires_valid_range(__first, __last);
03226 
03227       if (__first == __last)
03228     return false;
03229       _BidirectionalIterator __i = __first;
03230       ++__i;
03231       if (__i == __last)
03232     return false;
03233       __i = __last;
03234       --__i;
03235 
03236       for(;;)
03237     {
03238       _BidirectionalIterator __ii = __i;
03239       --__i;
03240       if (*__i < *__ii)
03241         {
03242           _BidirectionalIterator __j = __last;
03243           while (!(*__i < *--__j))
03244         {}
03245           std::iter_swap(__i, __j);
03246           std::reverse(__ii, __last);
03247           return true;
03248         }
03249       if (__i == __first)
03250         {
03251           std::reverse(__first, __last);
03252           return false;
03253         }
03254     }
03255     }
03256 
03257   /**
03258    *  @brief  Permute range into the next "dictionary" ordering using
03259    *          comparison functor.
03260    *  @param  first  Start of range.
03261    *  @param  last   End of range.
03262    *  @param  comp   A comparison functor.
03263    *  @return  False if wrapped to first permutation, true otherwise.
03264    *
03265    *  Treats all permutations of the range [first,last) as a set of
03266    *  "dictionary" sorted sequences ordered by @a comp.  Permutes the current
03267    *  sequence into the next one of this set.  Returns true if there are more
03268    *  sequences to generate.  If the sequence is the largest of the set, the
03269    *  smallest is generated and false returned.
03270   */
03271   template<typename _BidirectionalIterator, typename _Compare>
03272     bool
03273     next_permutation(_BidirectionalIterator __first,
03274              _BidirectionalIterator __last, _Compare __comp)
03275     {
03276       // concept requirements
03277       __glibcxx_function_requires(_BidirectionalIteratorConcept<
03278                   _BidirectionalIterator>)
03279       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03280         typename iterator_traits<_BidirectionalIterator>::value_type,
03281         typename iterator_traits<_BidirectionalIterator>::value_type>)
03282       __glibcxx_requires_valid_range(__first, __last);
03283 
03284       if (__first == __last)
03285     return false;
03286       _BidirectionalIterator __i = __first;
03287       ++__i;
03288       if (__i == __last)
03289     return false;
03290       __i = __last;
03291       --__i;
03292 
03293       for(;;)
03294     {
03295       _BidirectionalIterator __ii = __i;
03296       --__i;
03297       if (__comp(*__i, *__ii))
03298         {
03299           _BidirectionalIterator __j = __last;
03300           while (!bool(__comp(*__i, *--__j)))
03301         {}
03302           std::iter_swap(__i, __j);
03303           std::reverse(__ii, __last);
03304           return true;
03305         }
03306       if (__i == __first)
03307         {
03308           std::reverse(__first, __last);
03309           return false;
03310         }
03311     }
03312     }
03313 
03314   /**
03315    *  @brief  Permute range into the previous "dictionary" ordering.
03316    *  @param  first  Start of range.
03317    *  @param  last   End of range.
03318    *  @return  False if wrapped to last permutation, true otherwise.
03319    *
03320    *  Treats all permutations of the range as a set of "dictionary" sorted
03321    *  sequences.  Permutes the current sequence into the previous one of this
03322    *  set.  Returns true if there are more sequences to generate.  If the
03323    *  sequence is the smallest of the set, the largest is generated and false
03324    *  returned.
03325   */
03326   template<typename _BidirectionalIterator>
03327     bool
03328     prev_permutation(_BidirectionalIterator __first,
03329              _BidirectionalIterator __last)
03330     {
03331       // concept requirements
03332       __glibcxx_function_requires(_BidirectionalIteratorConcept<
03333                   _BidirectionalIterator>)
03334       __glibcxx_function_requires(_LessThanComparableConcept<
03335         typename iterator_traits<_BidirectionalIterator>::value_type>)
03336       __glibcxx_requires_valid_range(__first, __last);
03337 
03338       if (__first == __last)
03339     return false;
03340       _BidirectionalIterator __i = __first;
03341       ++__i;
03342       if (__i == __last)
03343     return false;
03344       __i = __last;
03345       --__i;
03346 
03347       for(;;)
03348     {
03349       _BidirectionalIterator __ii = __i;
03350       --__i;
03351       if (*__ii < *__i)
03352         {
03353           _BidirectionalIterator __j = __last;
03354           while (!(*--__j < *__i))
03355         {}
03356           std::iter_swap(__i, __j);
03357           std::reverse(__ii, __last);
03358           return true;
03359         }
03360       if (__i == __first)
03361         {
03362           std::reverse(__first, __last);
03363           return false;
03364         }
03365     }
03366     }
03367 
03368   /**
03369    *  @brief  Permute range into the previous "dictionary" ordering using
03370    *          comparison functor.
03371    *  @param  first  Start of range.
03372    *  @param  last   End of range.
03373    *  @param  comp   A comparison functor.
03374    *  @return  False if wrapped to last permutation, true otherwise.
03375    *
03376    *  Treats all permutations of the range [first,last) as a set of
03377    *  "dictionary" sorted sequences ordered by @a comp.  Permutes the current
03378    *  sequence into the previous one of this set.  Returns true if there are
03379    *  more sequences to generate.  If the sequence is the smallest of the set,
03380    *  the largest is generated and false returned.
03381   */
03382   template<typename _BidirectionalIterator, typename _Compare>
03383     bool
03384     prev_permutation(_BidirectionalIterator __first,
03385              _BidirectionalIterator __last, _Compare __comp)
03386     {
03387       // concept requirements
03388       __glibcxx_function_requires(_BidirectionalIteratorConcept<
03389                   _BidirectionalIterator>)
03390       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03391         typename iterator_traits<_BidirectionalIterator>::value_type,
03392         typename iterator_traits<_BidirectionalIterator>::value_type>)
03393       __glibcxx_requires_valid_range(__first, __last);
03394 
03395       if (__first == __last)
03396     return false;
03397       _BidirectionalIterator __i = __first;
03398       ++__i;
03399       if (__i == __last)
03400     return false;
03401       __i = __last;
03402       --__i;
03403 
03404       for(;;)
03405     {
03406       _BidirectionalIterator __ii = __i;
03407       --__i;
03408       if (__comp(*__ii, *__i))
03409         {
03410           _BidirectionalIterator __j = __last;
03411           while (!bool(__comp(*--__j, *__i)))
03412         {}
03413           std::iter_swap(__i, __j);
03414           std::reverse(__ii, __last);
03415           return true;
03416         }
03417       if (__i == __first)
03418         {
03419           std::reverse(__first, __last);
03420           return false;
03421         }
03422     }
03423     }
03424 
03425   // replace
03426   // replace_if
03427 
03428   /**
03429    *  @brief Copy a sequence, replacing each element of one value with another
03430    *         value.
03431    *  @param  first      An input iterator.
03432    *  @param  last       An input iterator.
03433    *  @param  result     An output iterator.
03434    *  @param  old_value  The value to be replaced.
03435    *  @param  new_value  The replacement value.
03436    *  @return   The end of the output sequence, @p result+(last-first).
03437    *
03438    *  Copies each element in the input range @p [first,last) to the
03439    *  output range @p [result,result+(last-first)) replacing elements
03440    *  equal to @p old_value with @p new_value.
03441   */
03442   template<typename _InputIterator, typename _OutputIterator, typename _Tp>
03443     _OutputIterator
03444     replace_copy(_InputIterator __first, _InputIterator __last,
03445          _OutputIterator __result,
03446          const _Tp& __old_value, const _Tp& __new_value)
03447     {
03448       // concept requirements
03449       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03450       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
03451         typename iterator_traits<_InputIterator>::value_type>)
03452       __glibcxx_function_requires(_EqualOpConcept<
03453         typename iterator_traits<_InputIterator>::value_type, _Tp>)
03454       __glibcxx_requires_valid_range(__first, __last);
03455 
03456       for (; __first != __last; ++__first, ++__result)
03457     if (*__first == __old_value)
03458       *__result = __new_value;
03459     else
03460       *__result = *__first;
03461       return __result;
03462     }
03463 
03464   /**
03465    *  @brief Copy a sequence, replacing each value for which a predicate
03466    *         returns true with another value.
03467    *  @param  first      An input iterator.
03468    *  @param  last       An input iterator.
03469    *  @param  result     An output iterator.
03470    *  @param  pred       A predicate.
03471    *  @param  new_value  The replacement value.
03472    *  @return   The end of the output sequence, @p result+(last-first).
03473    *
03474    *  Copies each element in the range @p [first,last) to the range
03475    *  @p [result,result+(last-first)) replacing elements for which
03476    *  @p pred returns true with @p new_value.
03477   */
03478   template<typename _InputIterator, typename _OutputIterator,
03479        typename _Predicate, typename _Tp>
03480     _OutputIterator
03481     replace_copy_if(_InputIterator __first, _InputIterator __last,
03482             _OutputIterator __result,
03483             _Predicate __pred, const _Tp& __new_value)
03484     {
03485       // concept requirements
03486       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03487       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
03488         typename iterator_traits<_InputIterator>::value_type>)
03489       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
03490         typename iterator_traits<_InputIterator>::value_type>)
03491       __glibcxx_requires_valid_range(__first, __last);
03492 
03493       for (; __first != __last; ++__first, ++__result)
03494     if (__pred(*__first))
03495       *__result = __new_value;
03496     else
03497       *__result = *__first;
03498       return __result;
03499     }
03500 
03501 #ifdef __GXX_EXPERIMENTAL_CXX0X__
03502   /**
03503    *  @brief  Determines whether the elements of a sequence are sorted.
03504    *  @param  first   An iterator.
03505    *  @param  last    Another iterator.
03506    *  @return  True if the elements are sorted, false otherwise.
03507   */
03508   template<typename _ForwardIterator>
03509     inline bool
03510     is_sorted(_ForwardIterator __first, _ForwardIterator __last)
03511     { return std::is_sorted_until(__first, __last) == __last; }
03512 
03513   /**
03514    *  @brief  Determines whether the elements of a sequence are sorted
03515    *          according to a comparison functor.
03516    *  @param  first   An iterator.
03517    *  @param  last    Another iterator.
03518    *  @param  comp    A comparison functor.
03519    *  @return  True if the elements are sorted, false otherwise.
03520   */
03521   template<typename _ForwardIterator, typename _Compare>
03522     inline bool
03523     is_sorted(_ForwardIterator __first, _ForwardIterator __last,
03524           _Compare __comp)
03525     { return std::is_sorted_until(__first, __last, __comp) == __last; }
03526 
03527   /**
03528    *  @brief  Determines the end of a sorted sequence.
03529    *  @param  first   An iterator.
03530    *  @param  last    Another iterator.
03531    *  @return  An iterator pointing to the last iterator i in [first, last)
03532    *           for which the range [first, i) is sorted.
03533   */
03534   template<typename _ForwardIterator>
03535     _ForwardIterator
03536     is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
03537     {
03538       // concept requirements
03539       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03540       __glibcxx_function_requires(_LessThanComparableConcept<
03541         typename iterator_traits<_ForwardIterator>::value_type>)
03542       __glibcxx_requires_valid_range(__first, __last);
03543 
03544       if (__first == __last)
03545     return __last;
03546 
03547       _ForwardIterator __next = __first;
03548       for (++__next; __next != __last; __first = __next, ++__next)
03549     if (*__next < *__first)
03550       return __next;
03551       return __next;
03552     }
03553 
03554   /**
03555    *  @brief  Determines the end of a sorted sequence using comparison functor.
03556    *  @param  first   An iterator.
03557    *  @param  last    Another iterator.
03558    *  @param  comp    A comparison functor.
03559    *  @return  An iterator pointing to the last iterator i in [first, last)
03560    *           for which the range [first, i) is sorted.
03561   */
03562   template<typename _ForwardIterator, typename _Compare>
03563     _ForwardIterator
03564     is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
03565             _Compare __comp)
03566     {
03567       // concept requirements
03568       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03569       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03570         typename iterator_traits<_ForwardIterator>::value_type,
03571         typename iterator_traits<_ForwardIterator>::value_type>)
03572       __glibcxx_requires_valid_range(__first, __last);
03573 
03574       if (__first == __last)
03575     return __last;
03576 
03577       _ForwardIterator __next = __first;
03578       for (++__next; __next != __last; __first = __next, ++__next)
03579     if (__comp(*__next, *__first))
03580       return __next;
03581       return __next;
03582     }
03583 
03584   /**
03585    *  @brief  Determines min and max at once as an ordered pair.
03586    *  @param  a  A thing of arbitrary type.
03587    *  @param  b  Another thing of arbitrary type.
03588    *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
03589   */
03590   template<typename _Tp>
03591     inline pair<const _Tp&, const _Tp&>
03592     minmax(const _Tp& __a, const _Tp& __b)
03593     {
03594       // concept requirements
03595       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
03596 
03597       return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
03598                    : pair<const _Tp&, const _Tp&>(__a, __b);
03599     }
03600 
03601   /**
03602    *  @brief  Determines min and max at once as an ordered pair.
03603    *  @param  a  A thing of arbitrary type.
03604    *  @param  b  Another thing of arbitrary type.
03605    *  @param  comp  A @link s20_3_3_comparisons comparison functor@endlink.
03606    *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
03607   */
03608   template<typename _Tp, typename _Compare>
03609     inline pair<const _Tp&, const _Tp&>
03610     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
03611     {
03612       return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
03613                           : pair<const _Tp&, const _Tp&>(__a, __b);
03614     }
03615 
03616   /**
03617    *  @brief  Return a pair of iterators pointing to the minimum and maximum
03618    *          elements in a range.
03619    *  @param  first  Start of range.
03620    *  @param  last   End of range.
03621    *  @return  make_pair(m, M), where m is the first iterator i in 
03622    *           [first, last) such that no other element in the range is
03623    *           smaller, and where M is the last iterator i in [first, last)
03624    *           such that no other element in the range is larger.
03625   */
03626   template<typename _ForwardIterator>
03627     pair<_ForwardIterator, _ForwardIterator>
03628     minmax_element(_ForwardIterator __first, _ForwardIterator __last)
03629     {
03630       // concept requirements
03631       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03632       __glibcxx_function_requires(_LessThanComparableConcept<
03633         typename iterator_traits<_ForwardIterator>::value_type>)
03634       __glibcxx_requires_valid_range(__first, __last);
03635 
03636       _ForwardIterator __next = __first;
03637       if (__first == __last
03638       || ++__next == __last)
03639     return std::make_pair(__first, __first);
03640 
03641       _ForwardIterator __min, __max;
03642       if (*__next < *__first)
03643     {
03644       __min = __next;
03645       __max = __first;
03646     }
03647       else
03648     {
03649       __min = __first;
03650       __max = __next;
03651     }
03652 
03653       __first = __next;
03654       ++__first;
03655 
03656       while (__first != __last)
03657     {
03658       __next = __first;
03659       if (++__next == __last)
03660         {
03661           if (*__first < *__min)
03662         __min = __first;
03663           else if (!(*__first < *__max))
03664         __max = __first;
03665           break;
03666         }
03667 
03668       if (*__next < *__first)
03669         {
03670           if (*__next < *__min)
03671         __min = __next;
03672           if (!(*__first < *__max))
03673         __max = __first;
03674         }
03675       else
03676         {
03677           if (*__first < *__min)
03678         __min = __first;
03679           if (!(*__next < *__max))
03680         __max = __next;
03681         }
03682 
03683       __first = __next;
03684       ++__first;
03685     }
03686 
03687       return std::make_pair(__min, __max);
03688     }
03689 
03690   /**
03691    *  @brief  Return a pair of iterators pointing to the minimum and maximum
03692    *          elements in a range.
03693    *  @param  first  Start of range.
03694    *  @param  last   End of range.
03695    *  @param  comp   Comparison functor.
03696    *  @return  make_pair(m, M), where m is the first iterator i in 
03697    *           [first, last) such that no other element in the range is
03698    *           smaller, and where M is the last iterator i in [first, last)
03699    *           such that no other element in the range is larger.
03700   */
03701   template<typename _ForwardIterator, typename _Compare>
03702     pair<_ForwardIterator, _ForwardIterator>
03703     minmax_element(_ForwardIterator __first, _ForwardIterator __last,
03704            _Compare __comp)
03705     {
03706       // concept requirements
03707       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03708       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03709         typename iterator_traits<_ForwardIterator>::value_type,
03710         typename iterator_traits<_ForwardIterator>::value_type>)
03711       __glibcxx_requires_valid_range(__first, __last);
03712 
03713       _ForwardIterator __next = __first;
03714       if (__first == __last
03715       || ++__next == __last)
03716     return std::make_pair(__first, __first);
03717 
03718       _ForwardIterator __min, __max;
03719       if (__comp(*__next, *__first))
03720     {
03721       __min = __next;
03722       __max = __first;
03723     }
03724       else
03725     {
03726       __min = __first;
03727       __max = __next;
03728     }
03729 
03730       __first = __next;
03731       ++__first;
03732 
03733       while (__first != __last)
03734     {
03735       __next = __first;
03736       if (++__next == __last)
03737         {
03738           if (__comp(*__first, *__min))
03739         __min = __first;
03740           else if (!__comp(*__first, *__max))
03741         __max = __first;
03742           break;
03743         }
03744 
03745       if (__comp(*__next, *__first))
03746         {
03747           if (__comp(*__next, *__min))
03748         __min = __next;
03749           if (!__comp(*__first, *__max))
03750         __max = __first;
03751         }
03752       else
03753         {
03754           if (__comp(*__first, *__min))
03755         __min = __first;
03756           if (!__comp(*__next, *__max))
03757         __max = __next;
03758         }
03759 
03760       __first = __next;
03761       ++__first;
03762     }
03763 
03764       return std::make_pair(__min, __max);
03765     }
03766 #endif // __GXX_EXPERIMENTAL_CXX0X__
03767 
03768 _GLIBCXX_END_NAMESPACE
03769 
03770 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
03771 
03772   /**
03773    *  @brief Apply a function to every element of a sequence.
03774    *  @param  first  An input iterator.
03775    *  @param  last   An input iterator.
03776    *  @param  f      A unary function object.
03777    *  @return   @p f.
03778    *
03779    *  Applies the function object @p f to each element in the range
03780    *  @p [first,last).  @p f must not modify the order of the sequence.
03781    *  If @p f has a return value it is ignored.
03782   */
03783   template<typename _InputIterator, typename _Function>
03784     _Function
03785     for_each(_InputIterator __first, _InputIterator __last, _Function __f)
03786     {
03787       // concept requirements
03788       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03789       __glibcxx_requires_valid_range(__first, __last);
03790       for (; __first != __last; ++__first)
03791     __f(*__first);
03792       return __f;
03793     }
03794 
03795   /**
03796    *  @brief Find the first occurrence of a value in a sequence.
03797    *  @param  first  An input iterator.
03798    *  @param  last   An input iterator.
03799    *  @param  val    The value to find.
03800    *  @return   The first iterator @c i in the range @p [first,last)
03801    *  such that @c *i == @p val, or @p last if no such iterator exists.
03802   */
03803   template<typename _InputIterator, typename _Tp>
03804     inline _InputIterator
03805     find(_InputIterator __first, _InputIterator __last,
03806      const _Tp& __val)
03807     {
03808       // concept requirements
03809       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03810       __glibcxx_function_requires(_EqualOpConcept<
03811         typename iterator_traits<_InputIterator>::value_type, _Tp>)
03812       __glibcxx_requires_valid_range(__first, __last);
03813       return std::__find(__first, __last, __val,
03814                  std::__iterator_category(__first));
03815     }
03816 
03817   /**
03818    *  @brief Find the first element in a sequence for which a
03819    *         predicate is true.
03820    *  @param  first  An input iterator.
03821    *  @param  last   An input iterator.
03822    *  @param  pred   A predicate.
03823    *  @return   The first iterator @c i in the range @p [first,last)
03824    *  such that @p pred(*i) is true, or @p last if no such iterator exists.
03825   */
03826   template<typename _InputIterator, typename _Predicate>
03827     inline _InputIterator
03828     find_if(_InputIterator __first, _InputIterator __last,
03829         _Predicate __pred)
03830     {
03831       // concept requirements
03832       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03833       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
03834           typename iterator_traits<_InputIterator>::value_type>)
03835       __glibcxx_requires_valid_range(__first, __last);
03836       return std::__find_if(__first, __last, __pred,
03837                 std::__iterator_category(__first));
03838     }
03839 
03840   /**
03841    *  @brief  Find element from a set in a sequence.
03842    *  @param  first1  Start of range to search.
03843    *  @param  last1   End of range to search.
03844    *  @param  first2  Start of match candidates.
03845    *  @param  last2   End of match candidates.
03846    *  @return   The first iterator @c i in the range
03847    *  @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
03848    *  iterator in [first2,last2), or @p last1 if no such iterator exists.
03849    *
03850    *  Searches the range @p [first1,last1) for an element that is equal to
03851    *  some element in the range [first2,last2).  If found, returns an iterator
03852    *  in the range [first1,last1), otherwise returns @p last1.
03853   */
03854   template<typename _InputIterator, typename _ForwardIterator>
03855     _InputIterator
03856     find_first_of(_InputIterator __first1, _InputIterator __last1,
03857           _ForwardIterator __first2, _ForwardIterator __last2)
03858     {
03859       // concept requirements
03860       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03861       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03862       __glibcxx_function_requires(_EqualOpConcept<
03863         typename iterator_traits<_InputIterator>::value_type,
03864         typename iterator_traits<_ForwardIterator>::value_type>)
03865       __glibcxx_requires_valid_range(__first1, __last1);
03866       __glibcxx_requires_valid_range(__first2, __last2);
03867 
03868       for (; __first1 != __last1; ++__first1)
03869     for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
03870       if (*__first1 == *__iter)
03871         return __first1;
03872       return __last1;
03873     }
03874 
03875   /**
03876    *  @brief  Find element from a set in a sequence using a predicate.
03877    *  @param  first1  Start of range to search.
03878    *  @param  last1   End of range to search.
03879    *  @param  first2  Start of match candidates.
03880    *  @param  last2   End of match candidates.
03881    *  @param  comp    Predicate to use.
03882    *  @return   The first iterator @c i in the range
03883    *  @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
03884    *  iterator in [first2,last2), or @p last1 if no such iterator exists.
03885    *
03886 
03887    *  Searches the range @p [first1,last1) for an element that is
03888    *  equal to some element in the range [first2,last2).  If found,
03889    *  returns an iterator in the range [first1,last1), otherwise
03890    *  returns @p last1.
03891   */
03892   template<typename _InputIterator, typename _ForwardIterator,
03893        typename _BinaryPredicate>
03894     _InputIterator
03895     find_first_of(_InputIterator __first1, _InputIterator __last1,
03896           _ForwardIterator __first2, _ForwardIterator __last2,
03897           _BinaryPredicate __comp)
03898     {
03899       // concept requirements
03900       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03901       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03902       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
03903         typename iterator_traits<_InputIterator>::value_type,
03904         typename iterator_traits<_ForwardIterator>::value_type>)
03905       __glibcxx_requires_valid_range(__first1, __last1);
03906       __glibcxx_requires_valid_range(__first2, __last2);
03907 
03908       for (; __first1 != __last1; ++__first1)
03909     for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
03910       if (__comp(*__first1, *__iter))
03911         return __first1;
03912       return __last1;
03913     }
03914 
03915   /**
03916    *  @brief Find two adjacent values in a sequence that are equal.
03917    *  @param  first  A forward iterator.
03918    *  @param  last   A forward iterator.
03919    *  @return   The first iterator @c i such that @c i and @c i+1 are both
03920    *  valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
03921    *  or @p last if no such iterator exists.
03922   */
03923   template<typename _ForwardIterator>
03924     _ForwardIterator
03925     adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
03926     {
03927       // concept requirements
03928       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03929       __glibcxx_function_requires(_EqualityComparableConcept<
03930         typename iterator_traits<_ForwardIterator>::value_type>)
03931       __glibcxx_requires_valid_range(__first, __last);
03932       if (__first == __last)
03933     return __last;
03934       _ForwardIterator __next = __first;
03935       while(++__next != __last)
03936     {
03937       if (*__first == *__next)
03938         return __first;
03939       __first = __next;
03940     }
03941       return __last;
03942     }
03943 
03944   /**
03945    *  @brief Find two adjacent values in a sequence using a predicate.
03946    *  @param  first         A forward iterator.
03947    *  @param  last          A forward iterator.
03948    *  @param  binary_pred   A binary predicate.
03949    *  @return   The first iterator @c i such that @c i and @c i+1 are both
03950    *  valid iterators in @p [first,last) and such that
03951    *  @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
03952    *  exists.
03953   */
03954   template<typename _ForwardIterator, typename _BinaryPredicate>
03955     _ForwardIterator
03956     adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
03957           _BinaryPredicate __binary_pred)
03958     {
03959       // concept requirements
03960       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03961       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
03962         typename iterator_traits<_ForwardIterator>::value_type,
03963         typename iterator_traits<_ForwardIterator>::value_type>)
03964       __glibcxx_requires_valid_range(__first, __last);
03965       if (__first == __last)
03966     return __last;
03967       _ForwardIterator __next = __first;
03968       while(++__next != __last)
03969     {
03970       if (__binary_pred(*__first, *__next))
03971         return __first;
03972       __first = __next;
03973     }
03974       return __last;
03975     }
03976 
03977   /**
03978    *  @brief Count the number of copies of a value in a sequence.
03979    *  @param  first  An input iterator.
03980    *  @param  last   An input iterator.
03981    *  @param  value  The value to be counted.
03982    *  @return   The number of iterators @c i in the range @p [first,last)
03983    *  for which @c *i == @p value
03984   */
03985   template<typename _InputIterator, typename _Tp>
03986     typename iterator_traits<_InputIterator>::difference_type
03987     count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
03988     {
03989       // concept requirements
03990       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03991       __glibcxx_function_requires(_EqualOpConcept<
03992     typename iterator_traits<_InputIterator>::value_type, _Tp>)
03993       __glibcxx_requires_valid_range(__first, __last);
03994       typename iterator_traits<_InputIterator>::difference_type __n = 0;
03995       for (; __first != __last; ++__first)
03996     if (*__first == __value)
03997       ++__n;
03998       return __n;
03999     }
04000 
04001   /**
04002    *  @brief Count the elements of a sequence for which a predicate is true.
04003    *  @param  first  An input iterator.
04004    *  @param  last   An input iterator.
04005    *  @param  pred   A predicate.
04006    *  @return   The number of iterators @c i in the range @p [first,last)
04007    *  for which @p pred(*i) is true.
04008   */
04009   template<typename _InputIterator, typename _Predicate>
04010     typename iterator_traits<_InputIterator>::difference_type
04011     count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
04012     {
04013       // concept requirements
04014       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04015       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
04016         typename iterator_traits<_InputIterator>::value_type>)
04017       __glibcxx_requires_valid_range(__first, __last);
04018       typename iterator_traits<_InputIterator>::difference_type __n = 0;
04019       for (; __first != __last; ++__first)
04020     if (__pred(*__first))
04021       ++__n;
04022       return __n;
04023     }
04024 
04025   /**
04026    *  @brief Search a sequence for a matching sub-sequence.
04027    *  @param  first1  A forward iterator.
04028    *  @param  last1   A forward iterator.
04029    *  @param  first2  A forward iterator.
04030    *  @param  last2   A forward iterator.
04031    *  @return   The first iterator @c i in the range
04032    *  @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
04033    *  for each @c N in the range @p [0,last2-first2), or @p last1 if no
04034    *  such iterator exists.
04035    *
04036    *  Searches the range @p [first1,last1) for a sub-sequence that compares
04037    *  equal value-by-value with the sequence given by @p [first2,last2) and
04038    *  returns an iterator to the first element of the sub-sequence, or
04039    *  @p last1 if the sub-sequence is not found.
04040    *
04041    *  Because the sub-sequence must lie completely within the range
04042    *  @p [first1,last1) it must start at a position less than
04043    *  @p last1-(last2-first2) where @p last2-first2 is the length of the
04044    *  sub-sequence.
04045    *  This means that the returned iterator @c i will be in the range
04046    *  @p [first1,last1-(last2-first2))
04047   */
04048   template<typename _ForwardIterator1, typename _ForwardIterator2>
04049     _ForwardIterator1
04050     search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
04051        _ForwardIterator2 __first2, _ForwardIterator2 __last2)
04052     {
04053       // concept requirements
04054       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
04055       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
04056       __glibcxx_function_requires(_EqualOpConcept<
04057         typename iterator_traits<_ForwardIterator1>::value_type,
04058         typename iterator_traits<_ForwardIterator2>::value_type>)
04059       __glibcxx_requires_valid_range(__first1, __last1);
04060       __glibcxx_requires_valid_range(__first2, __last2);
04061 
04062       // Test for empty ranges
04063       if (__first1 == __last1 || __first2 == __last2)
04064     return __first1;
04065 
04066       // Test for a pattern of length 1.
04067       _ForwardIterator2 __p1(__first2);
04068       if (++__p1 == __last2)
04069     return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
04070 
04071       // General case.
04072       _ForwardIterator2 __p;
04073       _ForwardIterator1 __current = __first1;
04074 
04075       for (;;)
04076     {
04077       __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
04078       if (__first1 == __last1)
04079         return __last1;
04080 
04081       __p = __p1;
04082       __current = __first1;
04083       if (++__current == __last1)
04084         return __last1;
04085 
04086       while (*__current == *__p)
04087         {
04088           if (++__p == __last2)
04089         return __first1;
04090           if (++__current == __last1)
04091         return __last1;
04092         }
04093       ++__first1;
04094     }
04095       return __first1;
04096     }
04097 
04098   /**
04099    *  @brief Search a sequence for a matching sub-sequence using a predicate.
04100    *  @param  first1     A forward iterator.
04101    *  @param  last1      A forward iterator.
04102    *  @param  first2     A forward iterator.
04103    *  @param  last2      A forward iterator.
04104    *  @param  predicate  A binary predicate.
04105    *  @return   The first iterator @c i in the range
04106    *  @p [first1,last1-(last2-first2)) such that
04107    *  @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
04108    *  @p [0,last2-first2), or @p last1 if no such iterator exists.
04109    *
04110    *  Searches the range @p [first1,last1) for a sub-sequence that compares
04111    *  equal value-by-value with the sequence given by @p [first2,last2),
04112    *  using @p predicate to determine equality, and returns an iterator
04113    *  to the first element of the sub-sequence, or @p last1 if no such
04114    *  iterator exists.
04115    *
04116    *  @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
04117   */
04118   template<typename _ForwardIterator1, typename _ForwardIterator2,
04119        typename _BinaryPredicate>
04120     _ForwardIterator1
04121     search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
04122        _ForwardIterator2 __first2, _ForwardIterator2 __last2,
04123        _BinaryPredicate  __predicate)
04124     {
04125       // concept requirements
04126       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
04127       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
04128       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04129         typename iterator_traits<_ForwardIterator1>::value_type,
04130         typename iterator_traits<_ForwardIterator2>::value_type>)
04131       __glibcxx_requires_valid_range(__first1, __last1);
04132       __glibcxx_requires_valid_range(__first2, __last2);
04133 
04134       // Test for empty ranges
04135       if (__first1 == __last1 || __first2 == __last2)
04136     return __first1;
04137 
04138       // Test for a pattern of length 1.
04139       _ForwardIterator2 __p1(__first2);
04140       if (++__p1 == __last2)
04141     {
04142       while (__first1 != __last1
04143          && !bool(__predicate(*__first1, *__first2)))
04144         ++__first1;
04145       return __first1;
04146     }
04147 
04148       // General case.
04149       _ForwardIterator2 __p;
04150       _ForwardIterator1 __current = __first1;
04151 
04152       for (;;)
04153     {
04154       while (__first1 != __last1
04155          && !bool(__predicate(*__first1, *__first2)))
04156         ++__first1;
04157       if (__first1 == __last1)
04158         return __last1;
04159 
04160       __p = __p1;
04161       __current = __first1;
04162       if (++__current == __last1)
04163         return __last1;
04164 
04165       while (__predicate(*__current, *__p))
04166         {
04167           if (++__p == __last2)
04168         return __first1;
04169           if (++__current == __last1)
04170         return __last1;
04171         }
04172       ++__first1;
04173     }
04174       return __first1;
04175     }
04176 
04177 
04178   /**
04179    *  @brief Search a sequence for a number of consecutive values.
04180    *  @param  first  A forward iterator.
04181    *  @param  last   A forward iterator.
04182    *  @param  count  The number of consecutive values.
04183    *  @param  val    The value to find.
04184    *  @return   The first iterator @c i in the range @p [first,last-count)
04185    *  such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
04186    *  or @p last if no such iterator exists.
04187    *
04188    *  Searches the range @p [first,last) for @p count consecutive elements
04189    *  equal to @p val.
04190   */
04191   template<typename _ForwardIterator, typename _Integer, typename _Tp>
04192     _ForwardIterator
04193     search_n(_ForwardIterator __first, _ForwardIterator __last,
04194          _Integer __count, const _Tp& __val)
04195     {
04196       // concept requirements
04197       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04198       __glibcxx_function_requires(_EqualOpConcept<
04199     typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
04200       __glibcxx_requires_valid_range(__first, __last);
04201 
04202       if (__count <= 0)
04203     return __first;
04204       if (__count == 1)
04205     return _GLIBCXX_STD_P::find(__first, __last, __val);
04206       return std::__search_n(__first, __last, __count, __val,
04207                  std::__iterator_category(__first));
04208     }
04209 
04210 
04211   /**
04212    *  @brief Search a sequence for a number of consecutive values using a
04213    *         predicate.
04214    *  @param  first        A forward iterator.
04215    *  @param  last         A forward iterator.
04216    *  @param  count        The number of consecutive values.
04217    *  @param  val          The value to find.
04218    *  @param  binary_pred  A binary predicate.
04219    *  @return   The first iterator @c i in the range @p [first,last-count)
04220    *  such that @p binary_pred(*(i+N),val) is true for each @c N in the
04221    *  range @p [0,count), or @p last if no such iterator exists.
04222    *
04223    *  Searches the range @p [first,last) for @p count consecutive elements
04224    *  for which the predicate returns true.
04225   */
04226   template<typename _ForwardIterator, typename _Integer, typename _Tp,
04227            typename _BinaryPredicate>
04228     _ForwardIterator
04229     search_n(_ForwardIterator __first, _ForwardIterator __last,
04230          _Integer __count, const _Tp& __val,
04231          _BinaryPredicate __binary_pred)
04232     {
04233       // concept requirements
04234       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04235       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04236         typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
04237       __glibcxx_requires_valid_range(__first, __last);
04238 
04239       if (__count <= 0)
04240     return __first;
04241       if (__count == 1)
04242     {
04243       while (__first != __last && !bool(__binary_pred(*__first, __val)))
04244         ++__first;
04245       return __first;
04246     }
04247       return std::__search_n(__first, __last, __count, __val, __binary_pred,
04248                  std::__iterator_category(__first));
04249     }
04250 
04251 
04252   /**
04253    *  @brief Perform an operation on a sequence.
04254    *  @param  first     An input iterator.
04255    *  @param  last      An input iterator.
04256    *  @param  result    An output iterator.
04257    *  @param  unary_op  A unary operator.
04258    *  @return   An output iterator equal to @p result+(last-first).
04259    *
04260    *  Applies the operator to each element in the input range and assigns
04261    *  the results to successive elements of the output sequence.
04262    *  Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
04263    *  range @p [0,last-first).
04264    *
04265    *  @p unary_op must not alter its argument.
04266   */
04267   template<typename _InputIterator, typename _OutputIterator,
04268        typename _UnaryOperation>
04269     _OutputIterator
04270     transform(_InputIterator __first, _InputIterator __last,
04271           _OutputIterator __result, _UnaryOperation __unary_op)
04272     {
04273       // concept requirements
04274       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04275       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04276             // "the type returned by a _UnaryOperation"
04277             __typeof__(__unary_op(*__first))>)
04278       __glibcxx_requires_valid_range(__first, __last);
04279 
04280       for (; __first != __last; ++__first, ++__result)
04281     *__result = __unary_op(*__first);
04282       return __result;
04283     }
04284 
04285   /**
04286    *  @brief Perform an operation on corresponding elements of two sequences.
04287    *  @param  first1     An input iterator.
04288    *  @param  last1      An input iterator.
04289    *  @param  first2     An input iterator.
04290    *  @param  result     An output iterator.
04291    *  @param  binary_op  A binary operator.
04292    *  @return   An output iterator equal to @p result+(last-first).
04293    *
04294    *  Applies the operator to the corresponding elements in the two
04295    *  input ranges and assigns the results to successive elements of the
04296    *  output sequence.
04297    *  Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
04298    *  @c N in the range @p [0,last1-first1).
04299    *
04300    *  @p binary_op must not alter either of its arguments.
04301   */
04302   template<typename _InputIterator1, typename _InputIterator2,
04303        typename _OutputIterator, typename _BinaryOperation>
04304     _OutputIterator
04305     transform(_InputIterator1 __first1, _InputIterator1 __last1,
04306           _InputIterator2 __first2, _OutputIterator __result,
04307           _BinaryOperation __binary_op)
04308     {
04309       // concept requirements
04310       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
04311       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
04312       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04313             // "the type returned by a _BinaryOperation"
04314             __typeof__(__binary_op(*__first1,*__first2))>)
04315       __glibcxx_requires_valid_range(__first1, __last1);
04316 
04317       for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
04318     *__result = __binary_op(*__first1, *__first2);
04319       return __result;
04320     }
04321 
04322   /**
04323    *  @brief Replace each occurrence of one value in a sequence with another
04324    *         value.
04325    *  @param  first      A forward iterator.
04326    *  @param  last       A forward iterator.
04327    *  @param  old_value  The value to be replaced.
04328    *  @param  new_value  The replacement value.
04329    *  @return   replace() returns no value.
04330    *
04331    *  For each iterator @c i in the range @p [first,last) if @c *i ==
04332    *  @p old_value then the assignment @c *i = @p new_value is performed.
04333   */
04334   template<typename _ForwardIterator, typename _Tp>
04335     void
04336     replace(_ForwardIterator __first, _ForwardIterator __last,
04337         const _Tp& __old_value, const _Tp& __new_value)
04338     {
04339       // concept requirements
04340       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
04341                   _ForwardIterator>)
04342       __glibcxx_function_requires(_EqualOpConcept<
04343         typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
04344       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
04345         typename iterator_traits<_ForwardIterator>::value_type>)
04346       __glibcxx_requires_valid_range(__first, __last);
04347 
04348       for (; __first != __last; ++__first)
04349     if (*__first == __old_value)
04350       *__first = __new_value;
04351     }
04352 
04353   /**
04354    *  @brief Replace each value in a sequence for which a predicate returns
04355    *         true with another value.
04356    *  @param  first      A forward iterator.
04357    *  @param  last       A forward iterator.
04358    *  @param  pred       A predicate.
04359    *  @param  new_value  The replacement value.
04360    *  @return   replace_if() returns no value.
04361    *
04362    *  For each iterator @c i in the range @p [first,last) if @p pred(*i)
04363    *  is true then the assignment @c *i = @p new_value is performed.
04364   */
04365   template<typename _ForwardIterator, typename _Predicate, typename _Tp>
04366     void
04367     replace_if(_ForwardIterator __first, _ForwardIterator __last,
04368            _Predicate __pred, const _Tp& __new_value)
04369     {
04370       // concept requirements
04371       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
04372                   _ForwardIterator>)
04373       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
04374         typename iterator_traits<_ForwardIterator>::value_type>)
04375       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
04376         typename iterator_traits<_ForwardIterator>::value_type>)
04377       __glibcxx_requires_valid_range(__first, __last);
04378 
04379       for (; __first != __last; ++__first)
04380     if (__pred(*__first))
04381       *__first = __new_value;
04382     }
04383 
04384   /**
04385    *  @brief Assign the result of a function object to each value in a
04386    *         sequence.
04387    *  @param  first  A forward iterator.
04388    *  @param  last   A forward iterator.
04389    *  @param  gen    A function object taking no arguments and returning
04390    *                 std::iterator_traits<_ForwardIterator>::value_type
04391    *  @return   generate() returns no value.
04392    *
04393    *  Performs the assignment @c *i = @p gen() for each @c i in the range
04394    *  @p [first,last).
04395   */
04396   template<typename _ForwardIterator, typename _Generator>
04397     void
04398     generate(_ForwardIterator __first, _ForwardIterator __last,
04399          _Generator __gen)
04400     {
04401       // concept requirements
04402       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04403       __glibcxx_function_requires(_GeneratorConcept<_Generator,
04404         typename iterator_traits<_ForwardIterator>::value_type>)
04405       __glibcxx_requires_valid_range(__first, __last);
04406 
04407       for (; __first != __last; ++__first)
04408     *__first = __gen();
04409     }
04410 
04411   /**
04412    *  @brief Assign the result of a function object to each value in a
04413    *         sequence.
04414    *  @param  first  A forward iterator.
04415    *  @param  n      The length of the sequence.
04416    *  @param  gen    A function object taking no arguments and returning
04417    *                 std::iterator_traits<_ForwardIterator>::value_type
04418    *  @return   The end of the sequence, @p first+n
04419    *
04420    *  Performs the assignment @c *i = @p gen() for each @c i in the range
04421    *  @p [first,first+n).
04422   */
04423   template<typename _OutputIterator, typename _Size, typename _Generator>
04424     _OutputIterator
04425     generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
04426     {
04427       // concept requirements
04428       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04429             // "the type returned by a _Generator"
04430             __typeof__(__gen())>)
04431 
04432       for (; __n > 0; --__n, ++__first)
04433     *__first = __gen();
04434       return __first;
04435     }
04436 
04437 
04438   /**
04439    *  @brief Copy a sequence, removing consecutive duplicate values.
04440    *  @param  first   An input iterator.
04441    *  @param  last    An input iterator.
04442    *  @param  result  An output iterator.
04443    *  @return   An iterator designating the end of the resulting sequence.
04444    *
04445    *  Copies each element in the range @p [first,last) to the range
04446    *  beginning at @p result, except that only the first element is copied
04447    *  from groups of consecutive elements that compare equal.
04448    *  unique_copy() is stable, so the relative order of elements that are
04449    *  copied is unchanged.
04450    *
04451    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
04452    *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
04453    *  
04454    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
04455    *  DR 538. 241 again: Does unique_copy() require CopyConstructible and 
04456    *  Assignable?
04457   */
04458   template<typename _InputIterator, typename _OutputIterator>
04459     inline _OutputIterator
04460     unique_copy(_InputIterator __first, _InputIterator __last,
04461         _OutputIterator __result)
04462     {
04463       // concept requirements
04464       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04465       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04466         typename iterator_traits<_InputIterator>::value_type>)
04467       __glibcxx_function_requires(_EqualityComparableConcept<
04468         typename iterator_traits<_InputIterator>::value_type>)
04469       __glibcxx_requires_valid_range(__first, __last);
04470 
04471       if (__first == __last)
04472     return __result;
04473       return std::__unique_copy(__first, __last, __result,
04474                 std::__iterator_category(__first),
04475                 std::__iterator_category(__result));
04476     }
04477 
04478   /**
04479    *  @brief Copy a sequence, removing consecutive values using a predicate.
04480    *  @param  first        An input iterator.
04481    *  @param  last         An input iterator.
04482    *  @param  result       An output iterator.
04483    *  @param  binary_pred  A binary predicate.
04484    *  @return   An iterator designating the end of the resulting sequence.
04485    *
04486    *  Copies each element in the range @p [first,last) to the range
04487    *  beginning at @p result, except that only the first element is copied
04488    *  from groups of consecutive elements for which @p binary_pred returns
04489    *  true.
04490    *  unique_copy() is stable, so the relative order of elements that are
04491    *  copied is unchanged.
04492    *
04493    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
04494    *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
04495   */
04496   template<typename _InputIterator, typename _OutputIterator,
04497        typename _BinaryPredicate>
04498     inline _OutputIterator
04499     unique_copy(_InputIterator __first, _InputIterator __last,
04500         _OutputIterator __result,
04501         _BinaryPredicate __binary_pred)
04502     {
04503       // concept requirements -- predicates checked later
04504       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04505       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04506         typename iterator_traits<_InputIterator>::value_type>)
04507       __glibcxx_requires_valid_range(__first, __last);
04508 
04509       if (__first == __last)
04510     return __result;
04511       return std::__unique_copy(__first, __last, __result, __binary_pred,
04512                 std::__iterator_category(__first),
04513                 std::__iterator_category(__result));
04514     }
04515 
04516 
04517   /**
04518    *  @brief Randomly shuffle the elements of a sequence.
04519    *  @param  first   A forward iterator.
04520    *  @param  last    A forward iterator.
04521    *  @return  Nothing.
04522    *
04523    *  Reorder the elements in the range @p [first,last) using a random
04524    *  distribution, so that every possible ordering of the sequence is
04525    *  equally likely.
04526   */
04527   template<typename _RandomAccessIterator>
04528     inline void
04529     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
04530     {
04531       // concept requirements
04532       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04533         _RandomAccessIterator>)
04534       __glibcxx_requires_valid_range(__first, __last);
04535 
04536       if (__first != __last)
04537     for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
04538       std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
04539     }
04540 
04541   /**
04542    *  @brief Shuffle the elements of a sequence using a random number
04543    *         generator.
04544    *  @param  first   A forward iterator.
04545    *  @param  last    A forward iterator.
04546    *  @param  rand    The RNG functor or function.
04547    *  @return  Nothing.
04548    *
04549    *  Reorders the elements in the range @p [first,last) using @p rand to
04550    *  provide a random distribution. Calling @p rand(N) for a positive
04551    *  integer @p N should return a randomly chosen integer from the
04552    *  range [0,N).
04553   */
04554   template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
04555     void
04556     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
04557            _RandomNumberGenerator& __rand)
04558     {
04559       // concept requirements
04560       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04561         _RandomAccessIterator>)
04562       __glibcxx_requires_valid_range(__first, __last);
04563 
04564       if (__first == __last)
04565     return;
04566       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
04567     std::iter_swap(__i, __first + __rand((__i - __first) + 1));
04568     }
04569 
04570 
04571   /**
04572    *  @brief Move elements for which a predicate is true to the beginning
04573    *         of a sequence.
04574    *  @param  first   A forward iterator.
04575    *  @param  last    A forward iterator.
04576    *  @param  pred    A predicate functor.
04577    *  @return  An iterator @p middle such that @p pred(i) is true for each
04578    *  iterator @p i in the range @p [first,middle) and false for each @p i
04579    *  in the range @p [middle,last).
04580    *
04581    *  @p pred must not modify its operand. @p partition() does not preserve
04582    *  the relative ordering of elements in each group, use
04583    *  @p stable_partition() if this is needed.
04584   */
04585   template<typename _ForwardIterator, typename _Predicate>
04586     inline _ForwardIterator
04587     partition(_ForwardIterator __first, _ForwardIterator __last,
04588           _Predicate   __pred)
04589     {
04590       // concept requirements
04591       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
04592                   _ForwardIterator>)
04593       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
04594         typename iterator_traits<_ForwardIterator>::value_type>)
04595       __glibcxx_requires_valid_range(__first, __last);
04596 
04597       return std::__partition(__first, __last, __pred,
04598                   std::__iterator_category(__first));
04599     }
04600 
04601 
04602 
04603   /**
04604    *  @brief Sort the smallest elements of a sequence.
04605    *  @param  first   An iterator.
04606    *  @param  middle  Another iterator.
04607    *  @param  last    Another iterator.
04608    *  @return  Nothing.
04609    *
04610    *  Sorts the smallest @p (middle-first) elements in the range
04611    *  @p [first,last) and moves them to the range @p [first,middle). The
04612    *  order of the remaining elements in the range @p [middle,last) is
04613    *  undefined.
04614    *  After the sort if @p i and @j are iterators in the range
04615    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
04616    *  the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
04617   */
04618   template<typename _RandomAccessIterator>
04619     inline void
04620     partial_sort(_RandomAccessIterator __first,
04621          _RandomAccessIterator __middle,
04622          _RandomAccessIterator __last)
04623     {
04624       typedef typename iterator_traits<_RandomAccessIterator>::value_type
04625     _ValueType;
04626 
04627       // concept requirements
04628       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04629         _RandomAccessIterator>)
04630       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
04631       __glibcxx_requires_valid_range(__first, __middle);
04632       __glibcxx_requires_valid_range(__middle, __last);
04633 
04634       std::__heap_select(__first, __middle, __last);
04635       std::sort_heap(__first, __middle);
04636     }
04637 
04638   /**
04639    *  @brief Sort the smallest elements of a sequence using a predicate
04640    *         for comparison.
04641    *  @param  first   An iterator.
04642    *  @param  middle  Another iterator.
04643    *  @param  last    Another iterator.
04644    *  @param  comp    A comparison functor.
04645    *  @return  Nothing.
04646    *
04647    *  Sorts the smallest @p (middle-first) elements in the range
04648    *  @p [first,last) and moves them to the range @p [first,middle). The
04649    *  order of the remaining elements in the range @p [middle,last) is
04650    *  undefined.
04651    *  After the sort if @p i and @j are iterators in the range
04652    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
04653    *  the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
04654    *  are both false.
04655   */
04656   template<typename _RandomAccessIterator, typename _Compare>
04657     inline void
04658     partial_sort(_RandomAccessIterator __first,
04659          _RandomAccessIterator __middle,
04660          _RandomAccessIterator __last,
04661          _Compare __comp)
04662     {
04663       typedef typename iterator_traits<_RandomAccessIterator>::value_type
04664     _ValueType;
04665 
04666       // concept requirements
04667       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04668         _RandomAccessIterator>)
04669       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
04670                   _ValueType, _ValueType>)
04671       __glibcxx_requires_valid_range(__first, __middle);
04672       __glibcxx_requires_valid_range(__middle, __last);
04673 
04674       std::__heap_select(__first, __middle, __last, __comp);
04675       std::sort_heap(__first, __middle, __comp);
04676     }
04677 
04678   /**
04679    *  @brief Sort a sequence just enough to find a particular position.
04680    *  @param  first   An iterator.
04681    *  @param  nth     Another iterator.
04682    *  @param  last    Another iterator.
04683    *  @return  Nothing.
04684    *
04685    *  Rearranges the elements in the range @p [first,last) so that @p *nth
04686    *  is the same element that would have been in that position had the
04687    *  whole sequence been sorted.
04688    *  whole sequence been sorted. The elements either side of @p *nth are
04689    *  not completely sorted, but for any iterator @i in the range
04690    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
04691    *  holds that @p *j<*i is false.
04692   */
04693   template<typename _RandomAccessIterator>
04694     inline void
04695     nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
04696         _RandomAccessIterator __last)
04697     {
04698       typedef typename iterator_traits<_RandomAccessIterator>::value_type
04699     _ValueType;
04700 
04701       // concept requirements
04702       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04703                   _RandomAccessIterator>)
04704       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
04705       __glibcxx_requires_valid_range(__first, __nth);
04706       __glibcxx_requires_valid_range(__nth, __last);
04707 
04708       if (__first == __last || __nth == __last)
04709     return;
04710 
04711       std::__introselect(__first, __nth, __last,
04712              std::__lg(__last - __first) * 2);
04713     }
04714 
04715   /**
04716    *  @brief Sort a sequence just enough to find a particular position
04717    *         using a predicate for comparison.
04718    *  @param  first   An iterator.
04719    *  @param  nth     Another iterator.
04720    *  @param  last    Another iterator.
04721    *  @param  comp    A comparison functor.
04722    *  @return  Nothing.
04723    *
04724    *  Rearranges the elements in the range @p [first,last) so that @p *nth
04725    *  is the same element that would have been in that position had the
04726    *  whole sequence been sorted. The elements either side of @p *nth are
04727    *  not completely sorted, but for any iterator @i in the range
04728    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
04729    *  holds that @p comp(*j,*i) is false.
04730   */
04731   template<typename _RandomAccessIterator, typename _Compare>
04732     inline void
04733     nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
04734         _RandomAccessIterator __last, _Compare __comp)
04735     {
04736       typedef typename iterator_traits<_RandomAccessIterator>::value_type
04737     _ValueType;
04738 
04739       // concept requirements
04740       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04741                   _RandomAccessIterator>)
04742       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
04743                   _ValueType, _ValueType>)
04744       __glibcxx_requires_valid_range(__first, __nth);
04745       __glibcxx_requires_valid_range(__nth, __last);
04746 
04747       if (__first == __last || __nth == __last)
04748     return;
04749 
04750       std::__introselect(__first, __nth, __last,
04751              std::__lg(__last - __first) * 2, __comp);
04752     }
04753 
04754 
04755   /**
04756    *  @brief Sort the elements of a sequence.
04757    *  @param  first   An iterator.
04758    *  @param  last    Another iterator.
04759    *  @return  Nothing.
04760    *
04761    *  Sorts the elements in the range @p [first,last) in ascending order,
04762    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
04763    *  @p [first,last-1).
04764    *
04765    *  The relative ordering of equivalent elements is not preserved, use
04766    *  @p stable_sort() if this is needed.
04767   */
04768   template<typename _RandomAccessIterator>
04769     inline void
04770     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
04771     {
04772       typedef typename iterator_traits<_RandomAccessIterator>::value_type
04773     _ValueType;
04774 
04775       // concept requirements
04776       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04777         _RandomAccessIterator>)
04778       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
04779       __glibcxx_requires_valid_range(__first, __last);
04780 
04781       if (__first != __last)
04782     {
04783       std::__introsort_loop(__first, __last,
04784                 std::__lg(__last - __first) * 2);
04785       std::__final_insertion_sort(__first, __last);
04786     }
04787     }
04788 
04789   /**
04790    *  @brief Sort the elements of a sequence using a predicate for comparison.
04791    *  @param  first   An iterator.
04792    *  @param  last    Another iterator.
04793    *  @param  comp    A comparison functor.
04794    *  @return  Nothing.
04795    *
04796    *  Sorts the elements in the range @p [first,last) in ascending order,
04797    *  such that @p comp(*(i+1),*i) is false for every iterator @p i in the
04798    *  range @p [first,last-1).
04799    *
04800    *  The relative ordering of equivalent elements is not preserved, use
04801    *  @p stable_sort() if this is needed.
04802   */
04803   template<typename _RandomAccessIterator, typename _Compare>
04804     inline void
04805     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
04806      _Compare __comp)
04807     {
04808       typedef typename iterator_traits<_RandomAccessIterator>::value_type
04809     _ValueType;
04810 
04811       // concept requirements
04812       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04813         _RandomAccessIterator>)
04814       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
04815                   _ValueType>)
04816       __glibcxx_requires_valid_range(__first, __last);
04817 
04818       if (__first != __last)
04819     {
04820       std::__introsort_loop(__first, __last,
04821                 std::__lg(__last - __first) * 2, __comp);
04822       std::__final_insertion_sort(__first, __last, __comp);
04823     }
04824     }
04825 
04826   /**
04827    *  @brief Merges two sorted ranges.
04828    *  @param  first1  An iterator.
04829    *  @param  first2  Another iterator.
04830    *  @param  last1   Another iterator.
04831    *  @param  last2   Another iterator.
04832    *  @param  result  An iterator pointing to the end of the merged range.
04833    *  @return         An iterator pointing to the first element "not less
04834    *                  than" @a val.
04835    *
04836    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
04837    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
04838    *  must be sorted, and the output range must not overlap with either of
04839    *  the input ranges.  The sort is @e stable, that is, for equivalent
04840    *  elements in the two ranges, elements from the first range will always
04841    *  come before elements from the second.
04842   */
04843   template<typename _InputIterator1, typename _InputIterator2,
04844        typename _OutputIterator>
04845     _OutputIterator
04846     merge(_InputIterator1 __first1, _InputIterator1 __last1,
04847       _InputIterator2 __first2, _InputIterator2 __last2,
04848       _OutputIterator __result)
04849     {
04850       typedef typename iterator_traits<_InputIterator1>::value_type
04851     _ValueType1;
04852       typedef typename iterator_traits<_InputIterator2>::value_type
04853     _ValueType2;
04854 
04855       // concept requirements
04856       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
04857       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
04858       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04859                   _ValueType1>)
04860       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04861                   _ValueType2>)
04862       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
04863       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
04864       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
04865 
04866       while (__first1 != __last1 && __first2 != __last2)
04867     {
04868       if (*__first2 < *__first1)
04869         {
04870           *__result = *__first2;
04871           ++__first2;
04872         }
04873       else
04874         {
04875           *__result = *__first1;
04876           ++__first1;
04877         }
04878       ++__result;
04879     }
04880       return std::copy(__first2, __last2, std::copy(__first1, __last1,
04881                             __result));
04882     }
04883 
04884   /**
04885    *  @brief Merges two sorted ranges.
04886    *  @param  first1  An iterator.
04887    *  @param  first2  Another iterator.
04888    *  @param  last1   Another iterator.
04889    *  @param  last2   Another iterator.
04890    *  @param  result  An iterator pointing to the end of the merged range.
04891    *  @param  comp    A functor to use for comparisons.
04892    *  @return         An iterator pointing to the first element "not less
04893    *                  than" @a val.
04894    *
04895    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
04896    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
04897    *  must be sorted, and the output range must not overlap with either of
04898    *  the input ranges.  The sort is @e stable, that is, for equivalent
04899    *  elements in the two ranges, elements from the first range will always
04900    *  come before elements from the second.
04901    *
04902    *  The comparison function should have the same effects on ordering as
04903    *  the function used for the initial sort.
04904   */
04905   template<typename _InputIterator1, typename _InputIterator2,
04906        typename _OutputIterator, typename _Compare>
04907     _OutputIterator
04908     merge(_InputIterator1 __first1, _InputIterator1 __last1,
04909       _InputIterator2 __first2, _InputIterator2 __last2,
04910       _OutputIterator __result, _Compare __comp)
04911     {
04912       typedef typename iterator_traits<_InputIterator1>::value_type
04913     _ValueType1;
04914       typedef typename iterator_traits<_InputIterator2>::value_type
04915     _ValueType2;
04916 
04917       // concept requirements
04918       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
04919       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
04920       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04921                   _ValueType1>)
04922       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04923                   _ValueType2>)
04924       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
04925                   _ValueType2, _ValueType1>)
04926       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
04927       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
04928 
04929       while (__first1 != __last1 && __first2 != __last2)
04930     {
04931       if (__comp(*__first2, *__first1))
04932         {
04933           *__result = *__first2;
04934           ++__first2;
04935         }
04936       else
04937         {
04938           *__result = *__first1;
04939           ++__first1;
04940         }
04941       ++__result;
04942     }
04943       return std::copy(__first2, __last2, std::copy(__first1, __last1,
04944                             __result));
04945     }
04946 
04947 
04948   /**
04949    *  @brief Sort the elements of a sequence, preserving the relative order
04950    *         of equivalent elements.
04951    *  @param  first   An iterator.
04952    *  @param  last    Another iterator.
04953    *  @return  Nothing.
04954    *
04955    *  Sorts the elements in the range @p [first,last) in ascending order,
04956    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
04957    *  @p [first,last-1).
04958    *
04959    *  The relative ordering of equivalent elements is preserved, so any two
04960    *  elements @p x and @p y in the range @p [first,last) such that
04961    *  @p x<y is false and @p y<x is false will have the same relative
04962    *  ordering after calling @p stable_sort().
04963   */
04964   template<typename _RandomAccessIterator>
04965     inline void
04966     stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
04967     {
04968       typedef typename iterator_traits<_RandomAccessIterator>::value_type
04969     _ValueType;
04970       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
04971     _DistanceType;
04972 
04973       // concept requirements
04974       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04975         _RandomAccessIterator>)
04976       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
04977       __glibcxx_requires_valid_range(__first, __last);
04978 
04979       _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
04980                                  __last);
04981       if (__buf.begin() == 0)
04982     std::__inplace_stable_sort(__first, __last);
04983       else
04984     std::__stable_sort_adaptive(__first, __last, __buf.begin(),
04985                     _DistanceType(__buf.size()));
04986     }
04987 
04988   /**
04989    *  @brief Sort the elements of a sequence using a predicate for comparison,
04990    *         preserving the relative order of equivalent elements.
04991    *  @param  first   An iterator.
04992    *  @param  last    Another iterator.
04993    *  @param  comp    A comparison functor.
04994    *  @return  Nothing.
04995    *
04996    *  Sorts the elements in the range @p [first,last) in ascending order,
04997    *  such that @p comp(*(i+1),*i) is false for each iterator @p i in the
04998    *  range @p [first,last-1).
04999    *
05000    *  The relative ordering of equivalent elements is preserved, so any two
05001    *  elements @p x and @p y in the range @p [first,last) such that
05002    *  @p comp(x,y) is false and @p comp(y,x) is false will have the same
05003    *  relative ordering after calling @p stable_sort().
05004   */
05005   template<typename _RandomAccessIterator, typename _Compare>
05006     inline void
05007     stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
05008         _Compare __comp)
05009     {
05010       typedef typename iterator_traits<_RandomAccessIterator>::value_type
05011     _ValueType;
05012       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
05013     _DistanceType;
05014 
05015       // concept requirements
05016       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05017         _RandomAccessIterator>)
05018       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05019                   _ValueType,
05020                   _ValueType>)
05021       __glibcxx_requires_valid_range(__first, __last);
05022 
05023       _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
05024                                  __last);
05025       if (__buf.begin() == 0)
05026     std::__inplace_stable_sort(__first, __last, __comp);
05027       else
05028     std::__stable_sort_adaptive(__first, __last, __buf.begin(),
05029                     _DistanceType(__buf.size()), __comp);
05030     }
05031 
05032 
05033   /**
05034    *  @brief Return the union of two sorted ranges.
05035    *  @param  first1  Start of first range.
05036    *  @param  last1   End of first range.
05037    *  @param  first2  Start of second range.
05038    *  @param  last2   End of second range.
05039    *  @return  End of the output range.
05040    *  @ingroup setoperations
05041    *
05042    *  This operation iterates over both ranges, copying elements present in
05043    *  each range in order to the output range.  Iterators increment for each
05044    *  range.  When the current element of one range is less than the other,
05045    *  that element is copied and the iterator advanced.  If an element is
05046    *  contained in both ranges, the element from the first range is copied and
05047    *  both ranges advance.  The output range may not overlap either input
05048    *  range.
05049   */
05050   template<typename _InputIterator1, typename _InputIterator2,
05051        typename _OutputIterator>
05052     _OutputIterator
05053     set_union(_InputIterator1 __first1, _InputIterator1 __last1,
05054           _InputIterator2 __first2, _InputIterator2 __last2,
05055           _OutputIterator __result)
05056     {
05057       typedef typename iterator_traits<_InputIterator1>::value_type
05058     _ValueType1;
05059       typedef typename iterator_traits<_InputIterator2>::value_type
05060     _ValueType2;
05061 
05062       // concept requirements
05063       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05064       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05065       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05066                   _ValueType1>)
05067       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05068                   _ValueType2>)
05069       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05070       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
05071       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05072       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05073 
05074       while (__first1 != __last1 && __first2 != __last2)
05075     {
05076       if (*__first1 < *__first2)
05077         {
05078           *__result = *__first1;
05079           ++__first1;
05080         }
05081       else if (*__first2 < *__first1)
05082         {
05083           *__result = *__first2;
05084           ++__first2;
05085         }
05086       else
05087         {
05088           *__result = *__first1;
05089           ++__first1;
05090           ++__first2;
05091         }
05092       ++__result;
05093     }
05094       return std::copy(__first2, __last2, std::copy(__first1, __last1,
05095                             __result));
05096     }
05097 
05098   /**
05099    *  @brief Return the union of two sorted ranges using a comparison functor.
05100    *  @param  first1  Start of first range.
05101    *  @param  last1   End of first range.
05102    *  @param  first2  Start of second range.
05103    *  @param  last2   End of second range.
05104    *  @param  comp    The comparison functor.
05105    *  @return  End of the output range.
05106    *  @ingroup setoperations
05107    *
05108    *  This operation iterates over both ranges, copying elements present in
05109    *  each range in order to the output range.  Iterators increment for each
05110    *  range.  When the current element of one range is less than the other
05111    *  according to @a comp, that element is copied and the iterator advanced.
05112    *  If an equivalent element according to @a comp is contained in both
05113    *  ranges, the element from the first range is copied and both ranges
05114    *  advance.  The output range may not overlap either input range.
05115   */
05116   template<typename _InputIterator1, typename _InputIterator2,
05117        typename _OutputIterator, typename _Compare>
05118     _OutputIterator
05119     set_union(_InputIterator1 __first1, _InputIterator1 __last1,
05120           _InputIterator2 __first2, _InputIterator2 __last2,
05121           _OutputIterator __result, _Compare __comp)
05122     {
05123       typedef typename iterator_traits<_InputIterator1>::value_type
05124     _ValueType1;
05125       typedef typename iterator_traits<_InputIterator2>::value_type
05126     _ValueType2;
05127 
05128       // concept requirements
05129       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05130       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05131       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05132                   _ValueType1>)
05133       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05134                   _ValueType2>)
05135       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05136                   _ValueType1, _ValueType2>)
05137       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05138                   _ValueType2, _ValueType1>)
05139       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05140       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05141 
05142       while (__first1 != __last1 && __first2 != __last2)
05143     {
05144       if (__comp(*__first1, *__first2))
05145         {
05146           *__result = *__first1;
05147           ++__first1;
05148         }
05149       else if (__comp(*__first2, *__first1))
05150         {
05151           *__result = *__first2;
05152           ++__first2;
05153         }
05154       else
05155         {
05156           *__result = *__first1;
05157           ++__first1;
05158           ++__first2;
05159         }
05160       ++__result;
05161     }
05162       return std::copy(__first2, __last2, std::copy(__first1, __last1,
05163                             __result));
05164     }
05165 
05166   /**
05167    *  @brief Return the intersection of two sorted ranges.
05168    *  @param  first1  Start of first range.
05169    *  @param  last1   End of first range.
05170    *  @param  first2  Start of second range.
05171    *  @param  last2   End of second range.
05172    *  @return  End of the output range.
05173    *  @ingroup setoperations
05174    *
05175    *  This operation iterates over both ranges, copying elements present in
05176    *  both ranges in order to the output range.  Iterators increment for each
05177    *  range.  When the current element of one range is less than the other,
05178    *  that iterator advances.  If an element is contained in both ranges, the
05179    *  element from the first range is copied and both ranges advance.  The
05180    *  output range may not overlap either input range.
05181   */
05182   template<typename _InputIterator1, typename _InputIterator2,
05183        typename _OutputIterator>
05184     _OutputIterator
05185     set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
05186              _InputIterator2 __first2, _InputIterator2 __last2,
05187              _OutputIterator __result)
05188     {
05189       typedef typename iterator_traits<_InputIterator1>::value_type
05190     _ValueType1;
05191       typedef typename iterator_traits<_InputIterator2>::value_type
05192     _ValueType2;
05193 
05194       // concept requirements
05195       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05196       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05197       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05198                   _ValueType1>)
05199       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05200       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
05201       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05202       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05203 
05204       while (__first1 != __last1 && __first2 != __last2)
05205     if (*__first1 < *__first2)
05206       ++__first1;
05207     else if (*__first2 < *__first1)
05208       ++__first2;
05209     else
05210       {
05211         *__result = *__first1;
05212         ++__first1;
05213         ++__first2;
05214         ++__result;
05215       }
05216       return __result;
05217     }
05218 
05219   /**
05220    *  @brief Return the intersection of two sorted ranges using comparison
05221    *  functor.
05222    *  @param  first1  Start of first range.
05223    *  @param  last1   End of first range.
05224    *  @param  first2  Start of second range.
05225    *  @param  last2   End of second range.
05226    *  @param  comp    The comparison functor.
05227    *  @return  End of the output range.
05228    *  @ingroup setoperations
05229    *
05230    *  This operation iterates over both ranges, copying elements present in
05231    *  both ranges in order to the output range.  Iterators increment for each
05232    *  range.  When the current element of one range is less than the other
05233    *  according to @a comp, that iterator advances.  If an element is
05234    *  contained in both ranges according to @a comp, the element from the
05235    *  first range is copied and both ranges advance.  The output range may not
05236    *  overlap either input range.
05237   */
05238   template<typename _InputIterator1, typename _InputIterator2,
05239        typename _OutputIterator, typename _Compare>
05240     _OutputIterator
05241     set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
05242              _InputIterator2 __first2, _InputIterator2 __last2,
05243              _OutputIterator __result, _Compare __comp)
05244     {
05245       typedef typename iterator_traits<_InputIterator1>::value_type
05246     _ValueType1;
05247       typedef typename iterator_traits<_InputIterator2>::value_type
05248     _ValueType2;
05249 
05250       // concept requirements
05251       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05252       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05253       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05254                   _ValueType1>)
05255       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05256                   _ValueType1, _ValueType2>)
05257       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05258                   _ValueType2, _ValueType1>)
05259       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05260       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05261 
05262       while (__first1 != __last1 && __first2 != __last2)
05263     if (__comp(*__first1, *__first2))
05264       ++__first1;
05265     else if (__comp(*__first2, *__first1))
05266       ++__first2;
05267     else
05268       {
05269         *__result = *__first1;
05270         ++__first1;
05271         ++__first2;
05272         ++__result;
05273       }
05274       return __result;
05275     }
05276 
05277   /**
05278    *  @brief Return the difference of two sorted ranges.
05279    *  @param  first1  Start of first range.
05280    *  @param  last1   End of first range.
05281    *  @param  first2  Start of second range.
05282    *  @param  last2   End of second range.
05283    *  @return  End of the output range.
05284    *  @ingroup setoperations
05285    *
05286    *  This operation iterates over both ranges, copying elements present in
05287    *  the first range but not the second in order to the output range.
05288    *  Iterators increment for each range.  When the current element of the
05289    *  first range is less than the second, that element is copied and the
05290    *  iterator advances.  If the current element of the second range is less,
05291    *  the iterator advances, but no element is copied.  If an element is
05292    *  contained in both ranges, no elements are copied and both ranges
05293    *  advance.  The output range may not overlap either input range.
05294   */
05295   template<typename _InputIterator1, typename _InputIterator2,
05296        typename _OutputIterator>
05297     _OutputIterator
05298     set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05299            _InputIterator2 __first2, _InputIterator2 __last2,
05300            _OutputIterator __result)
05301     {
05302       typedef typename iterator_traits<_InputIterator1>::value_type
05303     _ValueType1;
05304       typedef typename iterator_traits<_InputIterator2>::value_type
05305     _ValueType2;
05306 
05307       // concept requirements
05308       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05309       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05310       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05311                   _ValueType1>)
05312       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05313       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
05314       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05315       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05316 
05317       while (__first1 != __last1 && __first2 != __last2)
05318     if (*__first1 < *__first2)
05319       {
05320         *__result = *__first1;
05321         ++__first1;
05322         ++__result;
05323       }
05324     else if (*__first2 < *__first1)
05325       ++__first2;
05326     else
05327       {
05328         ++__first1;
05329         ++__first2;
05330       }
05331       return std::copy(__first1, __last1, __result);
05332     }
05333 
05334   /**
05335    *  @brief  Return the difference of two sorted ranges using comparison
05336    *  functor.
05337    *  @param  first1  Start of first range.
05338    *  @param  last1   End of first range.
05339    *  @param  first2  Start of second range.
05340    *  @param  last2   End of second range.
05341    *  @param  comp    The comparison functor.
05342    *  @return  End of the output range.
05343    *  @ingroup setoperations
05344    *
05345    *  This operation iterates over both ranges, copying elements present in
05346    *  the first range but not the second in order to the output range.
05347    *  Iterators increment for each range.  When the current element of the
05348    *  first range is less than the second according to @a comp, that element
05349    *  is copied and the iterator advances.  If the current element of the
05350    *  second range is less, no element is copied and the iterator advances.
05351    *  If an element is contained in both ranges according to @a comp, no
05352    *  elements are copied and both ranges advance.  The output range may not
05353    *  overlap either input range.
05354   */
05355   template<typename _InputIterator1, typename _InputIterator2,
05356        typename _OutputIterator, typename _Compare>
05357     _OutputIterator
05358     set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05359            _InputIterator2 __first2, _InputIterator2 __last2,
05360            _OutputIterator __result, _Compare __comp)
05361     {
05362       typedef typename iterator_traits<_InputIterator1>::value_type
05363     _ValueType1;
05364       typedef typename iterator_traits<_InputIterator2>::value_type
05365     _ValueType2;
05366 
05367       // concept requirements
05368       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05369       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05370       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05371                   _ValueType1>)
05372       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05373                   _ValueType1, _ValueType2>)
05374       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05375                   _ValueType2, _ValueType1>)
05376       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05377       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05378 
05379       while (__first1 != __last1 && __first2 != __last2)
05380     if (__comp(*__first1, *__first2))
05381       {
05382         *__result = *__first1;
05383         ++__first1;
05384         ++__result;
05385       }
05386     else if (__comp(*__first2, *__first1))
05387       ++__first2;
05388     else
05389       {
05390         ++__first1;
05391         ++__first2;
05392       }
05393       return std::copy(__first1, __last1, __result);
05394     }
05395 
05396   /**
05397    *  @brief  Return the symmetric difference of two sorted ranges.
05398    *  @param  first1  Start of first range.
05399    *  @param  last1   End of first range.
05400    *  @param  first2  Start of second range.
05401    *  @param  last2   End of second range.
05402    *  @return  End of the output range.
05403    *  @ingroup setoperations
05404    *
05405    *  This operation iterates over both ranges, copying elements present in
05406    *  one range but not the other in order to the output range.  Iterators
05407    *  increment for each range.  When the current element of one range is less
05408    *  than the other, that element is copied and the iterator advances.  If an
05409    *  element is contained in both ranges, no elements are copied and both
05410    *  ranges advance.  The output range may not overlap either input range.
05411   */
05412   template<typename _InputIterator1, typename _InputIterator2,
05413        typename _OutputIterator>
05414     _OutputIterator
05415     set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05416                  _InputIterator2 __first2, _InputIterator2 __last2,
05417                  _OutputIterator __result)
05418     {
05419       typedef typename iterator_traits<_InputIterator1>::value_type
05420     _ValueType1;
05421       typedef typename iterator_traits<_InputIterator2>::value_type
05422     _ValueType2;
05423 
05424       // concept requirements
05425       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05426       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05427       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05428                   _ValueType1>)
05429       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05430                   _ValueType2>)
05431       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05432       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
05433       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05434       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05435 
05436       while (__first1 != __last1 && __first2 != __last2)
05437     if (*__first1 < *__first2)
05438       {
05439         *__result = *__first1;
05440         ++__first1;
05441         ++__result;
05442       }
05443     else if (*__first2 < *__first1)
05444       {
05445         *__result = *__first2;
05446         ++__first2;
05447         ++__result;
05448       }
05449     else
05450       {
05451         ++__first1;
05452         ++__first2;
05453       }
05454       return std::copy(__first2, __last2, std::copy(__first1,
05455                             __last1, __result));
05456     }
05457 
05458   /**
05459    *  @brief  Return the symmetric difference of two sorted ranges using
05460    *  comparison functor.
05461    *  @param  first1  Start of first range.
05462    *  @param  last1   End of first range.
05463    *  @param  first2  Start of second range.
05464    *  @param  last2   End of second range.
05465    *  @param  comp    The comparison functor.
05466    *  @return  End of the output range.
05467    *  @ingroup setoperations
05468    *
05469    *  This operation iterates over both ranges, copying elements present in
05470    *  one range but not the other in order to the output range.  Iterators
05471    *  increment for each range.  When the current element of one range is less
05472    *  than the other according to @a comp, that element is copied and the
05473    *  iterator advances.  If an element is contained in both ranges according
05474    *  to @a comp, no elements are copied and both ranges advance.  The output
05475    *  range may not overlap either input range.
05476   */
05477   template<typename _InputIterator1, typename _InputIterator2,
05478        typename _OutputIterator, typename _Compare>
05479     _OutputIterator
05480     set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05481                  _InputIterator2 __first2, _InputIterator2 __last2,
05482                  _OutputIterator __result,
05483                  _Compare __comp)
05484     {
05485       typedef typename iterator_traits<_InputIterator1>::value_type
05486     _ValueType1;
05487       typedef typename iterator_traits<_InputIterator2>::value_type
05488     _ValueType2;
05489 
05490       // concept requirements
05491       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05492       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05493       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05494                   _ValueType1>)
05495       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05496                   _ValueType2>)
05497       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05498                   _ValueType1, _ValueType2>)
05499       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05500                   _ValueType2, _ValueType1>)
05501       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05502       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05503 
05504       while (__first1 != __last1 && __first2 != __last2)
05505     if (__comp(*__first1, *__first2))
05506       {
05507         *__result = *__first1;
05508         ++__first1;
05509         ++__result;
05510       }
05511     else if (__comp(*__first2, *__first1))
05512       {
05513         *__result = *__first2;
05514         ++__first2;
05515         ++__result;
05516       }
05517     else
05518       {
05519         ++__first1;
05520         ++__first2;
05521       }
05522       return std::copy(__first2, __last2, 
05523                std::copy(__first1, __last1, __result));
05524     }
05525 
05526 
05527   /**
05528    *  @brief  Return the minimum element in a range.
05529    *  @param  first  Start of range.
05530    *  @param  last   End of range.
05531    *  @return  Iterator referencing the first instance of the smallest value.
05532   */
05533   template<typename _ForwardIterator>
05534     _ForwardIterator
05535     min_element(_ForwardIterator __first, _ForwardIterator __last)
05536     {
05537       // concept requirements
05538       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
05539       __glibcxx_function_requires(_LessThanComparableConcept<
05540         typename iterator_traits<_ForwardIterator>::value_type>)
05541       __glibcxx_requires_valid_range(__first, __last);
05542 
05543       if (__first == __last)
05544     return __first;
05545       _ForwardIterator __result = __first;
05546       while (++__first != __last)
05547     if (*__first < *__result)
05548       __result = __first;
05549       return __result;
05550     }
05551 
05552   /**
05553    *  @brief  Return the minimum element in a range using comparison functor.
05554    *  @param  first  Start of range.
05555    *  @param  last   End of range.
05556    *  @param  comp   Comparison functor.
05557    *  @return  Iterator referencing the first instance of the smallest value
05558    *  according to comp.
05559   */
05560   template<typename _ForwardIterator, typename _Compare>
05561     _ForwardIterator
05562     min_element(_ForwardIterator __first, _ForwardIterator __last,
05563         _Compare __comp)
05564     {
05565       // concept requirements
05566       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
05567       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05568         typename iterator_traits<_ForwardIterator>::value_type,
05569         typename iterator_traits<_ForwardIterator>::value_type>)
05570       __glibcxx_requires_valid_range(__first, __last);
05571 
05572       if (__first == __last)
05573     return __first;
05574       _ForwardIterator __result = __first;
05575       while (++__first != __last)
05576     if (__comp(*__first, *__result))
05577       __result = __first;
05578       return __result;
05579     }
05580 
05581   /**
05582    *  @brief  Return the maximum element in a range.
05583    *  @param  first  Start of range.
05584    *  @param  last   End of range.
05585    *  @return  Iterator referencing the first instance of the largest value.
05586   */
05587   template<typename _ForwardIterator>
05588     _ForwardIterator
05589     max_element(_ForwardIterator __first, _ForwardIterator __last)
05590     {
05591       // concept requirements
05592       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
05593       __glibcxx_function_requires(_LessThanComparableConcept<
05594         typename iterator_traits<_ForwardIterator>::value_type>)
05595       __glibcxx_requires_valid_range(__first, __last);
05596 
05597       if (__first == __last)
05598     return __first;
05599       _ForwardIterator __result = __first;
05600       while (++__first != __last)
05601     if (*__result < *__first)
05602       __result = __first;
05603       return __result;
05604     }
05605 
05606   /**
05607    *  @brief  Return the maximum element in a range using comparison functor.
05608    *  @param  first  Start of range.
05609    *  @param  last   End of range.
05610    *  @param  comp   Comparison functor.
05611    *  @return  Iterator referencing the first instance of the largest value
05612    *  according to comp.
05613   */
05614   template<typename _ForwardIterator, typename _Compare>
05615     _ForwardIterator
05616     max_element(_ForwardIterator __first, _ForwardIterator __last,
05617         _Compare __comp)
05618     {
05619       // concept requirements
05620       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
05621       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05622         typename iterator_traits<_ForwardIterator>::value_type,
05623         typename iterator_traits<_ForwardIterator>::value_type>)
05624       __glibcxx_requires_valid_range(__first, __last);
05625 
05626       if (__first == __last) return __first;
05627       _ForwardIterator __result = __first;
05628       while (++__first != __last)
05629     if (__comp(*__result, *__first))
05630       __result = __first;
05631       return __result;
05632     }
05633 
05634 _GLIBCXX_END_NESTED_NAMESPACE
05635 
05636 #endif /* _STL_ALGO_H */

Generated on Wed Mar 26 00:43:13 2008 for libstdc++ by  doxygen 1.5.1