stl_algobase.h

Go to the documentation of this file.
00001 // Core algorithmic facilities -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /*
00027  *
00028  * Copyright (c) 1994
00029  * Hewlett-Packard Company
00030  *
00031  * Permission to use, copy, modify, distribute and sell this software
00032  * and its documentation for any purpose is hereby granted without fee,
00033  * provided that the above copyright notice appear in all copies and
00034  * that both that copyright notice and this permission notice appear
00035  * in supporting documentation.  Hewlett-Packard Company makes no
00036  * representations about the suitability of this software for any
00037  * purpose.  It is provided "as is" without express or implied warranty.
00038  *
00039  *
00040  * Copyright (c) 1996-1998
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_algobase.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_ALGOBASE_H
00058 #define _STL_ALGOBASE_H 1
00059 
00060 #include <bits/c++config.h>
00061 #include <bits/functexcept.h>
00062 #include <bits/cpp_type_traits.h>
00063 #include <ext/type_traits.h>
00064 #include <ext/numeric_traits.h>
00065 #include <bits/stl_pair.h>
00066 #include <bits/stl_iterator_base_types.h>
00067 #include <bits/stl_iterator_base_funcs.h>
00068 #include <bits/stl_iterator.h>
00069 #include <bits/concept_check.h>
00070 #include <debug/debug.h>
00071 #include <bits/move.h> // For std::swap and _GLIBCXX_MOVE
00072 
00073 _GLIBCXX_BEGIN_NAMESPACE(std)
00074 
00075   // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
00076   // nutshell, we are partially implementing the resolution of DR 187,
00077   // when it's safe, i.e., the value_types are equal.
00078   template<bool _BoolType>
00079     struct __iter_swap
00080     {
00081       template<typename _ForwardIterator1, typename _ForwardIterator2>
00082         static void
00083         iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
00084         {
00085           typedef typename iterator_traits<_ForwardIterator1>::value_type
00086             _ValueType1;
00087           _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
00088           *__a = _GLIBCXX_MOVE(*__b);
00089           *__b = _GLIBCXX_MOVE(__tmp);
00090     }
00091     };
00092 
00093   template<>
00094     struct __iter_swap<true>
00095     {
00096       template<typename _ForwardIterator1, typename _ForwardIterator2>
00097         static void 
00098         iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
00099         {
00100           swap(*__a, *__b);
00101         }
00102     };
00103 
00104   /**
00105    *  @brief Swaps the contents of two iterators.
00106    *  @ingroup mutating_algorithms
00107    *  @param  a  An iterator.
00108    *  @param  b  Another iterator.
00109    *  @return   Nothing.
00110    *
00111    *  This function swaps the values pointed to by two iterators, not the
00112    *  iterators themselves.
00113   */
00114   template<typename _ForwardIterator1, typename _ForwardIterator2>
00115     inline void
00116     iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
00117     {
00118       typedef typename iterator_traits<_ForwardIterator1>::value_type
00119     _ValueType1;
00120       typedef typename iterator_traits<_ForwardIterator2>::value_type
00121     _ValueType2;
00122 
00123       // concept requirements
00124       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
00125                   _ForwardIterator1>)
00126       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
00127                   _ForwardIterator2>)
00128       __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
00129                   _ValueType2>)
00130       __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
00131                   _ValueType1>)
00132 
00133       typedef typename iterator_traits<_ForwardIterator1>::reference
00134     _ReferenceType1;
00135       typedef typename iterator_traits<_ForwardIterator2>::reference
00136     _ReferenceType2;
00137       std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
00138     && __are_same<_ValueType1&, _ReferenceType1>::__value
00139     && __are_same<_ValueType2&, _ReferenceType2>::__value>::
00140     iter_swap(__a, __b);
00141     }
00142 
00143   /**
00144    *  @brief Swap the elements of two sequences.
00145    *  @ingroup mutating_algorithms
00146    *  @param  first1  A forward iterator.
00147    *  @param  last1   A forward iterator.
00148    *  @param  first2  A forward iterator.
00149    *  @return   An iterator equal to @p first2+(last1-first1).
00150    *
00151    *  Swaps each element in the range @p [first1,last1) with the
00152    *  corresponding element in the range @p [first2,(last1-first1)).
00153    *  The ranges must not overlap.
00154   */
00155   template<typename _ForwardIterator1, typename _ForwardIterator2>
00156     _ForwardIterator2
00157     swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
00158         _ForwardIterator2 __first2)
00159     {
00160       // concept requirements
00161       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
00162                   _ForwardIterator1>)
00163       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
00164                   _ForwardIterator2>)
00165       __glibcxx_requires_valid_range(__first1, __last1);
00166 
00167       for (; __first1 != __last1; ++__first1, ++__first2)
00168     std::iter_swap(__first1, __first2);
00169       return __first2;
00170     }
00171 
00172   /**
00173    *  @brief This does what you think it does.
00174    *  @ingroup sorting_algorithms
00175    *  @param  a  A thing of arbitrary type.
00176    *  @param  b  Another thing of arbitrary type.
00177    *  @return   The lesser of the parameters.
00178    *
00179    *  This is the simple classic generic implementation.  It will work on
00180    *  temporary expressions, since they are only evaluated once, unlike a
00181    *  preprocessor macro.
00182   */
00183   template<typename _Tp>
00184     inline const _Tp&
00185     min(const _Tp& __a, const _Tp& __b)
00186     {
00187       // concept requirements
00188       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
00189       //return __b < __a ? __b : __a;
00190       if (__b < __a)
00191     return __b;
00192       return __a;
00193     }
00194 
00195   /**
00196    *  @brief This does what you think it does.
00197    *  @ingroup sorting_algorithms
00198    *  @param  a  A thing of arbitrary type.
00199    *  @param  b  Another thing of arbitrary type.
00200    *  @return   The greater of the parameters.
00201    *
00202    *  This is the simple classic generic implementation.  It will work on
00203    *  temporary expressions, since they are only evaluated once, unlike a
00204    *  preprocessor macro.
00205   */
00206   template<typename _Tp>
00207     inline const _Tp&
00208     max(const _Tp& __a, const _Tp& __b)
00209     {
00210       // concept requirements
00211       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
00212       //return  __a < __b ? __b : __a;
00213       if (__a < __b)
00214     return __b;
00215       return __a;
00216     }
00217 
00218   /**
00219    *  @brief This does what you think it does.
00220    *  @ingroup sorting_algorithms
00221    *  @param  a  A thing of arbitrary type.
00222    *  @param  b  Another thing of arbitrary type.
00223    *  @param  comp  A @link comparison_functors comparison functor@endlink.
00224    *  @return   The lesser of the parameters.
00225    *
00226    *  This will work on temporary expressions, since they are only evaluated
00227    *  once, unlike a preprocessor macro.
00228   */
00229   template<typename _Tp, typename _Compare>
00230     inline const _Tp&
00231     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
00232     {
00233       //return __comp(__b, __a) ? __b : __a;
00234       if (__comp(__b, __a))
00235     return __b;
00236       return __a;
00237     }
00238 
00239   /**
00240    *  @brief This does what you think it does.
00241    *  @ingroup sorting_algorithms
00242    *  @param  a  A thing of arbitrary type.
00243    *  @param  b  Another thing of arbitrary type.
00244    *  @param  comp  A @link comparison_functors comparison functor@endlink.
00245    *  @return   The greater of the parameters.
00246    *
00247    *  This will work on temporary expressions, since they are only evaluated
00248    *  once, unlike a preprocessor macro.
00249   */
00250   template<typename _Tp, typename _Compare>
00251     inline const _Tp&
00252     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
00253     {
00254       //return __comp(__a, __b) ? __b : __a;
00255       if (__comp(__a, __b))
00256     return __b;
00257       return __a;
00258     }
00259 
00260   // If _Iterator is a __normal_iterator return its base (a plain pointer,
00261   // normally) otherwise return it untouched.  See copy, fill, ... 
00262   template<typename _Iterator>
00263     struct _Niter_base
00264     : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value>
00265     { };
00266 
00267   template<typename _Iterator>
00268     inline typename _Niter_base<_Iterator>::iterator_type
00269     __niter_base(_Iterator __it)
00270     { return std::_Niter_base<_Iterator>::_S_base(__it); }
00271 
00272   // Likewise, for move_iterator.
00273   template<typename _Iterator>
00274     struct _Miter_base
00275     : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value>
00276     { };
00277 
00278   template<typename _Iterator>
00279     inline typename _Miter_base<_Iterator>::iterator_type
00280     __miter_base(_Iterator __it)
00281     { return std::_Miter_base<_Iterator>::_S_base(__it); }
00282 
00283   // All of these auxiliary structs serve two purposes.  (1) Replace
00284   // calls to copy with memmove whenever possible.  (Memmove, not memcpy,
00285   // because the input and output ranges are permitted to overlap.)
00286   // (2) If we're using random access iterators, then write the loop as
00287   // a for loop with an explicit count.
00288 
00289   template<bool, bool, typename>
00290     struct __copy_move
00291     {
00292       template<typename _II, typename _OI>
00293         static _OI
00294         __copy_m(_II __first, _II __last, _OI __result)
00295         {
00296       for (; __first != __last; ++__result, ++__first)
00297         *__result = *__first;
00298       return __result;
00299     }
00300     };
00301 
00302 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00303   template<typename _Category>
00304     struct __copy_move<true, false, _Category>
00305     {
00306       template<typename _II, typename _OI>
00307         static _OI
00308         __copy_m(_II __first, _II __last, _OI __result)
00309         {
00310       for (; __first != __last; ++__result, ++__first)
00311         *__result = std::move(*__first);
00312       return __result;
00313     }
00314     };
00315 #endif
00316 
00317   template<>
00318     struct __copy_move<false, false, random_access_iterator_tag>
00319     {
00320       template<typename _II, typename _OI>
00321         static _OI
00322         __copy_m(_II __first, _II __last, _OI __result)
00323         { 
00324       typedef typename iterator_traits<_II>::difference_type _Distance;
00325       for(_Distance __n = __last - __first; __n > 0; --__n)
00326         {
00327           *__result = *__first;
00328           ++__first;
00329           ++__result;
00330         }
00331       return __result;
00332     }
00333     };
00334 
00335 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00336   template<>
00337     struct __copy_move<true, false, random_access_iterator_tag>
00338     {
00339       template<typename _II, typename _OI>
00340         static _OI
00341         __copy_m(_II __first, _II __last, _OI __result)
00342         { 
00343       typedef typename iterator_traits<_II>::difference_type _Distance;
00344       for(_Distance __n = __last - __first; __n > 0; --__n)
00345         {
00346           *__result = std::move(*__first);
00347           ++__first;
00348           ++__result;
00349         }
00350       return __result;
00351     }
00352     };
00353 #endif
00354 
00355   template<bool _IsMove>
00356     struct __copy_move<_IsMove, true, random_access_iterator_tag>
00357     {
00358       template<typename _Tp>
00359         static _Tp*
00360         __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
00361         {
00362       const ptrdiff_t _Num = __last - __first;
00363       if (_Num)
00364         __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
00365       return __result + _Num;
00366     }
00367     };
00368 
00369   template<bool _IsMove, typename _II, typename _OI>
00370     inline _OI
00371     __copy_move_a(_II __first, _II __last, _OI __result)
00372     {
00373       typedef typename iterator_traits<_II>::value_type _ValueTypeI;
00374       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
00375       typedef typename iterator_traits<_II>::iterator_category _Category;
00376       const bool __simple = (__is_trivial(_ValueTypeI)
00377                          && __is_pointer<_II>::__value
00378                          && __is_pointer<_OI>::__value
00379                  && __are_same<_ValueTypeI, _ValueTypeO>::__value);
00380 
00381       return std::__copy_move<_IsMove, __simple,
00382                           _Category>::__copy_m(__first, __last, __result);
00383     }
00384 
00385   // Helpers for streambuf iterators (either istream or ostream).
00386   // NB: avoid including <iosfwd>, relatively large.
00387   template<typename _CharT>
00388     struct char_traits;
00389 
00390   template<typename _CharT, typename _Traits>
00391     class istreambuf_iterator;
00392 
00393   template<typename _CharT, typename _Traits>
00394     class ostreambuf_iterator;
00395 
00396   template<bool _IsMove, typename _CharT>
00397     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 
00398          ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
00399     __copy_move_a2(_CharT*, _CharT*,
00400            ostreambuf_iterator<_CharT, char_traits<_CharT> >);
00401 
00402   template<bool _IsMove, typename _CharT>
00403     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 
00404          ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
00405     __copy_move_a2(const _CharT*, const _CharT*,
00406            ostreambuf_iterator<_CharT, char_traits<_CharT> >);
00407 
00408   template<bool _IsMove, typename _CharT>
00409     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
00410                     _CharT*>::__type
00411     __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
00412            istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
00413 
00414   template<bool _IsMove, typename _II, typename _OI>
00415     inline _OI
00416     __copy_move_a2(_II __first, _II __last, _OI __result)
00417     {
00418       return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
00419                          std::__niter_base(__last),
00420                          std::__niter_base(__result)));
00421     }
00422 
00423   /**
00424    *  @brief Copies the range [first,last) into result.
00425    *  @ingroup mutating_algorithms
00426    *  @param  first  An input iterator.
00427    *  @param  last   An input iterator.
00428    *  @param  result An output iterator.
00429    *  @return   result + (first - last)
00430    *
00431    *  This inline function will boil down to a call to @c memmove whenever
00432    *  possible.  Failing that, if random access iterators are passed, then the
00433    *  loop count will be known (and therefore a candidate for compiler
00434    *  optimizations such as unrolling).  Result may not be contained within
00435    *  [first,last); the copy_backward function should be used instead.
00436    *
00437    *  Note that the end of the output range is permitted to be contained
00438    *  within [first,last).
00439   */
00440   template<typename _II, typename _OI>
00441     inline _OI
00442     copy(_II __first, _II __last, _OI __result)
00443     {
00444       // concept requirements
00445       __glibcxx_function_requires(_InputIteratorConcept<_II>)
00446       __glibcxx_function_requires(_OutputIteratorConcept<_OI,
00447         typename iterator_traits<_II>::value_type>)
00448       __glibcxx_requires_valid_range(__first, __last);
00449 
00450       return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
00451           (std::__miter_base(__first), std::__miter_base(__last),
00452            __result));
00453     }
00454 
00455 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00456   /**
00457    *  @brief Moves the range [first,last) into result.
00458    *  @ingroup mutating_algorithms
00459    *  @param  first  An input iterator.
00460    *  @param  last   An input iterator.
00461    *  @param  result An output iterator.
00462    *  @return   result + (first - last)
00463    *
00464    *  This inline function will boil down to a call to @c memmove whenever
00465    *  possible.  Failing that, if random access iterators are passed, then the
00466    *  loop count will be known (and therefore a candidate for compiler
00467    *  optimizations such as unrolling).  Result may not be contained within
00468    *  [first,last); the move_backward function should be used instead.
00469    *
00470    *  Note that the end of the output range is permitted to be contained
00471    *  within [first,last).
00472   */
00473   template<typename _II, typename _OI>
00474     inline _OI
00475     move(_II __first, _II __last, _OI __result)
00476     {
00477       // concept requirements
00478       __glibcxx_function_requires(_InputIteratorConcept<_II>)
00479       __glibcxx_function_requires(_OutputIteratorConcept<_OI,
00480         typename iterator_traits<_II>::value_type>)
00481       __glibcxx_requires_valid_range(__first, __last);
00482 
00483       return std::__copy_move_a2<true>(std::__miter_base(__first),
00484                        std::__miter_base(__last), __result);
00485     }
00486 
00487 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
00488 #else
00489 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
00490 #endif
00491 
00492   template<bool, bool, typename>
00493     struct __copy_move_backward
00494     {
00495       template<typename _BI1, typename _BI2>
00496         static _BI2
00497         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
00498         {
00499       while (__first != __last)
00500         *--__result = *--__last;
00501       return __result;
00502     }
00503     };
00504 
00505 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00506   template<typename _Category>
00507     struct __copy_move_backward<true, false, _Category>
00508     {
00509       template<typename _BI1, typename _BI2>
00510         static _BI2
00511         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
00512         {
00513       while (__first != __last)
00514         *--__result = std::move(*--__last);
00515       return __result;
00516     }
00517     };
00518 #endif
00519 
00520   template<>
00521     struct __copy_move_backward<false, false, random_access_iterator_tag>
00522     {
00523       template<typename _BI1, typename _BI2>
00524         static _BI2
00525         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
00526         {
00527       typename iterator_traits<_BI1>::difference_type __n;
00528       for (__n = __last - __first; __n > 0; --__n)
00529         *--__result = *--__last;
00530       return __result;
00531     }
00532     };
00533 
00534 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00535   template<>
00536     struct __copy_move_backward<true, false, random_access_iterator_tag>
00537     {
00538       template<typename _BI1, typename _BI2>
00539         static _BI2
00540         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
00541         {
00542       typename iterator_traits<_BI1>::difference_type __n;
00543       for (__n = __last - __first; __n > 0; --__n)
00544         *--__result = std::move(*--__last);
00545       return __result;
00546     }
00547     };
00548 #endif
00549 
00550   template<bool _IsMove>
00551     struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
00552     {
00553       template<typename _Tp>
00554         static _Tp*
00555         __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
00556         {
00557       const ptrdiff_t _Num = __last - __first;
00558       if (_Num)
00559         __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
00560       return __result - _Num;
00561     }
00562     };
00563 
00564   template<bool _IsMove, typename _BI1, typename _BI2>
00565     inline _BI2
00566     __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
00567     {
00568       typedef typename iterator_traits<_BI1>::value_type _ValueType1;
00569       typedef typename iterator_traits<_BI2>::value_type _ValueType2;
00570       typedef typename iterator_traits<_BI1>::iterator_category _Category;
00571       const bool __simple = (__is_trivial(_ValueType1)
00572                          && __is_pointer<_BI1>::__value
00573                          && __is_pointer<_BI2>::__value
00574                  && __are_same<_ValueType1, _ValueType2>::__value);
00575 
00576       return std::__copy_move_backward<_IsMove, __simple,
00577                                    _Category>::__copy_move_b(__first,
00578                                  __last,
00579                                  __result);
00580     }
00581 
00582   template<bool _IsMove, typename _BI1, typename _BI2>
00583     inline _BI2
00584     __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
00585     {
00586       return _BI2(std::__copy_move_backward_a<_IsMove>
00587           (std::__niter_base(__first), std::__niter_base(__last),
00588            std::__niter_base(__result)));
00589     }
00590 
00591   /**
00592    *  @brief Copies the range [first,last) into result.
00593    *  @ingroup mutating_algorithms
00594    *  @param  first  A bidirectional iterator.
00595    *  @param  last   A bidirectional iterator.
00596    *  @param  result A bidirectional iterator.
00597    *  @return   result - (first - last)
00598    *
00599    *  The function has the same effect as copy, but starts at the end of the
00600    *  range and works its way to the start, returning the start of the result.
00601    *  This inline function will boil down to a call to @c memmove whenever
00602    *  possible.  Failing that, if random access iterators are passed, then the
00603    *  loop count will be known (and therefore a candidate for compiler
00604    *  optimizations such as unrolling).
00605    *
00606    *  Result may not be in the range [first,last).  Use copy instead.  Note
00607    *  that the start of the output range may overlap [first,last).
00608   */
00609   template<typename _BI1, typename _BI2>
00610     inline _BI2
00611     copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
00612     {
00613       // concept requirements
00614       __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
00615       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
00616       __glibcxx_function_requires(_ConvertibleConcept<
00617         typename iterator_traits<_BI1>::value_type,
00618         typename iterator_traits<_BI2>::value_type>)
00619       __glibcxx_requires_valid_range(__first, __last);
00620 
00621       return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
00622           (std::__miter_base(__first), std::__miter_base(__last),
00623            __result));
00624     }
00625 
00626 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00627   /**
00628    *  @brief Moves the range [first,last) into result.
00629    *  @ingroup mutating_algorithms
00630    *  @param  first  A bidirectional iterator.
00631    *  @param  last   A bidirectional iterator.
00632    *  @param  result A bidirectional iterator.
00633    *  @return   result - (first - last)
00634    *
00635    *  The function has the same effect as move, but starts at the end of the
00636    *  range and works its way to the start, returning the start of the result.
00637    *  This inline function will boil down to a call to @c memmove whenever
00638    *  possible.  Failing that, if random access iterators are passed, then the
00639    *  loop count will be known (and therefore a candidate for compiler
00640    *  optimizations such as unrolling).
00641    *
00642    *  Result may not be in the range [first,last).  Use move instead.  Note
00643    *  that the start of the output range may overlap [first,last).
00644   */
00645   template<typename _BI1, typename _BI2>
00646     inline _BI2
00647     move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
00648     {
00649       // concept requirements
00650       __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
00651       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
00652       __glibcxx_function_requires(_ConvertibleConcept<
00653         typename iterator_traits<_BI1>::value_type,
00654         typename iterator_traits<_BI2>::value_type>)
00655       __glibcxx_requires_valid_range(__first, __last);
00656 
00657       return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
00658                         std::__miter_base(__last),
00659                         __result);
00660     }
00661 
00662 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
00663 #else
00664 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
00665 #endif
00666 
00667   template<typename _ForwardIterator, typename _Tp>
00668     inline typename
00669     __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
00670     __fill_a(_ForwardIterator __first, _ForwardIterator __last,
00671          const _Tp& __value)
00672     {
00673       for (; __first != __last; ++__first)
00674     *__first = __value;
00675     }
00676     
00677   template<typename _ForwardIterator, typename _Tp>
00678     inline typename
00679     __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
00680     __fill_a(_ForwardIterator __first, _ForwardIterator __last,
00681          const _Tp& __value)
00682     {
00683       const _Tp __tmp = __value;
00684       for (; __first != __last; ++__first)
00685     *__first = __tmp;
00686     }
00687 
00688   // Specialization: for char types we can use memset.
00689   template<typename _Tp>
00690     inline typename
00691     __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
00692     __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
00693     {
00694       const _Tp __tmp = __c;
00695       __builtin_memset(__first, static_cast<unsigned char>(__tmp),
00696                __last - __first);
00697     }
00698 
00699   /**
00700    *  @brief Fills the range [first,last) with copies of value.
00701    *  @ingroup mutating_algorithms
00702    *  @param  first  A forward iterator.
00703    *  @param  last   A forward iterator.
00704    *  @param  value  A reference-to-const of arbitrary type.
00705    *  @return   Nothing.
00706    *
00707    *  This function fills a range with copies of the same value.  For char
00708    *  types filling contiguous areas of memory, this becomes an inline call
00709    *  to @c memset or @c wmemset.
00710   */
00711   template<typename _ForwardIterator, typename _Tp>
00712     inline void
00713     fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
00714     {
00715       // concept requirements
00716       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
00717                   _ForwardIterator>)
00718       __glibcxx_requires_valid_range(__first, __last);
00719 
00720       std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
00721             __value);
00722     }
00723 
00724   template<typename _OutputIterator, typename _Size, typename _Tp>
00725     inline typename
00726     __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
00727     __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
00728     {
00729       for (__decltype(__n + 0) __niter = __n;
00730        __niter > 0; --__niter, ++__first)
00731     *__first = __value;
00732       return __first;
00733     }
00734 
00735   template<typename _OutputIterator, typename _Size, typename _Tp>
00736     inline typename
00737     __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
00738     __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
00739     {
00740       const _Tp __tmp = __value;
00741       for (__decltype(__n + 0) __niter = __n;
00742        __niter > 0; --__niter, ++__first)
00743     *__first = __tmp;
00744       return __first;
00745     }
00746 
00747   template<typename _Size, typename _Tp>
00748     inline typename
00749     __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
00750     __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
00751     {
00752       std::__fill_a(__first, __first + __n, __c);
00753       return __first + __n;
00754     }
00755 
00756   /**
00757    *  @brief Fills the range [first,first+n) with copies of value.
00758    *  @ingroup mutating_algorithms
00759    *  @param  first  An output iterator.
00760    *  @param  n      The count of copies to perform.
00761    *  @param  value  A reference-to-const of arbitrary type.
00762    *  @return   The iterator at first+n.
00763    *
00764    *  This function fills a range with copies of the same value.  For char
00765    *  types filling contiguous areas of memory, this becomes an inline call
00766    *  to @c memset or @ wmemset.
00767    *
00768    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
00769    *  DR 865. More algorithms that throw away information
00770   */
00771   template<typename _OI, typename _Size, typename _Tp>
00772     inline _OI
00773     fill_n(_OI __first, _Size __n, const _Tp& __value)
00774     {
00775       // concept requirements
00776       __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
00777 
00778       return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
00779     }
00780 
00781   template<bool _BoolType>
00782     struct __equal
00783     {
00784       template<typename _II1, typename _II2>
00785         static bool
00786         equal(_II1 __first1, _II1 __last1, _II2 __first2)
00787         {
00788       for (; __first1 != __last1; ++__first1, ++__first2)
00789         if (!(*__first1 == *__first2))
00790           return false;
00791       return true;
00792     }
00793     };
00794 
00795   template<>
00796     struct __equal<true>
00797     {
00798       template<typename _Tp>
00799         static bool
00800         equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
00801         {
00802       return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
00803                    * (__last1 - __first1));
00804     }
00805     };
00806 
00807   template<typename _II1, typename _II2>
00808     inline bool
00809     __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
00810     {
00811       typedef typename iterator_traits<_II1>::value_type _ValueType1;
00812       typedef typename iterator_traits<_II2>::value_type _ValueType2;
00813       const bool __simple = (__is_integer<_ValueType1>::__value
00814                          && __is_pointer<_II1>::__value
00815                          && __is_pointer<_II2>::__value
00816                  && __are_same<_ValueType1, _ValueType2>::__value);
00817 
00818       return std::__equal<__simple>::equal(__first1, __last1, __first2);
00819     }
00820 
00821 
00822   template<typename, typename>
00823     struct __lc_rai
00824     {
00825       template<typename _II1, typename _II2>
00826         static _II1
00827         __newlast1(_II1, _II1 __last1, _II2, _II2)
00828         { return __last1; }
00829 
00830       template<typename _II>
00831         static bool
00832         __cnd2(_II __first, _II __last)
00833         { return __first != __last; }
00834     };
00835 
00836   template<>
00837     struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
00838     {
00839       template<typename _RAI1, typename _RAI2>
00840         static _RAI1
00841         __newlast1(_RAI1 __first1, _RAI1 __last1,
00842            _RAI2 __first2, _RAI2 __last2)
00843         {
00844       const typename iterator_traits<_RAI1>::difference_type
00845         __diff1 = __last1 - __first1;
00846       const typename iterator_traits<_RAI2>::difference_type
00847         __diff2 = __last2 - __first2;
00848       return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
00849     }
00850 
00851       template<typename _RAI>
00852         static bool
00853         __cnd2(_RAI, _RAI)
00854         { return true; }
00855     };
00856 
00857   template<bool _BoolType>
00858     struct __lexicographical_compare
00859     {
00860       template<typename _II1, typename _II2>
00861         static bool __lc(_II1, _II1, _II2, _II2);
00862     };
00863 
00864   template<bool _BoolType>
00865     template<typename _II1, typename _II2>
00866       bool
00867       __lexicographical_compare<_BoolType>::
00868       __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
00869       {
00870     typedef typename iterator_traits<_II1>::iterator_category _Category1;
00871     typedef typename iterator_traits<_II2>::iterator_category _Category2;
00872     typedef std::__lc_rai<_Category1, _Category2>   __rai_type;
00873     
00874     __last1 = __rai_type::__newlast1(__first1, __last1,
00875                      __first2, __last2);
00876     for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
00877          ++__first1, ++__first2)
00878       {
00879         if (*__first1 < *__first2)
00880           return true;
00881         if (*__first2 < *__first1)
00882           return false;
00883       }
00884     return __first1 == __last1 && __first2 != __last2;
00885       }
00886 
00887   template<>
00888     struct __lexicographical_compare<true>
00889     {
00890       template<typename _Tp, typename _Up>
00891         static bool
00892         __lc(const _Tp* __first1, const _Tp* __last1,
00893          const _Up* __first2, const _Up* __last2)
00894     {
00895       const size_t __len1 = __last1 - __first1;
00896       const size_t __len2 = __last2 - __first2;
00897       const int __result = __builtin_memcmp(__first1, __first2,
00898                         std::min(__len1, __len2));
00899       return __result != 0 ? __result < 0 : __len1 < __len2;
00900     }
00901     };
00902 
00903   template<typename _II1, typename _II2>
00904     inline bool
00905     __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
00906                   _II2 __first2, _II2 __last2)
00907     {
00908       typedef typename iterator_traits<_II1>::value_type _ValueType1;
00909       typedef typename iterator_traits<_II2>::value_type _ValueType2;
00910       const bool __simple =
00911     (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
00912      && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
00913      && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
00914      && __is_pointer<_II1>::__value
00915      && __is_pointer<_II2>::__value);
00916 
00917       return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
00918                                 __first2, __last2);
00919     }
00920 
00921   /**
00922    *  @brief Finds the first position in which @a val could be inserted
00923    *         without changing the ordering.
00924    *  @param  first   An iterator.
00925    *  @param  last    Another iterator.
00926    *  @param  val     The search term.
00927    *  @return         An iterator pointing to the first element <em>not less
00928    *                  than</em> @a val, or end() if every element is less than 
00929    *                  @a val.
00930    *  @ingroup binary_search_algorithms
00931   */
00932   template<typename _ForwardIterator, typename _Tp>
00933     _ForwardIterator
00934     lower_bound(_ForwardIterator __first, _ForwardIterator __last,
00935         const _Tp& __val)
00936     {
00937       typedef typename iterator_traits<_ForwardIterator>::value_type
00938     _ValueType;
00939       typedef typename iterator_traits<_ForwardIterator>::difference_type
00940     _DistanceType;
00941 
00942       // concept requirements
00943       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
00944       __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
00945       __glibcxx_requires_partitioned_lower(__first, __last, __val);
00946 
00947       _DistanceType __len = std::distance(__first, __last);
00948 
00949       while (__len > 0)
00950     {
00951       _DistanceType __half = __len >> 1;
00952       _ForwardIterator __middle = __first;
00953       std::advance(__middle, __half);
00954       if (*__middle < __val)
00955         {
00956           __first = __middle;
00957           ++__first;
00958           __len = __len - __half - 1;
00959         }
00960       else
00961         __len = __half;
00962     }
00963       return __first;
00964     }
00965 
00966   /// This is a helper function for the sort routines and for random.tcc.
00967   //  Precondition: __n > 0.
00968   template<typename _Size>
00969     inline _Size
00970     __lg(_Size __n)
00971     {
00972       _Size __k;
00973       for (__k = 0; __n != 0; __n >>= 1)
00974     ++__k;
00975       return __k - 1;
00976     }
00977 
00978   inline int
00979   __lg(int __n)
00980   { return sizeof(int) * __CHAR_BIT__  - 1 - __builtin_clz(__n); }
00981 
00982   inline long
00983   __lg(long __n)
00984   { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
00985 
00986   inline long long
00987   __lg(long long __n)
00988   { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
00989 
00990 _GLIBCXX_END_NAMESPACE
00991 
00992 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
00993 
00994   /**
00995    *  @brief Tests a range for element-wise equality.
00996    *  @ingroup non_mutating_algorithms
00997    *  @param  first1  An input iterator.
00998    *  @param  last1   An input iterator.
00999    *  @param  first2  An input iterator.
01000    *  @return   A boolean true or false.
01001    *
01002    *  This compares the elements of two ranges using @c == and returns true or
01003    *  false depending on whether all of the corresponding elements of the
01004    *  ranges are equal.
01005   */
01006   template<typename _II1, typename _II2>
01007     inline bool
01008     equal(_II1 __first1, _II1 __last1, _II2 __first2)
01009     {
01010       // concept requirements
01011       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
01012       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
01013       __glibcxx_function_requires(_EqualOpConcept<
01014         typename iterator_traits<_II1>::value_type,
01015         typename iterator_traits<_II2>::value_type>)
01016       __glibcxx_requires_valid_range(__first1, __last1);
01017 
01018       return std::__equal_aux(std::__niter_base(__first1),
01019                   std::__niter_base(__last1),
01020                   std::__niter_base(__first2));
01021     }
01022 
01023   /**
01024    *  @brief Tests a range for element-wise equality.
01025    *  @ingroup non_mutating_algorithms
01026    *  @param  first1  An input iterator.
01027    *  @param  last1   An input iterator.
01028    *  @param  first2  An input iterator.
01029    *  @param binary_pred A binary predicate @link functors
01030    *                  functor@endlink.
01031    *  @return         A boolean true or false.
01032    *
01033    *  This compares the elements of two ranges using the binary_pred
01034    *  parameter, and returns true or
01035    *  false depending on whether all of the corresponding elements of the
01036    *  ranges are equal.
01037   */
01038   template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
01039     inline bool
01040     equal(_IIter1 __first1, _IIter1 __last1,
01041       _IIter2 __first2, _BinaryPredicate __binary_pred)
01042     {
01043       // concept requirements
01044       __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
01045       __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
01046       __glibcxx_requires_valid_range(__first1, __last1);
01047 
01048       for (; __first1 != __last1; ++__first1, ++__first2)
01049     if (!bool(__binary_pred(*__first1, *__first2)))
01050       return false;
01051       return true;
01052     }
01053 
01054   /**
01055    *  @brief Performs @b dictionary comparison on ranges.
01056    *  @ingroup sorting_algorithms
01057    *  @param  first1  An input iterator.
01058    *  @param  last1   An input iterator.
01059    *  @param  first2  An input iterator.
01060    *  @param  last2   An input iterator.
01061    *  @return   A boolean true or false.
01062    *
01063    *  <em>Returns true if the sequence of elements defined by the range
01064    *  [first1,last1) is lexicographically less than the sequence of elements
01065    *  defined by the range [first2,last2).  Returns false otherwise.</em>
01066    *  (Quoted from [25.3.8]/1.)  If the iterators are all character pointers,
01067    *  then this is an inline call to @c memcmp.
01068   */
01069   template<typename _II1, typename _II2>
01070     inline bool
01071     lexicographical_compare(_II1 __first1, _II1 __last1,
01072                 _II2 __first2, _II2 __last2)
01073     {
01074       // concept requirements
01075       typedef typename iterator_traits<_II1>::value_type _ValueType1;
01076       typedef typename iterator_traits<_II2>::value_type _ValueType2;
01077       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
01078       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
01079       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
01080       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
01081       __glibcxx_requires_valid_range(__first1, __last1);
01082       __glibcxx_requires_valid_range(__first2, __last2);
01083 
01084       return std::__lexicographical_compare_aux(std::__niter_base(__first1),
01085                         std::__niter_base(__last1),
01086                         std::__niter_base(__first2),
01087                         std::__niter_base(__last2));
01088     }
01089 
01090   /**
01091    *  @brief Performs @b dictionary comparison on ranges.
01092    *  @ingroup sorting_algorithms
01093    *  @param  first1  An input iterator.
01094    *  @param  last1   An input iterator.
01095    *  @param  first2  An input iterator.
01096    *  @param  last2   An input iterator.
01097    *  @param  comp  A @link comparison_functors comparison functor@endlink.
01098    *  @return   A boolean true or false.
01099    *
01100    *  The same as the four-parameter @c lexicographical_compare, but uses the
01101    *  comp parameter instead of @c <.
01102   */
01103   template<typename _II1, typename _II2, typename _Compare>
01104     bool
01105     lexicographical_compare(_II1 __first1, _II1 __last1,
01106                 _II2 __first2, _II2 __last2, _Compare __comp)
01107     {
01108       typedef typename iterator_traits<_II1>::iterator_category _Category1;
01109       typedef typename iterator_traits<_II2>::iterator_category _Category2;
01110       typedef std::__lc_rai<_Category1, _Category2>     __rai_type;
01111 
01112       // concept requirements
01113       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
01114       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
01115       __glibcxx_requires_valid_range(__first1, __last1);
01116       __glibcxx_requires_valid_range(__first2, __last2);
01117 
01118       __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
01119       for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
01120        ++__first1, ++__first2)
01121     {
01122       if (__comp(*__first1, *__first2))
01123         return true;
01124       if (__comp(*__first2, *__first1))
01125         return false;
01126     }
01127       return __first1 == __last1 && __first2 != __last2;
01128     }
01129 
01130   /**
01131    *  @brief Finds the places in ranges which don't match.
01132    *  @ingroup non_mutating_algorithms
01133    *  @param  first1  An input iterator.
01134    *  @param  last1   An input iterator.
01135    *  @param  first2  An input iterator.
01136    *  @return   A pair of iterators pointing to the first mismatch.
01137    *
01138    *  This compares the elements of two ranges using @c == and returns a pair
01139    *  of iterators.  The first iterator points into the first range, the
01140    *  second iterator points into the second range, and the elements pointed
01141    *  to by the iterators are not equal.
01142   */
01143   template<typename _InputIterator1, typename _InputIterator2>
01144     pair<_InputIterator1, _InputIterator2>
01145     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
01146          _InputIterator2 __first2)
01147     {
01148       // concept requirements
01149       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
01150       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
01151       __glibcxx_function_requires(_EqualOpConcept<
01152         typename iterator_traits<_InputIterator1>::value_type,
01153         typename iterator_traits<_InputIterator2>::value_type>)
01154       __glibcxx_requires_valid_range(__first1, __last1);
01155 
01156       while (__first1 != __last1 && *__first1 == *__first2)
01157         {
01158       ++__first1;
01159       ++__first2;
01160         }
01161       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
01162     }
01163 
01164   /**
01165    *  @brief Finds the places in ranges which don't match.
01166    *  @ingroup non_mutating_algorithms
01167    *  @param  first1  An input iterator.
01168    *  @param  last1   An input iterator.
01169    *  @param  first2  An input iterator.
01170    *  @param binary_pred A binary predicate @link functors
01171    *         functor@endlink.
01172    *  @return   A pair of iterators pointing to the first mismatch.
01173    *
01174    *  This compares the elements of two ranges using the binary_pred
01175    *  parameter, and returns a pair
01176    *  of iterators.  The first iterator points into the first range, the
01177    *  second iterator points into the second range, and the elements pointed
01178    *  to by the iterators are not equal.
01179   */
01180   template<typename _InputIterator1, typename _InputIterator2,
01181        typename _BinaryPredicate>
01182     pair<_InputIterator1, _InputIterator2>
01183     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
01184          _InputIterator2 __first2, _BinaryPredicate __binary_pred)
01185     {
01186       // concept requirements
01187       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
01188       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
01189       __glibcxx_requires_valid_range(__first1, __last1);
01190 
01191       while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
01192         {
01193       ++__first1;
01194       ++__first2;
01195         }
01196       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
01197     }
01198 
01199 _GLIBCXX_END_NESTED_NAMESPACE
01200 
01201 // NB: This file is included within many other C++ includes, as a way
01202 // of getting the base algorithms. So, make sure that parallel bits
01203 // come in too if requested. 
01204 #ifdef _GLIBCXX_PARALLEL
01205 # include <parallel/algobase.h>
01206 #endif
01207 
01208 #endif