X-Git-Url: http://git.sourceforge.jp/view?p=pf3gnuchains%2Fgcc-fork.git;a=blobdiff_plain;f=libstdc%2B%2B-v3%2Finclude%2Fbits%2Fstl_algobase.h;h=0489c413b95c34c09ecbb166c653fbba089dc900;hp=937a2425982240e6e729d3332002c3ce535e61ad;hb=6ec4f2a349652f65572581028598fb0cdaa0d3c7;hpb=7b50c7052b920bdd129c144732ce4cf098180cc3 diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index 937a2425982..0489c413b95 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -1,11 +1,12 @@ -// Bits and pieces used in algorithms -*- C++ -*- +// Core algorithmic facilities -*- C++ -*- -// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 +// Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the -// Free Software Foundation; either version 2, or (at your option) +// Free Software Foundation; either version 3, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, @@ -13,19 +14,14 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -// You should have received a copy of the GNU General Public License along -// with this library; see the file COPYING. If not, write to the Free -// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, -// USA. +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. -// As a special exception, you may use this file as part of a free software -// library without restriction. Specifically, if other files instantiate -// templates or use macros or inline functions from this file, or you compile -// this file and link it with other files to produce an executable, this -// file does not by itself cause the resulting executable to be covered by -// the GNU General Public License. This exception does not however -// invalidate any other reasons why the executable file might be covered by -// the GNU General Public License. +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// . /* * @@ -58,46 +54,24 @@ * You should not attempt to use it directly. */ -#ifndef _ALGOBASE_H -#define _ALGOBASE_H 1 +#ifndef _STL_ALGOBASE_H +#define _STL_ALGOBASE_H 1 #include -#include -#include -#include #include -#include -#include +#include #include +#include +#include +#include #include #include #include #include #include +#include // For std::swap and _GLIBCXX_MOVE -namespace std -{ - - /** - * @brief Swaps two values. - * @param a A thing of arbitrary type. - * @param b Another thing of arbitrary type. - * @return Nothing. - * - * This is the simple classic generic implementation. It will work on - * any type which has a copy constructor and an assignment operator. - */ - template - inline void - swap(_Tp& __a, _Tp& __b) - { - // concept requirements - __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) - - const _Tp __tmp = __a; - __a = __b; - __b = __tmp; - } +_GLIBCXX_BEGIN_NAMESPACE(std) // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a // nutshell, we are partially implementing the resolution of DR 187, @@ -111,9 +85,9 @@ namespace std { typedef typename iterator_traits<_ForwardIterator1>::value_type _ValueType1; - const _ValueType1 __tmp = *__a; - *__a = *__b; - *__b = __tmp; + _ValueType1 __tmp = _GLIBCXX_MOVE(*__a); + *__a = _GLIBCXX_MOVE(*__b); + *__b = _GLIBCXX_MOVE(__tmp); } }; @@ -130,6 +104,7 @@ namespace std /** * @brief Swaps the contents of two iterators. + * @ingroup mutating_algorithms * @param a An iterator. * @param b Another iterator. * @return Nothing. @@ -155,15 +130,49 @@ namespace std _ValueType2>) __glibcxx_function_requires(_ConvertibleConcept<_ValueType2, _ValueType1>) - std::__iter_swap<__are_same<_ValueType1, _ValueType2>::_M_type>:: + + typedef typename iterator_traits<_ForwardIterator1>::reference + _ReferenceType1; + typedef typename iterator_traits<_ForwardIterator2>::reference + _ReferenceType2; + std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value + && __are_same<_ValueType1&, _ReferenceType1>::__value + && __are_same<_ValueType2&, _ReferenceType2>::__value>:: iter_swap(__a, __b); } - #undef min - #undef max + /** + * @brief Swap the elements of two sequences. + * @ingroup mutating_algorithms + * @param first1 A forward iterator. + * @param last1 A forward iterator. + * @param first2 A forward iterator. + * @return An iterator equal to @p first2+(last1-first1). + * + * Swaps each element in the range @p [first1,last1) with the + * corresponding element in the range @p [first2,(last1-first1)). + * The ranges must not overlap. + */ + template + _ForwardIterator2 + swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2) + { + // concept requirements + __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< + _ForwardIterator1>) + __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< + _ForwardIterator2>) + __glibcxx_requires_valid_range(__first1, __last1); + + for (; __first1 != __last1; ++__first1, ++__first2) + std::iter_swap(__first1, __first2); + return __first2; + } /** * @brief This does what you think it does. + * @ingroup sorting_algorithms * @param a A thing of arbitrary type. * @param b Another thing of arbitrary type. * @return The lesser of the parameters. @@ -186,6 +195,7 @@ namespace std /** * @brief This does what you think it does. + * @ingroup sorting_algorithms * @param a A thing of arbitrary type. * @param b Another thing of arbitrary type. * @return The greater of the parameters. @@ -208,9 +218,10 @@ namespace std /** * @brief This does what you think it does. + * @ingroup sorting_algorithms * @param a A thing of arbitrary type. * @param b Another thing of arbitrary type. - * @param comp A @link s20_3_3_comparisons comparison functor@endlink. + * @param comp A @link comparison_functors comparison functor@endlink. * @return The lesser of the parameters. * * This will work on temporary expressions, since they are only evaluated @@ -228,9 +239,10 @@ namespace std /** * @brief This does what you think it does. + * @ingroup sorting_algorithms * @param a A thing of arbitrary type. * @param b Another thing of arbitrary type. - * @param comp A @link s20_3_3_comparisons comparison functor@endlink. + * @param comp A @link comparison_functors comparison functor@endlink. * @return The greater of the parameters. * * This will work on temporary expressions, since they are only evaluated @@ -246,18 +258,62 @@ namespace std return __a; } + + // If _Iterator has a base returns it otherwise _Iterator is returned + // untouched + template + struct _Iter_base + { + typedef _Iterator iterator_type; + static iterator_type + _S_base(_Iterator __it) + { return __it; } + }; + + template + struct _Iter_base<_Iterator, true> + { + typedef typename _Iterator::iterator_type iterator_type; + static iterator_type + _S_base(_Iterator __it) + { return __it.base(); } + }; + + // If _Iterator is a __normal_iterator return its base (a plain pointer, + // normally) otherwise return it untouched. See copy, fill, ... + template + struct _Niter_base + : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value> + { }; + + template + inline typename _Niter_base<_Iterator>::iterator_type + __niter_base(_Iterator __it) + { return std::_Niter_base<_Iterator>::_S_base(__it); } + + // Likewise, for move_iterator. + template + struct _Miter_base + : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value> + { }; + + template + inline typename _Miter_base<_Iterator>::iterator_type + __miter_base(_Iterator __it) + { return std::_Miter_base<_Iterator>::_S_base(__it); } + // All of these auxiliary structs serve two purposes. (1) Replace // calls to copy with memmove whenever possible. (Memmove, not memcpy, // because the input and output ranges are permitted to overlap.) // (2) If we're using random access iterators, then write the loop as // a for loop with an explicit count. - template - struct __copy + template + struct __copy_move { template static _OI - copy(_II __first, _II __last, _OI __result) + __copy_m(_II __first, _II __last, _OI __result) { for (; __first != __last; ++__result, ++__first) *__result = *__first; @@ -265,12 +321,27 @@ namespace std } }; - template - struct __copy<_BoolType, random_access_iterator_tag> +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + template + struct __copy_move + { + template + static _OI + __copy_m(_II __first, _II __last, _OI __result) + { + for (; __first != __last; ++__result, ++__first) + *__result = std::move(*__first); + return __result; + } + }; +#endif + + template<> + struct __copy_move { template static _OI - copy(_II __first, _II __last, _OI __result) + __copy_m(_II __first, _II __last, _OI __result) { typedef typename iterator_traits<_II>::difference_type _Distance; for(_Distance __n = __last - __first; __n > 0; --__n) @@ -283,72 +354,97 @@ namespace std } }; +#ifdef __GXX_EXPERIMENTAL_CXX0X__ template<> - struct __copy + struct __copy_move + { + template + static _OI + __copy_m(_II __first, _II __last, _OI __result) + { + typedef typename iterator_traits<_II>::difference_type _Distance; + for(_Distance __n = __last - __first; __n > 0; --__n) + { + *__result = std::move(*__first); + ++__first; + ++__result; + } + return __result; + } + }; +#endif + + template + struct __copy_move<_IsMove, true, random_access_iterator_tag> { template static _Tp* - copy(const _Tp* __first, const _Tp* __last, _Tp* __result) - { - std::memmove(__result, __first, sizeof(_Tp) * (__last - __first)); - return __result + (__last - __first); + __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result) + { + const ptrdiff_t _Num = __last - __first; + if (_Num) + __builtin_memmove(__result, __first, sizeof(_Tp) * _Num); + return __result + _Num; } }; - template + template inline _OI - __copy_aux(_II __first, _II __last, _OI __result) + __copy_move_a(_II __first, _II __last, _OI __result) { typedef typename iterator_traits<_II>::value_type _ValueTypeI; typedef typename iterator_traits<_OI>::value_type _ValueTypeO; typedef typename iterator_traits<_II>::iterator_category _Category; - const bool __simple = (__is_scalar<_ValueTypeI>::_M_type - && __is_pointer<_II>::_M_type - && __is_pointer<_OI>::_M_type - && __are_same<_ValueTypeI, _ValueTypeO>::_M_type); + const bool __simple = (__is_pod(_ValueTypeI) + && __is_pointer<_II>::__value + && __is_pointer<_OI>::__value + && __are_same<_ValueTypeI, _ValueTypeO>::__value); - return std::__copy<__simple, _Category>::copy(__first, __last, __result); + return std::__copy_move<_IsMove, __simple, + _Category>::__copy_m(__first, __last, __result); } - template - struct __copy_normal - { - template - static _OI - copy_n(_II __first, _II __last, _OI __result) - { return std::__copy_aux(__first, __last, __result); } - }; + // Helpers for streambuf iterators (either istream or ostream). + // NB: avoid including , relatively large. + template + struct char_traits; - template<> - struct __copy_normal - { - template - static _OI - copy_n(_II __first, _II __last, _OI __result) - { return std::__copy_aux(__first.base(), __last.base(), __result); } - }; + template + class istreambuf_iterator; - template<> - struct __copy_normal - { - template - static _OI - copy_n(_II __first, _II __last, _OI __result) - { return _OI(std::__copy_aux(__first, __last, __result.base())); } - }; + template + class ostreambuf_iterator; - template<> - struct __copy_normal + template + typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, + ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type + __copy_move_a2(_CharT*, _CharT*, + ostreambuf_iterator<_CharT, char_traits<_CharT> >); + + template + typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, + ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type + __copy_move_a2(const _CharT*, const _CharT*, + ostreambuf_iterator<_CharT, char_traits<_CharT> >); + + template + typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, + _CharT*>::__type + __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >, + istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*); + + template + inline _OI + __copy_move_a2(_II __first, _II __last, _OI __result) { - template - static _OI - copy_n(_II __first, _II __last, _OI __result) - { return _OI(std::__copy_aux(__first.base(), __last.base(), - __result.base())); } - }; + return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first), + std::__niter_base(__last), + std::__niter_base(__result))); + } /** * @brief Copies the range [first,last) into result. + * @ingroup mutating_algorithms * @param first An input iterator. * @param last An input iterator. * @param result An output iterator. @@ -363,43 +459,93 @@ namespace std * Note that the end of the output range is permitted to be contained * within [first,last). */ - template - inline _OutputIterator - copy(_InputIterator __first, _InputIterator __last, - _OutputIterator __result) + template + inline _OI + copy(_II __first, _II __last, _OI __result) { // concept requirements - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) - __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, - typename iterator_traits<_InputIterator>::value_type>) + __glibcxx_function_requires(_InputIteratorConcept<_II>) + __glibcxx_function_requires(_OutputIteratorConcept<_OI, + typename iterator_traits<_II>::value_type>) __glibcxx_requires_valid_range(__first, __last); - const bool __in = __is_normal_iterator<_InputIterator>::_M_type; - const bool __out = __is_normal_iterator<_OutputIterator>::_M_type; - return std::__copy_normal<__in, __out>::copy_n(__first, __last, - __result); + return (std::__copy_move_a2<__is_move_iterator<_II>::__value> + (std::__miter_base(__first), std::__miter_base(__last), + __result)); } - - template - struct __copy_backward + +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + /** + * @brief Moves the range [first,last) into result. + * @ingroup mutating_algorithms + * @param first An input iterator. + * @param last An input iterator. + * @param result An output iterator. + * @return result + (first - last) + * + * This inline function will boil down to a call to @c memmove whenever + * possible. Failing that, if random access iterators are passed, then the + * loop count will be known (and therefore a candidate for compiler + * optimizations such as unrolling). Result may not be contained within + * [first,last); the move_backward function should be used instead. + * + * Note that the end of the output range is permitted to be contained + * within [first,last). + */ + template + inline _OI + move(_II __first, _II __last, _OI __result) + { + // concept requirements + __glibcxx_function_requires(_InputIteratorConcept<_II>) + __glibcxx_function_requires(_OutputIteratorConcept<_OI, + typename iterator_traits<_II>::value_type>) + __glibcxx_requires_valid_range(__first, __last); + + return std::__copy_move_a2(std::__miter_base(__first), + std::__miter_base(__last), __result); + } + +#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp) +#else +#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp) +#endif + + template + struct __copy_move_backward { template static _BI2 - copy_b(_BI1 __first, _BI1 __last, _BI2 __result) - { + __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) + { while (__first != __last) *--__result = *--__last; return __result; } }; - template - struct __copy_backward<_BoolType, random_access_iterator_tag> +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + template + struct __copy_move_backward { template static _BI2 - copy_b(_BI1 __first, _BI1 __last, _BI2 __result) - { + __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) + { + while (__first != __last) + *--__result = std::move(*--__last); + return __result; + } + }; +#endif + + template<> + struct __copy_move_backward + { + template + static _BI2 + __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) + { typename iterator_traits<_BI1>::difference_type __n; for (__n = __last - __first; __n > 0; --__n) *--__result = *--__last; @@ -407,76 +553,66 @@ namespace std } }; +#ifdef __GXX_EXPERIMENTAL_CXX0X__ template<> - struct __copy_backward + struct __copy_move_backward + { + template + static _BI2 + __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) + { + typename iterator_traits<_BI1>::difference_type __n; + for (__n = __last - __first; __n > 0; --__n) + *--__result = std::move(*--__last); + return __result; + } + }; +#endif + + template + struct __copy_move_backward<_IsMove, true, random_access_iterator_tag> { template static _Tp* - copy_b(const _Tp* __first, const _Tp* __last, _Tp* __result) - { + __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result) + { const ptrdiff_t _Num = __last - __first; - std::memmove(__result - _Num, __first, sizeof(_Tp) * _Num); + if (_Num) + __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num); return __result - _Num; } }; - template + template inline _BI2 - __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result) + __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result) { typedef typename iterator_traits<_BI1>::value_type _ValueType1; typedef typename iterator_traits<_BI2>::value_type _ValueType2; typedef typename iterator_traits<_BI1>::iterator_category _Category; - const bool __simple = (__is_scalar<_ValueType1>::_M_type - && __is_pointer<_BI1>::_M_type - && __is_pointer<_BI2>::_M_type - && __are_same<_ValueType1, _ValueType2>::_M_type); - - return std::__copy_backward<__simple, _Category>::copy_b(__first, __last, - __result); + const bool __simple = (__is_pod(_ValueType1) + && __is_pointer<_BI1>::__value + && __is_pointer<_BI2>::__value + && __are_same<_ValueType1, _ValueType2>::__value); + + return std::__copy_move_backward<_IsMove, __simple, + _Category>::__copy_move_b(__first, + __last, + __result); } - template - struct __copy_backward_normal - { - template - static _BI2 - copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result) - { return std::__copy_backward_aux(__first, __last, __result); } - }; - - template<> - struct __copy_backward_normal - { - template - static _BI2 - copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result) - { return std::__copy_backward_aux(__first.base(), __last.base(), - __result); } - }; - - template<> - struct __copy_backward_normal - { - template - static _BI2 - copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result) - { return _BI2(std::__copy_backward_aux(__first, __last, - __result.base())); } - }; - - template<> - struct __copy_backward_normal + template + inline _BI2 + __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result) { - template - static _BI2 - copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result) - { return _BI2(std::__copy_backward_aux(__first.base(), __last.base(), - __result.base())); } - }; + return _BI2(std::__copy_move_backward_a<_IsMove> + (std::__niter_base(__first), std::__niter_base(__last), + std::__niter_base(__result))); + } /** * @brief Copies the range [first,last) into result. + * @ingroup mutating_algorithms * @param first A bidirectional iterator. * @param last A bidirectional iterator. * @param result A bidirectional iterator. @@ -492,7 +628,7 @@ namespace std * Result may not be in the range [first,last). Use copy instead. Note * that the start of the output range may overlap [first,last). */ - template + template inline _BI2 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result) { @@ -504,52 +640,98 @@ namespace std typename iterator_traits<_BI2>::value_type>) __glibcxx_requires_valid_range(__first, __last); - const bool __bi1 = __is_normal_iterator<_BI1>::_M_type; - const bool __bi2 = __is_normal_iterator<_BI2>::_M_type; - return std::__copy_backward_normal<__bi1, __bi2>::copy_b_n(__first, __last, - __result); + return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value> + (std::__miter_base(__first), std::__miter_base(__last), + __result)); } - template - struct __fill +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + /** + * @brief Moves the range [first,last) into result. + * @ingroup mutating_algorithms + * @param first A bidirectional iterator. + * @param last A bidirectional iterator. + * @param result A bidirectional iterator. + * @return result - (first - last) + * + * The function has the same effect as move, but starts at the end of the + * range and works its way to the start, returning the start of the result. + * This inline function will boil down to a call to @c memmove whenever + * possible. Failing that, if random access iterators are passed, then the + * loop count will be known (and therefore a candidate for compiler + * optimizations such as unrolling). + * + * Result may not be in the range [first,last). Use move instead. Note + * that the start of the output range may overlap [first,last). + */ + template + inline _BI2 + move_backward(_BI1 __first, _BI1 __last, _BI2 __result) { - template - static void - fill(_ForwardIterator __first, _ForwardIterator __last, - const _Tp& __value) - { - for (; __first != __last; ++__first) - *__first = __value; - } - }; + // concept requirements + __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>) + __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>) + __glibcxx_function_requires(_ConvertibleConcept< + typename iterator_traits<_BI1>::value_type, + typename iterator_traits<_BI2>::value_type>) + __glibcxx_requires_valid_range(__first, __last); - template<> - struct __fill + return std::__copy_move_backward_a2(std::__miter_base(__first), + std::__miter_base(__last), + __result); + } + +#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp) +#else +#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp) +#endif + + template + inline typename + __gnu_cxx::__enable_if::__value, void>::__type + __fill_a(_ForwardIterator __first, _ForwardIterator __last, + const _Tp& __value) { - template - static void - fill(_ForwardIterator __first, _ForwardIterator __last, + for (; __first != __last; ++__first) + *__first = __value; + } + + template + inline typename + __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type + __fill_a(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) - { - const _Tp __tmp = __value; - for (; __first != __last; ++__first) - *__first = __tmp; - } - }; + { + const _Tp __tmp = __value; + for (; __first != __last; ++__first) + *__first = __tmp; + } + + // Specialization: for char types we can use memset. + template + inline typename + __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type + __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c) + { + const _Tp __tmp = __c; + __builtin_memset(__first, static_cast(__tmp), + __last - __first); + } /** * @brief Fills the range [first,last) with copies of value. + * @ingroup mutating_algorithms * @param first A forward iterator. * @param last A forward iterator. * @param value A reference-to-const of arbitrary type. * @return Nothing. * - * This function fills a range with copies of the same value. For one-byte - * types filling contiguous areas of memory, this becomes an inline call to - * @c memset. + * This function fills a range with copies of the same value. For char + * types filling contiguous areas of memory, this becomes an inline call + * to @c memset or @c wmemset. */ template - void + inline void fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) { // concept requirements @@ -557,176 +739,285 @@ namespace std _ForwardIterator>) __glibcxx_requires_valid_range(__first, __last); - const bool __scalar = __is_scalar<_Tp>::_M_type; - std::__fill<__scalar>::fill(__first, __last, __value); - } - - // Specialization: for one-byte types we can use memset. - inline void - fill(unsigned char* __first, unsigned char* __last, const unsigned char& __c) - { - __glibcxx_requires_valid_range(__first, __last); - const unsigned char __tmp = __c; - std::memset(__first, __tmp, __last - __first); - } - - inline void - fill(signed char* __first, signed char* __last, const signed char& __c) - { - __glibcxx_requires_valid_range(__first, __last); - const signed char __tmp = __c; - std::memset(__first, static_cast(__tmp), __last - __first); - } - - inline void - fill(char* __first, char* __last, const char& __c) - { - __glibcxx_requires_valid_range(__first, __last); - const char __tmp = __c; - std::memset(__first, static_cast(__tmp), __last - __first); - } - - template - struct __fill_n - { - template - static _OutputIterator - fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) - { - for (; __n > 0; --__n, ++__first) - *__first = __value; - return __first; - } - }; + std::__fill_a(std::__niter_base(__first), std::__niter_base(__last), + __value); + } - template<> - struct __fill_n + template + inline typename + __gnu_cxx::__enable_if::__value, _OutputIterator>::__type + __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) { - template - static _OutputIterator - fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) - { - const _Tp __tmp = __value; - for (; __n > 0; --__n, ++__first) - *__first = __tmp; - return __first; - } - }; + for (__decltype(__n + 0) __niter = __n; + __niter > 0; --__niter, ++__first) + *__first = __value; + return __first; + } + + template + inline typename + __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type + __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) + { + const _Tp __tmp = __value; + for (__decltype(__n + 0) __niter = __n; + __niter > 0; --__niter, ++__first) + *__first = __tmp; + return __first; + } + + template + inline typename + __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type + __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c) + { + std::__fill_a(__first, __first + __n, __c); + return __first + __n; + } /** * @brief Fills the range [first,first+n) with copies of value. + * @ingroup mutating_algorithms * @param first An output iterator. * @param n The count of copies to perform. * @param value A reference-to-const of arbitrary type. * @return The iterator at first+n. * - * This function fills a range with copies of the same value. For one-byte - * types filling contiguous areas of memory, this becomes an inline call to - * @c memset. + * This function fills a range with copies of the same value. For char + * types filling contiguous areas of memory, this becomes an inline call + * to @c memset or @ wmemset. + * + * _GLIBCXX_RESOLVE_LIB_DEFECTS + * DR 865. More algorithms that throw away information */ - template - _OutputIterator - fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) + template + inline _OI + fill_n(_OI __first, _Size __n, const _Tp& __value) { // concept requirements - __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _Tp>) + __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>) - const bool __scalar = __is_scalar<_Tp>::_M_type; - return std::__fill_n<__scalar>::fill_n(__first, __n, __value); + return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value)); } - template - inline unsigned char* - fill_n(unsigned char* __first, _Size __n, const unsigned char& __c) + template + struct __equal { - std::fill(__first, __first + __n, __c); - return __first + __n; - } + template + static bool + equal(_II1 __first1, _II1 __last1, _II2 __first2) + { + for (; __first1 != __last1; ++__first1, ++__first2) + if (!(*__first1 == *__first2)) + return false; + return true; + } + }; - template - inline signed char* - fill_n(char* __first, _Size __n, const signed char& __c) + template<> + struct __equal { - std::fill(__first, __first + __n, __c); - return __first + __n; - } + template + static bool + equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2) + { + return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) + * (__last1 - __first1)); + } + }; - template - inline char* - fill_n(char* __first, _Size __n, const char& __c) + template + inline bool + __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2) { - std::fill(__first, __first + __n, __c); - return __first + __n; + typedef typename iterator_traits<_II1>::value_type _ValueType1; + typedef typename iterator_traits<_II2>::value_type _ValueType2; + const bool __simple = (__is_integer<_ValueType1>::__value + && __is_pointer<_II1>::__value + && __is_pointer<_II2>::__value + && __are_same<_ValueType1, _ValueType2>::__value); + + return std::__equal<__simple>::equal(__first1, __last1, __first2); } - /** - * @brief Finds the places in ranges which don't match. - * @param first1 An input iterator. - * @param last1 An input iterator. - * @param first2 An input iterator. - * @return A pair of iterators pointing to the first mismatch. - * - * This compares the elements of two ranges using @c == and returns a pair - * of iterators. The first iterator points into the first range, the - * second iterator points into the second range, and the elements pointed - * to by the iterators are not equal. - */ - template - pair<_InputIterator1, _InputIterator2> - mismatch(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2) + + template + struct __lc_rai { - // concept requirements - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) - __glibcxx_function_requires(_EqualOpConcept< - typename iterator_traits<_InputIterator1>::value_type, - typename iterator_traits<_InputIterator2>::value_type>) - __glibcxx_requires_valid_range(__first1, __last1); + template + static _II1 + __newlast1(_II1, _II1 __last1, _II2, _II2) + { return __last1; } + + template + static bool + __cnd2(_II __first, _II __last) + { return __first != __last; } + }; - while (__first1 != __last1 && *__first1 == *__first2) + template<> + struct __lc_rai + { + template + static _RAI1 + __newlast1(_RAI1 __first1, _RAI1 __last1, + _RAI2 __first2, _RAI2 __last2) { - ++__first1; - ++__first2; - } - return pair<_InputIterator1, _InputIterator2>(__first1, __first2); + const typename iterator_traits<_RAI1>::difference_type + __diff1 = __last1 - __first1; + const typename iterator_traits<_RAI2>::difference_type + __diff2 = __last2 - __first2; + return __diff2 < __diff1 ? __first1 + __diff2 : __last1; + } + + template + static bool + __cnd2(_RAI, _RAI) + { return true; } + }; + + template + struct __lexicographical_compare + { + template + static bool __lc(_II1, _II1, _II2, _II2); + }; + + template + template + bool + __lexicographical_compare<_BoolType>:: + __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2) + { + typedef typename iterator_traits<_II1>::iterator_category _Category1; + typedef typename iterator_traits<_II2>::iterator_category _Category2; + typedef std::__lc_rai<_Category1, _Category2> __rai_type; + + __last1 = __rai_type::__newlast1(__first1, __last1, + __first2, __last2); + for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2); + ++__first1, ++__first2) + { + if (*__first1 < *__first2) + return true; + if (*__first2 < *__first1) + return false; + } + return __first1 == __last1 && __first2 != __last2; + } + + template<> + struct __lexicographical_compare + { + template + static bool + __lc(const _Tp* __first1, const _Tp* __last1, + const _Up* __first2, const _Up* __last2) + { + const size_t __len1 = __last1 - __first1; + const size_t __len2 = __last2 - __first2; + const int __result = __builtin_memcmp(__first1, __first2, + std::min(__len1, __len2)); + return __result != 0 ? __result < 0 : __len1 < __len2; + } + }; + + template + inline bool + __lexicographical_compare_aux(_II1 __first1, _II1 __last1, + _II2 __first2, _II2 __last2) + { + typedef typename iterator_traits<_II1>::value_type _ValueType1; + typedef typename iterator_traits<_II2>::value_type _ValueType2; + const bool __simple = + (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value + && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed + && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed + && __is_pointer<_II1>::__value + && __is_pointer<_II2>::__value); + + return std::__lexicographical_compare<__simple>::__lc(__first1, __last1, + __first2, __last2); } /** - * @brief Finds the places in ranges which don't match. - * @param first1 An input iterator. - * @param last1 An input iterator. - * @param first2 An input iterator. - * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink. - * @return A pair of iterators pointing to the first mismatch. - * - * This compares the elements of two ranges using the binary_pred - * parameter, and returns a pair - * of iterators. The first iterator points into the first range, the - * second iterator points into the second range, and the elements pointed - * to by the iterators are not equal. + * @brief Finds the first position in which @a val could be inserted + * without changing the ordering. + * @param first An iterator. + * @param last Another iterator. + * @param val The search term. + * @return An iterator pointing to the first element not less + * than @a val, or end() if every element is less than + * @a val. + * @ingroup binary_search_algorithms */ - template - pair<_InputIterator1, _InputIterator2> - mismatch(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _BinaryPredicate __binary_pred) + template + _ForwardIterator + lower_bound(_ForwardIterator __first, _ForwardIterator __last, + const _Tp& __val) { + typedef typename iterator_traits<_ForwardIterator>::value_type + _ValueType; + typedef typename iterator_traits<_ForwardIterator>::difference_type + _DistanceType; + // concept requirements - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) - __glibcxx_requires_valid_range(__first1, __last1); + __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) + __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>) + __glibcxx_requires_partitioned_lower(__first, __last, __val); - while (__first1 != __last1 && __binary_pred(*__first1, *__first2)) - { - ++__first1; - ++__first2; - } - return pair<_InputIterator1, _InputIterator2>(__first1, __first2); + _DistanceType __len = std::distance(__first, __last); + _DistanceType __half; + _ForwardIterator __middle; + + while (__len > 0) + { + __half = __len >> 1; + __middle = __first; + std::advance(__middle, __half); + if (*__middle < __val) + { + __first = __middle; + ++__first; + __len = __len - __half - 1; + } + else + __len = __half; + } + return __first; } + /// This is a helper function for the sort routines and for random.tcc. + // Precondition: __n > 0. + template + inline _Size + __lg(_Size __n) + { + _Size __k; + for (__k = 0; __n != 0; __n >>= 1) + ++__k; + return __k - 1; + } + + inline int + __lg(int __n) + { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); } + + inline long + __lg(long __n) + { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); } + + inline long long + __lg(long long __n) + { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); } + +_GLIBCXX_END_NAMESPACE + +_GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P) + /** * @brief Tests a range for element-wise equality. + * @ingroup non_mutating_algorithms * @param first1 An input iterator. * @param last1 An input iterator. * @param first2 An input iterator. @@ -736,122 +1027,120 @@ namespace std * false depending on whether all of the corresponding elements of the * ranges are equal. */ - template + template inline bool - equal(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2) + equal(_II1 __first1, _II1 __last1, _II2 __first2) { // concept requirements - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) + __glibcxx_function_requires(_InputIteratorConcept<_II1>) + __glibcxx_function_requires(_InputIteratorConcept<_II2>) __glibcxx_function_requires(_EqualOpConcept< - typename iterator_traits<_InputIterator1>::value_type, - typename iterator_traits<_InputIterator2>::value_type>) + typename iterator_traits<_II1>::value_type, + typename iterator_traits<_II2>::value_type>) __glibcxx_requires_valid_range(__first1, __last1); - - for (; __first1 != __last1; ++__first1, ++__first2) - if (!(*__first1 == *__first2)) - return false; - return true; + + return std::__equal_aux(std::__niter_base(__first1), + std::__niter_base(__last1), + std::__niter_base(__first2)); } /** * @brief Tests a range for element-wise equality. + * @ingroup non_mutating_algorithms * @param first1 An input iterator. * @param last1 An input iterator. * @param first2 An input iterator. - * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink. - * @return A boolean true or false. + * @param binary_pred A binary predicate @link functors + * functor@endlink. + * @return A boolean true or false. * * This compares the elements of two ranges using the binary_pred * parameter, and returns true or * false depending on whether all of the corresponding elements of the * ranges are equal. */ - template + template inline bool - equal(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, - _BinaryPredicate __binary_pred) + equal(_IIter1 __first1, _IIter1 __last1, + _IIter2 __first2, _BinaryPredicate __binary_pred) { // concept requirements - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) + __glibcxx_function_requires(_InputIteratorConcept<_IIter1>) + __glibcxx_function_requires(_InputIteratorConcept<_IIter2>) __glibcxx_requires_valid_range(__first1, __last1); for (; __first1 != __last1; ++__first1, ++__first2) - if (!__binary_pred(*__first1, *__first2)) + if (!bool(__binary_pred(*__first1, *__first2))) return false; return true; } /** - * @brief Performs "dictionary" comparison on ranges. + * @brief Performs @b dictionary comparison on ranges. + * @ingroup sorting_algorithms * @param first1 An input iterator. * @param last1 An input iterator. * @param first2 An input iterator. * @param last2 An input iterator. * @return A boolean true or false. * - * "Returns true if the sequence of elements defined by the range + * Returns true if the sequence of elements defined by the range * [first1,last1) is lexicographically less than the sequence of elements - * defined by the range [first2,last2). Returns false otherwise." + * defined by the range [first2,last2). Returns false otherwise. * (Quoted from [25.3.8]/1.) If the iterators are all character pointers, * then this is an inline call to @c memcmp. */ - template - bool - lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _InputIterator2 __last2) + template + inline bool + lexicographical_compare(_II1 __first1, _II1 __last1, + _II2 __first2, _II2 __last2) { // concept requirements - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) - __glibcxx_function_requires(_LessThanComparableConcept< - typename iterator_traits<_InputIterator1>::value_type>) - __glibcxx_function_requires(_LessThanComparableConcept< - typename iterator_traits<_InputIterator2>::value_type>) + typedef typename iterator_traits<_II1>::value_type _ValueType1; + typedef typename iterator_traits<_II2>::value_type _ValueType2; + __glibcxx_function_requires(_InputIteratorConcept<_II1>) + __glibcxx_function_requires(_InputIteratorConcept<_II2>) + __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>) + __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) __glibcxx_requires_valid_range(__first1, __last1); __glibcxx_requires_valid_range(__first2, __last2); - for (; __first1 != __last1 && __first2 != __last2; - ++__first1, ++__first2) - { - if (*__first1 < *__first2) - return true; - if (*__first2 < *__first1) - return false; - } - return __first1 == __last1 && __first2 != __last2; + return std::__lexicographical_compare_aux(std::__niter_base(__first1), + std::__niter_base(__last1), + std::__niter_base(__first2), + std::__niter_base(__last2)); } /** - * @brief Performs "dictionary" comparison on ranges. + * @brief Performs @b dictionary comparison on ranges. + * @ingroup sorting_algorithms * @param first1 An input iterator. * @param last1 An input iterator. * @param first2 An input iterator. * @param last2 An input iterator. - * @param comp A @link s20_3_3_comparisons comparison functor@endlink. + * @param comp A @link comparison_functors comparison functor@endlink. * @return A boolean true or false. * - * The same as the four-parameter @c lexigraphical_compare, but uses the + * The same as the four-parameter @c lexicographical_compare, but uses the * comp parameter instead of @c <. */ - template + template bool - lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _InputIterator2 __last2, - _Compare __comp) + lexicographical_compare(_II1 __first1, _II1 __last1, + _II2 __first2, _II2 __last2, _Compare __comp) { + typedef typename iterator_traits<_II1>::iterator_category _Category1; + typedef typename iterator_traits<_II2>::iterator_category _Category2; + typedef std::__lc_rai<_Category1, _Category2> __rai_type; + // concept requirements - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) - __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) + __glibcxx_function_requires(_InputIteratorConcept<_II1>) + __glibcxx_function_requires(_InputIteratorConcept<_II2>) __glibcxx_requires_valid_range(__first1, __last1); __glibcxx_requires_valid_range(__first2, __last2); - for (; __first1 != __last1 && __first2 != __last2; + __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2); + for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2); ++__first1, ++__first2) { if (__comp(*__first1, *__first2)) @@ -862,42 +1151,82 @@ namespace std return __first1 == __last1 && __first2 != __last2; } - inline bool - lexicographical_compare(const unsigned char* __first1, - const unsigned char* __last1, - const unsigned char* __first2, - const unsigned char* __last2) - { - __glibcxx_requires_valid_range(__first1, __last1); - __glibcxx_requires_valid_range(__first2, __last2); - - const size_t __len1 = __last1 - __first1; - const size_t __len2 = __last2 - __first2; - const int __result = std::memcmp(__first1, __first2, - std::min(__len1, __len2)); - return __result != 0 ? __result < 0 : __len1 < __len2; - } - - inline bool - lexicographical_compare(const char* __first1, const char* __last1, - const char* __first2, const char* __last2) - { - __glibcxx_requires_valid_range(__first1, __last1); - __glibcxx_requires_valid_range(__first2, __last2); - -#if CHAR_MAX == SCHAR_MAX - return std::lexicographical_compare((const signed char*) __first1, - (const signed char*) __last1, - (const signed char*) __first2, - (const signed char*) __last2); -#else /* CHAR_MAX == SCHAR_MAX */ - return std::lexicographical_compare((const unsigned char*) __first1, - (const unsigned char*) __last1, - (const unsigned char*) __first2, - (const unsigned char*) __last2); -#endif /* CHAR_MAX == SCHAR_MAX */ - } - -} // namespace std + /** + * @brief Finds the places in ranges which don't match. + * @ingroup non_mutating_algorithms + * @param first1 An input iterator. + * @param last1 An input iterator. + * @param first2 An input iterator. + * @return A pair of iterators pointing to the first mismatch. + * + * This compares the elements of two ranges using @c == and returns a pair + * of iterators. The first iterator points into the first range, the + * second iterator points into the second range, and the elements pointed + * to by the iterators are not equal. + */ + template + pair<_InputIterator1, _InputIterator2> + mismatch(_InputIterator1 __first1, _InputIterator1 __last1, + _InputIterator2 __first2) + { + // concept requirements + __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) + __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) + __glibcxx_function_requires(_EqualOpConcept< + typename iterator_traits<_InputIterator1>::value_type, + typename iterator_traits<_InputIterator2>::value_type>) + __glibcxx_requires_valid_range(__first1, __last1); + + while (__first1 != __last1 && *__first1 == *__first2) + { + ++__first1; + ++__first2; + } + return pair<_InputIterator1, _InputIterator2>(__first1, __first2); + } + + /** + * @brief Finds the places in ranges which don't match. + * @ingroup non_mutating_algorithms + * @param first1 An input iterator. + * @param last1 An input iterator. + * @param first2 An input iterator. + * @param binary_pred A binary predicate @link functors + * functor@endlink. + * @return A pair of iterators pointing to the first mismatch. + * + * This compares the elements of two ranges using the binary_pred + * parameter, and returns a pair + * of iterators. The first iterator points into the first range, the + * second iterator points into the second range, and the elements pointed + * to by the iterators are not equal. + */ + template + pair<_InputIterator1, _InputIterator2> + mismatch(_InputIterator1 __first1, _InputIterator1 __last1, + _InputIterator2 __first2, _BinaryPredicate __binary_pred) + { + // concept requirements + __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) + __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) + __glibcxx_requires_valid_range(__first1, __last1); + + while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2))) + { + ++__first1; + ++__first2; + } + return pair<_InputIterator1, _InputIterator2>(__first1, __first2); + } + +_GLIBCXX_END_NESTED_NAMESPACE + +// NB: This file is included within many other C++ includes, as a way +// of getting the base algorithms. So, make sure that parallel bits +// come in too if requested. +#ifdef _GLIBCXX_PARALLEL +# include +#endif #endif