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

Generated on 11 Jan 2010 for libstdc++ by  doxygen 1.6.1