This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[v3] Implement tr1/array::swap and assign
- From: Paolo Carlini <pcarlini at suse dot de>
- To: "'gcc-patches at gcc dot gnu dot org'" <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 24 Feb 2006 20:18:04 +0100
- Subject: [v3] Implement tr1/array::swap and assign
Hi,
a couple of member functions were still unimplemented, not
intentionally. Took the occasion to add some missing inline keywords.
Tested x86-linux, will go in 4_1-branch too, after the release.
Paolo.
////////////////////
2006-02-24 Paolo Carlini <pcarlini@suse.de>
* include/tr1/array (array<>::swap, assign): Implement.
* include/tr1/array (operator==, operator!=, operator<,
operator>, operator>=, operator<=, swap, get): Inline.
* testsuite/tr1/6_containers/array/requirements/member_swap.cc: New.
* testsuite/tr1/6_containers/array/requirements/assign.cc: Likewise.
* testsuite/tr1/6_containers/array/specialized_algorithms/swap.cc:
Likewise.
Index: include/tr1/array
===================================================================
--- include/tr1/array (revision 111394)
+++ include/tr1/array (working copy)
@@ -50,15 +50,15 @@
template<typename _Tp, std::size_t _Nm = 1>
struct array
{
- typedef _Tp value_type;
- typedef value_type& reference;
- typedef const value_type& const_reference;
- typedef value_type* iterator;
- typedef const value_type* const_iterator;
- typedef std::size_t size_type;
- typedef std::ptrdiff_t difference_type;
- typedef std::reverse_iterator<iterator> reverse_iterator;
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
+ typedef _Tp value_type;
+ typedef value_type& reference;
+ typedef const value_type& const_reference;
+ typedef value_type* iterator;
+ typedef const value_type* const_iterator;
+ typedef std::size_t size_type;
+ typedef std::ptrdiff_t difference_type;
+ typedef std::reverse_iterator<iterator> reverse_iterator;
+ typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
// Compile time constant without other dependencies.
enum { _S_index = _Nm };
@@ -69,10 +69,12 @@
// No explicit construct/copy/destroy for aggregate type.
void
- assign(const value_type& u);
+ assign(const value_type& __u)
+ { std::fill_n(begin(), size(), __u); }
void
- swap(array&);
+ swap(array& __other)
+ { std::swap_ranges(begin(), end(), __other.begin()); }
// Iterators.
iterator
@@ -169,17 +171,17 @@
// Array comparisons.
template<typename _Tp, std::size_t _Nm>
- bool
+ inline bool
operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
{ return std::equal(__one.begin(), __one.end(), __two.begin()); }
template<typename _Tp, std::size_t _Nm>
- bool
+ inline bool
operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
{ return !(__one == __two); }
template<typename _Tp, std::size_t _Nm>
- bool
+ inline bool
operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
{
return std::lexicographical_compare(__a.begin(), __a.end(),
@@ -187,25 +189,25 @@
}
template<typename _Tp, std::size_t _Nm>
- bool
+ inline bool
operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
{ return __two < __one; }
template<typename _Tp, std::size_t _Nm>
- bool
+ inline bool
operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
{ return !(__one > __two); }
template<typename _Tp, std::size_t _Nm>
- bool
+ inline bool
operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
{ return !(__one < __two); }
// Specialized algorithms [6.2.2.2].
template<typename _Tp, std::size_t _Nm>
- void
+ inline void
swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
- { swap_ranges(__one.begin(), __one.end(), __two.begin()); }
+ { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
// Tuple interface to class template array [6.2.2.5].
template<typename _Tp> class tuple_size;
@@ -220,12 +222,12 @@
{ typedef _Tp type; };
template<int _Int, typename _Tp, std::size_t _Nm>
- _Tp&
+ inline _Tp&
get(array<_Tp, _Nm>& __arr)
{ return __arr[_Int]; }
template<int _Int, typename _Tp, std::size_t _Nm>
- const _Tp&
+ inline const _Tp&
get(const array<_Tp, _Nm>& __arr)
{ return __arr[_Int]; }
Index: testsuite/tr1/6_containers/array/requirements/member_swap.cc
===================================================================
--- testsuite/tr1/6_containers/array/requirements/member_swap.cc (revision 0)
+++ testsuite/tr1/6_containers/array/requirements/member_swap.cc (revision 0)
@@ -0,0 +1,49 @@
+// 2006-02-24 Paolo Carlini <pcarlini@suse.de>
+//
+// Copyright (C) 2006 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.
+
+// 6.2.2 Class template array
+
+#include <tr1/array>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ bool test __attribute__((unused)) = true;
+
+ const size_t len = 5;
+ typedef std::tr1::array<int, len> array_type;
+
+ array_type a = { 0, 1, 2, 3, 4 };
+ const array_type a_ref = a;
+
+ array_type b = { 4, 3, 2, 1, 0 };
+ const array_type b_ref = b;
+
+ a.swap(b);
+ VERIFY( a == b_ref );
+ VERIFY( b == a_ref );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
Index: testsuite/tr1/6_containers/array/requirements/assign.cc
===================================================================
--- testsuite/tr1/6_containers/array/requirements/assign.cc (revision 0)
+++ testsuite/tr1/6_containers/array/requirements/assign.cc (revision 0)
@@ -0,0 +1,47 @@
+// 2006-02-24 Paolo Carlini <pcarlini@suse.de>
+//
+// Copyright (C) 2006 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.
+
+// 6.2.2 Class template array
+
+#include <tr1/array>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ bool test __attribute__((unused)) = true;
+
+ const size_t len = 3;
+ typedef std::tr1::array<int, len> array_type;
+
+ array_type a = { 0, 1, 2 };
+ const int value = 5;
+
+ a.assign(value);
+ VERIFY( a[0] == value );
+ VERIFY( a[1] == value );
+ VERIFY( a[2] == value );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
Index: testsuite/tr1/6_containers/array/specialized_algorithms/swap.cc
===================================================================
--- testsuite/tr1/6_containers/array/specialized_algorithms/swap.cc (revision 0)
+++ testsuite/tr1/6_containers/array/specialized_algorithms/swap.cc (revision 0)
@@ -0,0 +1,49 @@
+// 2006-02-24 Paolo Carlini <pcarlini@suse.de>
+//
+// Copyright (C) 2006 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.
+
+// 6.2.2.2 array specialized algorithms
+
+#include <tr1/array>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ bool test __attribute__((unused)) = true;
+
+ const size_t len = 5;
+ typedef std::tr1::array<int, len> array_type;
+
+ array_type a = { 0, 1, 2, 3, 4 };
+ const array_type a_ref = a;
+
+ array_type b = { 4, 3, 2, 1, 0 };
+ const array_type b_ref = b;
+
+ std::tr1::swap(a, b);
+ VERIFY( a == b_ref );
+ VERIFY( b == a_ref );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}