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