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

Generated on 12 Mar 2010 for libstdc++ by  doxygen 1.6.1