From: Paolo Carlini Date: Sun, 29 Jun 2008 11:08:26 +0000 (+0000) Subject: stl_algo.h (copy_n): Add in C++0x mode. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=b03717765d0d62af54bdb3112ac90ca6d7dd7c75;p=gcc.git stl_algo.h (copy_n): Add in C++0x mode. 2008-06-29 Paolo Carlini * include/bits/stl_algo.h (copy_n): Add in C++0x mode. * include/bits/algorithmfwd.h: Add. * include/bits/stl_uninitialized.h (uninitialized_copy_n): Add in C++0x mode. * testsuite/20_util/specialized_algorithms/uninitialized_copy_n/ move_iterators/1.cc: New * testsuite/25_algorithms/headers/algorithm/synopsis.cc: Update. * testsuite/25_algorithms/copy_n/1.cc: New. * testsuite/25_algorithms/copy_n/2.cc: Likewise. * testsuite/25_algorithms/copy_n/3.cc: Likewise. * testsuite/25_algorithms/copy_n/4.cc: Likewise. * testsuite/25_algorithms/copy_n/move_iterators/1.cc: Likewise. * testsuite/25_algorithms/copy_n/requirements/ explicit_instantiation/2.cc: Likewise. * testsuite/25_algorithms/partition_point/requirements/ explicit_instantiation/pod.cc: Likewise. From-SVN: r137251 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c8c6af9cc68..efb05eadf49 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,22 @@ +2008-06-29 Paolo Carlini + + * include/bits/stl_algo.h (copy_n): Add in C++0x mode. + * include/bits/algorithmfwd.h: Add. + * include/bits/stl_uninitialized.h (uninitialized_copy_n): Add + in C++0x mode. + * testsuite/20_util/specialized_algorithms/uninitialized_copy_n/ + move_iterators/1.cc: New + * testsuite/25_algorithms/headers/algorithm/synopsis.cc: Update. + * testsuite/25_algorithms/copy_n/1.cc: New. + * testsuite/25_algorithms/copy_n/2.cc: Likewise. + * testsuite/25_algorithms/copy_n/3.cc: Likewise. + * testsuite/25_algorithms/copy_n/4.cc: Likewise. + * testsuite/25_algorithms/copy_n/move_iterators/1.cc: Likewise. + * testsuite/25_algorithms/copy_n/requirements/ + explicit_instantiation/2.cc: Likewise. + * testsuite/25_algorithms/partition_point/requirements/ + explicit_instantiation/pod.cc: Likewise. + 2008-06-28 Paolo Carlini * include/bits/stl_algo.h (partition_point): Add in C++0x mode. diff --git a/libstdc++-v3/include/bits/algorithmfwd.h b/libstdc++-v3/include/bits/algorithmfwd.h index 23c4970dcf6..6fefe46b525 100644 --- a/libstdc++-v3/include/bits/algorithmfwd.h +++ b/libstdc++-v3/include/bits/algorithmfwd.h @@ -31,6 +31,7 @@ copy copy_backward copy_if (C++0x) + copy_n (C++0x) count count_if equal @@ -149,6 +150,10 @@ _GLIBCXX_BEGIN_NAMESPACE(std) template _OIter copy_if(_IIter, _IIter, _OIter, _Predicate); + + template + _OIter + copy_n(_IIter, _Size, _OIter); #endif // count diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 61f7003040f..40ce60b23ef 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -975,6 +975,53 @@ _GLIBCXX_BEGIN_NAMESPACE(std) 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. diff --git a/libstdc++-v3/include/bits/stl_uninitialized.h b/libstdc++-v3/include/bits/stl_uninitialized.h index 88dac3adcb0..af51264a249 100644 --- a/libstdc++-v3/include/bits/stl_uninitialized.h +++ b/libstdc++-v3/include/bits/stl_uninitialized.h @@ -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 @@ _GLIBCXX_BEGIN_NAMESPACE(std) } } +#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 */ diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy_n/move_iterators/1.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy_n/move_iterators/1.cc new file mode 100644 index 00000000000..bbe36f3b642 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy_n/move_iterators/1.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/1.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/1.cc new file mode 100644 index 00000000000..03af13674ec --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/1.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/2.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/2.cc new file mode 100644 index 00000000000..23cf9296c5c --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/2.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/3.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/3.cc new file mode 100644 index 00000000000..14bb356e722 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/3.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/4.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/4.cc new file mode 100644 index 00000000000..9320b7e9fc9 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/4.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/move_iterators/1.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/move_iterators/1.cc new file mode 100644 index 00000000000..08b26d4e9f2 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/move_iterators/1.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/2.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/2.cc new file mode 100644 index 00000000000..004479ce07d --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/2.cc @@ -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); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/pod.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/pod.cc new file mode 100644 index 00000000000..64a68de6184 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/requirements/explicit_instantiation/pod.cc @@ -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); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc b/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc index 005892d70e2..2d73e6f15bc 100644 --- a/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc +++ b/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc @@ -217,6 +217,10 @@ namespace std _OIter copy_if(_IIter, _IIter, _OIter, _Predicate); + template + _OIter + copy_n(_IIter, _Size, _OIter); + template pair<_OIter1, _OIter2>