stl_algo.h

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

Generated on 9 Feb 2010 for libstdc++ by  doxygen 1.6.1