# 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
    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 2, 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 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 /*
    00032  *
    00033  * Copyright (c) 1994
    00034  * Hewlett-Packard Company
    00035  *
    00036  * Permission to use, copy, modify, distribute and sell this software
    00037  * and its documentation for any purpose is hereby granted without fee,
    00038  * provided that the above copyright notice appear in all copies and
    00039  * that both that copyright notice and this permission notice appear
    00040  * in supporting documentation.  Hewlett-Packard Company makes no
    00041  * representations about the suitability of this software for any
    00042  * purpose.  It is provided "as is" without express or implied warranty.
    00043  *
    00044  *
    00045  * Copyright (c) 1996-1998
    00046  * Silicon Graphics Computer Systems, Inc.
    00047  *
    00048  * Permission to use, copy, modify, distribute and sell this software
    00049  * and its documentation for any purpose is hereby granted without fee,
    00050  * provided that the above copyright notice appear in all copies and
    00051  * that both that copyright notice and this permission notice appear
    00052  * in supporting documentation.  Silicon Graphics makes no
    00053  * representations about the suitability of this software for any
    00054  * purpose.  It is provided "as is" without express or implied warranty.
    00055  */
    00056
    00057 /** @file stl_algobase.h
    00058  *  This is an internal header file, included by other library headers.
    00059  *  You should not attempt to use it directly.
    00060  */
    00061
    00062 #ifndef _STL_ALGOBASE_H
    00063 #define _STL_ALGOBASE_H 1
    00064
    00065 #include <bits/c++config.h>
    00066 #include <cstddef>
    00067 #include <bits/functexcept.h>
    00068 #include <bits/cpp_type_traits.h>
    00069 #include <ext/type_traits.h>
    00070 #include <ext/numeric_traits.h>
    00071 #include <bits/stl_pair.h>
    00072 #include <bits/stl_iterator_base_types.h>
    00073 #include <bits/stl_iterator_base_funcs.h>
    00074 #include <bits/stl_iterator.h>
    00075 #include <bits/concept_check.h>
    00076 #include <debug/debug.h>
    00077 #include <bits/stl_move.h> // For std::swap and _GLIBCXX_MOVE
    00078
    00079 _GLIBCXX_BEGIN_NAMESPACE(std)
    00080
    00081   // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
    00082   // nutshell, we are partially implementing the resolution of DR 187,
    00083   // when it's safe, i.e., the value_types are equal.
    00084   template<bool _BoolType>
    00085     struct __iter_swap
    00086     {
    00087       template<typename _ForwardIterator1, typename _ForwardIterator2>
    00088         static void
    00089         iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
    00090         {
    00091           typedef typename iterator_traits<_ForwardIterator1>::value_type
    00092             _ValueType1;
    00093           _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
    00094           *__a = _GLIBCXX_MOVE(*__b);
    00095           *__b = _GLIBCXX_MOVE(__tmp);
    00096     }
    00097     };
    00098
    00099   template<>
    00100     struct __iter_swap<true>
    00101     {
    00102       template<typename _ForwardIterator1, typename _ForwardIterator2>
    00103         static void
    00104         iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
    00105         {
    00106           swap(*__a, *__b);
    00107         }
    00108     };
    00109
    00110   /**
    00111    *  @brief Swaps the contents of two iterators.
    00112    *  @param  a  An iterator.
    00113    *  @param  b  Another iterator.
    00114    *  @return   Nothing.
    00115    *
    00116    *  This function swaps the values pointed to by two iterators, not the
    00117    *  iterators themselves.
    00118   */
    00119   template<typename _ForwardIterator1, typename _ForwardIterator2>
    00120     inline void
    00121     iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
    00122     {
    00123       typedef typename iterator_traits<_ForwardIterator1>::value_type
    00124     _ValueType1;
    00125       typedef typename iterator_traits<_ForwardIterator2>::value_type
    00126     _ValueType2;
    00127
    00128       // concept requirements
    00129       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
    00130                   _ForwardIterator1>)
    00131       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
    00132                   _ForwardIterator2>)
    00133       __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
    00134                   _ValueType2>)
    00135       __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
    00136                   _ValueType1>)
    00137
    00138       typedef typename iterator_traits<_ForwardIterator1>::reference
    00139     _ReferenceType1;
    00140       typedef typename iterator_traits<_ForwardIterator2>::reference
    00141     _ReferenceType2;
    00142       std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
    00143     && __are_same<_ValueType1&, _ReferenceType1>::__value
    00144     && __are_same<_ValueType2&, _ReferenceType2>::__value>::
    00145 	iter_swap(__a, __b);
    00146     }
    00147
    00148   /**
    00149    *  @brief Swap the elements of two sequences.
    00150    *  @param  first1  A forward iterator.
    00151    *  @param  last1   A forward iterator.
    00152    *  @param  first2  A forward iterator.
    00153    *  @return   An iterator equal to @p first2+(last1-first1).
    00154    *
    00155    *  Swaps each element in the range @p [first1,last1) with the
    00156    *  corresponding element in the range @p [first2,(last1-first1)).
    00157    *  The ranges must not overlap.
    00158   */
    00159   template<typename _ForwardIterator1, typename _ForwardIterator2>
    00160     _ForwardIterator2
    00161     swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
    00162         _ForwardIterator2 __first2)
    00163     {
    00164       // concept requirements
    00165       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
    00166                   _ForwardIterator1>)
    00167       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
    00168                   _ForwardIterator2>)
    00169       __glibcxx_requires_valid_range(__first1, __last1);
    00170
    00171       for (; __first1 != __last1; ++__first1, ++__first2)
    00172     std::iter_swap(__first1, __first2);
    00173       return __first2;
    00174     }
    00175
    00176   /**
    00177    *  @brief This does what you think it does.
    00178    *  @param  a  A thing of arbitrary type.
    00179    *  @param  b  Another thing of arbitrary type.
    00180    *  @return   The lesser of the parameters.
    00181    *
    00182    *  This is the simple classic generic implementation.  It will work on
    00183    *  temporary expressions, since they are only evaluated once, unlike a
    00184    *  preprocessor macro.
    00185   */
    00186   template<typename _Tp>
    00187     inline const _Tp&
    00188     min(const _Tp& __a, const _Tp& __b)
    00189     {
    00190       // concept requirements
    00191       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
    00192       //return __b < __a ? __b : __a;
    00193       if (__b < __a)
    00194     return __b;
    00195       return __a;
    00196     }
    00197
    00198   /**
    00199    *  @brief This does what you think it does.
    00200    *  @param  a  A thing of arbitrary type.
    00201    *  @param  b  Another thing of arbitrary type.
    00202    *  @return   The greater of the parameters.
    00203    *
    00204    *  This is the simple classic generic implementation.  It will work on
    00205    *  temporary expressions, since they are only evaluated once, unlike a
    00206    *  preprocessor macro.
    00207   */
    00208   template<typename _Tp>
    00209     inline const _Tp&
    00210     max(const _Tp& __a, const _Tp& __b)
    00211     {
    00212       // concept requirements
    00213       __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
    00214       //return  __a < __b ? __b : __a;
    00215       if (__a < __b)
    00216     return __b;
    00217       return __a;
    00218     }
    00219
    00220   /**
    00221    *  @brief This does what you think it does.
    00222    *  @param  a  A thing of arbitrary type.
    00223    *  @param  b  Another thing of arbitrary type.
    00224    *  @param  comp  A @link s20_3_3_comparisons comparison functor@endlink.
    00225    *  @return   The lesser of the parameters.
    00226    *
    00227    *  This will work on temporary expressions, since they are only evaluated
    00228    *  once, unlike a preprocessor macro.
    00229   */
    00230   template<typename _Tp, typename _Compare>
    00231     inline const _Tp&
    00232     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
    00233     {
    00234       //return __comp(__b, __a) ? __b : __a;
    00235       if (__comp(__b, __a))
    00236     return __b;
    00237       return __a;
    00238     }
    00239
    00240   /**
    00241    *  @brief This does what you think it does.
    00242    *  @param  a  A thing of arbitrary type.
    00243    *  @param  b  Another thing of arbitrary type.
    00244    *  @param  comp  A @link s20_3_3_comparisons 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
    00261   // If _Iterator is a __normal_iterator return its base (a plain pointer,
    00262   // normally) otherwise return it untouched.  See copy, fill, ...
    00263   template<typename _Iterator,
    00264        bool _IsNormal = __is_normal_iterator<_Iterator>::__value>
    00265     struct __niter_base
    00266     {
    00267       static _Iterator
    00268       __b(_Iterator __it)
    00269       { return __it; }
    00270     };
    00271
    00272   template<typename _Iterator>
    00273     struct __niter_base<_Iterator, true>
    00274     {
    00275       static typename _Iterator::iterator_type
    00276       __b(_Iterator __it)
    00277       { return __it.base(); }
    00278     };
    00279
    00280   // Likewise, for move_iterator.
    00281   template<typename _Iterator,
    00282        bool _IsMove = __is_move_iterator<_Iterator>::__value>
    00283     struct __miter_base
    00284     {
    00285       static _Iterator
    00286       __b(_Iterator __it)
    00287       { return __it; }
    00288     };
    00289
    00290   template<typename _Iterator>
    00291     struct __miter_base<_Iterator, true>
    00292     {
    00293       static typename _Iterator::iterator_type
    00294       __b(_Iterator __it)
    00295       { return __it.base(); }
    00296     };
    00297
    00298   // All of these auxiliary structs serve two purposes.  (1) Replace
    00299   // calls to copy with memmove whenever possible.  (Memmove, not memcpy,
    00300   // because the input and output ranges are permitted to overlap.)
    00301   // (2) If we're using random access iterators, then write the loop as
    00302   // a for loop with an explicit count.
    00303
    00304   template<bool, bool, typename>
    00305     struct __copy_move
    00306     {
    00307       template<typename _II, typename _OI>
    00308         static _OI
    00309         __copy_m(_II __first, _II __last, _OI __result)
    00310         {
    00311       for (; __first != __last; ++__result, ++__first)
    00312         *__result = *__first;
    00313       return __result;
    00314     }
    00315     };
    00316
    00317 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00318   template<typename _Category>
    00319     struct __copy_move<true, false, _Category>
    00320     {
    00321       template<typename _II, typename _OI>
    00322         static _OI
    00323         __copy_m(_II __first, _II __last, _OI __result)
    00324         {
    00325       for (; __first != __last; ++__result, ++__first)
    00326         *__result = std::move(*__first);
    00327       return __result;
    00328     }
    00329     };
    00330 #endif
    00331
    00332   template<>
    00333     struct __copy_move<false, false, random_access_iterator_tag>
    00334     {
    00335       template<typename _II, typename _OI>
    00336         static _OI
    00337         __copy_m(_II __first, _II __last, _OI __result)
    00338         {
    00339       typedef typename iterator_traits<_II>::difference_type _Distance;
    00340       for(_Distance __n = __last - __first; __n > 0; --__n)
    00341         {
    00342           *__result = *__first;
    00343           ++__first;
    00344           ++__result;
    00345         }
    00346       return __result;
    00347     }
    00348     };
    00349
    00350 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00351   template<>
    00352     struct __copy_move<true, false, random_access_iterator_tag>
    00353     {
    00354       template<typename _II, typename _OI>
    00355         static _OI
    00356         __copy_m(_II __first, _II __last, _OI __result)
    00357         {
    00358       typedef typename iterator_traits<_II>::difference_type _Distance;
    00359       for(_Distance __n = __last - __first; __n > 0; --__n)
    00360         {
    00361           *__result = std::move(*__first);
    00362           ++__first;
    00363           ++__result;
    00364         }
    00365       return __result;
    00366     }
    00367     };
    00368 #endif
    00369
    00370   template<bool _IsMove>
    00371     struct __copy_move<_IsMove, true, random_access_iterator_tag>
    00372     {
    00373       template<typename _Tp>
    00374         static _Tp*
    00375         __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
    00376         {
    00377       __builtin_memmove(__result, __first,
    00378                 sizeof(_Tp) * (__last - __first));
    00379       return __result + (__last - __first);
    00380     }
    00381     };
    00382
    00383   template<bool _IsMove, typename _II, typename _OI>
    00384     inline _OI
    00385     __copy_move_a(_II __first, _II __last, _OI __result)
    00386     {
    00387       typedef typename iterator_traits<_II>::value_type _ValueTypeI;
    00388       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
    00389       typedef typename iterator_traits<_II>::iterator_category _Category;
    00390       const bool __simple = (__is_pod(_ValueTypeI)
    00391                          && __is_pointer<_II>::__value
    00392                          && __is_pointer<_OI>::__value
    00393                  && __are_same<_ValueTypeI, _ValueTypeO>::__value);
    00394
    00395       return std::__copy_move<_IsMove, __simple,
    00396                           _Category>::__copy_m(__first, __last, __result);
    00397     }
    00398
    00399   // Helpers for streambuf iterators (either istream or ostream).
    00400   // NB: avoid including <iosfwd>, relatively large.
    00401   template<typename _CharT>
    00402     struct char_traits;
    00403
    00404   template<typename _CharT, typename _Traits>
    00405     class istreambuf_iterator;
    00406
    00407   template<typename _CharT, typename _Traits>
    00408     class ostreambuf_iterator;
    00409
    00410   template<bool _IsMove, typename _CharT>
    00411     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
    00412          ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
    00413     __copy_move_a2(_CharT*, _CharT*,
    00414            ostreambuf_iterator<_CharT, char_traits<_CharT> >);
    00415
    00416   template<bool _IsMove, typename _CharT>
    00417     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
    00418          ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
    00419     __copy_move_a2(const _CharT*, const _CharT*,
    00420            ostreambuf_iterator<_CharT, char_traits<_CharT> >);
    00421
    00422   template<bool _IsMove, typename _CharT>
    00423     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
    00424                     _CharT*>::__type
    00425     __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
    00426            istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
    00427
    00428   template<bool _IsMove, typename _II, typename _OI>
    00429     inline _OI
    00430     __copy_move_a2(_II __first, _II __last, _OI __result)
    00431     {
    00432       return _OI(std::__copy_move_a<_IsMove>
    00433          (std::__niter_base<_II>::__b(__first),
    00434           std::__niter_base<_II>::__b(__last),
    00435           std::__niter_base<_OI>::__b(__result)));
    00436     }
    00437
    00438   /**
    00439    *  @brief Copies the range [first,last) into result.
    00440    *  @param  first  An input iterator.
    00441    *  @param  last   An input iterator.
    00442    *  @param  result An output iterator.
    00443    *  @return   result + (first - last)
    00444    *
    00445    *  This inline function will boil down to a call to @c memmove whenever
    00446    *  possible.  Failing that, if random access iterators are passed, then the
    00447    *  loop count will be known (and therefore a candidate for compiler
    00448    *  optimizations such as unrolling).  Result may not be contained within
    00449    *  [first,last); the copy_backward function should be used instead.
    00450    *
    00451    *  Note that the end of the output range is permitted to be contained
    00452    *  within [first,last).
    00453   */
    00454   template<typename _II, typename _OI>
    00455     inline _OI
    00456     copy(_II __first, _II __last, _OI __result)
    00457     {
    00458       // concept requirements
    00459       __glibcxx_function_requires(_InputIteratorConcept<_II>)
    00460       __glibcxx_function_requires(_OutputIteratorConcept<_OI,
    00461         typename iterator_traits<_II>::value_type>)
    00462       __glibcxx_requires_valid_range(__first, __last);
    00463
    00464       return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
    00465           (std::__miter_base<_II>::__b(__first),
    00466            std::__miter_base<_II>::__b(__last), __result));
    00467     }
    00468
    00469 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00470   /**
    00471    *  @brief Moves the range [first,last) into result.
    00472    *  @param  first  An input iterator.
    00473    *  @param  last   An input iterator.
    00474    *  @param  result An output iterator.
    00475    *  @return   result + (first - last)
    00476    *
    00477    *  This inline function will boil down to a call to @c memmove whenever
    00478    *  possible.  Failing that, if random access iterators are passed, then the
    00479    *  loop count will be known (and therefore a candidate for compiler
    00480    *  optimizations such as unrolling).  Result may not be contained within
    00481    *  [first,last); the move_backward function should be used instead.
    00482    *
    00483    *  Note that the end of the output range is permitted to be contained
    00484    *  within [first,last).
    00485   */
    00486   template<typename _II, typename _OI>
    00487     inline _OI
    00488     move(_II __first, _II __last, _OI __result)
    00489     {
    00490       // concept requirements
    00491       __glibcxx_function_requires(_InputIteratorConcept<_II>)
    00492       __glibcxx_function_requires(_OutputIteratorConcept<_OI,
    00493         typename iterator_traits<_II>::value_type>)
    00494       __glibcxx_requires_valid_range(__first, __last);
    00495
    00496       return (std::__copy_move_a2<true>
    00497           (std::__miter_base<_II>::__b(__first),
    00498            std::__miter_base<_II>::__b(__last), __result));
    00499     }
    00500
    00501 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
    00502 #else
    00503 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
    00504 #endif
    00505
    00506   template<bool, bool, typename>
    00507     struct __copy_move_backward
    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 = *--__last;
    00515       return __result;
    00516     }
    00517     };
    00518
    00519 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00520   template<typename _Category>
    00521     struct __copy_move_backward<true, false, _Category>
    00522     {
    00523       template<typename _BI1, typename _BI2>
    00524         static _BI2
    00525         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
    00526         {
    00527       while (__first != __last)
    00528         *--__result = std::move(*--__last);
    00529       return __result;
    00530     }
    00531     };
    00532 #endif
    00533
    00534   template<>
    00535     struct __copy_move_backward<false, false, random_access_iterator_tag>
    00536     {
    00537       template<typename _BI1, typename _BI2>
    00538         static _BI2
    00539         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
    00540         {
    00541       typename iterator_traits<_BI1>::difference_type __n;
    00542       for (__n = __last - __first; __n > 0; --__n)
    00543         *--__result = *--__last;
    00544       return __result;
    00545     }
    00546     };
    00547
    00548 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00549   template<>
    00550     struct __copy_move_backward<true, false, random_access_iterator_tag>
    00551     {
    00552       template<typename _BI1, typename _BI2>
    00553         static _BI2
    00554         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
    00555         {
    00556       typename iterator_traits<_BI1>::difference_type __n;
    00557       for (__n = __last - __first; __n > 0; --__n)
    00558         *--__result = std::move(*--__last);
    00559       return __result;
    00560     }
    00561     };
    00562 #endif
    00563
    00564   template<bool _IsMove>
    00565     struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
    00566     {
    00567       template<typename _Tp>
    00568         static _Tp*
    00569         __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
    00570         {
    00571       const ptrdiff_t _Num = __last - __first;
    00572       __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
    00573       return __result - _Num;
    00574     }
    00575     };
    00576
    00577   template<bool _IsMove, typename _BI1, typename _BI2>
    00578     inline _BI2
    00579     __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
    00580     {
    00581       typedef typename iterator_traits<_BI1>::value_type _ValueType1;
    00582       typedef typename iterator_traits<_BI2>::value_type _ValueType2;
    00583       typedef typename iterator_traits<_BI1>::iterator_category _Category;
    00584       const bool __simple = (__is_pod(_ValueType1)
    00585                          && __is_pointer<_BI1>::__value
    00586                          && __is_pointer<_BI2>::__value
    00587                  && __are_same<_ValueType1, _ValueType2>::__value);
    00588
    00589       return std::__copy_move_backward<_IsMove, __simple,
    00590                                    _Category>::__copy_move_b(__first,
    00591                                  __last,
    00592                                  __result);
    00593     }
    00594
    00595   template<bool _IsMove, typename _BI1, typename _BI2>
    00596     inline _BI2
    00597     __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
    00598     {
    00599       return _BI2(std::__copy_move_backward_a<_IsMove>
    00600           (std::__niter_base<_BI1>::__b(__first),
    00601            std::__niter_base<_BI1>::__b(__last),
    00602            std::__niter_base<_BI2>::__b(__result)));
    00603     }
    00604
    00605   /**
    00606    *  @brief Copies the range [first,last) into result.
    00607    *  @param  first  A bidirectional iterator.
    00608    *  @param  last   A bidirectional iterator.
    00609    *  @param  result A bidirectional iterator.
    00610    *  @return   result - (first - last)
    00611    *
    00612    *  The function has the same effect as copy, but starts at the end of the
    00613    *  range and works its way to the start, returning the start of the result.
    00614    *  This inline function will boil down to a call to @c memmove whenever
    00615    *  possible.  Failing that, if random access iterators are passed, then the
    00616    *  loop count will be known (and therefore a candidate for compiler
    00617    *  optimizations such as unrolling).
    00618    *
    00619    *  Result may not be in the range [first,last).  Use copy instead.  Note
    00620    *  that the start of the output range may overlap [first,last).
    00621   */
    00622   template<typename _BI1, typename _BI2>
    00623     inline _BI2
    00624     copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
    00625     {
    00626       // concept requirements
    00627       __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
    00628       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
    00629       __glibcxx_function_requires(_ConvertibleConcept<
    00630         typename iterator_traits<_BI1>::value_type,
    00631         typename iterator_traits<_BI2>::value_type>)
    00632       __glibcxx_requires_valid_range(__first, __last);
    00633
    00634       return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
    00635           (std::__miter_base<_BI1>::__b(__first),
    00636            std::__miter_base<_BI1>::__b(__last), __result));
    00637     }
    00638
    00639 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00640   /**
    00641    *  @brief Moves the range [first,last) into result.
    00642    *  @param  first  A bidirectional iterator.
    00643    *  @param  last   A bidirectional iterator.
    00644    *  @param  result A bidirectional iterator.
    00645    *  @return   result - (first - last)
    00646    *
    00647    *  The function has the same effect as move, but starts at the end of the
    00648    *  range and works its way to the start, returning the start of the result.
    00649    *  This inline function will boil down to a call to @c memmove whenever
    00650    *  possible.  Failing that, if random access iterators are passed, then the
    00651    *  loop count will be known (and therefore a candidate for compiler
    00652    *  optimizations such as unrolling).
    00653    *
    00654    *  Result may not be in the range [first,last).  Use move instead.  Note
    00655    *  that the start of the output range may overlap [first,last).
    00656   */
    00657   template<typename _BI1, typename _BI2>
    00658     inline _BI2
    00659     move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
    00660     {
    00661       // concept requirements
    00662       __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
    00663       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
    00664       __glibcxx_function_requires(_ConvertibleConcept<
    00665         typename iterator_traits<_BI1>::value_type,
    00666         typename iterator_traits<_BI2>::value_type>)
    00667       __glibcxx_requires_valid_range(__first, __last);
    00668
    00669       return (std::__copy_move_backward_a2<true>
    00670           (std::__miter_base<_BI1>::__b(__first),
    00671            std::__miter_base<_BI1>::__b(__last), __result));
    00672     }
    00673
    00674 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
    00675 #else
    00676 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
    00677 #endif
    00678
    00679   template<typename _ForwardIterator, typename _Tp>
    00680     inline typename
    00681     __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
    00682     __fill_a(_ForwardIterator __first, _ForwardIterator __last,
    00683          const _Tp& __value)
    00684     {
    00685       for (; __first != __last; ++__first)
    00686     *__first = __value;
    00687     }
    00688
    00689   template<typename _ForwardIterator, typename _Tp>
    00690     inline typename
    00691     __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
    00692     __fill_a(_ForwardIterator __first, _ForwardIterator __last, _Tp __value)
    00693     {
    00694       for (; __first != __last; ++__first)
    00695     *__first = __value;
    00696     }
    00697
    00698   // Specialization: for char types we can use memset.
    00699   template<typename _Tp>
    00700     inline typename
    00701     __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
    00702     __fill_a(_Tp* __first, _Tp* __last, _Tp __c)
    00703     { __builtin_memset(__first, static_cast<unsigned char>(__c),
    00704                __last - __first); }
    00705
    00706   /**
    00707    *  @brief Fills the range [first,last) with copies of value.
    00708    *  @param  first  A forward iterator.
    00709    *  @param  last   A forward iterator.
    00710    *  @param  value  A reference-to-const of arbitrary type.
    00711    *  @return   Nothing.
    00712    *
    00713    *  This function fills a range with copies of the same value.  For char
    00714    *  types filling contiguous areas of memory, this becomes an inline call
    00715    *  to @c memset or @c wmemset.
    00716   */
    00717   template<typename _ForwardIterator, typename _Tp>
    00718     inline void
    00719     fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
    00720     {
    00721       // concept requirements
    00722       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
    00723                   _ForwardIterator>)
    00724       __glibcxx_requires_valid_range(__first, __last);
    00725
    00726       std::__fill_a(std::__niter_base<_ForwardIterator>::__b(__first),
    00727             std::__niter_base<_ForwardIterator>::__b(__last), __value);
    00728     }
    00729
    00730   template<typename _OutputIterator, typename _Size, typename _Tp>
    00731     inline typename
    00732     __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
    00733     __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
    00734     {
    00735       for (; __n > 0; --__n, ++__first)
    00736     *__first = __value;
    00737       return __first;
    00738     }
    00739
    00740   template<typename _OutputIterator, typename _Size, typename _Tp>
    00741     inline typename
    00742     __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
    00743     __fill_n_a(_OutputIterator __first, _Size __n, _Tp __value)
    00744     {
    00745       for (; __n > 0; --__n, ++__first)
    00746     *__first = __value;
    00747       return __first;
    00748     }
    00749
    00750   template<typename _Size, typename _Tp>
    00751     inline typename
    00752     __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
    00753     __fill_n_a(_Tp* __first, _Size __n, _Tp __c)
    00754     {
    00755       std::__fill_a(__first, __first + __n, __c);
    00756       return __first + __n;
    00757     }
    00758
    00759   /**
    00760    *  @brief Fills the range [first,first+n) with copies of value.
    00761    *  @param  first  An output iterator.
    00762    *  @param  n      The count of copies to perform.
    00763    *  @param  value  A reference-to-const of arbitrary type.
    00764    *  @return   The iterator at first+n.
    00765    *
    00766    *  This function fills a range with copies of the same value.  For char
    00767    *  types filling contiguous areas of memory, this becomes an inline call
    00768    *  to @c memset or @ wmemset.
    00769   */
    00770   template<typename _OI, typename _Size, typename _Tp>
    00771     inline _OI
    00772     fill_n(_OI __first, _Size __n, const _Tp& __value)
    00773     {
    00774       // concept requirements
    00775       __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
    00776
    00777       return _OI(std::__fill_n_a(std::__niter_base<_OI>::__b(__first),
    00778                  __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 _GLIBCXX_END_NAMESPACE
    00922
    00923 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
    00924
    00925   /**
    00926    *  @brief Tests a range for element-wise equality.
    00927    *  @param  first1  An input iterator.
    00928    *  @param  last1   An input iterator.
    00929    *  @param  first2  An input iterator.
    00930    *  @return   A boolean true or false.
    00931    *
    00932    *  This compares the elements of two ranges using @c == and returns true or
    00933    *  false depending on whether all of the corresponding elements of the
    00934    *  ranges are equal.
    00935   */
    00936   template<typename _II1, typename _II2>
    00937     inline bool
    00938     equal(_II1 __first1, _II1 __last1, _II2 __first2)
    00939     {
    00940       // concept requirements
    00941       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
    00942       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
    00943       __glibcxx_function_requires(_EqualOpConcept<
    00944         typename iterator_traits<_II1>::value_type,
    00945         typename iterator_traits<_II2>::value_type>)
    00946       __glibcxx_requires_valid_range(__first1, __last1);
    00947
    00948       return std::__equal_aux(std::__niter_base<_II1>::__b(__first1),
    00949                   std::__niter_base<_II1>::__b(__last1),
    00950                   std::__niter_base<_II2>::__b(__first2));
    00951     }
    00952
    00953   /**
    00954    *  @brief Tests a range for element-wise equality.
    00955    *  @param  first1  An input iterator.
    00956    *  @param  last1   An input iterator.
    00957    *  @param  first2  An input iterator.
    00958    *  @param binary_pred A binary predicate @link s20_3_1_base
    00959    *                  functor@endlink.
    00960    *  @return         A boolean true or false.
    00961    *
    00962    *  This compares the elements of two ranges using the binary_pred
    00963    *  parameter, and returns true or
    00964    *  false depending on whether all of the corresponding elements of the
    00965    *  ranges are equal.
    00966   */
    00967   template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
    00968     inline bool
    00969     equal(_IIter1 __first1, _IIter1 __last1,
    00970       _IIter2 __first2, _BinaryPredicate __binary_pred)
    00971     {
    00972       // concept requirements
    00973       __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
    00974       __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
    00975       __glibcxx_requires_valid_range(__first1, __last1);
    00976
    00977       for (; __first1 != __last1; ++__first1, ++__first2)
    00978     if (!bool(__binary_pred(*__first1, *__first2)))
    00979       return false;
    00980       return true;
    00981     }
    00982
    00983   /**
    00984    *  @brief Performs "dictionary" comparison on ranges.
    00985    *  @param  first1  An input iterator.
    00986    *  @param  last1   An input iterator.
    00987    *  @param  first2  An input iterator.
    00988    *  @param  last2   An input iterator.
    00989    *  @return   A boolean true or false.
    00990    *
    00991    *  "Returns true if the sequence of elements defined by the range
    00992    *  [first1,last1) is lexicographically less than the sequence of elements
    00993    *  defined by the range [first2,last2).  Returns false otherwise."
    00994    *  (Quoted from [25.3.8]/1.)  If the iterators are all character pointers,
    00995    *  then this is an inline call to @c memcmp.
    00996   */
    00997   template<typename _II1, typename _II2>
    00998     inline bool
    00999     lexicographical_compare(_II1 __first1, _II1 __last1,
    01000                 _II2 __first2, _II2 __last2)
    01001     {
    01002       // concept requirements
    01003       typedef typename iterator_traits<_II1>::value_type _ValueType1;
    01004       typedef typename iterator_traits<_II2>::value_type _ValueType2;
    01005       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
    01006       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
    01007       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
    01008       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
    01009       __glibcxx_requires_valid_range(__first1, __last1);
    01010       __glibcxx_requires_valid_range(__first2, __last2);
    01011
    01012       return std::__lexicographical_compare_aux
    01013     (std::__niter_base<_II1>::__b(__first1),
    01014      std::__niter_base<_II1>::__b(__last1),
    01015      std::__niter_base<_II2>::__b(__first2),
    01016      std::__niter_base<_II2>::__b(__last2));
    01017     }
    01018
    01019   /**
    01020    *  @brief Performs "dictionary" comparison on ranges.
    01021    *  @param  first1  An input iterator.
    01022    *  @param  last1   An input iterator.
    01023    *  @param  first2  An input iterator.
    01024    *  @param  last2   An input iterator.
    01025    *  @param  comp  A @link s20_3_3_comparisons comparison functor@endlink.
    01026    *  @return   A boolean true or false.
    01027    *
    01028    *  The same as the four-parameter @c lexicographical_compare, but uses the
    01029    *  comp parameter instead of @c <.
    01030   */
    01031   template<typename _II1, typename _II2, typename _Compare>
    01032     bool
    01033     lexicographical_compare(_II1 __first1, _II1 __last1,
    01034                 _II2 __first2, _II2 __last2, _Compare __comp)
    01035     {
    01036       typedef typename iterator_traits<_II1>::iterator_category _Category1;
    01037       typedef typename iterator_traits<_II2>::iterator_category _Category2;
    01038       typedef std::__lc_rai<_Category1, _Category2>     __rai_type;
    01039
    01040       // concept requirements
    01041       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
    01042       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
    01043       __glibcxx_requires_valid_range(__first1, __last1);
    01044       __glibcxx_requires_valid_range(__first2, __last2);
    01045
    01046       __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
    01047       for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
    01048        ++__first1, ++__first2)
    01049     {
    01050       if (__comp(*__first1, *__first2))
    01051         return true;
    01052       if (__comp(*__first2, *__first1))
    01053         return false;
    01054     }
    01055       return __first1 == __last1 && __first2 != __last2;
    01056     }
    01057
    01058   /**
    01059    *  @brief Finds the places in ranges which don't match.
    01060    *  @param  first1  An input iterator.
    01061    *  @param  last1   An input iterator.
    01062    *  @param  first2  An input iterator.
    01063    *  @return   A pair of iterators pointing to the first mismatch.
    01064    *
    01065    *  This compares the elements of two ranges using @c == and returns a pair
    01066    *  of iterators.  The first iterator points into the first range, the
    01067    *  second iterator points into the second range, and the elements pointed
    01068    *  to by the iterators are not equal.
    01069   */
    01070   template<typename _InputIterator1, typename _InputIterator2>
    01071     pair<_InputIterator1, _InputIterator2>
    01072     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
    01073          _InputIterator2 __first2)
    01074     {
    01075       // concept requirements
    01076       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
    01077       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
    01078       __glibcxx_function_requires(_EqualOpConcept<
    01079         typename iterator_traits<_InputIterator1>::value_type,
    01080         typename iterator_traits<_InputIterator2>::value_type>)
    01081       __glibcxx_requires_valid_range(__first1, __last1);
    01082
    01083       while (__first1 != __last1 && *__first1 == *__first2)
    01084         {
    01085       ++__first1;
    01086       ++__first2;
    01087         }
    01088       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
    01089     }
    01090
    01091   /**
    01092    *  @brief Finds the places in ranges which don't match.
    01093    *  @param  first1  An input iterator.
    01094    *  @param  last1   An input iterator.
    01095    *  @param  first2  An input iterator.
    01096    *  @param binary_pred A binary predicate @link s20_3_1_base
    01097    *         functor@endlink.
    01098    *  @return   A pair of iterators pointing to the first mismatch.
    01099    *
    01100    *  This compares the elements of two ranges using the binary_pred
    01101    *  parameter, and returns a pair
    01102    *  of iterators.  The first iterator points into the first range, the
    01103    *  second iterator points into the second range, and the elements pointed
    01104    *  to by the iterators are not equal.
    01105   */
    01106   template<typename _InputIterator1, typename _InputIterator2,
    01107        typename _BinaryPredicate>
    01108     pair<_InputIterator1, _InputIterator2>
    01109     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
    01110          _InputIterator2 __first2, _BinaryPredicate __binary_pred)
    01111     {
    01112       // concept requirements
    01113       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
    01114       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
    01115       __glibcxx_requires_valid_range(__first1, __last1);
    01116
    01117       while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
    01118         {
    01119       ++__first1;
    01120       ++__first2;
    01121         }
    01122       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
    01123     }
    01124
    01125 _GLIBCXX_END_NESTED_NAMESPACE
    01126
    01127 // NB: This file is included within many other C++ includes, as a way
    01128 // of getting the base algorithms. So, make sure that parallel bits
    01129 // come in too if requested.
    01130 #ifdef _GLIBCXX_PARALLEL
    01131 # include <parallel/algobase.h>
    01132 #endif
    01133
    01134 #endif