This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[v3] Use moves in vector::erase / reserve and deque::erase
- From: Paolo Carlini <pcarlini at suse dot de>
- To: Gcc Patch List <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 29 Oct 2007 03:02:33 +0100
- Subject: [v3] Use moves in vector::erase / reserve and deque::erase
Hi,
tested x86_64-linux, committed to mainline.
Paolo.
///////////////////
2007-10-28 Paolo Carlini <pcarlini@suse.de>
* include/bits/stl_algobase.h (_GLIBCXX_MOVE3,
_GLIBCXX_MOVE_BACKWARD3): Add.
* include/bits/stl_iterator.h (_GLIBCXX_MAKE_MOVE_ITERATOR): Add.
* include/bits/vector.tcc (vector<>::reserve): Use the latter.
(vector<>::erase): Use _GLIBCXX_MOVE3.
* include/bits/deque.tcc (deque<>::erase): Use _GLIBCXX_MOVE3
and _GLIBCXX_MOVE_BACKWARD3.
* testsuite/23_containers/vector/modifiers/erase/moveable.cc: New.
* testsuite/23_containers/vector/capacity/reserve/moveable.cc: New.
* testsuite/23_containers/deque/modifiers/erase/moveable.cc: New.
Index: include/bits/stl_algobase.h
===================================================================
--- include/bits/stl_algobase.h (revision 129707)
+++ include/bits/stl_algobase.h (working copy)
@@ -488,6 +488,10 @@
(std::__miter_base<_II>::__b(__first),
std::__miter_base<_II>::__b(__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<bool _IsMove, bool, typename>
@@ -626,6 +630,10 @@
(std::__miter_base<_BI1>::__b(__first),
std::__miter_base<_BI1>::__b(__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<typename _ForwardIterator, typename _Tp>
Index: include/bits/stl_iterator.h
===================================================================
--- include/bits/stl_iterator.h (revision 129707)
+++ include/bits/stl_iterator.h (working copy)
@@ -1021,6 +1021,9 @@
_GLIBCXX_END_NAMESPACE
+#define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
+#else
+#define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
#endif // __GXX_EXPERIMENTAL_CXX0X__
#endif
Index: include/bits/vector.tcc
===================================================================
--- include/bits/vector.tcc (revision 129707)
+++ include/bits/vector.tcc (working copy)
@@ -74,8 +74,9 @@
if (this->capacity() < __n)
{
const size_type __old_size = size();
- pointer __tmp = _M_allocate_and_copy(__n, this->_M_impl._M_start,
- this->_M_impl._M_finish);
+ pointer __tmp = _M_allocate_and_copy(__n,
+ _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_start),
+ _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_finish));
std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
_M_get_Tp_allocator());
_M_deallocate(this->_M_impl._M_start,
@@ -110,7 +111,7 @@
erase(iterator __position)
{
if (__position + 1 != end())
- std::copy(__position + 1, end(), __position);
+ _GLIBCXX_MOVE3(__position + 1, end(), __position);
--this->_M_impl._M_finish;
this->_M_impl.destroy(this->_M_impl._M_finish);
return __position;
@@ -122,7 +123,7 @@
erase(iterator __first, iterator __last)
{
if (__last != end())
- std::copy(__last, end(), __first);
+ _GLIBCXX_MOVE3(__last, end(), __first);
_M_erase_at_end(__first.base() + (end() - __last));
return __first;
}
Index: include/bits/deque.tcc
===================================================================
--- include/bits/deque.tcc (revision 129707)
+++ include/bits/deque.tcc (working copy)
@@ -117,13 +117,13 @@
if (static_cast<size_type>(__index) < (size() >> 1))
{
if (__position != begin())
- std::copy_backward(begin(), __position, __next);
+ _GLIBCXX_MOVE_BACKWARD3(begin(), __position, __next);
pop_front();
}
else
{
if (__next != end())
- std::copy(__next, end(), __position);
+ _GLIBCXX_MOVE3(__next, end(), __position);
pop_back();
}
return begin() + __index;
@@ -146,13 +146,13 @@
if (static_cast<size_type>(__elems_before) <= (size() - __n) / 2)
{
if (__first != begin())
- std::copy_backward(begin(), __first, __last);
+ _GLIBCXX_MOVE_BACKWARD3(begin(), __first, __last);
_M_erase_at_begin(begin() + __n);
}
else
{
if (__last != end())
- std::copy(__last, end(), __first);
+ _GLIBCXX_MOVE3(__last, end(), __first);
_M_erase_at_end(end() - __n);
}
return begin() + __elems_before;
Index: testsuite/23_containers/vector/modifiers/erase/moveable.cc
===================================================================
--- testsuite/23_containers/vector/modifiers/erase/moveable.cc (revision 0)
+++ testsuite/23_containers/vector/modifiers/erase/moveable.cc (revision 0)
@@ -0,0 +1,71 @@
+// { dg-options "-std=gnu++0x" }
+
+// 2007-10-28 Paolo Carlini <pcarlini@suse.de>
+
+// Copyright (C) 2007 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.
+
+// 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 <vector>
+#include <testsuite_hooks.h>
+#include <testsuite_rvalref.h>
+
+void
+test01()
+{
+ bool test __attribute__((unused)) = true;
+ using namespace __gnu_test;
+
+ std::vector<copycounter> a(40);
+ copycounter::copycount = 0;
+
+ a.erase(a.begin() + 20);
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.begin());
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.end() - 1);
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.begin() + 10, a.end() - 10);
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.begin(), a.begin() + 5);
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.end() - 5, a.end());
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.begin(), a.end());
+ VERIFY( copycounter::copycount == 0 );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
Index: testsuite/23_containers/vector/capacity/reserve/moveable.cc
===================================================================
--- testsuite/23_containers/vector/capacity/reserve/moveable.cc (revision 0)
+++ testsuite/23_containers/vector/capacity/reserve/moveable.cc (revision 0)
@@ -0,0 +1,56 @@
+// { dg-options "-std=gnu++0x" }
+
+// 2007-10-28 Paolo Carlini <pcarlini@suse.de>
+
+// Copyright (C) 2007 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.
+
+// 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 <vector>
+#include <testsuite_hooks.h>
+#include <testsuite_rvalref.h>
+
+void
+test01()
+{
+ bool test __attribute__((unused)) = true;
+ using namespace __gnu_test;
+
+ std::vector<copycounter> a(40);
+ copycounter::copycount = 0;
+
+ a.reserve(50);
+ VERIFY( copycounter::copycount == 0 );
+
+ a.reserve(200);
+ VERIFY( copycounter::copycount == 0 );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
Index: testsuite/23_containers/deque/modifiers/erase/moveable.cc
===================================================================
--- testsuite/23_containers/deque/modifiers/erase/moveable.cc (revision 0)
+++ testsuite/23_containers/deque/modifiers/erase/moveable.cc (revision 0)
@@ -0,0 +1,71 @@
+// { dg-options "-std=gnu++0x" }
+
+// 2007-10-28 Paolo Carlini <pcarlini@suse.de>
+
+// Copyright (C) 2007 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.
+
+// 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 <deque>
+#include <testsuite_hooks.h>
+#include <testsuite_rvalref.h>
+
+void
+test01()
+{
+ bool test __attribute__((unused)) = true;
+ using namespace __gnu_test;
+
+ std::deque<copycounter> a(40);
+ copycounter::copycount = 0;
+
+ a.erase(a.begin() + 20);
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.begin());
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.end() - 1);
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.begin() + 10, a.end() - 10);
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.begin(), a.begin() + 5);
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.end() - 5, a.end());
+ VERIFY( copycounter::copycount == 0 );
+
+ a.erase(a.begin(), a.end());
+ VERIFY( copycounter::copycount == 0 );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}