From 6e539e23964d08d584a9c16050949424d44a3601 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Fri, 25 Jun 2004 09:25:57 +0000 Subject: [PATCH] [multiple changes] 2004-06-25 Dan Nicolaescu Paolo Carlini * include/bits/stl_algobase.h (__fill, __fill_n): New helpers for fill and fill_n, respectively: when copying is cheap, use a temporary to avoid a memory read in each iteration. 2004-06-25 Paolo Carlini * testsuite/25_algorithms/fill/1.cc: New. * testsuite/25_algorithms/fill/2.cc: Likewise. From-SVN: r83645 --- libstdc++-v3/ChangeLog | 12 ++ libstdc++-v3/include/bits/stl_algobase.h | 105 +++++++++++++----- .../testsuite/25_algorithms/fill/1.cc | 69 ++++++++++++ .../testsuite/25_algorithms/fill/2.cc | 69 ++++++++++++ 4 files changed, 229 insertions(+), 26 deletions(-) create mode 100644 libstdc++-v3/testsuite/25_algorithms/fill/1.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/fill/2.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 399452ba332..0ecd5333402 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2004-06-25 Dan Nicolaescu + Paolo Carlini + + * include/bits/stl_algobase.h (__fill, __fill_n): New helpers + for fill and fill_n, respectively: when copying is cheap, use a + temporary to avoid a memory read in each iteration. + +2004-06-25 Paolo Carlini + + * testsuite/25_algorithms/fill/1.cc: New. + * testsuite/25_algorithms/fill/2.cc: Likewise. + 2004-06-25 Benjamin Kosnik * include/debug/formatter.h (__gnu_debug::_Error_formatter): diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index 17c3007f1f4..0db0ef7daac 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -499,6 +499,32 @@ namespace std __result, __Normal()); } + template + struct __fill + { + template + static void + fill(_ForwardIterator __first, _ForwardIterator __last, + const _Tp& __value) + { + for (; __first != __last; ++__first) + *__first = __value; + } + }; + + template<> + struct __fill<__true_type> + { + template + static void + fill(_ForwardIterator __first, _ForwardIterator __last, + const _Tp& __value) + { + const _Tp __tmp = __value; + for (; __first != __last; ++__first) + *__first = __tmp; + } + }; /** * @brief Fills the range [first,last) with copies of value. @@ -520,31 +546,9 @@ namespace std _ForwardIterator>) __glibcxx_requires_valid_range(__first, __last); - for ( ; __first != __last; ++__first) - *__first = __value; - } - - /** - * @brief Fills the range [first,first+n) with copies of value. - * @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. - */ - template - _OutputIterator - fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) - { - // concept requirements - __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,_Tp>) - - for ( ; __n > 0; --__n, ++__first) - *__first = __value; - return __first; + typedef typename __type_traits<_Tp>::has_trivial_copy_constructor + _Trivial; + std::__fill<_Trivial>::fill(__first, __last, __value); } // Specialization: for one-byte types we can use memset. @@ -572,6 +576,56 @@ namespace std 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; + } + }; + + template<> + struct __fill_n<__true_type> + { + 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; + } + }; + + /** + * @brief Fills the range [first,first+n) with copies of value. + * @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. + */ + template + _OutputIterator + fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) + { + // concept requirements + __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _Tp>) + + typedef typename __type_traits<_Tp>::has_trivial_copy_constructor + _Trivial; + return std::__fill_n<_Trivial>::fill_n(__first, __n, __value); + } + template inline unsigned char* fill_n(unsigned char* __first, _Size __n, const unsigned char& __c) @@ -596,7 +650,6 @@ namespace std return __first + __n; } - /** * @brief Finds the places in ranges which don't match. * @param first1 An input iterator. diff --git a/libstdc++-v3/testsuite/25_algorithms/fill/1.cc b/libstdc++-v3/testsuite/25_algorithms/fill/1.cc new file mode 100644 index 00000000000..62994bc5865 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/fill/1.cc @@ -0,0 +1,69 @@ +// 2004-06-25 Paolo Carlini + +// Copyright (C) 2004 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) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// 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. + +// 25.2.5 [lib.alg.fill] Fill + +#include +#include +#include + +class num +{ + int stored; + +public: + num(int init = 0) + : stored(init) + { } + + operator int() const + { return stored; } +}; + +// fill +void test01() +{ + bool test __attribute__((unused)) = true; + using namespace std; + + const int val = 1; + + const int V[] = { val, val, val, val, val, val, val }; + const list::size_type N = sizeof(V) / sizeof(int); + + list coll(N); + fill(coll.begin(), coll.end(), val); + VERIFY( equal(coll.begin(), coll.end(), V) ); + + list coll2(N); + fill(coll2.begin(), coll2.end(), val); + VERIFY( equal(coll2.begin(), coll2.end(), V) ); +} + +#if !__GXX_WEAK__ && _MT_ALLOCATOR_H +// Explicitly instantiate for systems with no COMDAT or weak support. +template class __gnu_cxx::__mt_alloc >; +#endif + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/fill/2.cc b/libstdc++-v3/testsuite/25_algorithms/fill/2.cc new file mode 100644 index 00000000000..4f0437e80fb --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/fill/2.cc @@ -0,0 +1,69 @@ +// 2004-06-25 Paolo Carlini + +// Copyright (C) 2004 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) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// 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. + +// 25.2.5 [lib.alg.fill] Fill + +#include +#include +#include + +class num +{ + int stored; + +public: + num(int init = 0) + : stored(init) + { } + + operator int() const + { return stored; } +}; + +// fill_n +void test01() +{ + bool test __attribute__((unused)) = true; + using namespace std; + + const int val = 3; + + const int V[] = { val, val, val, val, val, val, val, val, val }; + const list::size_type N = sizeof(V) / sizeof(int); + + list coll(N); + fill_n(coll.begin(), coll.size(), val); + VERIFY( equal(coll.begin(), coll.end(), V) ); + + list coll2(N); + fill_n(coll2.begin(), coll2.size(), val); + VERIFY( equal(coll2.begin(), coll2.end(), V) ); +} + +#if !__GXX_WEAK__ && _MT_ALLOCATOR_H +// Explicitly instantiate for systems with no COMDAT or weak support. +template class __gnu_cxx::__mt_alloc >; +#endif + +int main() +{ + test01(); + return 0; +} -- 2.30.2