Index: include/bits/stl_uninitialized.h =================================================================== --- include/bits/stl_uninitialized.h (revision 137236) +++ include/bits/stl_uninitialized.h (working copy) @@ -1,6 +1,6 @@ // Raw memory manipulators -*- C++ -*- -// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 +// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 // Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free @@ -431,6 +431,53 @@ } } +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + template + _ForwardIterator + __uninitialized_copy_n(_InputIterator __first, _Size __n, + _ForwardIterator __result, input_iterator_tag) + { + _ForwardIterator __cur = __result; + try + { + for (; __n > 0; --__n, ++__first, ++__cur) + ::new(static_cast(&*__cur)) typename + iterator_traits<_ForwardIterator>::value_type(*__first); + return __cur; + } + catch(...) + { + std::_Destroy(__result, __cur); + __throw_exception_again; + } + } + + template + inline _ForwardIterator + __uninitialized_copy_n(_RandomAccessIterator __first, _Size __n, + _ForwardIterator __result, + random_access_iterator_tag) + { return std::uninitialized_copy(__first, __first + __n, __result); } + + /** + * @brief Copies the range [first,first+n) into result. + * @param first An input iterator. + * @param n The number of elements to copy. + * @param result An output iterator. + * @return result + n + * + * Like copy_n(), but does not require an initialized output range. + */ + template + inline _ForwardIterator + uninitialized_copy_n(_InputIterator __first, _Size __n, + _ForwardIterator __result) + { return std::__uninitialized_copy_n(__first, __n, __result, + std::__iterator_category(__first)); } +#endif + _GLIBCXX_END_NAMESPACE #endif /* _STL_UNINITIALIZED_H */ Index: include/bits/stl_algo.h =================================================================== --- include/bits/stl_algo.h (revision 137237) +++ include/bits/stl_algo.h (working copy) @@ -975,6 +975,53 @@ return __result; } + + template + _OutputIterator + __copy_n(_InputIterator __first, _Size __n, + _OutputIterator __result, input_iterator_tag) + { + for (; __n > 0; --__n) + { + *__result = *__first; + ++__first; + ++__result; + } + return __result; + } + + template + inline _OutputIterator + __copy_n(_RandomAccessIterator __first, _Size __n, + _OutputIterator __result, random_access_iterator_tag) + { return std::copy(__first, __first + __n, __result); } + + /** + * @brief Copies the range [first,first+n) into [result,result+n). + * @param first An input iterator. + * @param n The number of elements to copy. + * @param result An output iterator. + * @return result+n. + * + * 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). + */ + template + inline _OutputIterator + copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) + { + // concept requirements + __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) + __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, + typename iterator_traits<_InputIterator>::value_type>) + + return std::__copy_n(__first, __n, __result, + std::__iterator_category(__first)); + } + /** * @brief Copy the elements of a sequence to separate output sequences * depending on the truth value of a predicate. Index: include/bits/algorithmfwd.h =================================================================== --- include/bits/algorithmfwd.h (revision 137237) +++ include/bits/algorithmfwd.h (working copy) @@ -31,6 +31,7 @@ copy copy_backward copy_if (C++0x) + copy_n (C++0x) count count_if equal @@ -149,6 +150,10 @@ template _OIter copy_if(_IIter, _IIter, _OIter, _Predicate); + + template + _OIter + copy_n(_IIter, _Size, _OIter); #endif // count Index: testsuite/25_algorithms/copy_n/move_iterators/1.cc =================================================================== --- testsuite/25_algorithms/copy_n/move_iterators/1.cc (revision 0) +++ testsuite/25_algorithms/copy_n/move_iterators/1.cc (revision 0) @@ -0,0 +1,67 @@ +// { dg-options "-std=gnu++0x" } + +// 2008-06-29 Paolo Carlini + +// Copyright (C) 2008 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 Pred 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. + +#undef _GLIBCXX_CONCEPT_CHECKS +#define _GLIBCXX_TESTSUITE_ALLOW_RVALREF_ALIASING + +#include +#include +#include +#include +#include + +using __gnu_test::test_container; +using __gnu_test::input_iterator_wrapper; +using __gnu_test::output_iterator_wrapper; +using __gnu_test::rvalstruct; +using std::copy_n; + +typedef test_container container_in; +typedef test_container container_out; + +void test01() +{ + bool test __attribute__((unused)) = true; + + int inarray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + const int size = sizeof(inarray) / sizeof(int); + + rvalstruct in[size], out[size]; + std::copy(inarray, inarray + size, in); + + container_in incon(in, in + size); + container_out outcon(out, out + size); + + copy_n(std::move_iterator >(incon.begin()), + size, outcon.begin()); + VERIFY( std::equal(out, out + size, inarray) ); + for (int z = 0; z < size; ++z) + VERIFY( out[z].valid ); + for (int z = 0; z < size; ++z) + VERIFY( !in[z].valid ); +} + +int main() +{ + test01(); + return 0; +} Index: testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/2.cc =================================================================== --- testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/2.cc (revision 0) +++ testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/2.cc (revision 0) @@ -0,0 +1,45 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } + +// 2008-06-29 Paolo Carlini + +// Copyright (C) 2008 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// 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. + +#include +#include + +namespace std +{ + using __gnu_test::NonDefaultConstructible; + + typedef NonDefaultConstructible value_type; + typedef value_type* iterator_type; + typedef unsigned short size_type; + + template iterator_type copy_n(iterator_type, size_type, iterator_type); +} Index: testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/pod.cc =================================================================== --- testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/pod.cc (revision 0) +++ testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/pod.cc (revision 0) @@ -0,0 +1,45 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } + +// 2008-06-24 Paolo Carlini + +// Copyright (C) 2008 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// 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. + +#include +#include + +namespace std +{ + using __gnu_test::pod_int; + + typedef pod_int value_type; + typedef value_type* iterator_type; + typedef unsigned short size_type; + + template iterator_type copy_n(iterator_type, size_type, iterator_type); +} Index: testsuite/25_algorithms/copy_n/1.cc =================================================================== --- testsuite/25_algorithms/copy_n/1.cc (revision 0) +++ testsuite/25_algorithms/copy_n/1.cc (revision 0) @@ -0,0 +1,54 @@ +// { dg-options "-std=gnu++0x" } + +// 2008-06-29 Paolo Carlini + +// Copyright (C) 2008 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 Pred 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include + +void +test01() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; + const int N = sizeof(A) / sizeof(int); + + int i1[N]; + copy_n(A, N, i1); + VERIFY( equal(i1, i1 + N, A) ); + + vector v1(N); + copy_n(A, N, v1.begin()); + VERIFY( equal(v1.begin(), v1.end(), A) ); + + short s1[N]; + copy_n(A, N, s1); + VERIFY( equal(s1, s1 + N, A) ); +} + +int +main() +{ + test01(); + return 0; +} Index: testsuite/25_algorithms/copy_n/2.cc =================================================================== --- testsuite/25_algorithms/copy_n/2.cc (revision 0) +++ testsuite/25_algorithms/copy_n/2.cc (revision 0) @@ -0,0 +1,55 @@ +// { dg-options "-std=gnu++0x" } + +// 2008-06-29 Paolo Carlini + +// Copyright (C) 2008 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 Pred 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include + +void +test01() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; + const int N = sizeof(A) / sizeof(int); + const vector a(A, A + N); + + int i1[N]; + copy_n(a.begin(), N, i1); + VERIFY( equal(i1, i1 + N, a.begin()) ); + + vector v1(N); + copy_n(a.begin(), N, v1.begin()); + VERIFY( equal(v1.begin(), v1.end(), a.begin()) ); + + short s1[N]; + copy_n(a.begin(), N, s1); + VERIFY( equal(s1, s1 + N, a.begin()) ); +} + +int +main() +{ + test01(); + return 0; +} Index: testsuite/25_algorithms/copy_n/3.cc =================================================================== --- testsuite/25_algorithms/copy_n/3.cc (revision 0) +++ testsuite/25_algorithms/copy_n/3.cc (revision 0) @@ -0,0 +1,56 @@ +// { dg-options "-std=gnu++0x" } + +// 2008-06-29 Paolo Carlini + +// Copyright (C) 2008 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 Pred 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include +#include + +void +test01() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; + const int N = sizeof(A) / sizeof(int); + const deque a(A, A + N); + + int i1[N]; + copy_n(a.begin(), N, i1); + VERIFY( equal(i1, i1 + N, a.begin()) ); + + vector v1(N); + copy_n(a.begin(), N, v1.begin()); + VERIFY( equal(v1.begin(), v1.end(), a.begin()) ); + + short s1[N]; + copy_n(a.begin(), N, s1); + VERIFY( equal(s1, s1 + N, a.begin()) ); +} + +int +main() +{ + test01(); + return 0; +} Index: testsuite/25_algorithms/copy_n/4.cc =================================================================== --- testsuite/25_algorithms/copy_n/4.cc (revision 0) +++ testsuite/25_algorithms/copy_n/4.cc (revision 0) @@ -0,0 +1,56 @@ +// { dg-options "-std=gnu++0x" } + +// 2008-06-29 Paolo Carlini + +// Copyright (C) 2008 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 Pred 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include +#include + +void +test01() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; + const int N = sizeof(A) / sizeof(int); + const list a(A, A + N); + + int i1[N]; + copy_n(a.begin(), N, i1); + VERIFY( equal(i1, i1 + N, a.begin()) ); + + vector v1(N); + copy_n(a.begin(), N, v1.begin()); + VERIFY( equal(v1.begin(), v1.end(), a.begin()) ); + + short s1[N]; + copy_n(a.begin(), N, s1); + VERIFY( equal(s1, s1 + N, a.begin()) ); +} + +int +main() +{ + test01(); + return 0; +} Index: testsuite/25_algorithms/headers/algorithm/synopsis.cc =================================================================== --- testsuite/25_algorithms/headers/algorithm/synopsis.cc (revision 137237) +++ testsuite/25_algorithms/headers/algorithm/synopsis.cc (working copy) @@ -217,6 +217,10 @@ _OIter copy_if(_IIter, _IIter, _OIter, _Predicate); + template + _OIter + copy_n(_IIter, _Size, _OIter); + template pair<_OIter1, _OIter2> Index: testsuite/20_util/specialized_algorithms/uninitialized_copy_n/move_iterators/1.cc =================================================================== --- testsuite/20_util/specialized_algorithms/uninitialized_copy_n/move_iterators/1.cc (revision 0) +++ testsuite/20_util/specialized_algorithms/uninitialized_copy_n/move_iterators/1.cc (revision 0) @@ -0,0 +1,68 @@ +// { dg-options "-std=gnu++0x" } + +// 2008-06-29 Paolo Carlini + +// Copyright (C) 2008 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 Pred 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. + +#undef _GLIBCXX_CONCEPT_CHECKS +#define _GLIBCXX_TESTSUITE_ALLOW_RVALREF_ALIASING + +#include +#include +#include +#include +#include + +using __gnu_test::test_container; +using __gnu_test::input_iterator_wrapper; +using __gnu_test::forward_iterator_wrapper; +using __gnu_test::rvalstruct; +using std::uninitialized_copy_n; + +typedef test_container container_in; +typedef test_container container_out; + +void test01() +{ + bool test __attribute__((unused)) = true; + + int inarray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + const int size = sizeof(inarray) / sizeof(int); + + rvalstruct in[size], out[size]; + std::copy(inarray, inarray + size, in); + + container_in incon(in, in + size); + container_out outcon(out, out + size); + + uninitialized_copy_n(std::move_iterator >(incon.begin()), + size, outcon.begin()); + VERIFY( std::equal(out, out + size, inarray) ); + for (int z = 0; z < size; ++z) + VERIFY( out[z].valid ); + for (int z = 0; z < size; ++z) + VERIFY( !in[z].valid ); +} + + +int main() +{ + test01(); + return 0; +}