From ad20e16565562dbeed844b6c317e7f980da5e0d0 Mon Sep 17 00:00:00 2001 From: redi Date: Thu, 1 Nov 2012 01:30:34 +0000 Subject: * include/bits/forward_list.h (forward_list::assign): Dispatch to new functions based on assignability of elements. (forward_list::_M_assign): Add overloaded functions for assigning via assignment or via clearing and insertion. (forward_list::_M_assign_val): Likewise. (forward_list::_M_move_assign(forward_list&&, false_type)): Do not erase elements that are not moved. * include/bits/forward_list.tcc (forward_list::operator=): Call assign() to copy elements. * testsuite/23_containers/forward_list/cons/10.cc: New. * testsuite/23_containers/forward_list/cons/11.cc: New. * testsuite/23_containers/forward_list/cons/12.cc: New. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@193057 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/include/bits/forward_list.h | 89 +++++++++++++++++++++++------- libstdc++-v3/include/bits/forward_list.tcc | 17 +----- 2 files changed, 71 insertions(+), 35 deletions(-) (limited to 'libstdc++-v3/include') diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h index 8d4915d0e4d..b40fe9b4a9f 100644 --- a/libstdc++-v3/include/bits/forward_list.h +++ b/libstdc++-v3/include/bits/forward_list.h @@ -589,7 +589,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * in the range [@a __first,@a __last). * * Note that the assignment completely changes the %forward_list and - * that the number of elements of the resulting %forward_list's is the + * that the number of elements of the resulting %forward_list is the * same as the number of elements assigned. Old data is lost. */ template __assignable; + _M_assign(__first, __last, __assignable()); + } /** * @brief Assigns a given value to a %forward_list. @@ -613,10 +613,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ void assign(size_type __n, const _Tp& __val) - { - clear(); - insert_after(cbefore_begin(), __n, __val); - } + { _M_assign_n(__n, __val, is_copy_assignable<_Tp>()); } /** * @brief Assigns an initializer_list to a %forward_list. @@ -628,10 +625,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ void assign(std::initializer_list<_Tp> __il) - { - clear(); - insert_after(cbefore_begin(), __il); - } + { assign(__il.begin(), __il.end()); } /// Get a copy of the memory allocation object. allocator_type @@ -1255,13 +1249,70 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER if (__list._M_get_Node_allocator() == this->_M_get_Node_allocator()) _M_move_assign(std::move(__list), std::true_type()); else - { - // The rvalue's allocator cannot be moved, or is not equal, - // so we need to individually move each element. - this->assign(std::__make_move_if_noexcept_iterator(__list.begin()), - std::__make_move_if_noexcept_iterator(__list.end())); - __list.clear(); - } + // The rvalue's allocator cannot be moved, or is not equal, + // so we need to individually move each element. + this->assign(std::__make_move_if_noexcept_iterator(__list.begin()), + std::__make_move_if_noexcept_iterator(__list.end())); + } + + // Called by assign(_InputIterator, _InputIterator) if _Tp is + // CopyAssignable. + template + void + _M_assign(_InputIterator __first, _InputIterator __last, true_type) + { + auto __prev = before_begin(); + auto __curr = begin(); + auto __end = end(); + while (__curr != __end && __first != __last) + { + *__curr = *__first; + ++__prev; + ++__curr; + ++__first; + } + if (__first != __last) + insert_after(__prev, __first, __last); + else if (__curr != __end) + erase_after(__prev, __end); + } + + // Called by assign(_InputIterator, _InputIterator) if _Tp is not + // CopyAssignable. + template + void + _M_assign(_InputIterator __first, _InputIterator __last, false_type) + { + clear(); + insert_after(cbefore_begin(), __first, __last); + } + + // Called by assign(size_type, const _Tp&) if Tp is CopyAssignable + void + _M_assign_n(size_type __n, const _Tp& __val, true_type) + { + auto __prev = before_begin(); + auto __curr = begin(); + auto __end = end(); + while (__curr != __end && __n > 0) + { + *__curr = __val; + ++__prev; + ++__curr; + --__n; + } + if (__n > 0) + insert_after(__prev, __n, __val); + else if (__curr != __end) + erase_after(__prev, __end); + } + + // Called by assign(size_type, const _Tp&) if Tp is non-CopyAssignable + void + _M_assign_n(size_type __n, const _Tp& __val, false_type) + { + clear(); + insert_after(cbefore_begin(), __n, __val); } }; diff --git a/libstdc++-v3/include/bits/forward_list.tcc b/libstdc++-v3/include/bits/forward_list.tcc index 4f9a7fad4eb..7395b2029ad 100644 --- a/libstdc++-v3/include/bits/forward_list.tcc +++ b/libstdc++-v3/include/bits/forward_list.tcc @@ -165,22 +165,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER } std::__alloc_on_copy(__this_alloc, __that_alloc); } - iterator __prev1 = before_begin(); - iterator __curr1 = begin(); - iterator __last1 = end(); - const_iterator __first2 = __list.cbegin(); - const_iterator __last2 = __list.cend(); - while (__curr1 != __last1 && __first2 != __last2) - { - *__curr1 = *__first2; - ++__prev1; - ++__curr1; - ++__first2; - } - if (__first2 == __last2) - erase_after(__prev1, __last1); - else - insert_after(__prev1, __first2, __last2); + assign(__list.cbegin(), __list.cend()); } return *this; } -- cgit v1.2.1