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 
02407       while (__len > 0)
02408     {
02409       _DistanceType __half = __len >> 1;
02410       _ForwardIterator __middle = __first;
02411       std::advance(__middle, __half);
02412       if (__comp(*__middle, __val))
02413         {
02414           __first = __middle;
02415           ++__first;
02416           __len = __len - __half - 1;
02417         }
02418       else
02419         __len = __half;
02420     }
02421       return __first;
02422     }
02423 
02424   /**
02425    *  @brief Finds the last position in which @a val could be inserted
02426    *         without changing the ordering.
02427    *  @ingroup binary_search_algorithms
02428    *  @param  first   An iterator.
02429    *  @param  last    Another iterator.
02430    *  @param  val     The search term.
02431    *  @return  An iterator pointing to the first element greater than @a val,
02432    *           or end() if no elements are greater than @a val.
02433    *  @ingroup binary_search_algorithms
02434   */
02435   template<typename _ForwardIterator, typename _Tp>
02436     _ForwardIterator
02437     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
02438         const _Tp& __val)
02439     {
02440       typedef typename iterator_traits<_ForwardIterator>::value_type
02441     _ValueType;
02442       typedef typename iterator_traits<_ForwardIterator>::difference_type
02443     _DistanceType;
02444 
02445       // concept requirements
02446       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02447       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
02448       __glibcxx_requires_partitioned_upper(__first, __last, __val);
02449 
02450       _DistanceType __len = std::distance(__first, __last);
02451 
02452       while (__len > 0)
02453     {
02454       _DistanceType __half = __len >> 1;
02455       _ForwardIterator __middle = __first;
02456       std::advance(__middle, __half);
02457       if (__val < *__middle)
02458         __len = __half;
02459       else
02460         {
02461           __first = __middle;
02462           ++__first;
02463           __len = __len - __half - 1;
02464         }
02465     }
02466       return __first;
02467     }
02468 
02469   /**
02470    *  @brief Finds the last position in which @a val could be inserted
02471    *         without changing the ordering.
02472    *  @ingroup binary_search_algorithms
02473    *  @param  first   An iterator.
02474    *  @param  last    Another iterator.
02475    *  @param  val     The search term.
02476    *  @param  comp    A functor to use for comparisons.
02477    *  @return  An iterator pointing to the first element greater than @a val,
02478    *           or end() if no elements are greater than @a val.
02479    *  @ingroup binary_search_algorithms
02480    *
02481    *  The comparison function should have the same effects on ordering as
02482    *  the function used for the initial sort.
02483   */
02484   template<typename _ForwardIterator, typename _Tp, typename _Compare>
02485     _ForwardIterator
02486     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
02487         const _Tp& __val, _Compare __comp)
02488     {
02489       typedef typename iterator_traits<_ForwardIterator>::value_type
02490     _ValueType;
02491       typedef typename iterator_traits<_ForwardIterator>::difference_type
02492     _DistanceType;
02493 
02494       // concept requirements
02495       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02496       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02497                   _Tp, _ValueType>)
02498       __glibcxx_requires_partitioned_upper_pred(__first, __last,
02499                         __val, __comp);
02500 
02501       _DistanceType __len = std::distance(__first, __last);
02502 
02503       while (__len > 0)
02504     {
02505       _DistanceType __half = __len >> 1;
02506       _ForwardIterator __middle = __first;
02507       std::advance(__middle, __half);
02508       if (__comp(__val, *__middle))
02509         __len = __half;
02510       else
02511         {
02512           __first = __middle;
02513           ++__first;
02514           __len = __len - __half - 1;
02515         }
02516     }
02517       return __first;
02518     }
02519 
02520   /**
02521    *  @brief Finds the largest subrange in which @a val could be inserted
02522    *         at any place in it without changing the ordering.
02523    *  @ingroup binary_search_algorithms
02524    *  @param  first   An iterator.
02525    *  @param  last    Another iterator.
02526    *  @param  val     The search term.
02527    *  @return  An pair of iterators defining the subrange.
02528    *  @ingroup binary_search_algorithms
02529    *
02530    *  This is equivalent to
02531    *  @code
02532    *    std::make_pair(lower_bound(first, last, val),
02533    *                   upper_bound(first, last, val))
02534    *  @endcode
02535    *  but does not actually call those functions.
02536   */
02537   template<typename _ForwardIterator, typename _Tp>
02538     pair<_ForwardIterator, _ForwardIterator>
02539     equal_range(_ForwardIterator __first, _ForwardIterator __last,
02540         const _Tp& __val)
02541     {
02542       typedef typename iterator_traits<_ForwardIterator>::value_type
02543     _ValueType;
02544       typedef typename iterator_traits<_ForwardIterator>::difference_type
02545     _DistanceType;
02546 
02547       // concept requirements
02548       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02549       __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
02550       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)  
02551       __glibcxx_requires_partitioned_lower(__first, __last, __val);
02552       __glibcxx_requires_partitioned_upper(__first, __last, __val);      
02553 
02554       _DistanceType __len = std::distance(__first, __last);
02555  
02556       while (__len > 0)
02557     {
02558       _DistanceType __half = __len >> 1;
02559       _ForwardIterator __middle = __first;
02560       std::advance(__middle, __half);
02561       if (*__middle < __val)
02562         {
02563           __first = __middle;
02564           ++__first;
02565           __len = __len - __half - 1;
02566         }
02567       else if (__val < *__middle)
02568         __len = __half;
02569       else
02570         {
02571           _ForwardIterator __left = std::lower_bound(__first, __middle,
02572                              __val);
02573           std::advance(__first, __len);
02574           _ForwardIterator __right = std::upper_bound(++__middle, __first,
02575                               __val);
02576           return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
02577         }
02578     }
02579       return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
02580     }
02581 
02582   /**
02583    *  @brief Finds the largest subrange in which @a val could be inserted
02584    *         at any place in it without changing the ordering.
02585    *  @param  first   An iterator.
02586    *  @param  last    Another iterator.
02587    *  @param  val     The search term.
02588    *  @param  comp    A functor to use for comparisons.
02589    *  @return  An pair of iterators defining the subrange.
02590    *  @ingroup binary_search_algorithms
02591    *
02592    *  This is equivalent to
02593    *  @code
02594    *    std::make_pair(lower_bound(first, last, val, comp),
02595    *                   upper_bound(first, last, val, comp))
02596    *  @endcode
02597    *  but does not actually call those functions.
02598   */
02599   template<typename _ForwardIterator, typename _Tp, typename _Compare>
02600     pair<_ForwardIterator, _ForwardIterator>
02601     equal_range(_ForwardIterator __first, _ForwardIterator __last,
02602         const _Tp& __val, _Compare __comp)
02603     {
02604       typedef typename iterator_traits<_ForwardIterator>::value_type
02605     _ValueType;
02606       typedef typename iterator_traits<_ForwardIterator>::difference_type
02607     _DistanceType;
02608 
02609       // concept requirements
02610       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02611       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02612                   _ValueType, _Tp>)
02613       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02614                   _Tp, _ValueType>)
02615       __glibcxx_requires_partitioned_lower_pred(__first, __last,
02616                         __val, __comp);
02617       __glibcxx_requires_partitioned_upper_pred(__first, __last,
02618                         __val, __comp);
02619 
02620       _DistanceType __len = std::distance(__first, __last);
02621 
02622       while (__len > 0)
02623     {
02624       _DistanceType __half = __len >> 1;
02625       _ForwardIterator __middle = __first;
02626       std::advance(__middle, __half);
02627       if (__comp(*__middle, __val))
02628         {
02629           __first = __middle;
02630           ++__first;
02631           __len = __len - __half - 1;
02632         }
02633       else if (__comp(__val, *__middle))
02634         __len = __half;
02635       else
02636         {
02637           _ForwardIterator __left = std::lower_bound(__first, __middle,
02638                              __val, __comp);
02639           std::advance(__first, __len);
02640           _ForwardIterator __right = std::upper_bound(++__middle, __first,
02641                               __val, __comp);
02642           return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
02643         }
02644     }
02645       return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
02646     }
02647 
02648   /**
02649    *  @brief Determines whether an element exists in a range.
02650    *  @ingroup binary_search_algorithms
02651    *  @param  first   An iterator.
02652    *  @param  last    Another iterator.
02653    *  @param  val     The search term.
02654    *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
02655    *
02656    *  Note that this does not actually return an iterator to @a val.  For
02657    *  that, use std::find or a container's specialized find member functions.
02658   */
02659   template<typename _ForwardIterator, typename _Tp>
02660     bool
02661     binary_search(_ForwardIterator __first, _ForwardIterator __last,
02662                   const _Tp& __val)
02663     {
02664       typedef typename iterator_traits<_ForwardIterator>::value_type
02665     _ValueType;
02666 
02667       // concept requirements
02668       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02669       __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
02670       __glibcxx_requires_partitioned_lower(__first, __last, __val);
02671       __glibcxx_requires_partitioned_upper(__first, __last, __val);
02672 
02673       _ForwardIterator __i = std::lower_bound(__first, __last, __val);
02674       return __i != __last && !(__val < *__i);
02675     }
02676 
02677   /**
02678    *  @brief Determines whether an element exists in a range.
02679    *  @ingroup binary_search_algorithms
02680    *  @param  first   An iterator.
02681    *  @param  last    Another iterator.
02682    *  @param  val     The search term.
02683    *  @param  comp    A functor to use for comparisons.
02684    *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
02685    *
02686    *  Note that this does not actually return an iterator to @a val.  For
02687    *  that, use std::find or a container's specialized find member functions.
02688    *
02689    *  The comparison function should have the same effects on ordering as
02690    *  the function used for the initial sort.
02691   */
02692   template<typename _ForwardIterator, typename _Tp, typename _Compare>
02693     bool
02694     binary_search(_ForwardIterator __first, _ForwardIterator __last,
02695                   const _Tp& __val, _Compare __comp)
02696     {
02697       typedef typename iterator_traits<_ForwardIterator>::value_type
02698     _ValueType;
02699 
02700       // concept requirements
02701       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02702       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02703                   _Tp, _ValueType>)
02704       __glibcxx_requires_partitioned_lower_pred(__first, __last,
02705                         __val, __comp);
02706       __glibcxx_requires_partitioned_upper_pred(__first, __last,
02707                         __val, __comp);
02708 
02709       _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
02710       return __i != __last && !bool(__comp(__val, *__i));
02711     }
02712 
02713   // merge
02714 
02715   /// This is a helper function for the merge routines.
02716   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
02717        typename _BidirectionalIterator3>
02718     _BidirectionalIterator3
02719     __merge_backward(_BidirectionalIterator1 __first1,
02720              _BidirectionalIterator1 __last1,
02721              _BidirectionalIterator2 __first2,
02722              _BidirectionalIterator2 __last2,
02723              _BidirectionalIterator3 __result)
02724     {
02725       if (__first1 == __last1)
02726     return std::copy_backward(__first2, __last2, __result);
02727       if (__first2 == __last2)
02728     return std::copy_backward(__first1, __last1, __result);
02729       --__last1;
02730       --__last2;
02731       while (true)
02732     {
02733       if (*__last2 < *__last1)
02734         {
02735           *--__result = *__last1;
02736           if (__first1 == __last1)
02737         return std::copy_backward(__first2, ++__last2, __result);
02738           --__last1;
02739         }
02740       else
02741         {
02742           *--__result = *__last2;
02743           if (__first2 == __last2)
02744         return std::copy_backward(__first1, ++__last1, __result);
02745           --__last2;
02746         }
02747     }
02748     }
02749 
02750   /// This is a helper function for the merge routines.
02751   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
02752        typename _BidirectionalIterator3, typename _Compare>
02753     _BidirectionalIterator3
02754     __merge_backward(_BidirectionalIterator1 __first1,
02755              _BidirectionalIterator1 __last1,
02756              _BidirectionalIterator2 __first2,
02757              _BidirectionalIterator2 __last2,
02758              _BidirectionalIterator3 __result,
02759              _Compare __comp)
02760     {
02761       if (__first1 == __last1)
02762     return std::copy_backward(__first2, __last2, __result);
02763       if (__first2 == __last2)
02764     return std::copy_backward(__first1, __last1, __result);
02765       --__last1;
02766       --__last2;
02767       while (true)
02768     {
02769       if (__comp(*__last2, *__last1))
02770         {
02771           *--__result = *__last1;
02772           if (__first1 == __last1)
02773         return std::copy_backward(__first2, ++__last2, __result);
02774           --__last1;
02775         }
02776       else
02777         {
02778           *--__result = *__last2;
02779           if (__first2 == __last2)
02780         return std::copy_backward(__first1, ++__last1, __result);
02781           --__last2;
02782         }
02783     }
02784     }
02785 
02786   /// This is a helper function for the merge routines.
02787   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
02788        typename _Distance>
02789     _BidirectionalIterator1
02790     __rotate_adaptive(_BidirectionalIterator1 __first,
02791               _BidirectionalIterator1 __middle,
02792               _BidirectionalIterator1 __last,
02793               _Distance __len1, _Distance __len2,
02794               _BidirectionalIterator2 __buffer,
02795               _Distance __buffer_size)
02796     {
02797       _BidirectionalIterator2 __buffer_end;
02798       if (__len1 > __len2 && __len2 <= __buffer_size)
02799     {
02800       __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
02801       _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
02802       return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
02803     }
02804       else if (__len1 <= __buffer_size)
02805     {
02806       __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
02807       _GLIBCXX_MOVE3(__middle, __last, __first);
02808       return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
02809     }
02810       else
02811     {
02812       std::rotate(__first, __middle, __last);
02813       std::advance(__first, std::distance(__middle, __last));
02814       return __first;
02815     }
02816     }
02817 
02818   /// This is a helper function for the merge routines.
02819   template<typename _BidirectionalIterator, typename _Distance,
02820        typename _Pointer>
02821     void
02822     __merge_adaptive(_BidirectionalIterator __first,
02823                      _BidirectionalIterator __middle,
02824              _BidirectionalIterator __last,
02825              _Distance __len1, _Distance __len2,
02826              _Pointer __buffer, _Distance __buffer_size)
02827     {
02828       if (__len1 <= __len2 && __len1 <= __buffer_size)
02829     {
02830       _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
02831       _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
02832                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
02833                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
02834                 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
02835                 __first);
02836     }
02837       else if (__len2 <= __buffer_size)
02838     {
02839       _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
02840       std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
02841                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
02842                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
02843                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
02844                 __last);
02845     }
02846       else
02847     {
02848       _BidirectionalIterator __first_cut = __first;
02849       _BidirectionalIterator __second_cut = __middle;
02850       _Distance __len11 = 0;
02851       _Distance __len22 = 0;
02852       if (__len1 > __len2)
02853         {
02854           __len11 = __len1 / 2;
02855           std::advance(__first_cut, __len11);
02856           __second_cut = std::lower_bound(__middle, __last,
02857                           *__first_cut);
02858           __len22 = std::distance(__middle, __second_cut);
02859         }
02860       else
02861         {
02862           __len22 = __len2 / 2;
02863           std::advance(__second_cut, __len22);
02864           __first_cut = std::upper_bound(__first, __middle,
02865                          *__second_cut);
02866           __len11 = std::distance(__first, __first_cut);
02867         }
02868       _BidirectionalIterator __new_middle =
02869         std::__rotate_adaptive(__first_cut, __middle, __second_cut,
02870                    __len1 - __len11, __len22, __buffer,
02871                    __buffer_size);
02872       std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
02873                 __len22, __buffer, __buffer_size);
02874       std::__merge_adaptive(__new_middle, __second_cut, __last,
02875                 __len1 - __len11,
02876                 __len2 - __len22, __buffer, __buffer_size);
02877     }
02878     }
02879 
02880   /// This is a helper function for the merge routines.
02881   template<typename _BidirectionalIterator, typename _Distance, 
02882        typename _Pointer, typename _Compare>
02883     void
02884     __merge_adaptive(_BidirectionalIterator __first,
02885                      _BidirectionalIterator __middle,
02886              _BidirectionalIterator __last,
02887              _Distance __len1, _Distance __len2,
02888              _Pointer __buffer, _Distance __buffer_size,
02889              _Compare __comp)
02890     {
02891       if (__len1 <= __len2 && __len1 <= __buffer_size)
02892     {
02893       _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
02894       _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
02895                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
02896                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
02897                 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
02898                 __first, __comp);
02899     }
02900       else if (__len2 <= __buffer_size)
02901     {
02902       _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
02903       std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
02904                 _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
02905                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
02906                 _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
02907                 __last,__comp);
02908     }
02909       else
02910     {
02911       _BidirectionalIterator __first_cut = __first;
02912       _BidirectionalIterator __second_cut = __middle;
02913       _Distance __len11 = 0;
02914       _Distance __len22 = 0;
02915       if (__len1 > __len2)
02916         {
02917           __len11 = __len1 / 2;
02918           std::advance(__first_cut, __len11);
02919           __second_cut = std::lower_bound(__middle, __last, *__first_cut,
02920                           __comp);
02921           __len22 = std::distance(__middle, __second_cut);
02922         }
02923       else
02924         {
02925           __len22 = __len2 / 2;
02926           std::advance(__second_cut, __len22);
02927           __first_cut = std::upper_bound(__first, __middle, *__second_cut,
02928                          __comp);
02929           __len11 = std::distance(__first, __first_cut);
02930         }
02931       _BidirectionalIterator __new_middle =
02932         std::__rotate_adaptive(__first_cut, __middle, __second_cut,
02933                    __len1 - __len11, __len22, __buffer,
02934                    __buffer_size);
02935       std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
02936                 __len22, __buffer, __buffer_size, __comp);
02937       std::__merge_adaptive(__new_middle, __second_cut, __last,
02938                 __len1 - __len11,
02939                 __len2 - __len22, __buffer,
02940                 __buffer_size, __comp);
02941     }
02942     }
02943 
02944   /// This is a helper function for the merge routines.
02945   template<typename _BidirectionalIterator, typename _Distance>
02946     void
02947     __merge_without_buffer(_BidirectionalIterator __first,
02948                _BidirectionalIterator __middle,
02949                _BidirectionalIterator __last,
02950                _Distance __len1, _Distance __len2)
02951     {
02952       if (__len1 == 0 || __len2 == 0)
02953     return;
02954       if (__len1 + __len2 == 2)
02955     {
02956       if (*__middle < *__first)
02957         std::iter_swap(__first, __middle);
02958       return;
02959     }
02960       _BidirectionalIterator __first_cut = __first;
02961       _BidirectionalIterator __second_cut = __middle;
02962       _Distance __len11 = 0;
02963       _Distance __len22 = 0;
02964       if (__len1 > __len2)
02965     {
02966       __len11 = __len1 / 2;
02967       std::advance(__first_cut, __len11);
02968       __second_cut = std::lower_bound(__middle, __last, *__first_cut);
02969       __len22 = std::distance(__middle, __second_cut);
02970     }
02971       else
02972     {
02973       __len22 = __len2 / 2;
02974       std::advance(__second_cut, __len22);
02975       __first_cut = std::upper_bound(__first, __middle, *__second_cut);
02976       __len11 = std::distance(__first, __first_cut);
02977     }
02978       std::rotate(__first_cut, __middle, __second_cut);
02979       _BidirectionalIterator __new_middle = __first_cut;
02980       std::advance(__new_middle, std::distance(__middle, __second_cut));
02981       std::__merge_without_buffer(__first, __first_cut, __new_middle,
02982                   __len11, __len22);
02983       std::__merge_without_buffer(__new_middle, __second_cut, __last,
02984                   __len1 - __len11, __len2 - __len22);
02985     }
02986 
02987   /// This is a helper function for the merge routines.
02988   template<typename _BidirectionalIterator, typename _Distance,
02989        typename _Compare>
02990     void
02991     __merge_without_buffer(_BidirectionalIterator __first,
02992                            _BidirectionalIterator __middle,
02993                _BidirectionalIterator __last,
02994                _Distance __len1, _Distance __len2,
02995                _Compare __comp)
02996     {
02997       if (__len1 == 0 || __len2 == 0)
02998     return;
02999       if (__len1 + __len2 == 2)
03000     {
03001       if (__comp(*__middle, *__first))
03002         std::iter_swap(__first, __middle);
03003       return;
03004     }
03005       _BidirectionalIterator __first_cut = __first;
03006       _BidirectionalIterator __second_cut = __middle;
03007       _Distance __len11 = 0;
03008       _Distance __len22 = 0;
03009       if (__len1 > __len2)
03010     {
03011       __len11 = __len1 / 2;
03012       std::advance(__first_cut, __len11);
03013       __second_cut = std::lower_bound(__middle, __last, *__first_cut,
03014                       __comp);
03015       __len22 = std::distance(__middle, __second_cut);
03016     }
03017       else
03018     {
03019       __len22 = __len2 / 2;
03020       std::advance(__second_cut, __len22);
03021       __first_cut = std::upper_bound(__first, __middle, *__second_cut,
03022                      __comp);
03023       __len11 = std::distance(__first, __first_cut);
03024     }
03025       std::rotate(__first_cut, __middle, __second_cut);
03026       _BidirectionalIterator __new_middle = __first_cut;
03027       std::advance(__new_middle, std::distance(__middle, __second_cut));
03028       std::__merge_without_buffer(__first, __first_cut, __new_middle,
03029                   __len11, __len22, __comp);
03030       std::__merge_without_buffer(__new_middle, __second_cut, __last,
03031                   __len1 - __len11, __len2 - __len22, __comp);
03032     }
03033 
03034   /**
03035    *  @brief Merges two sorted ranges in place.
03036    *  @ingroup sorting_algorithms
03037    *  @param  first   An iterator.
03038    *  @param  middle  Another iterator.
03039    *  @param  last    Another iterator.
03040    *  @return  Nothing.
03041    *
03042    *  Merges two sorted and consecutive ranges, [first,middle) and
03043    *  [middle,last), and puts the result in [first,last).  The output will
03044    *  be sorted.  The sort is @e stable, that is, for equivalent
03045    *  elements in the two ranges, elements from the first range will always
03046    *  come before elements from the second.
03047    *
03048    *  If enough additional memory is available, this takes (last-first)-1
03049    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
03050    *  distance(first,last).
03051   */
03052   template<typename _BidirectionalIterator>
03053     void
03054     inplace_merge(_BidirectionalIterator __first,
03055           _BidirectionalIterator __middle,
03056           _BidirectionalIterator __last)
03057     {
03058       typedef typename iterator_traits<_BidirectionalIterator>::value_type
03059           _ValueType;
03060       typedef typename iterator_traits<_BidirectionalIterator>::difference_type
03061           _DistanceType;
03062 
03063       // concept requirements
03064       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
03065         _BidirectionalIterator>)
03066       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
03067       __glibcxx_requires_sorted(__first, __middle);
03068       __glibcxx_requires_sorted(__middle, __last);
03069 
03070       if (__first == __middle || __middle == __last)
03071     return;
03072 
03073       _DistanceType __len1 = std::distance(__first, __middle);
03074       _DistanceType __len2 = std::distance(__middle, __last);
03075 
03076       _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
03077                                   __last);
03078       if (__buf.begin() == 0)
03079     std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
03080       else
03081     std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
03082                   __buf.begin(), _DistanceType(__buf.size()));
03083     }
03084 
03085   /**
03086    *  @brief Merges two sorted ranges in place.
03087    *  @ingroup sorting_algorithms
03088    *  @param  first   An iterator.
03089    *  @param  middle  Another iterator.
03090    *  @param  last    Another iterator.
03091    *  @param  comp    A functor to use for comparisons.
03092    *  @return  Nothing.
03093    *
03094    *  Merges two sorted and consecutive ranges, [first,middle) and
03095    *  [middle,last), and puts the result in [first,last).  The output will
03096    *  be sorted.  The sort is @e stable, that is, for equivalent
03097    *  elements in the two ranges, elements from the first range will always
03098    *  come before elements from the second.
03099    *
03100    *  If enough additional memory is available, this takes (last-first)-1
03101    *  comparisons.  Otherwise an NlogN algorithm is used, where N is
03102    *  distance(first,last).
03103    *
03104    *  The comparison function should have the same effects on ordering as
03105    *  the function used for the initial sort.
03106   */
03107   template<typename _BidirectionalIterator, typename _Compare>
03108     void
03109     inplace_merge(_BidirectionalIterator __first,
03110           _BidirectionalIterator __middle,
03111           _BidirectionalIterator __last,
03112           _Compare __comp)
03113     {
03114       typedef typename iterator_traits<_BidirectionalIterator>::value_type
03115           _ValueType;
03116       typedef typename iterator_traits<_BidirectionalIterator>::difference_type
03117           _DistanceType;
03118 
03119       // concept requirements
03120       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
03121         _BidirectionalIterator>)
03122       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03123         _ValueType, _ValueType>)
03124       __glibcxx_requires_sorted_pred(__first, __middle, __comp);
03125       __glibcxx_requires_sorted_pred(__middle, __last, __comp);
03126 
03127       if (__first == __middle || __middle == __last)
03128     return;
03129 
03130       const _DistanceType __len1 = std::distance(__first, __middle);
03131       const _DistanceType __len2 = std::distance(__middle, __last);
03132 
03133       _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
03134                                   __last);
03135       if (__buf.begin() == 0)
03136     std::__merge_without_buffer(__first, __middle, __last, __len1,
03137                     __len2, __comp);
03138       else
03139     std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
03140                   __buf.begin(), _DistanceType(__buf.size()),
03141                   __comp);
03142     }
03143 
03144   template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
03145        typename _Distance>
03146     void
03147     __merge_sort_loop(_RandomAccessIterator1 __first,
03148               _RandomAccessIterator1 __last,
03149               _RandomAccessIterator2 __result,
03150               _Distance __step_size)
03151     {
03152       const _Distance __two_step = 2 * __step_size;
03153 
03154       while (__last - __first >= __two_step)
03155     {
03156       __result = _GLIBCXX_STD_P::merge(
03157             _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
03158             _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
03159             _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
03160             _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
03161             __result);
03162       __first += __two_step;
03163     }
03164 
03165       __step_size = std::min(_Distance(__last - __first), __step_size);
03166       _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
03167                 _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
03168                             __step_size),
03169                 _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
03170                             __step_size),
03171                 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
03172                 __result);
03173     }
03174 
03175   template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
03176        typename _Distance, typename _Compare>
03177     void
03178     __merge_sort_loop(_RandomAccessIterator1 __first,
03179               _RandomAccessIterator1 __last,
03180               _RandomAccessIterator2 __result, _Distance __step_size,
03181               _Compare __comp)
03182     {
03183       const _Distance __two_step = 2 * __step_size;
03184 
03185       while (__last - __first >= __two_step)
03186     {
03187       __result = _GLIBCXX_STD_P::merge(
03188             _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
03189             _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
03190             _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
03191             _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
03192             __result, __comp);
03193       __first += __two_step;
03194     }
03195       __step_size = std::min(_Distance(__last - __first), __step_size);
03196 
03197       _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
03198                 _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
03199                             __step_size),
03200                 _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
03201                             __step_size),
03202                 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
03203                 __result, __comp);
03204     }
03205 
03206   template<typename _RandomAccessIterator, typename _Distance>
03207     void
03208     __chunk_insertion_sort(_RandomAccessIterator __first,
03209                _RandomAccessIterator __last,
03210                _Distance __chunk_size)
03211     {
03212       while (__last - __first >= __chunk_size)
03213     {
03214       std::__insertion_sort(__first, __first + __chunk_size);
03215       __first += __chunk_size;
03216     }
03217       std::__insertion_sort(__first, __last);
03218     }
03219 
03220   template<typename _RandomAccessIterator, typename _Distance,
03221        typename _Compare>
03222     void
03223     __chunk_insertion_sort(_RandomAccessIterator __first,
03224                _RandomAccessIterator __last,
03225                _Distance __chunk_size, _Compare __comp)
03226     {
03227       while (__last - __first >= __chunk_size)
03228     {
03229       std::__insertion_sort(__first, __first + __chunk_size, __comp);
03230       __first += __chunk_size;
03231     }
03232       std::__insertion_sort(__first, __last, __comp);
03233     }
03234 
03235   enum { _S_chunk_size = 7 };
03236 
03237   template<typename _RandomAccessIterator, typename _Pointer>
03238     void
03239     __merge_sort_with_buffer(_RandomAccessIterator __first,
03240                  _RandomAccessIterator __last,
03241                              _Pointer __buffer)
03242     {
03243       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
03244     _Distance;
03245 
03246       const _Distance __len = __last - __first;
03247       const _Pointer __buffer_last = __buffer + __len;
03248 
03249       _Distance __step_size = _S_chunk_size;
03250       std::__chunk_insertion_sort(__first, __last, __step_size);
03251 
03252       while (__step_size < __len)
03253     {
03254       std::__merge_sort_loop(__first, __last, __buffer, __step_size);
03255       __step_size *= 2;
03256       std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
03257       __step_size *= 2;
03258     }
03259     }
03260 
03261   template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
03262     void
03263     __merge_sort_with_buffer(_RandomAccessIterator __first,
03264                  _RandomAccessIterator __last,
03265                              _Pointer __buffer, _Compare __comp)
03266     {
03267       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
03268     _Distance;
03269 
03270       const _Distance __len = __last - __first;
03271       const _Pointer __buffer_last = __buffer + __len;
03272 
03273       _Distance __step_size = _S_chunk_size;
03274       std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
03275 
03276       while (__step_size < __len)
03277     {
03278       std::__merge_sort_loop(__first, __last, __buffer,
03279                  __step_size, __comp);
03280       __step_size *= 2;
03281       std::__merge_sort_loop(__buffer, __buffer_last, __first,
03282                  __step_size, __comp);
03283       __step_size *= 2;
03284     }
03285     }
03286 
03287   template<typename _RandomAccessIterator, typename _Pointer,
03288        typename _Distance>
03289     void
03290     __stable_sort_adaptive(_RandomAccessIterator __first,
03291                _RandomAccessIterator __last,
03292                            _Pointer __buffer, _Distance __buffer_size)
03293     {
03294       const _Distance __len = (__last - __first + 1) / 2;
03295       const _RandomAccessIterator __middle = __first + __len;
03296       if (__len > __buffer_size)
03297     {
03298       std::__stable_sort_adaptive(__first, __middle,
03299                       __buffer, __buffer_size);
03300       std::__stable_sort_adaptive(__middle, __last,
03301                       __buffer, __buffer_size);
03302     }
03303       else
03304     {
03305       std::__merge_sort_with_buffer(__first, __middle, __buffer);
03306       std::__merge_sort_with_buffer(__middle, __last, __buffer);
03307     }
03308       std::__merge_adaptive(__first, __middle, __last,
03309                 _Distance(__middle - __first),
03310                 _Distance(__last - __middle),
03311                 __buffer, __buffer_size);
03312     }
03313 
03314   template<typename _RandomAccessIterator, typename _Pointer,
03315        typename _Distance, typename _Compare>
03316     void
03317     __stable_sort_adaptive(_RandomAccessIterator __first,
03318                _RandomAccessIterator __last,
03319                            _Pointer __buffer, _Distance __buffer_size,
03320                            _Compare __comp)
03321     {
03322       const _Distance __len = (__last - __first + 1) / 2;
03323       const _RandomAccessIterator __middle = __first + __len;
03324       if (__len > __buffer_size)
03325     {
03326       std::__stable_sort_adaptive(__first, __middle, __buffer,
03327                       __buffer_size, __comp);
03328       std::__stable_sort_adaptive(__middle, __last, __buffer,
03329                       __buffer_size, __comp);
03330     }
03331       else
03332     {
03333       std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
03334       std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
03335     }
03336       std::__merge_adaptive(__first, __middle, __last,
03337                 _Distance(__middle - __first),
03338                 _Distance(__last - __middle),
03339                 __buffer, __buffer_size,
03340                 __comp);
03341     }
03342 
03343   /// This is a helper function for the stable sorting routines.
03344   template<typename _RandomAccessIterator>
03345     void
03346     __inplace_stable_sort(_RandomAccessIterator __first,
03347               _RandomAccessIterator __last)
03348     {
03349       if (__last - __first < 15)
03350     {
03351       std::__insertion_sort(__first, __last);
03352       return;
03353     }
03354       _RandomAccessIterator __middle = __first + (__last - __first) / 2;
03355       std::__inplace_stable_sort(__first, __middle);
03356       std::__inplace_stable_sort(__middle, __last);
03357       std::__merge_without_buffer(__first, __middle, __last,
03358                   __middle - __first,
03359                   __last - __middle);
03360     }
03361 
03362   /// This is a helper function for the stable sorting routines.
03363   template<typename _RandomAccessIterator, typename _Compare>
03364     void
03365     __inplace_stable_sort(_RandomAccessIterator __first,
03366               _RandomAccessIterator __last, _Compare __comp)
03367     {
03368       if (__last - __first < 15)
03369     {
03370       std::__insertion_sort(__first, __last, __comp);
03371       return;
03372     }
03373       _RandomAccessIterator __middle = __first + (__last - __first) / 2;
03374       std::__inplace_stable_sort(__first, __middle, __comp);
03375       std::__inplace_stable_sort(__middle, __last, __comp);
03376       std::__merge_without_buffer(__first, __middle, __last,
03377                   __middle - __first,
03378                   __last - __middle,
03379                   __comp);
03380     }
03381 
03382   // stable_sort
03383 
03384   // Set algorithms: includes, set_union, set_intersection, set_difference,
03385   // set_symmetric_difference.  All of these algorithms have the precondition
03386   // that their input ranges are sorted and the postcondition that their output
03387   // ranges are sorted.
03388 
03389   /**
03390    *  @brief Determines whether all elements of a sequence exists in a range.
03391    *  @param  first1  Start of search range.
03392    *  @param  last1   End of search range.
03393    *  @param  first2  Start of sequence
03394    *  @param  last2   End of sequence.
03395    *  @return  True if each element in [first2,last2) is contained in order
03396    *  within [first1,last1).  False otherwise.
03397    *  @ingroup set_algorithms
03398    *
03399    *  This operation expects both [first1,last1) and [first2,last2) to be
03400    *  sorted.  Searches for the presence of each element in [first2,last2)
03401    *  within [first1,last1).  The iterators over each range only move forward,
03402    *  so this is a linear algorithm.  If an element in [first2,last2) is not
03403    *  found before the search iterator reaches @a last2, false is returned.
03404   */
03405   template<typename _InputIterator1, typename _InputIterator2>
03406     bool
03407     includes(_InputIterator1 __first1, _InputIterator1 __last1,
03408          _InputIterator2 __first2, _InputIterator2 __last2)
03409     {
03410       typedef typename iterator_traits<_InputIterator1>::value_type
03411     _ValueType1;
03412       typedef typename iterator_traits<_InputIterator2>::value_type
03413     _ValueType2;
03414 
03415       // concept requirements
03416       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
03417       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
03418       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
03419       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
03420       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
03421       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
03422 
03423       while (__first1 != __last1 && __first2 != __last2)
03424     if (*__first2 < *__first1)
03425       return false;
03426     else if(*__first1 < *__first2)
03427       ++__first1;
03428     else
03429       ++__first1, ++__first2;
03430 
03431       return __first2 == __last2;
03432     }
03433 
03434   /**
03435    *  @brief Determines whether all elements of a sequence exists in a range
03436    *  using comparison.
03437    *  @ingroup set_algorithms
03438    *  @param  first1  Start of search range.
03439    *  @param  last1   End of search range.
03440    *  @param  first2  Start of sequence
03441    *  @param  last2   End of sequence.
03442    *  @param  comp    Comparison function to use.
03443    *  @return  True if each element in [first2,last2) is contained in order
03444    *  within [first1,last1) according to comp.  False otherwise.
03445    *  @ingroup set_algorithms
03446    *
03447    *  This operation expects both [first1,last1) and [first2,last2) to be
03448    *  sorted.  Searches for the presence of each element in [first2,last2)
03449    *  within [first1,last1), using comp to decide.  The iterators over each
03450    *  range only move forward, so this is a linear algorithm.  If an element
03451    *  in [first2,last2) is not found before the search iterator reaches @a
03452    *  last2, false is returned.
03453   */
03454   template<typename _InputIterator1, typename _InputIterator2,
03455        typename _Compare>
03456     bool
03457     includes(_InputIterator1 __first1, _InputIterator1 __last1,
03458          _InputIterator2 __first2, _InputIterator2 __last2,
03459          _Compare __comp)
03460     {
03461       typedef typename iterator_traits<_InputIterator1>::value_type
03462     _ValueType1;
03463       typedef typename iterator_traits<_InputIterator2>::value_type
03464     _ValueType2;
03465 
03466       // concept requirements
03467       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
03468       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
03469       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03470                   _ValueType1, _ValueType2>)
03471       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03472                   _ValueType2, _ValueType1>)
03473       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
03474       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
03475 
03476       while (__first1 != __last1 && __first2 != __last2)
03477     if (__comp(*__first2, *__first1))
03478       return false;
03479     else if(__comp(*__first1, *__first2))
03480       ++__first1;
03481     else
03482       ++__first1, ++__first2;
03483 
03484       return __first2 == __last2;
03485     }
03486 
03487   // nth_element
03488   // merge
03489   // set_difference
03490   // set_intersection
03491   // set_union
03492   // stable_sort
03493   // set_symmetric_difference
03494   // min_element
03495   // max_element
03496 
03497   /**
03498    *  @brief  Permute range into the next @a dictionary ordering.
03499    *  @ingroup sorting_algorithms
03500    *  @param  first  Start of range.
03501    *  @param  last   End of range.
03502    *  @return  False if wrapped to first permutation, true otherwise.
03503    *
03504    *  Treats all permutations of the range as a set of @a dictionary sorted
03505    *  sequences.  Permutes the current sequence into the next one of this set.
03506    *  Returns true if there are more sequences to generate.  If the sequence
03507    *  is the largest of the set, the smallest is generated and false returned.
03508   */
03509   template<typename _BidirectionalIterator>
03510     bool
03511     next_permutation(_BidirectionalIterator __first,
03512              _BidirectionalIterator __last)
03513     {
03514       // concept requirements
03515       __glibcxx_function_requires(_BidirectionalIteratorConcept<
03516                   _BidirectionalIterator>)
03517       __glibcxx_function_requires(_LessThanComparableConcept<
03518         typename iterator_traits<_BidirectionalIterator>::value_type>)
03519       __glibcxx_requires_valid_range(__first, __last);
03520 
03521       if (__first == __last)
03522     return false;
03523       _BidirectionalIterator __i = __first;
03524       ++__i;
03525       if (__i == __last)
03526     return false;
03527       __i = __last;
03528       --__i;
03529 
03530       for(;;)
03531     {
03532       _BidirectionalIterator __ii = __i;
03533       --__i;
03534       if (*__i < *__ii)
03535         {
03536           _BidirectionalIterator __j = __last;
03537           while (!(*__i < *--__j))
03538         {}
03539           std::iter_swap(__i, __j);
03540           std::reverse(__ii, __last);
03541           return true;
03542         }
03543       if (__i == __first)
03544         {
03545           std::reverse(__first, __last);
03546           return false;
03547         }
03548     }
03549     }
03550 
03551   /**
03552    *  @brief  Permute range into the next @a dictionary ordering using
03553    *          comparison functor.
03554    *  @ingroup sorting_algorithms
03555    *  @param  first  Start of range.
03556    *  @param  last   End of range.
03557    *  @param  comp   A comparison functor.
03558    *  @return  False if wrapped to first permutation, true otherwise.
03559    *
03560    *  Treats all permutations of the range [first,last) as a set of
03561    *  @a dictionary sorted sequences ordered by @a comp.  Permutes the current
03562    *  sequence into the next one of this set.  Returns true if there are more
03563    *  sequences to generate.  If the sequence is the largest of the set, the
03564    *  smallest is generated and false returned.
03565   */
03566   template<typename _BidirectionalIterator, typename _Compare>
03567     bool
03568     next_permutation(_BidirectionalIterator __first,
03569              _BidirectionalIterator __last, _Compare __comp)
03570     {
03571       // concept requirements
03572       __glibcxx_function_requires(_BidirectionalIteratorConcept<
03573                   _BidirectionalIterator>)
03574       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03575         typename iterator_traits<_BidirectionalIterator>::value_type,
03576         typename iterator_traits<_BidirectionalIterator>::value_type>)
03577       __glibcxx_requires_valid_range(__first, __last);
03578 
03579       if (__first == __last)
03580     return false;
03581       _BidirectionalIterator __i = __first;
03582       ++__i;
03583       if (__i == __last)
03584     return false;
03585       __i = __last;
03586       --__i;
03587 
03588       for(;;)
03589     {
03590       _BidirectionalIterator __ii = __i;
03591       --__i;
03592       if (__comp(*__i, *__ii))
03593         {
03594           _BidirectionalIterator __j = __last;
03595           while (!bool(__comp(*__i, *--__j)))
03596         {}
03597           std::iter_swap(__i, __j);
03598           std::reverse(__ii, __last);
03599           return true;
03600         }
03601       if (__i == __first)
03602         {
03603           std::reverse(__first, __last);
03604           return false;
03605         }
03606     }
03607     }
03608 
03609   /**
03610    *  @brief  Permute range into the previous @a dictionary ordering.
03611    *  @ingroup sorting_algorithms
03612    *  @param  first  Start of range.
03613    *  @param  last   End of range.
03614    *  @return  False if wrapped to last permutation, true otherwise.
03615    *
03616    *  Treats all permutations of the range as a set of @a dictionary sorted
03617    *  sequences.  Permutes the current sequence into the previous one of this
03618    *  set.  Returns true if there are more sequences to generate.  If the
03619    *  sequence is the smallest of the set, the largest is generated and false
03620    *  returned.
03621   */
03622   template<typename _BidirectionalIterator>
03623     bool
03624     prev_permutation(_BidirectionalIterator __first,
03625              _BidirectionalIterator __last)
03626     {
03627       // concept requirements
03628       __glibcxx_function_requires(_BidirectionalIteratorConcept<
03629                   _BidirectionalIterator>)
03630       __glibcxx_function_requires(_LessThanComparableConcept<
03631         typename iterator_traits<_BidirectionalIterator>::value_type>)
03632       __glibcxx_requires_valid_range(__first, __last);
03633 
03634       if (__first == __last)
03635     return false;
03636       _BidirectionalIterator __i = __first;
03637       ++__i;
03638       if (__i == __last)
03639     return false;
03640       __i = __last;
03641       --__i;
03642 
03643       for(;;)
03644     {
03645       _BidirectionalIterator __ii = __i;
03646       --__i;
03647       if (*__ii < *__i)
03648         {
03649           _BidirectionalIterator __j = __last;
03650           while (!(*--__j < *__i))
03651         {}
03652           std::iter_swap(__i, __j);
03653           std::reverse(__ii, __last);
03654           return true;
03655         }
03656       if (__i == __first)
03657         {
03658           std::reverse(__first, __last);
03659           return false;
03660         }
03661     }
03662     }
03663 
03664   /**
03665    *  @brief  Permute range into the previous @a dictionary ordering using
03666    *          comparison functor.
03667    *  @ingroup sorting_algorithms
03668    *  @param  first  Start of range.
03669    *  @param  last   End of range.
03670    *  @param  comp   A comparison functor.
03671    *  @return  False if wrapped to last permutation, true otherwise.
03672    *
03673    *  Treats all permutations of the range [first,last) as a set of
03674    *  @a dictionary sorted sequences ordered by @a comp.  Permutes the current
03675    *  sequence into the previous one of this set.  Returns true if there are
03676    *  more sequences to generate.  If the sequence is the smallest of the set,
03677    *  the largest is generated and false returned.
03678   */
03679   template<typename _BidirectionalIterator, typename _Compare>
03680     bool
03681     prev_permutation(_BidirectionalIterator __first,
03682              _BidirectionalIterator __last, _Compare __comp)
03683     {
03684       // concept requirements
03685       __glibcxx_function_requires(_BidirectionalIteratorConcept<
03686                   _BidirectionalIterator>)
03687       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03688         typename iterator_traits<_BidirectionalIterator>::value_type,
03689         typename iterator_traits<_BidirectionalIterator>::value_type>)
03690       __glibcxx_requires_valid_range(__first, __last);
03691 
03692       if (__first == __last)
03693     return false;
03694       _BidirectionalIterator __i = __first;
03695       ++__i;
03696       if (__i == __last)
03697     return false;
03698       __i = __last;
03699       --__i;
03700 
03701       for(;;)
03702     {
03703       _BidirectionalIterator __ii = __i;
03704       --__i;
03705       if (__comp(*__ii, *__i))
03706         {
03707           _BidirectionalIterator __j = __last;
03708           while (!bool(__comp(*--__j, *__i)))
03709         {}
03710           std::iter_swap(__i, __j);
03711           std::reverse(__ii, __last);
03712           return true;
03713         }
03714       if (__i == __first)
03715         {
03716           std::reverse(__first, __last);
03717           return false;
03718         }
03719     }
03720     }
03721 
03722   // replace
03723   // replace_if
03724 
03725   /**
03726    *  @brief Copy a sequence, replacing each element of one value with another
03727    *         value.
03728    *  @param  first      An input iterator.
03729    *  @param  last       An input iterator.
03730    *  @param  result     An output iterator.
03731    *  @param  old_value  The value to be replaced.
03732    *  @param  new_value  The replacement value.
03733    *  @return   The end of the output sequence, @p result+(last-first).
03734    *
03735    *  Copies each element in the input range @p [first,last) to the
03736    *  output range @p [result,result+(last-first)) replacing elements
03737    *  equal to @p old_value with @p new_value.
03738   */
03739   template<typename _InputIterator, typename _OutputIterator, typename _Tp>
03740     _OutputIterator
03741     replace_copy(_InputIterator __first, _InputIterator __last,
03742          _OutputIterator __result,
03743          const _Tp& __old_value, const _Tp& __new_value)
03744     {
03745       // concept requirements
03746       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03747       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
03748         typename iterator_traits<_InputIterator>::value_type>)
03749       __glibcxx_function_requires(_EqualOpConcept<
03750         typename iterator_traits<_InputIterator>::value_type, _Tp>)
03751       __glibcxx_requires_valid_range(__first, __last);
03752 
03753       for (; __first != __last; ++__first, ++__result)
03754     if (*__first == __old_value)
03755       *__result = __new_value;
03756     else
03757       *__result = *__first;
03758       return __result;
03759     }
03760 
03761   /**
03762    *  @brief Copy a sequence, replacing each value for which a predicate
03763    *         returns true with another value.
03764    *  @ingroup mutating_algorithms
03765    *  @param  first      An input iterator.
03766    *  @param  last       An input iterator.
03767    *  @param  result     An output iterator.
03768    *  @param  pred       A predicate.
03769    *  @param  new_value  The replacement value.
03770    *  @return   The end of the output sequence, @p result+(last-first).
03771    *
03772    *  Copies each element in the range @p [first,last) to the range
03773    *  @p [result,result+(last-first)) replacing elements for which
03774    *  @p pred returns true with @p new_value.
03775   */
03776   template<typename _InputIterator, typename _OutputIterator,
03777        typename _Predicate, typename _Tp>
03778     _OutputIterator
03779     replace_copy_if(_InputIterator __first, _InputIterator __last,
03780             _OutputIterator __result,
03781             _Predicate __pred, const _Tp& __new_value)
03782     {
03783       // concept requirements
03784       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03785       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
03786         typename iterator_traits<_InputIterator>::value_type>)
03787       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
03788         typename iterator_traits<_InputIterator>::value_type>)
03789       __glibcxx_requires_valid_range(__first, __last);
03790 
03791       for (; __first != __last; ++__first, ++__result)
03792     if (__pred(*__first))
03793       *__result = __new_value;
03794     else
03795       *__result = *__first;
03796       return __result;
03797     }
03798 
03799 #ifdef __GXX_EXPERIMENTAL_CXX0X__
03800   /**
03801    *  @brief  Determines whether the elements of a sequence are sorted.
03802    *  @ingroup sorting_algorithms
03803    *  @param  first   An iterator.
03804    *  @param  last    Another iterator.
03805    *  @return  True if the elements are sorted, false otherwise.
03806   */
03807   template<typename _ForwardIterator>
03808     inline bool
03809     is_sorted(_ForwardIterator __first, _ForwardIterator __last)
03810     { return std::is_sorted_until(__first, __last) == __last; }
03811 
03812   /**
03813    *  @brief  Determines whether the elements of a sequence are sorted
03814    *          according to a comparison functor.
03815    *  @ingroup sorting_algorithms
03816    *  @param  first   An iterator.
03817    *  @param  last    Another iterator.
03818    *  @param  comp    A comparison functor.
03819    *  @return  True if the elements are sorted, false otherwise.
03820   */
03821   template<typename _ForwardIterator, typename _Compare>
03822     inline bool
03823     is_sorted(_ForwardIterator __first, _ForwardIterator __last,
03824           _Compare __comp)
03825     { return std::is_sorted_until(__first, __last, __comp) == __last; }
03826 
03827   /**
03828    *  @brief  Determines the end of a sorted sequence.
03829    *  @ingroup sorting_algorithms
03830    *  @param  first   An iterator.
03831    *  @param  last    Another iterator.
03832    *  @return  An iterator pointing to the last iterator i in [first, last)
03833    *           for which the range [first, i) is sorted.
03834   */
03835   template<typename _ForwardIterator>
03836     _ForwardIterator
03837     is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
03838     {
03839       // concept requirements
03840       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03841       __glibcxx_function_requires(_LessThanComparableConcept<
03842         typename iterator_traits<_ForwardIterator>::value_type>)
03843       __glibcxx_requires_valid_range(__first, __last);
03844 
03845       if (__first == __last)
03846     return __last;
03847 
03848       _ForwardIterator __next = __first;
03849       for (++__next; __next != __last; __first = __next, ++__next)
03850     if (*__next < *__first)
03851       return __next;
03852       return __next;
03853     }
03854 
03855   /**
03856    *  @brief  Determines the end of a sorted sequence using comparison functor.
03857    *  @ingroup sorting_algorithms
03858    *  @param  first   An iterator.
03859    *  @param  last    Another iterator.
03860    *  @param  comp    A comparison functor.
03861    *  @return  An iterator pointing to the last iterator i in [first, last)
03862    *           for which the range [first, i) is sorted.
03863   */
03864   template<typename _ForwardIterator, typename _Compare>
03865     _ForwardIterator
03866     is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
03867             _Compare __comp)
03868     {
03869       // concept requirements
03870       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03871       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03872         typename iterator_traits<_ForwardIterator>::value_type,
03873         typename iterator_traits<_ForwardIterator>::value_type>)
03874       __glibcxx_requires_valid_range(__first, __last);
03875 
03876       if (__first == __last)
03877     return __last;
03878 
03879       _ForwardIterator __next = __first;
03880       for (++__next; __next != __last; __first = __next, ++__next)
03881     if (__comp(*__next, *__first))
03882       return __next;
03883       return __next;
03884     }
03885 
03886   /**
03887    *  @brief  Determines min and max at once as an ordered pair.
03888    *  @ingroup sorting_algorithms
03889    *  @param  a  A thing of arbitrary type.
03890    *  @param  b  Another thing of arbitrary type.
03891    *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
03892   */
03893   template<typename _Tp>
03894     inline pair<const _Tp&, const _Tp&>
03895     minmax(const _Tp& __a, const _Tp& __b)
03896     {
03897       // concept requirements
03898       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
03899 
03900       return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
03901                    : pair<const _Tp&, const _Tp&>(__a, __b);
03902     }
03903 
03904   /**
03905    *  @brief  Determines min and max at once as an ordered pair.
03906    *  @ingroup sorting_algorithms
03907    *  @param  a  A thing of arbitrary type.
03908    *  @param  b  Another thing of arbitrary type.
03909    *  @param  comp  A @link comparison_functor comparison functor@endlink.
03910    *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
03911   */
03912   template<typename _Tp, typename _Compare>
03913     inline pair<const _Tp&, const _Tp&>
03914     minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
03915     {
03916       return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
03917                           : pair<const _Tp&, const _Tp&>(__a, __b);
03918     }
03919 
03920   /**
03921    *  @brief  Return a pair of iterators pointing to the minimum and maximum
03922    *          elements in a range.
03923    *  @ingroup sorting_algorithms
03924    *  @param  first  Start of range.
03925    *  @param  last   End of range.
03926    *  @return  make_pair(m, M), where m is the first iterator i in 
03927    *           [first, last) such that no other element in the range is
03928    *           smaller, and where M is the last iterator i in [first, last)
03929    *           such that no other element in the range is larger.
03930   */
03931   template<typename _ForwardIterator>
03932     pair<_ForwardIterator, _ForwardIterator>
03933     minmax_element(_ForwardIterator __first, _ForwardIterator __last)
03934     {
03935       // concept requirements
03936       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03937       __glibcxx_function_requires(_LessThanComparableConcept<
03938         typename iterator_traits<_ForwardIterator>::value_type>)
03939       __glibcxx_requires_valid_range(__first, __last);
03940 
03941       _ForwardIterator __next = __first;
03942       if (__first == __last
03943       || ++__next == __last)
03944     return std::make_pair(__first, __first);
03945 
03946       _ForwardIterator __min, __max;
03947       if (*__next < *__first)
03948     {
03949       __min = __next;
03950       __max = __first;
03951     }
03952       else
03953     {
03954       __min = __first;
03955       __max = __next;
03956     }
03957 
03958       __first = __next;
03959       ++__first;
03960 
03961       while (__first != __last)
03962     {
03963       __next = __first;
03964       if (++__next == __last)
03965         {
03966           if (*__first < *__min)
03967         __min = __first;
03968           else if (!(*__first < *__max))
03969         __max = __first;
03970           break;
03971         }
03972 
03973       if (*__next < *__first)
03974         {
03975           if (*__next < *__min)
03976         __min = __next;
03977           if (!(*__first < *__max))
03978         __max = __first;
03979         }
03980       else
03981         {
03982           if (*__first < *__min)
03983         __min = __first;
03984           if (!(*__next < *__max))
03985         __max = __next;
03986         }
03987 
03988       __first = __next;
03989       ++__first;
03990     }
03991 
03992       return std::make_pair(__min, __max);
03993     }
03994 
03995   /**
03996    *  @brief  Return a pair of iterators pointing to the minimum and maximum
03997    *          elements in a range.
03998    *  @ingroup sorting_algorithms
03999    *  @param  first  Start of range.
04000    *  @param  last   End of range.
04001    *  @param  comp   Comparison functor.
04002    *  @return  make_pair(m, M), where m is the first iterator i in 
04003    *           [first, last) such that no other element in the range is
04004    *           smaller, and where M is the last iterator i in [first, last)
04005    *           such that no other element in the range is larger.
04006   */
04007   template<typename _ForwardIterator, typename _Compare>
04008     pair<_ForwardIterator, _ForwardIterator>
04009     minmax_element(_ForwardIterator __first, _ForwardIterator __last,
04010            _Compare __comp)
04011     {
04012       // concept requirements
04013       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04014       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
04015         typename iterator_traits<_ForwardIterator>::value_type,
04016         typename iterator_traits<_ForwardIterator>::value_type>)
04017       __glibcxx_requires_valid_range(__first, __last);
04018 
04019       _ForwardIterator __next = __first;
04020       if (__first == __last
04021       || ++__next == __last)
04022     return std::make_pair(__first, __first);
04023 
04024       _ForwardIterator __min, __max;
04025       if (__comp(*__next, *__first))
04026     {
04027       __min = __next;
04028       __max = __first;
04029     }
04030       else
04031     {
04032       __min = __first;
04033       __max = __next;
04034     }
04035 
04036       __first = __next;
04037       ++__first;
04038 
04039       while (__first != __last)
04040     {
04041       __next = __first;
04042       if (++__next == __last)
04043         {
04044           if (__comp(*__first, *__min))
04045         __min = __first;
04046           else if (!__comp(*__first, *__max))
04047         __max = __first;
04048           break;
04049         }
04050 
04051       if (__comp(*__next, *__first))
04052         {
04053           if (__comp(*__next, *__min))
04054         __min = __next;
04055           if (!__comp(*__first, *__max))
04056         __max = __first;
04057         }
04058       else
04059         {
04060           if (__comp(*__first, *__min))
04061         __min = __first;
04062           if (!__comp(*__next, *__max))
04063         __max = __next;
04064         }
04065 
04066       __first = __next;
04067       ++__first;
04068     }
04069 
04070       return std::make_pair(__min, __max);
04071     }
04072 
04073   // N2722 + DR 915.
04074   template<typename _Tp>
04075     inline _Tp
04076     min(initializer_list<_Tp> __l)
04077     { return *std::min_element(__l.begin(), __l.end()); }
04078 
04079   template<typename _Tp, typename _Compare>
04080     inline _Tp
04081     min(initializer_list<_Tp> __l, _Compare __comp)
04082     { return *std::min_element(__l.begin(), __l.end(), __comp); }
04083 
04084   template<typename _Tp>
04085     inline _Tp
04086     max(initializer_list<_Tp> __l)
04087     { return *std::max_element(__l.begin(), __l.end()); }
04088 
04089   template<typename _Tp, typename _Compare>
04090     inline _Tp
04091     max(initializer_list<_Tp> __l, _Compare __comp)
04092     { return *std::max_element(__l.begin(), __l.end(), __comp); }
04093 
04094   template<typename _Tp>
04095     inline pair<_Tp, _Tp>
04096     minmax(initializer_list<_Tp> __l)
04097     {
04098       pair<const _Tp*, const _Tp*> __p =
04099     std::minmax_element(__l.begin(), __l.end());
04100       return std::make_pair(*__p.first, *__p.second);
04101     }
04102 
04103   template<typename _Tp, typename _Compare>
04104     inline pair<_Tp, _Tp>
04105     minmax(initializer_list<_Tp> __l, _Compare __comp)
04106     {
04107       pair<const _Tp*, const _Tp*> __p =
04108     std::minmax_element(__l.begin(), __l.end(), __comp);
04109       return std::make_pair(*__p.first, *__p.second);
04110     }
04111 
04112 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
04113   /**
04114    *  @brief Shuffle the elements of a sequence using a uniform random
04115    *         number generator.
04116    *  @ingroup mutating_algorithms
04117    *  @param  first   A forward iterator.
04118    *  @param  last    A forward iterator.
04119    *  @param  g       A UniformRandomNumberGenerator (26.5.1.3).
04120    *  @return  Nothing.
04121    *
04122    *  Reorders the elements in the range @p [first,last) using @p g to
04123    *  provide random numbers.
04124   */
04125   template<typename _RandomAccessIterator,
04126        typename _UniformRandomNumberGenerator>
04127     void
04128     shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
04129         _UniformRandomNumberGenerator&& __g)
04130     {
04131       // concept requirements
04132       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04133         _RandomAccessIterator>)
04134       __glibcxx_requires_valid_range(__first, __last);
04135 
04136       if (__first == __last)
04137     return;
04138 
04139       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
04140     _DistanceType;
04141 
04142       typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
04143       typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
04144       typedef typename __distr_type::param_type __p_type;
04145       __distr_type __d;
04146 
04147       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
04148     std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
04149     }
04150 #endif
04151 
04152 #endif // __GXX_EXPERIMENTAL_CXX0X__
04153 
04154 _GLIBCXX_END_NAMESPACE
04155 
04156 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
04157 
04158   /**
04159    *  @brief Apply a function to every element of a sequence.
04160    *  @ingroup non_mutating_algorithms
04161    *  @param  first  An input iterator.
04162    *  @param  last   An input iterator.
04163    *  @param  f      A unary function object.
04164    *  @return   @p f (std::move(@p f) in C++0x).
04165    *
04166    *  Applies the function object @p f to each element in the range
04167    *  @p [first,last).  @p f must not modify the order of the sequence.
04168    *  If @p f has a return value it is ignored.
04169   */
04170   template<typename _InputIterator, typename _Function>
04171     _Function
04172     for_each(_InputIterator __first, _InputIterator __last, _Function __f)
04173     {
04174       // concept requirements
04175       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04176       __glibcxx_requires_valid_range(__first, __last);
04177       for (; __first != __last; ++__first)
04178     __f(*__first);
04179       return _GLIBCXX_MOVE(__f);
04180     }
04181 
04182   /**
04183    *  @brief Find the first occurrence of a value in a sequence.
04184    *  @ingroup non_mutating_algorithms
04185    *  @param  first  An input iterator.
04186    *  @param  last   An input iterator.
04187    *  @param  val    The value to find.
04188    *  @return   The first iterator @c i in the range @p [first,last)
04189    *  such that @c *i == @p val, or @p last if no such iterator exists.
04190   */
04191   template<typename _InputIterator, typename _Tp>
04192     inline _InputIterator
04193     find(_InputIterator __first, _InputIterator __last,
04194      const _Tp& __val)
04195     {
04196       // concept requirements
04197       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04198       __glibcxx_function_requires(_EqualOpConcept<
04199         typename iterator_traits<_InputIterator>::value_type, _Tp>)
04200       __glibcxx_requires_valid_range(__first, __last);
04201       return std::__find(__first, __last, __val,
04202                  std::__iterator_category(__first));
04203     }
04204 
04205   /**
04206    *  @brief Find the first element in a sequence for which a
04207    *         predicate is true.
04208    *  @ingroup non_mutating_algorithms
04209    *  @param  first  An input iterator.
04210    *  @param  last   An input iterator.
04211    *  @param  pred   A predicate.
04212    *  @return   The first iterator @c i in the range @p [first,last)
04213    *  such that @p pred(*i) is true, or @p last if no such iterator exists.
04214   */
04215   template<typename _InputIterator, typename _Predicate>
04216     inline _InputIterator
04217     find_if(_InputIterator __first, _InputIterator __last,
04218         _Predicate __pred)
04219     {
04220       // concept requirements
04221       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04222       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
04223           typename iterator_traits<_InputIterator>::value_type>)
04224       __glibcxx_requires_valid_range(__first, __last);
04225       return std::__find_if(__first, __last, __pred,
04226                 std::__iterator_category(__first));
04227     }
04228 
04229   /**
04230    *  @brief  Find element from a set in a sequence.
04231    *  @ingroup non_mutating_algorithms
04232    *  @param  first1  Start of range to search.
04233    *  @param  last1   End of range to search.
04234    *  @param  first2  Start of match candidates.
04235    *  @param  last2   End of match candidates.
04236    *  @return   The first iterator @c i in the range
04237    *  @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
04238    *  iterator in [first2,last2), or @p last1 if no such iterator exists.
04239    *
04240    *  Searches the range @p [first1,last1) for an element that is equal to
04241    *  some element in the range [first2,last2).  If found, returns an iterator
04242    *  in the range [first1,last1), otherwise returns @p last1.
04243   */
04244   template<typename _InputIterator, typename _ForwardIterator>
04245     _InputIterator
04246     find_first_of(_InputIterator __first1, _InputIterator __last1,
04247           _ForwardIterator __first2, _ForwardIterator __last2)
04248     {
04249       // concept requirements
04250       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04251       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04252       __glibcxx_function_requires(_EqualOpConcept<
04253         typename iterator_traits<_InputIterator>::value_type,
04254         typename iterator_traits<_ForwardIterator>::value_type>)
04255       __glibcxx_requires_valid_range(__first1, __last1);
04256       __glibcxx_requires_valid_range(__first2, __last2);
04257 
04258       for (; __first1 != __last1; ++__first1)
04259     for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
04260       if (*__first1 == *__iter)
04261         return __first1;
04262       return __last1;
04263     }
04264 
04265   /**
04266    *  @brief  Find element from a set in a sequence using a predicate.
04267    *  @ingroup non_mutating_algorithms
04268    *  @param  first1  Start of range to search.
04269    *  @param  last1   End of range to search.
04270    *  @param  first2  Start of match candidates.
04271    *  @param  last2   End of match candidates.
04272    *  @param  comp    Predicate to use.
04273    *  @return   The first iterator @c i in the range
04274    *  @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
04275    *  iterator in [first2,last2), or @p last1 if no such iterator exists.
04276    *
04277 
04278    *  Searches the range @p [first1,last1) for an element that is
04279    *  equal to some element in the range [first2,last2).  If found,
04280    *  returns an iterator in the range [first1,last1), otherwise
04281    *  returns @p last1.
04282   */
04283   template<typename _InputIterator, typename _ForwardIterator,
04284        typename _BinaryPredicate>
04285     _InputIterator
04286     find_first_of(_InputIterator __first1, _InputIterator __last1,
04287           _ForwardIterator __first2, _ForwardIterator __last2,
04288           _BinaryPredicate __comp)
04289     {
04290       // concept requirements
04291       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04292       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04293       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04294         typename iterator_traits<_InputIterator>::value_type,
04295         typename iterator_traits<_ForwardIterator>::value_type>)
04296       __glibcxx_requires_valid_range(__first1, __last1);
04297       __glibcxx_requires_valid_range(__first2, __last2);
04298 
04299       for (; __first1 != __last1; ++__first1)
04300     for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
04301       if (__comp(*__first1, *__iter))
04302         return __first1;
04303       return __last1;
04304     }
04305 
04306   /**
04307    *  @brief Find two adjacent values in a sequence that are equal.
04308    *  @ingroup non_mutating_algorithms
04309    *  @param  first  A forward iterator.
04310    *  @param  last   A forward iterator.
04311    *  @return   The first iterator @c i such that @c i and @c i+1 are both
04312    *  valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
04313    *  or @p last if no such iterator exists.
04314   */
04315   template<typename _ForwardIterator>
04316     _ForwardIterator
04317     adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
04318     {
04319       // concept requirements
04320       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04321       __glibcxx_function_requires(_EqualityComparableConcept<
04322         typename iterator_traits<_ForwardIterator>::value_type>)
04323       __glibcxx_requires_valid_range(__first, __last);
04324       if (__first == __last)
04325     return __last;
04326       _ForwardIterator __next = __first;
04327       while(++__next != __last)
04328     {
04329       if (*__first == *__next)
04330         return __first;
04331       __first = __next;
04332     }
04333       return __last;
04334     }
04335 
04336   /**
04337    *  @brief Find two adjacent values in a sequence using a predicate.
04338    *  @ingroup non_mutating_algorithms
04339    *  @param  first         A forward iterator.
04340    *  @param  last          A forward iterator.
04341    *  @param  binary_pred   A binary predicate.
04342    *  @return   The first iterator @c i such that @c i and @c i+1 are both
04343    *  valid iterators in @p [first,last) and such that
04344    *  @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
04345    *  exists.
04346   */
04347   template<typename _ForwardIterator, typename _BinaryPredicate>
04348     _ForwardIterator
04349     adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
04350           _BinaryPredicate __binary_pred)
04351     {
04352       // concept requirements
04353       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04354       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04355         typename iterator_traits<_ForwardIterator>::value_type,
04356         typename iterator_traits<_ForwardIterator>::value_type>)
04357       __glibcxx_requires_valid_range(__first, __last);
04358       if (__first == __last)
04359     return __last;
04360       _ForwardIterator __next = __first;
04361       while(++__next != __last)
04362     {
04363       if (__binary_pred(*__first, *__next))
04364         return __first;
04365       __first = __next;
04366     }
04367       return __last;
04368     }
04369 
04370   /**
04371    *  @brief Count the number of copies of a value in a sequence.
04372    *  @ingroup non_mutating_algorithms
04373    *  @param  first  An input iterator.
04374    *  @param  last   An input iterator.
04375    *  @param  value  The value to be counted.
04376    *  @return   The number of iterators @c i in the range @p [first,last)
04377    *  for which @c *i == @p value
04378   */
04379   template<typename _InputIterator, typename _Tp>
04380     typename iterator_traits<_InputIterator>::difference_type
04381     count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
04382     {
04383       // concept requirements
04384       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04385       __glibcxx_function_requires(_EqualOpConcept<
04386     typename iterator_traits<_InputIterator>::value_type, _Tp>)
04387       __glibcxx_requires_valid_range(__first, __last);
04388       typename iterator_traits<_InputIterator>::difference_type __n = 0;
04389       for (; __first != __last; ++__first)
04390     if (*__first == __value)
04391       ++__n;
04392       return __n;
04393     }
04394 
04395   /**
04396    *  @brief Count the elements of a sequence for which a predicate is true.
04397    *  @ingroup non_mutating_algorithms
04398    *  @param  first  An input iterator.
04399    *  @param  last   An input iterator.
04400    *  @param  pred   A predicate.
04401    *  @return   The number of iterators @c i in the range @p [first,last)
04402    *  for which @p pred(*i) is true.
04403   */
04404   template<typename _InputIterator, typename _Predicate>
04405     typename iterator_traits<_InputIterator>::difference_type
04406     count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
04407     {
04408       // concept requirements
04409       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04410       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
04411         typename iterator_traits<_InputIterator>::value_type>)
04412       __glibcxx_requires_valid_range(__first, __last);
04413       typename iterator_traits<_InputIterator>::difference_type __n = 0;
04414       for (; __first != __last; ++__first)
04415     if (__pred(*__first))
04416       ++__n;
04417       return __n;
04418     }
04419 
04420   /**
04421    *  @brief Search a sequence for a matching sub-sequence.
04422    *  @ingroup non_mutating_algorithms
04423    *  @param  first1  A forward iterator.
04424    *  @param  last1   A forward iterator.
04425    *  @param  first2  A forward iterator.
04426    *  @param  last2   A forward iterator.
04427    *  @return   The first iterator @c i in the range
04428    *  @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
04429    *  for each @c N in the range @p [0,last2-first2), or @p last1 if no
04430    *  such iterator exists.
04431    *
04432    *  Searches the range @p [first1,last1) for a sub-sequence that compares
04433    *  equal value-by-value with the sequence given by @p [first2,last2) and
04434    *  returns an iterator to the first element of the sub-sequence, or
04435    *  @p last1 if the sub-sequence is not found.
04436    *
04437    *  Because the sub-sequence must lie completely within the range
04438    *  @p [first1,last1) it must start at a position less than
04439    *  @p last1-(last2-first2) where @p last2-first2 is the length of the
04440    *  sub-sequence.
04441    *  This means that the returned iterator @c i will be in the range
04442    *  @p [first1,last1-(last2-first2))
04443   */
04444   template<typename _ForwardIterator1, typename _ForwardIterator2>
04445     _ForwardIterator1
04446     search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
04447        _ForwardIterator2 __first2, _ForwardIterator2 __last2)
04448     {
04449       // concept requirements
04450       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
04451       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
04452       __glibcxx_function_requires(_EqualOpConcept<
04453         typename iterator_traits<_ForwardIterator1>::value_type,
04454         typename iterator_traits<_ForwardIterator2>::value_type>)
04455       __glibcxx_requires_valid_range(__first1, __last1);
04456       __glibcxx_requires_valid_range(__first2, __last2);
04457 
04458       // Test for empty ranges
04459       if (__first1 == __last1 || __first2 == __last2)
04460     return __first1;
04461 
04462       // Test for a pattern of length 1.
04463       _ForwardIterator2 __p1(__first2);
04464       if (++__p1 == __last2)
04465     return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
04466 
04467       // General case.
04468       _ForwardIterator2 __p;
04469       _ForwardIterator1 __current = __first1;
04470 
04471       for (;;)
04472     {
04473       __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
04474       if (__first1 == __last1)
04475         return __last1;
04476 
04477       __p = __p1;
04478       __current = __first1;
04479       if (++__current == __last1)
04480         return __last1;
04481 
04482       while (*__current == *__p)
04483         {
04484           if (++__p == __last2)
04485         return __first1;
04486           if (++__current == __last1)
04487         return __last1;
04488         }
04489       ++__first1;
04490     }
04491       return __first1;
04492     }
04493 
04494   /**
04495    *  @brief Search a sequence for a matching sub-sequence using a predicate.
04496    *  @ingroup non_mutating_algorithms
04497    *  @param  first1     A forward iterator.
04498    *  @param  last1      A forward iterator.
04499    *  @param  first2     A forward iterator.
04500    *  @param  last2      A forward iterator.
04501    *  @param  predicate  A binary predicate.
04502    *  @return   The first iterator @c i in the range
04503    *  @p [first1,last1-(last2-first2)) such that
04504    *  @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
04505    *  @p [0,last2-first2), or @p last1 if no such iterator exists.
04506    *
04507    *  Searches the range @p [first1,last1) for a sub-sequence that compares
04508    *  equal value-by-value with the sequence given by @p [first2,last2),
04509    *  using @p predicate to determine equality, and returns an iterator
04510    *  to the first element of the sub-sequence, or @p last1 if no such
04511    *  iterator exists.
04512    *
04513    *  @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
04514   */
04515   template<typename _ForwardIterator1, typename _ForwardIterator2,
04516        typename _BinaryPredicate>
04517     _ForwardIterator1
04518     search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
04519        _ForwardIterator2 __first2, _ForwardIterator2 __last2,
04520        _BinaryPredicate  __predicate)
04521     {
04522       // concept requirements
04523       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
04524       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
04525       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04526         typename iterator_traits<_ForwardIterator1>::value_type,
04527         typename iterator_traits<_ForwardIterator2>::value_type>)
04528       __glibcxx_requires_valid_range(__first1, __last1);
04529       __glibcxx_requires_valid_range(__first2, __last2);
04530 
04531       // Test for empty ranges
04532       if (__first1 == __last1 || __first2 == __last2)
04533     return __first1;
04534 
04535       // Test for a pattern of length 1.
04536       _ForwardIterator2 __p1(__first2);
04537       if (++__p1 == __last2)
04538     {
04539       while (__first1 != __last1
04540          && !bool(__predicate(*__first1, *__first2)))
04541         ++__first1;
04542       return __first1;
04543     }
04544 
04545       // General case.
04546       _ForwardIterator2 __p;
04547       _ForwardIterator1 __current = __first1;
04548 
04549       for (;;)
04550     {
04551       while (__first1 != __last1
04552          && !bool(__predicate(*__first1, *__first2)))
04553         ++__first1;
04554       if (__first1 == __last1)
04555         return __last1;
04556 
04557       __p = __p1;
04558       __current = __first1;
04559       if (++__current == __last1)
04560         return __last1;
04561 
04562       while (__predicate(*__current, *__p))
04563         {
04564           if (++__p == __last2)
04565         return __first1;
04566           if (++__current == __last1)
04567         return __last1;
04568         }
04569       ++__first1;
04570     }
04571       return __first1;
04572     }
04573 
04574 
04575   /**
04576    *  @brief Search a sequence for a number of consecutive values.
04577    *  @ingroup non_mutating_algorithms
04578    *  @param  first  A forward iterator.
04579    *  @param  last   A forward iterator.
04580    *  @param  count  The number of consecutive values.
04581    *  @param  val    The value to find.
04582    *  @return   The first iterator @c i in the range @p [first,last-count)
04583    *  such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
04584    *  or @p last if no such iterator exists.
04585    *
04586    *  Searches the range @p [first,last) for @p count consecutive elements
04587    *  equal to @p val.
04588   */
04589   template<typename _ForwardIterator, typename _Integer, typename _Tp>
04590     _ForwardIterator
04591     search_n(_ForwardIterator __first, _ForwardIterator __last,
04592          _Integer __count, const _Tp& __val)
04593     {
04594       // concept requirements
04595       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04596       __glibcxx_function_requires(_EqualOpConcept<
04597     typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
04598       __glibcxx_requires_valid_range(__first, __last);
04599 
04600       if (__count <= 0)
04601     return __first;
04602       if (__count == 1)
04603     return _GLIBCXX_STD_P::find(__first, __last, __val);
04604       return std::__search_n(__first, __last, __count, __val,
04605                  std::__iterator_category(__first));
04606     }
04607 
04608 
04609   /**
04610    *  @brief Search a sequence for a number of consecutive values using a
04611    *         predicate.
04612    *  @ingroup non_mutating_algorithms
04613    *  @param  first        A forward iterator.
04614    *  @param  last         A forward iterator.
04615    *  @param  count        The number of consecutive values.
04616    *  @param  val          The value to find.
04617    *  @param  binary_pred  A binary predicate.
04618    *  @return   The first iterator @c i in the range @p [first,last-count)
04619    *  such that @p binary_pred(*(i+N),val) is true for each @c N in the
04620    *  range @p [0,count), or @p last if no such iterator exists.
04621    *
04622    *  Searches the range @p [first,last) for @p count consecutive elements
04623    *  for which the predicate returns true.
04624   */
04625   template<typename _ForwardIterator, typename _Integer, typename _Tp,
04626            typename _BinaryPredicate>
04627     _ForwardIterator
04628     search_n(_ForwardIterator __first, _ForwardIterator __last,
04629          _Integer __count, const _Tp& __val,
04630          _BinaryPredicate __binary_pred)
04631     {
04632       // concept requirements
04633       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04634       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04635         typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
04636       __glibcxx_requires_valid_range(__first, __last);
04637 
04638       if (__count <= 0)
04639     return __first;
04640       if (__count == 1)
04641     {
04642       while (__first != __last && !bool(__binary_pred(*__first, __val)))
04643         ++__first;
04644       return __first;
04645     }
04646       return std::__search_n(__first, __last, __count, __val, __binary_pred,
04647                  std::__iterator_category(__first));
04648     }
04649 
04650 
04651   /**
04652    *  @brief Perform an operation on a sequence.
04653    *  @ingroup mutating_algorithms
04654    *  @param  first     An input iterator.
04655    *  @param  last      An input iterator.
04656    *  @param  result    An output iterator.
04657    *  @param  unary_op  A unary operator.
04658    *  @return   An output iterator equal to @p result+(last-first).
04659    *
04660    *  Applies the operator to each element in the input range and assigns
04661    *  the results to successive elements of the output sequence.
04662    *  Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
04663    *  range @p [0,last-first).
04664    *
04665    *  @p unary_op must not alter its argument.
04666   */
04667   template<typename _InputIterator, typename _OutputIterator,
04668        typename _UnaryOperation>
04669     _OutputIterator
04670     transform(_InputIterator __first, _InputIterator __last,
04671           _OutputIterator __result, _UnaryOperation __unary_op)
04672     {
04673       // concept requirements
04674       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04675       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04676             // "the type returned by a _UnaryOperation"
04677             __typeof__(__unary_op(*__first))>)
04678       __glibcxx_requires_valid_range(__first, __last);
04679 
04680       for (; __first != __last; ++__first, ++__result)
04681     *__result = __unary_op(*__first);
04682       return __result;
04683     }
04684 
04685   /**
04686    *  @brief Perform an operation on corresponding elements of two sequences.
04687    *  @ingroup mutating_algorithms
04688    *  @param  first1     An input iterator.
04689    *  @param  last1      An input iterator.
04690    *  @param  first2     An input iterator.
04691    *  @param  result     An output iterator.
04692    *  @param  binary_op  A binary operator.
04693    *  @return   An output iterator equal to @p result+(last-first).
04694    *
04695    *  Applies the operator to the corresponding elements in the two
04696    *  input ranges and assigns the results to successive elements of the
04697    *  output sequence.
04698    *  Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
04699    *  @c N in the range @p [0,last1-first1).
04700    *
04701    *  @p binary_op must not alter either of its arguments.
04702   */
04703   template<typename _InputIterator1, typename _InputIterator2,
04704        typename _OutputIterator, typename _BinaryOperation>
04705     _OutputIterator
04706     transform(_InputIterator1 __first1, _InputIterator1 __last1,
04707           _InputIterator2 __first2, _OutputIterator __result,
04708           _BinaryOperation __binary_op)
04709     {
04710       // concept requirements
04711       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
04712       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
04713       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04714             // "the type returned by a _BinaryOperation"
04715             __typeof__(__binary_op(*__first1,*__first2))>)
04716       __glibcxx_requires_valid_range(__first1, __last1);
04717 
04718       for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
04719     *__result = __binary_op(*__first1, *__first2);
04720       return __result;
04721     }
04722 
04723   /**
04724    *  @brief Replace each occurrence of one value in a sequence with another
04725    *         value.
04726    *  @ingroup mutating_algorithms
04727    *  @param  first      A forward iterator.
04728    *  @param  last       A forward iterator.
04729    *  @param  old_value  The value to be replaced.
04730    *  @param  new_value  The replacement value.
04731    *  @return   replace() returns no value.
04732    *
04733    *  For each iterator @c i in the range @p [first,last) if @c *i ==
04734    *  @p old_value then the assignment @c *i = @p new_value is performed.
04735   */
04736   template<typename _ForwardIterator, typename _Tp>
04737     void
04738     replace(_ForwardIterator __first, _ForwardIterator __last,
04739         const _Tp& __old_value, const _Tp& __new_value)
04740     {
04741       // concept requirements
04742       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
04743                   _ForwardIterator>)
04744       __glibcxx_function_requires(_EqualOpConcept<
04745         typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
04746       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
04747         typename iterator_traits<_ForwardIterator>::value_type>)
04748       __glibcxx_requires_valid_range(__first, __last);
04749 
04750       for (; __first != __last; ++__first)
04751     if (*__first == __old_value)
04752       *__first = __new_value;
04753     }
04754 
04755   /**
04756    *  @brief Replace each value in a sequence for which a predicate returns
04757    *         true with another value.
04758    *  @ingroup mutating_algorithms
04759    *  @param  first      A forward iterator.
04760    *  @param  last       A forward iterator.
04761    *  @param  pred       A predicate.
04762    *  @param  new_value  The replacement value.
04763    *  @return   replace_if() returns no value.
04764    *
04765    *  For each iterator @c i in the range @p [first,last) if @p pred(*i)
04766    *  is true then the assignment @c *i = @p new_value is performed.
04767   */
04768   template<typename _ForwardIterator, typename _Predicate, typename _Tp>
04769     void
04770     replace_if(_ForwardIterator __first, _ForwardIterator __last,
04771            _Predicate __pred, const _Tp& __new_value)
04772     {
04773       // concept requirements
04774       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
04775                   _ForwardIterator>)
04776       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
04777         typename iterator_traits<_ForwardIterator>::value_type>)
04778       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
04779         typename iterator_traits<_ForwardIterator>::value_type>)
04780       __glibcxx_requires_valid_range(__first, __last);
04781 
04782       for (; __first != __last; ++__first)
04783     if (__pred(*__first))
04784       *__first = __new_value;
04785     }
04786 
04787   /**
04788    *  @brief Assign the result of a function object to each value in a
04789    *         sequence.
04790    *  @ingroup mutating_algorithms
04791    *  @param  first  A forward iterator.
04792    *  @param  last   A forward iterator.
04793    *  @param  gen    A function object taking no arguments and returning
04794    *                 std::iterator_traits<_ForwardIterator>::value_type
04795    *  @return   generate() returns no value.
04796    *
04797    *  Performs the assignment @c *i = @p gen() for each @c i in the range
04798    *  @p [first,last).
04799   */
04800   template<typename _ForwardIterator, typename _Generator>
04801     void
04802     generate(_ForwardIterator __first, _ForwardIterator __last,
04803          _Generator __gen)
04804     {
04805       // concept requirements
04806       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04807       __glibcxx_function_requires(_GeneratorConcept<_Generator,
04808         typename iterator_traits<_ForwardIterator>::value_type>)
04809       __glibcxx_requires_valid_range(__first, __last);
04810 
04811       for (; __first != __last; ++__first)
04812     *__first = __gen();
04813     }
04814 
04815   /**
04816    *  @brief Assign the result of a function object to each value in a
04817    *         sequence.
04818    *  @ingroup mutating_algorithms
04819    *  @param  first  A forward iterator.
04820    *  @param  n      The length of the sequence.
04821    *  @param  gen    A function object taking no arguments and returning
04822    *                 std::iterator_traits<_ForwardIterator>::value_type
04823    *  @return   The end of the sequence, @p first+n
04824    *
04825    *  Performs the assignment @c *i = @p gen() for each @c i in the range
04826    *  @p [first,first+n).
04827    *
04828    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
04829    *  DR 865. More algorithms that throw away information
04830   */
04831   template<typename _OutputIterator, typename _Size, typename _Generator>
04832     _OutputIterator
04833     generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
04834     {
04835       // concept requirements
04836       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04837             // "the type returned by a _Generator"
04838             __typeof__(__gen())>)
04839 
04840       for (__decltype(__n + 0) __niter = __n;
04841        __niter > 0; --__niter, ++__first)
04842     *__first = __gen();
04843       return __first;
04844     }
04845 
04846 
04847   /**
04848    *  @brief Copy a sequence, removing consecutive duplicate values.
04849    *  @ingroup mutating_algorithms
04850    *  @param  first   An input iterator.
04851    *  @param  last    An input iterator.
04852    *  @param  result  An output iterator.
04853    *  @return   An iterator designating the end of the resulting sequence.
04854    *
04855    *  Copies each element in the range @p [first,last) to the range
04856    *  beginning at @p result, except that only the first element is copied
04857    *  from groups of consecutive elements that compare equal.
04858    *  unique_copy() is stable, so the relative order of elements that are
04859    *  copied is unchanged.
04860    *
04861    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
04862    *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
04863    *  
04864    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
04865    *  DR 538. 241 again: Does unique_copy() require CopyConstructible and 
04866    *  Assignable?
04867   */
04868   template<typename _InputIterator, typename _OutputIterator>
04869     inline _OutputIterator
04870     unique_copy(_InputIterator __first, _InputIterator __last,
04871         _OutputIterator __result)
04872     {
04873       // concept requirements
04874       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04875       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04876         typename iterator_traits<_InputIterator>::value_type>)
04877       __glibcxx_function_requires(_EqualityComparableConcept<
04878         typename iterator_traits<_InputIterator>::value_type>)
04879       __glibcxx_requires_valid_range(__first, __last);
04880 
04881       if (__first == __last)
04882     return __result;
04883       return std::__unique_copy(__first, __last, __result,
04884                 std::__iterator_category(__first),
04885                 std::__iterator_category(__result));
04886     }
04887 
04888   /**
04889    *  @brief Copy a sequence, removing consecutive values using a predicate.
04890    *  @ingroup mutating_algorithms
04891    *  @param  first        An input iterator.
04892    *  @param  last         An input iterator.
04893    *  @param  result       An output iterator.
04894    *  @param  binary_pred  A binary predicate.
04895    *  @return   An iterator designating the end of the resulting sequence.
04896    *
04897    *  Copies each element in the range @p [first,last) to the range
04898    *  beginning at @p result, except that only the first element is copied
04899    *  from groups of consecutive elements for which @p binary_pred returns
04900    *  true.
04901    *  unique_copy() is stable, so the relative order of elements that are
04902    *  copied is unchanged.
04903    *
04904    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
04905    *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
04906   */
04907   template<typename _InputIterator, typename _OutputIterator,
04908        typename _BinaryPredicate>
04909     inline _OutputIterator
04910     unique_copy(_InputIterator __first, _InputIterator __last,
04911         _OutputIterator __result,
04912         _BinaryPredicate __binary_pred)
04913     {
04914       // concept requirements -- predicates checked later
04915       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04916       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04917         typename iterator_traits<_InputIterator>::value_type>)
04918       __glibcxx_requires_valid_range(__first, __last);
04919 
04920       if (__first == __last)
04921     return __result;
04922       return std::__unique_copy(__first, __last, __result, __binary_pred,
04923                 std::__iterator_category(__first),
04924                 std::__iterator_category(__result));
04925     }
04926 
04927 
04928   /**
04929    *  @brief Randomly shuffle the elements of a sequence.
04930    *  @ingroup mutating_algorithms
04931    *  @param  first   A forward iterator.
04932    *  @param  last    A forward iterator.
04933    *  @return  Nothing.
04934    *
04935    *  Reorder the elements in the range @p [first,last) using a random
04936    *  distribution, so that every possible ordering of the sequence is
04937    *  equally likely.
04938   */
04939   template<typename _RandomAccessIterator>
04940     inline void
04941     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
04942     {
04943       // concept requirements
04944       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04945         _RandomAccessIterator>)
04946       __glibcxx_requires_valid_range(__first, __last);
04947 
04948       if (__first != __last)
04949     for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
04950       std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
04951     }
04952 
04953   /**
04954    *  @brief Shuffle the elements of a sequence using a random number
04955    *         generator.
04956    *  @ingroup mutating_algorithms
04957    *  @param  first   A forward iterator.
04958    *  @param  last    A forward iterator.
04959    *  @param  rand    The RNG functor or function.
04960    *  @return  Nothing.
04961    *
04962    *  Reorders the elements in the range @p [first,last) using @p rand to
04963    *  provide a random distribution. Calling @p rand(N) for a positive
04964    *  integer @p N should return a randomly chosen integer from the
04965    *  range [0,N).
04966   */
04967   template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
04968     void
04969     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
04970 #ifdef __GXX_EXPERIMENTAL_CXX0X__
04971            _RandomNumberGenerator&& __rand)
04972 #else
04973            _RandomNumberGenerator& __rand)
04974 #endif
04975     {
04976       // concept requirements
04977       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04978         _RandomAccessIterator>)
04979       __glibcxx_requires_valid_range(__first, __last);
04980 
04981       if (__first == __last)
04982     return;
04983       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
04984     std::iter_swap(__i, __first + __rand((__i - __first) + 1));
04985     }
04986 
04987 
04988   /**
04989    *  @brief Move elements for which a predicate is true to the beginning
04990    *         of a sequence.
04991    *  @ingroup mutating_algorithms
04992    *  @param  first   A forward iterator.
04993    *  @param  last    A forward iterator.
04994    *  @param  pred    A predicate functor.
04995    *  @return  An iterator @p middle such that @p pred(i) is true for each
04996    *  iterator @p i in the range @p [first,middle) and false for each @p i
04997    *  in the range @p [middle,last).
04998    *
04999    *  @p pred must not modify its operand. @p partition() does not preserve
05000    *  the relative ordering of elements in each group, use
05001    *  @p stable_partition() if this is needed.
05002   */
05003   template<typename _ForwardIterator, typename _Predicate>
05004     inline _ForwardIterator
05005     partition(_ForwardIterator __first, _ForwardIterator __last,
05006           _Predicate   __pred)
05007     {
05008       // concept requirements
05009       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
05010                   _ForwardIterator>)
05011       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
05012         typename iterator_traits<_ForwardIterator>::value_type>)
05013       __glibcxx_requires_valid_range(__first, __last);
05014 
05015       return std::__partition(__first, __last, __pred,
05016                   std::__iterator_category(__first));
05017     }
05018 
05019 
05020 
05021   /**
05022    *  @brief Sort the smallest elements of a sequence.
05023    *  @ingroup sorting_algorithms
05024    *  @param  first   An iterator.
05025    *  @param  middle  Another iterator.
05026    *  @param  last    Another iterator.
05027    *  @return  Nothing.
05028    *
05029    *  Sorts the smallest @p (middle-first) elements in the range
05030    *  @p [first,last) and moves them to the range @p [first,middle). The
05031    *  order of the remaining elements in the range @p [middle,last) is
05032    *  undefined.
05033    *  After the sort if @p i and @j are iterators in the range
05034    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
05035    *  the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
05036   */
05037   template<typename _RandomAccessIterator>
05038     inline void
05039     partial_sort(_RandomAccessIterator __first,
05040          _RandomAccessIterator __middle,
05041          _RandomAccessIterator __last)
05042     {
05043       typedef typename iterator_traits<_RandomAccessIterator>::value_type
05044     _ValueType;
05045 
05046       // concept requirements
05047       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05048         _RandomAccessIterator>)
05049       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
05050       __glibcxx_requires_valid_range(__first, __middle);
05051       __glibcxx_requires_valid_range(__middle, __last);
05052 
05053       std::__heap_select(__first, __middle, __last);
05054       std::sort_heap(__first, __middle);
05055     }
05056 
05057   /**
05058    *  @brief Sort the smallest elements of a sequence using a predicate
05059    *         for comparison.
05060    *  @ingroup sorting_algorithms
05061    *  @param  first   An iterator.
05062    *  @param  middle  Another iterator.
05063    *  @param  last    Another iterator.
05064    *  @param  comp    A comparison functor.
05065    *  @return  Nothing.
05066    *
05067    *  Sorts the smallest @p (middle-first) elements in the range
05068    *  @p [first,last) and moves them to the range @p [first,middle). The
05069    *  order of the remaining elements in the range @p [middle,last) is
05070    *  undefined.
05071    *  After the sort if @p i and @j are iterators in the range
05072    *  @p [first,middle) such that @i precedes @j and @k is an iterator in
05073    *  the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
05074    *  are both false.
05075   */
05076   template<typename _RandomAccessIterator, typename _Compare>
05077     inline void
05078     partial_sort(_RandomAccessIterator __first,
05079          _RandomAccessIterator __middle,
05080          _RandomAccessIterator __last,
05081          _Compare __comp)
05082     {
05083       typedef typename iterator_traits<_RandomAccessIterator>::value_type
05084     _ValueType;
05085 
05086       // concept requirements
05087       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05088         _RandomAccessIterator>)
05089       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05090                   _ValueType, _ValueType>)
05091       __glibcxx_requires_valid_range(__first, __middle);
05092       __glibcxx_requires_valid_range(__middle, __last);
05093 
05094       std::__heap_select(__first, __middle, __last, __comp);
05095       std::sort_heap(__first, __middle, __comp);
05096     }
05097 
05098   /**
05099    *  @brief Sort a sequence just enough to find a particular position.
05100    *  @ingroup sorting_algorithms
05101    *  @param  first   An iterator.
05102    *  @param  nth     Another iterator.
05103    *  @param  last    Another iterator.
05104    *  @return  Nothing.
05105    *
05106    *  Rearranges the elements in the range @p [first,last) so that @p *nth
05107    *  is the same element that would have been in that position had the
05108    *  whole sequence been sorted.
05109    *  whole sequence been sorted. The elements either side of @p *nth are
05110    *  not completely sorted, but for any iterator @i in the range
05111    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
05112    *  holds that @p *j<*i is false.
05113   */
05114   template<typename _RandomAccessIterator>
05115     inline void
05116     nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
05117         _RandomAccessIterator __last)
05118     {
05119       typedef typename iterator_traits<_RandomAccessIterator>::value_type
05120     _ValueType;
05121 
05122       // concept requirements
05123       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05124                   _RandomAccessIterator>)
05125       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
05126       __glibcxx_requires_valid_range(__first, __nth);
05127       __glibcxx_requires_valid_range(__nth, __last);
05128 
05129       if (__first == __last || __nth == __last)
05130     return;
05131 
05132       std::__introselect(__first, __nth, __last,
05133              std::__lg(__last - __first) * 2);
05134     }
05135 
05136   /**
05137    *  @brief Sort a sequence just enough to find a particular position
05138    *         using a predicate for comparison.
05139    *  @ingroup sorting_algorithms
05140    *  @param  first   An iterator.
05141    *  @param  nth     Another iterator.
05142    *  @param  last    Another iterator.
05143    *  @param  comp    A comparison functor.
05144    *  @return  Nothing.
05145    *
05146    *  Rearranges the elements in the range @p [first,last) so that @p *nth
05147    *  is the same element that would have been in that position had the
05148    *  whole sequence been sorted. The elements either side of @p *nth are
05149    *  not completely sorted, but for any iterator @i in the range
05150    *  @p [first,nth) and any iterator @j in the range @p [nth,last) it
05151    *  holds that @p comp(*j,*i) is false.
05152   */
05153   template<typename _RandomAccessIterator, typename _Compare>
05154     inline void
05155     nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
05156         _RandomAccessIterator __last, _Compare __comp)
05157     {
05158       typedef typename iterator_traits<_RandomAccessIterator>::value_type
05159     _ValueType;
05160 
05161       // concept requirements
05162       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05163                   _RandomAccessIterator>)
05164       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05165                   _ValueType, _ValueType>)
05166       __glibcxx_requires_valid_range(__first, __nth);
05167       __glibcxx_requires_valid_range(__nth, __last);
05168 
05169       if (__first == __last || __nth == __last)
05170     return;
05171 
05172       std::__introselect(__first, __nth, __last,
05173              std::__lg(__last - __first) * 2, __comp);
05174     }
05175 
05176 
05177   /**
05178    *  @brief Sort the elements of a sequence.
05179    *  @ingroup sorting_algorithms
05180    *  @param  first   An iterator.
05181    *  @param  last    Another iterator.
05182    *  @return  Nothing.
05183    *
05184    *  Sorts the elements in the range @p [first,last) in ascending order,
05185    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
05186    *  @p [first,last-1).
05187    *
05188    *  The relative ordering of equivalent elements is not preserved, use
05189    *  @p stable_sort() if this is needed.
05190   */
05191   template<typename _RandomAccessIterator>
05192     inline void
05193     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
05194     {
05195       typedef typename iterator_traits<_RandomAccessIterator>::value_type
05196     _ValueType;
05197 
05198       // concept requirements
05199       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05200         _RandomAccessIterator>)
05201       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
05202       __glibcxx_requires_valid_range(__first, __last);
05203 
05204       if (__first != __last)
05205     {
05206       std::__introsort_loop(__first, __last,
05207                 std::__lg(__last - __first) * 2);
05208       std::__final_insertion_sort(__first, __last);
05209     }
05210     }
05211 
05212   /**
05213    *  @brief Sort the elements of a sequence using a predicate for comparison.
05214    *  @ingroup sorting_algorithms
05215    *  @param  first   An iterator.
05216    *  @param  last    Another iterator.
05217    *  @param  comp    A comparison functor.
05218    *  @return  Nothing.
05219    *
05220    *  Sorts the elements in the range @p [first,last) in ascending order,
05221    *  such that @p comp(*(i+1),*i) is false for every iterator @p i in the
05222    *  range @p [first,last-1).
05223    *
05224    *  The relative ordering of equivalent elements is not preserved, use
05225    *  @p stable_sort() if this is needed.
05226   */
05227   template<typename _RandomAccessIterator, typename _Compare>
05228     inline void
05229     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
05230      _Compare __comp)
05231     {
05232       typedef typename iterator_traits<_RandomAccessIterator>::value_type
05233     _ValueType;
05234 
05235       // concept requirements
05236       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05237         _RandomAccessIterator>)
05238       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
05239                   _ValueType>)
05240       __glibcxx_requires_valid_range(__first, __last);
05241 
05242       if (__first != __last)
05243     {
05244       std::__introsort_loop(__first, __last,
05245                 std::__lg(__last - __first) * 2, __comp);
05246       std::__final_insertion_sort(__first, __last, __comp);
05247     }
05248     }
05249 
05250   /**
05251    *  @brief Merges two sorted ranges.
05252    *  @ingroup sorting_algorithms
05253    *  @param  first1  An iterator.
05254    *  @param  first2  Another iterator.
05255    *  @param  last1   Another iterator.
05256    *  @param  last2   Another iterator.
05257    *  @param  result  An iterator pointing to the end of the merged range.
05258    *  @return         An iterator pointing to the first element <em>not less
05259    *                  than</em> @a val.
05260    *
05261    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
05262    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
05263    *  must be sorted, and the output range must not overlap with either of
05264    *  the input ranges.  The sort is @e stable, that is, for equivalent
05265    *  elements in the two ranges, elements from the first range will always
05266    *  come before elements from the second.
05267   */
05268   template<typename _InputIterator1, typename _InputIterator2,
05269        typename _OutputIterator>
05270     _OutputIterator
05271     merge(_InputIterator1 __first1, _InputIterator1 __last1,
05272       _InputIterator2 __first2, _InputIterator2 __last2,
05273       _OutputIterator __result)
05274     {
05275       typedef typename iterator_traits<_InputIterator1>::value_type
05276     _ValueType1;
05277       typedef typename iterator_traits<_InputIterator2>::value_type
05278     _ValueType2;
05279 
05280       // concept requirements
05281       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05282       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05283       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05284                   _ValueType1>)
05285       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05286                   _ValueType2>)
05287       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
05288       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05289       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05290 
05291       while (__first1 != __last1 && __first2 != __last2)
05292     {
05293       if (*__first2 < *__first1)
05294         {
05295           *__result = *__first2;
05296           ++__first2;
05297         }
05298       else
05299         {
05300           *__result = *__first1;
05301           ++__first1;
05302         }
05303       ++__result;
05304     }
05305       return std::copy(__first2, __last2, std::copy(__first1, __last1,
05306                             __result));
05307     }
05308 
05309   /**
05310    *  @brief Merges two sorted ranges.
05311    *  @ingroup sorting_algorithms
05312    *  @param  first1  An iterator.
05313    *  @param  first2  Another iterator.
05314    *  @param  last1   Another iterator.
05315    *  @param  last2   Another iterator.
05316    *  @param  result  An iterator pointing to the end of the merged range.
05317    *  @param  comp    A functor to use for comparisons.
05318    *  @return         An iterator pointing to the first element "not less
05319    *                  than" @a val.
05320    *
05321    *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
05322    *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
05323    *  must be sorted, and the output range must not overlap with either of
05324    *  the input ranges.  The sort is @e stable, that is, for equivalent
05325    *  elements in the two ranges, elements from the first range will always
05326    *  come before elements from the second.
05327    *
05328    *  The comparison function should have the same effects on ordering as
05329    *  the function used for the initial sort.
05330   */
05331   template<typename _InputIterator1, typename _InputIterator2,
05332        typename _OutputIterator, typename _Compare>
05333     _OutputIterator
05334     merge(_InputIterator1 __first1, _InputIterator1 __last1,
05335       _InputIterator2 __first2, _InputIterator2 __last2,
05336       _OutputIterator __result, _Compare __comp)
05337     {
05338       typedef typename iterator_traits<_InputIterator1>::value_type
05339     _ValueType1;
05340       typedef typename iterator_traits<_InputIterator2>::value_type
05341     _ValueType2;
05342 
05343       // concept requirements
05344       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05345       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05346       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05347                   _ValueType1>)
05348       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05349                   _ValueType2>)
05350       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05351                   _ValueType2, _ValueType1>)
05352       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05353       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05354 
05355       while (__first1 != __last1 && __first2 != __last2)
05356     {
05357       if (__comp(*__first2, *__first1))
05358         {
05359           *__result = *__first2;
05360           ++__first2;
05361         }
05362       else
05363         {
05364           *__result = *__first1;
05365           ++__first1;
05366         }
05367       ++__result;
05368     }
05369       return std::copy(__first2, __last2, std::copy(__first1, __last1,
05370                             __result));
05371     }
05372 
05373 
05374   /**
05375    *  @brief Sort the elements of a sequence, preserving the relative order
05376    *         of equivalent elements.
05377    *  @ingroup sorting_algorithms
05378    *  @param  first   An iterator.
05379    *  @param  last    Another iterator.
05380    *  @return  Nothing.
05381    *
05382    *  Sorts the elements in the range @p [first,last) in ascending order,
05383    *  such that @p *(i+1)<*i is false for each iterator @p i in the range
05384    *  @p [first,last-1).
05385    *
05386    *  The relative ordering of equivalent elements is preserved, so any two
05387    *  elements @p x and @p y in the range @p [first,last) such that
05388    *  @p x<y is false and @p y<x is false will have the same relative
05389    *  ordering after calling @p stable_sort().
05390   */
05391   template<typename _RandomAccessIterator>
05392     inline void
05393     stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
05394     {
05395       typedef typename iterator_traits<_RandomAccessIterator>::value_type
05396     _ValueType;
05397       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
05398     _DistanceType;
05399 
05400       // concept requirements
05401       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05402         _RandomAccessIterator>)
05403       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
05404       __glibcxx_requires_valid_range(__first, __last);
05405 
05406       _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
05407                                  __last);
05408       if (__buf.begin() == 0)
05409     std::__inplace_stable_sort(__first, __last);
05410       else
05411     std::__stable_sort_adaptive(__first, __last, __buf.begin(),
05412                     _DistanceType(__buf.size()));
05413     }
05414 
05415   /**
05416    *  @brief Sort the elements of a sequence using a predicate for comparison,
05417    *         preserving the relative order of equivalent elements.
05418    *  @ingroup sorting_algorithms
05419    *  @param  first   An iterator.
05420    *  @param  last    Another iterator.
05421    *  @param  comp    A comparison functor.
05422    *  @return  Nothing.
05423    *
05424    *  Sorts the elements in the range @p [first,last) in ascending order,
05425    *  such that @p comp(*(i+1),*i) is false for each iterator @p i in the
05426    *  range @p [first,last-1).
05427    *
05428    *  The relative ordering of equivalent elements is preserved, so any two
05429    *  elements @p x and @p y in the range @p [first,last) such that
05430    *  @p comp(x,y) is false and @p comp(y,x) is false will have the same
05431    *  relative ordering after calling @p stable_sort().
05432   */
05433   template<typename _RandomAccessIterator, typename _Compare>
05434     inline void
05435     stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
05436         _Compare __comp)
05437     {
05438       typedef typename iterator_traits<_RandomAccessIterator>::value_type
05439     _ValueType;
05440       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
05441     _DistanceType;
05442 
05443       // concept requirements
05444       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05445         _RandomAccessIterator>)
05446       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05447                   _ValueType,
05448                   _ValueType>)
05449       __glibcxx_requires_valid_range(__first, __last);
05450 
05451       _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
05452                                  __last);
05453       if (__buf.begin() == 0)
05454     std::__inplace_stable_sort(__first, __last, __comp);
05455       else
05456     std::__stable_sort_adaptive(__first, __last, __buf.begin(),
05457                     _DistanceType(__buf.size()), __comp);
05458     }
05459 
05460 
05461   /**
05462    *  @brief Return the union of two sorted ranges.
05463    *  @ingroup set_algorithms
05464    *  @param  first1  Start of first range.
05465    *  @param  last1   End of first range.
05466    *  @param  first2  Start of second range.
05467    *  @param  last2   End of second range.
05468    *  @return  End of the output range.
05469    *  @ingroup set_algorithms
05470    *
05471    *  This operation iterates over both ranges, copying elements present in
05472    *  each range in order to the output range.  Iterators increment for each
05473    *  range.  When the current element of one range is less than the other,
05474    *  that element is copied and the iterator advanced.  If an element is
05475    *  contained in both ranges, the element from the first range is copied and
05476    *  both ranges advance.  The output range may not overlap either input
05477    *  range.
05478   */
05479   template<typename _InputIterator1, typename _InputIterator2,
05480        typename _OutputIterator>
05481     _OutputIterator
05482     set_union(_InputIterator1 __first1, _InputIterator1 __last1,
05483           _InputIterator2 __first2, _InputIterator2 __last2,
05484           _OutputIterator __result)
05485     {
05486       typedef typename iterator_traits<_InputIterator1>::value_type
05487     _ValueType1;
05488       typedef typename iterator_traits<_InputIterator2>::value_type
05489     _ValueType2;
05490 
05491       // concept requirements
05492       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05493       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05494       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05495                   _ValueType1>)
05496       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05497                   _ValueType2>)
05498       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05499       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
05500       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05501       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05502 
05503       while (__first1 != __last1 && __first2 != __last2)
05504     {
05505       if (*__first1 < *__first2)
05506         {
05507           *__result = *__first1;
05508           ++__first1;
05509         }
05510       else if (*__first2 < *__first1)
05511         {
05512           *__result = *__first2;
05513           ++__first2;
05514         }
05515       else
05516         {
05517           *__result = *__first1;
05518           ++__first1;
05519           ++__first2;
05520         }
05521       ++__result;
05522     }
05523       return std::copy(__first2, __last2, std::copy(__first1, __last1,
05524                             __result));
05525     }
05526 
05527   /**
05528    *  @brief Return the union of two sorted ranges using a comparison functor.
05529    *  @ingroup set_algorithms
05530    *  @param  first1  Start of first range.
05531    *  @param  last1   End of first range.
05532    *  @param  first2  Start of second range.
05533    *  @param  last2   End of second range.
05534    *  @param  comp    The comparison functor.
05535    *  @return  End of the output range.
05536    *  @ingroup set_algorithms
05537    *
05538    *  This operation iterates over both ranges, copying elements present in
05539    *  each range in order to the output range.  Iterators increment for each
05540    *  range.  When the current element of one range is less than the other
05541    *  according to @a comp, that element is copied and the iterator advanced.
05542    *  If an equivalent element according to @a comp is contained in both
05543    *  ranges, the element from the first range is copied and both ranges
05544    *  advance.  The output range may not overlap either input range.
05545   */
05546   template<typename _InputIterator1, typename _InputIterator2,
05547        typename _OutputIterator, typename _Compare>
05548     _OutputIterator
05549     set_union(_InputIterator1 __first1, _InputIterator1 __last1,
05550           _InputIterator2 __first2, _InputIterator2 __last2,
05551           _OutputIterator __result, _Compare __comp)
05552     {
05553       typedef typename iterator_traits<_InputIterator1>::value_type
05554     _ValueType1;
05555       typedef typename iterator_traits<_InputIterator2>::value_type
05556     _ValueType2;
05557 
05558       // concept requirements
05559       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05560       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05561       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05562                   _ValueType1>)
05563       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05564                   _ValueType2>)
05565       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05566                   _ValueType1, _ValueType2>)
05567       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05568                   _ValueType2, _ValueType1>)
05569       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05570       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05571 
05572       while (__first1 != __last1 && __first2 != __last2)
05573     {
05574       if (__comp(*__first1, *__first2))
05575         {
05576           *__result = *__first1;
05577           ++__first1;
05578         }
05579       else if (__comp(*__first2, *__first1))
05580         {
05581           *__result = *__first2;
05582           ++__first2;
05583         }
05584       else
05585         {
05586           *__result = *__first1;
05587           ++__first1;
05588           ++__first2;
05589         }
05590       ++__result;
05591     }
05592       return std::copy(__first2, __last2, std::copy(__first1, __last1,
05593                             __result));
05594     }
05595 
05596   /**
05597    *  @brief Return the intersection of two sorted ranges.
05598    *  @ingroup set_algorithms
05599    *  @param  first1  Start of first range.
05600    *  @param  last1   End of first range.
05601    *  @param  first2  Start of second range.
05602    *  @param  last2   End of second range.
05603    *  @return  End of the output range.
05604    *  @ingroup set_algorithms
05605    *
05606    *  This operation iterates over both ranges, copying elements present in
05607    *  both ranges in order to the output range.  Iterators increment for each
05608    *  range.  When the current element of one range is less than the other,
05609    *  that iterator advances.  If an element is contained in both ranges, the
05610    *  element from the first range is copied and both ranges advance.  The
05611    *  output range may not overlap either input range.
05612   */
05613   template<typename _InputIterator1, typename _InputIterator2,
05614        typename _OutputIterator>
05615     _OutputIterator
05616     set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
05617              _InputIterator2 __first2, _InputIterator2 __last2,
05618              _OutputIterator __result)
05619     {
05620       typedef typename iterator_traits<_InputIterator1>::value_type
05621     _ValueType1;
05622       typedef typename iterator_traits<_InputIterator2>::value_type
05623     _ValueType2;
05624 
05625       // concept requirements
05626       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05627       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05628       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05629                   _ValueType1>)
05630       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05631       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
05632       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05633       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05634 
05635       while (__first1 != __last1 && __first2 != __last2)
05636     if (*__first1 < *__first2)
05637       ++__first1;
05638     else if (*__first2 < *__first1)
05639       ++__first2;
05640     else
05641       {
05642         *__result = *__first1;
05643         ++__first1;
05644         ++__first2;
05645         ++__result;
05646       }
05647       return __result;
05648     }
05649 
05650   /**
05651    *  @brief Return the intersection of two sorted ranges using comparison
05652    *  functor.
05653    *  @ingroup set_algorithms
05654    *  @param  first1  Start of first range.
05655    *  @param  last1   End of first range.
05656    *  @param  first2  Start of second range.
05657    *  @param  last2   End of second range.
05658    *  @param  comp    The comparison functor.
05659    *  @return  End of the output range.
05660    *  @ingroup set_algorithms
05661    *
05662    *  This operation iterates over both ranges, copying elements present in
05663    *  both ranges in order to the output range.  Iterators increment for each
05664    *  range.  When the current element of one range is less than the other
05665    *  according to @a comp, that iterator advances.  If an element is
05666    *  contained in both ranges according to @a comp, the element from the
05667    *  first range is copied and both ranges advance.  The output range may not
05668    *  overlap either input range.
05669   */
05670   template<typename _InputIterator1, typename _InputIterator2,
05671        typename _OutputIterator, typename _Compare>
05672     _OutputIterator
05673     set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
05674              _InputIterator2 __first2, _InputIterator2 __last2,
05675              _OutputIterator __result, _Compare __comp)
05676     {
05677       typedef typename iterator_traits<_InputIterator1>::value_type
05678     _ValueType1;
05679       typedef typename iterator_traits<_InputIterator2>::value_type
05680     _ValueType2;
05681 
05682       // concept requirements
05683       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05684       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05685       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05686                   _ValueType1>)
05687       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05688                   _ValueType1, _ValueType2>)
05689       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05690                   _ValueType2, _ValueType1>)
05691       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05692       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05693 
05694       while (__first1 != __last1 && __first2 != __last2)
05695     if (__comp(*__first1, *__first2))
05696       ++__first1;
05697     else if (__comp(*__first2, *__first1))
05698       ++__first2;
05699     else
05700       {
05701         *__result = *__first1;
05702         ++__first1;
05703         ++__first2;
05704         ++__result;
05705       }
05706       return __result;
05707     }
05708 
05709   /**
05710    *  @brief Return the difference of two sorted ranges.
05711    *  @ingroup set_algorithms
05712    *  @param  first1  Start of first range.
05713    *  @param  last1   End of first range.
05714    *  @param  first2  Start of second range.
05715    *  @param  last2   End of second range.
05716    *  @return  End of the output range.
05717    *  @ingroup set_algorithms
05718    *
05719    *  This operation iterates over both ranges, copying elements present in
05720    *  the first range but not the second in order to the output range.
05721    *  Iterators increment for each range.  When the current element of the
05722    *  first range is less than the second, that element is copied and the
05723    *  iterator advances.  If the current element of the second range is less,
05724    *  the iterator advances, but no element is copied.  If an element is
05725    *  contained in both ranges, no elements are copied and both ranges
05726    *  advance.  The output range may not overlap either input range.
05727   */
05728   template<typename _InputIterator1, typename _InputIterator2,
05729        typename _OutputIterator>
05730     _OutputIterator
05731     set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05732            _InputIterator2 __first2, _InputIterator2 __last2,
05733            _OutputIterator __result)
05734     {
05735       typedef typename iterator_traits<_InputIterator1>::value_type
05736     _ValueType1;
05737       typedef typename iterator_traits<_InputIterator2>::value_type
05738     _ValueType2;
05739 
05740       // concept requirements
05741       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05742       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05743       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05744                   _ValueType1>)
05745       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05746       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
05747       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05748       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05749 
05750       while (__first1 != __last1 && __first2 != __last2)
05751     if (*__first1 < *__first2)
05752       {
05753         *__result = *__first1;
05754         ++__first1;
05755         ++__result;
05756       }
05757     else if (*__first2 < *__first1)
05758       ++__first2;
05759     else
05760       {
05761         ++__first1;
05762         ++__first2;
05763       }
05764       return std::copy(__first1, __last1, __result);
05765     }
05766 
05767   /**
05768    *  @brief  Return the difference of two sorted ranges using comparison
05769    *  functor.
05770    *  @ingroup set_algorithms
05771    *  @param  first1  Start of first range.
05772    *  @param  last1   End of first range.
05773    *  @param  first2  Start of second range.
05774    *  @param  last2   End of second range.
05775    *  @param  comp    The comparison functor.
05776    *  @return  End of the output range.
05777    *  @ingroup set_algorithms
05778    *
05779    *  This operation iterates over both ranges, copying elements present in
05780    *  the first range but not the second in order to the output range.
05781    *  Iterators increment for each range.  When the current element of the
05782    *  first range is less than the second according to @a comp, that element
05783    *  is copied and the iterator advances.  If the current element of the
05784    *  second range is less, no element is copied and the iterator advances.
05785    *  If an element is contained in both ranges according to @a comp, no
05786    *  elements are copied and both ranges advance.  The output range may not
05787    *  overlap either input range.
05788   */
05789   template<typename _InputIterator1, typename _InputIterator2,
05790        typename _OutputIterator, typename _Compare>
05791     _OutputIterator
05792     set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05793            _InputIterator2 __first2, _InputIterator2 __last2,
05794            _OutputIterator __result, _Compare __comp)
05795     {
05796       typedef typename iterator_traits<_InputIterator1>::value_type
05797     _ValueType1;
05798       typedef typename iterator_traits<_InputIterator2>::value_type
05799     _ValueType2;
05800 
05801       // concept requirements
05802       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05803       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05804       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05805                   _ValueType1>)
05806       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05807                   _ValueType1, _ValueType2>)
05808       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05809                   _ValueType2, _ValueType1>)
05810       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05811       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05812 
05813       while (__first1 != __last1 && __first2 != __last2)
05814     if (__comp(*__first1, *__first2))
05815       {
05816         *__result = *__first1;
05817         ++__first1;
05818         ++__result;
05819       }
05820     else if (__comp(*__first2, *__first1))
05821       ++__first2;
05822     else
05823       {
05824         ++__first1;
05825         ++__first2;
05826       }
05827       return std::copy(__first1, __last1, __result);
05828     }
05829 
05830   /**
05831    *  @brief  Return the symmetric difference of two sorted ranges.
05832    *  @ingroup set_algorithms
05833    *  @param  first1  Start of first range.
05834    *  @param  last1   End of first range.
05835    *  @param  first2  Start of second range.
05836    *  @param  last2   End of second range.
05837    *  @return  End of the output range.
05838    *  @ingroup set_algorithms
05839    *
05840    *  This operation iterates over both ranges, copying elements present in
05841    *  one range but not the other in order to the output range.  Iterators
05842    *  increment for each range.  When the current element of one range is less
05843    *  than the other, that element is copied and the iterator advances.  If an
05844    *  element is contained in both ranges, no elements are copied and both
05845    *  ranges advance.  The output range may not overlap either input range.
05846   */
05847   template<typename _InputIterator1, typename _InputIterator2,
05848        typename _OutputIterator>
05849     _OutputIterator
05850     set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05851                  _InputIterator2 __first2, _InputIterator2 __last2,
05852                  _OutputIterator __result)
05853     {
05854       typedef typename iterator_traits<_InputIterator1>::value_type
05855     _ValueType1;
05856       typedef typename iterator_traits<_InputIterator2>::value_type
05857     _ValueType2;
05858 
05859       // concept requirements
05860       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05861       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05862       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05863                   _ValueType1>)
05864       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05865                   _ValueType2>)
05866       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05867       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 
05868       __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05869       __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05870 
05871       while (__first1 != __last1 && __first2 != __last2)
05872     if (*__first1 < *__first2)
05873       {
05874         *__result = *__first1;
05875         ++__first1;
05876         ++__result;
05877       }
05878     else if (*__first2 < *__first1)
05879       {
05880         *__result = *__first2;
05881         ++__first2;
05882         ++__result;
05883       }
05884     else
05885       {
05886         ++__first1;
05887         ++__first2;
05888       }
05889       return std::copy(__first2, __last2, std::copy(__first1,
05890                             __last1, __result));
05891     }
05892 
05893   /**
05894    *  @brief  Return the symmetric difference of two sorted ranges using
05895    *  comparison functor.
05896    *  @ingroup set_algorithms
05897    *  @param  first1  Start of first range.
05898    *  @param  last1   End of first range.
05899    *  @param  first2  Start of second range.
05900    *  @param  last2   End of second range.
05901    *  @param  comp    The comparison functor.
05902    *  @return  End of the output range.
05903    *  @ingroup set_algorithms
05904    *
05905    *  This operation iterates over both ranges, copying elements present in
05906    *  one range but not the other in order to the output range.  Iterators
05907    *  increment for each range.  When the current element of one range is less
05908    *  than the other according to @a comp, that element is copied and the
05909    *  iterator advances.  If an element is contained in both ranges according
05910    *  to @a comp, no elements are copied and both ranges advance.  The output
05911    *  range may not overlap either input range.
05912   */
05913   template<typename _InputIterator1, typename _InputIterator2,
05914        typename _OutputIterator, typename _Compare>
05915     _OutputIterator
05916     set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05917                  _InputIterator2 __first2, _InputIterator2 __last2,
05918                  _OutputIterator __result,
05919                  _Compare __comp)
05920     {
05921       typedef typename iterator_traits<_InputIterator1>::value_type
05922     _ValueType1;
05923       typedef typename iterator_traits<_InputIterator2>::value_type
05924     _ValueType2;
05925 
05926       // concept requirements
05927       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05928       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05929       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05930                   _ValueType1>)
05931       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05932                   _ValueType2>)
05933       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05934                   _ValueType1, _ValueType2>)
05935       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05936                   _ValueType2, _ValueType1>)
05937       __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05938       __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05939 
05940       while (__first1 != __last1 && __first2 != __last2)
05941     if (__comp(*__first1, *__first2))
05942       {
05943         *__result = *__first1;
05944         ++__first1;
05945         ++__result;
05946       }
05947     else if (__comp(*__first2, *__first1))
05948       {
05949         *__result = *__first2;
05950         ++__first2;
05951         ++__result;
05952       }
05953     else
05954       {
05955         ++__first1;
05956         ++__first2;
05957       }
05958       return std::copy(__first2, __last2, 
05959                std::copy(__first1, __last1, __result));
05960     }
05961 
05962 
05963   /**
05964    *  @brief  Return the minimum element in a range.
05965    *  @ingroup sorting_algorithms
05966    *  @param  first  Start of range.
05967    *  @param  last   End of range.
05968    *  @return  Iterator referencing the first instance of the smallest value.
05969   */
05970   template<typename _ForwardIterator>
05971     _ForwardIterator
05972     min_element(_ForwardIterator __first, _ForwardIterator __last)
05973     {
05974       // concept requirements
05975       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
05976       __glibcxx_function_requires(_LessThanComparableConcept<
05977         typename iterator_traits<_ForwardIterator>::value_type>)
05978       __glibcxx_requires_valid_range(__first, __last);
05979 
05980       if (__first == __last)
05981     return __first;
05982       _ForwardIterator __result = __first;
05983       while (++__first != __last)
05984     if (*__first < *__result)
05985       __result = __first;
05986       return __result;
05987     }
05988 
05989   /**
05990    *  @brief  Return the minimum element in a range using comparison functor.
05991    *  @ingroup sorting_algorithms
05992    *  @param  first  Start of range.
05993    *  @param  last   End of range.
05994    *  @param  comp   Comparison functor.
05995    *  @return  Iterator referencing the first instance of the smallest value
05996    *  according to comp.
05997   */
05998   template<typename _ForwardIterator, typename _Compare>
05999     _ForwardIterator
06000     min_element(_ForwardIterator __first, _ForwardIterator __last,
06001         _Compare __comp)
06002     {
06003       // concept requirements
06004       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
06005       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
06006         typename iterator_traits<_ForwardIterator>::value_type,
06007         typename iterator_traits<_ForwardIterator>::value_type>)
06008       __glibcxx_requires_valid_range(__first, __last);
06009 
06010       if (__first == __last)
06011     return __first;
06012       _ForwardIterator __result = __first;
06013       while (++__first != __last)
06014     if (__comp(*__first, *__result))
06015       __result = __first;
06016       return __result;
06017     }
06018 
06019   /**
06020    *  @brief  Return the maximum element in a range.
06021    *  @ingroup sorting_algorithms
06022    *  @param  first  Start of range.
06023    *  @param  last   End of range.
06024    *  @return  Iterator referencing the first instance of the largest value.
06025   */
06026   template<typename _ForwardIterator>
06027     _ForwardIterator
06028     max_element(_ForwardIterator __first, _ForwardIterator __last)
06029     {
06030       // concept requirements
06031       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
06032       __glibcxx_function_requires(_LessThanComparableConcept<
06033         typename iterator_traits<_ForwardIterator>::value_type>)
06034       __glibcxx_requires_valid_range(__first, __last);
06035 
06036       if (__first == __last)
06037     return __first;
06038       _ForwardIterator __result = __first;
06039       while (++__first != __last)
06040     if (*__result < *__first)
06041       __result = __first;
06042       return __result;
06043     }
06044 
06045   /**
06046    *  @brief  Return the maximum element in a range using comparison functor.
06047    *  @ingroup sorting_algorithms
06048    *  @param  first  Start of range.
06049    *  @param  last   End of range.
06050    *  @param  comp   Comparison functor.
06051    *  @return  Iterator referencing the first instance of the largest value
06052    *  according to comp.
06053   */
06054   template<typename _ForwardIterator, typename _Compare>
06055     _ForwardIterator
06056     max_element(_ForwardIterator __first, _ForwardIterator __last,
06057         _Compare __comp)
06058     {
06059       // concept requirements
06060       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
06061       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
06062         typename iterator_traits<_ForwardIterator>::value_type,
06063         typename iterator_traits<_ForwardIterator>::value_type>)
06064       __glibcxx_requires_valid_range(__first, __last);
06065 
06066       if (__first == __last) return __first;
06067       _ForwardIterator __result = __first;
06068       while (++__first != __last)
06069     if (__comp(*__result, *__first))
06070       __result = __first;
06071       return __result;
06072     }
06073 
06074 _GLIBCXX_END_NESTED_NAMESPACE
06075 
06076 #endif /* _STL_ALGO_H */