From 78263296991e5b329c8bf6d105b775151e439ce8 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Wed, 11 Apr 2012 17:31:57 +0000 Subject: [PATCH] forward_list.h (splice_after(const_iterator, forward_list&), [...]): Add per C++11 as published (and LWG 1310). 2012-04-11 Paolo Carlini * include/bits/forward_list.h (splice_after(const_iterator, forward_list&), splice_after(const_iterator, forward_list&, consst_iterator), splice_after(const_iterator, forward_list&, const_iterator, const_iterator), merge(forward_list&), merge(forward_list&, _Comp)): Add per C++11 as published (and LWG 1310). * include/debug/forward_list: Adjust. * include/bits/forward_list.h (splice_after(const_iterator, forward_list&&, const_iterator)): Only declare. (_M_transfer_after): Remove. (_M_splice_after(const_iterator, forward_list&&)): Change signature. (splice_after(const_iterator, forward_list&&, const_iterator, const_iterator)): Use the latter. * include/bits/forward_list.tcc (splice_after(const_iterator, forward_list&&, const_iterator)): Define here. (_M_splice_after): Define, use throughout. * include/bits/forward_list.h (insert_after(const_iterator, std::initializer_list<_Tp>)): Forward to insert_after(const_iterator, _InputIterator, _InputIterator). * include/bits/forward_list.tcc: Remove definition. * testsuite/23_containers/forward_list/modifiers/6.cc: New. * testsuite/23_containers/forward_list/operations/1.cc: Adjust. From-SVN: r186338 --- libstdc++-v3/ChangeLog | 28 +++++++ libstdc++-v3/include/bits/forward_list.h | 59 ++++++++------ libstdc++-v3/include/bits/forward_list.tcc | 45 ++++++----- libstdc++-v3/include/debug/forward_list | 23 ++++++ .../23_containers/forward_list/modifiers/6.cc | 94 ++++++++++++++++++++++ .../23_containers/forward_list/operations/1.cc | 4 +- 6 files changed, 204 insertions(+), 49 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/forward_list/modifiers/6.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index de08098..6e47c8c 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,33 @@ 2012-04-11 Paolo Carlini + * include/bits/forward_list.h (splice_after(const_iterator, + forward_list&), splice_after(const_iterator, forward_list&, + consst_iterator), splice_after(const_iterator, forward_list&, + const_iterator, const_iterator), merge(forward_list&), + merge(forward_list&, _Comp)): Add per C++11 as published (and + LWG 1310). + * include/debug/forward_list: Adjust. + + * include/bits/forward_list.h (splice_after(const_iterator, + forward_list&&, const_iterator)): Only declare. + (_M_transfer_after): Remove. + (_M_splice_after(const_iterator, forward_list&&)): Change signature. + (splice_after(const_iterator, forward_list&&, const_iterator, + const_iterator)): Use the latter. + * include/bits/forward_list.tcc (splice_after(const_iterator, + forward_list&&, const_iterator)): Define here. + (_M_splice_after): Define, use throughout. + + * include/bits/forward_list.h (insert_after(const_iterator, + std::initializer_list<_Tp>)): Forward to insert_after(const_iterator, + _InputIterator, _InputIterator). + * include/bits/forward_list.tcc: Remove definition. + + * testsuite/23_containers/forward_list/modifiers/6.cc: New. + * testsuite/23_containers/forward_list/operations/1.cc: Adjust. + +2012-04-11 Paolo Carlini + PR libstdc++/52931 * include/bits/functional_hash.h (struct hash): Remove definition. * testsuite/20_util/hash/52931.cc: New. diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h index 01020c5..76c3e33 100644 --- a/libstdc++-v3/include/bits/forward_list.h +++ b/libstdc++-v3/include/bits/forward_list.h @@ -53,15 +53,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _Fwd_list_node_base* _M_next; _Fwd_list_node_base* - _M_transfer_after(_Fwd_list_node_base* __begin) - { - _Fwd_list_node_base* __end = __begin; - while (__end && __end->_M_next) - __end = __end->_M_next; - return _M_transfer_after(__begin, __end); - } - - _Fwd_list_node_base* _M_transfer_after(_Fwd_list_node_base* __begin, _Fwd_list_node_base* __end) { @@ -925,7 +916,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * does not invalidate iterators and references. */ iterator - insert_after(const_iterator __pos, std::initializer_list<_Tp> __il); + insert_after(const_iterator __pos, std::initializer_list<_Tp> __il) + { return insert_after(__pos, __il.begin(), __il.end()); } /** * @brief Removes the element pointed to by the iterator following @@ -1047,9 +1039,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER splice_after(const_iterator __pos, forward_list&& __list) { if (!__list.empty()) - _M_splice_after(__pos, std::move(__list)); + _M_splice_after(__pos, __list.before_begin(), __list.end()); } + void + splice_after(const_iterator __pos, forward_list& __list) + { splice_after(__pos, std::move(__list)); } + /** * @brief Insert element from another %forward_list. * @param __pos Iterator referencing the element to insert after. @@ -1062,15 +1058,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ void splice_after(const_iterator __pos, forward_list&& __list, - const_iterator __i) - { - const_iterator __j = __i; - ++__j; - if (__pos == __i || __pos == __j) - return; + const_iterator __i); - splice_after(__pos, std::move(__list), __i, __j); - } + void + splice_after(const_iterator __pos, forward_list& __list, + const_iterator __i) + { splice_after(__pos, std::move(__list), __i); } /** * @brief Insert range from another %forward_list. @@ -1086,8 +1079,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * Undefined if @a __pos is in (__before,__last). */ void - splice_after(const_iterator __pos, forward_list&& __list, - const_iterator __before, const_iterator __last); + splice_after(const_iterator __pos, forward_list&&, + const_iterator __before, const_iterator __last) + { _M_splice_after(__pos, __before, __last); } + + void + splice_after(const_iterator __pos, forward_list&, + const_iterator __before, const_iterator __last) + { _M_splice_after(__pos, __before, __last); } /** * @brief Remove all elements equal to value. @@ -1130,7 +1129,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ void unique() - { this->unique(std::equal_to<_Tp>()); } + { unique(std::equal_to<_Tp>()); } /** * @brief Remove consecutive elements satisfying a predicate. @@ -1159,7 +1158,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ void merge(forward_list&& __list) - { this->merge(std::move(__list), std::less<_Tp>()); } + { merge(std::move(__list), std::less<_Tp>()); } + + void + merge(forward_list& __list) + { merge(std::move(__list)); } /** * @brief Merge sorted lists according to comparison function. @@ -1176,6 +1179,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER void merge(forward_list&& __list, _Comp __comp); + template + void + merge(forward_list& __list, _Comp __comp) + { merge(std::move(__list), __comp); } + /** * @brief Sort the elements of the list. * @@ -1184,7 +1192,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER */ void sort() - { this->sort(std::less<_Tp>()); } + { sort(std::less<_Tp>()); } /** * @brief Sort the forward_list using a comparison function. @@ -1218,7 +1226,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER // Called by splice_after and insert_after. iterator - _M_splice_after(const_iterator __pos, forward_list&& __list); + _M_splice_after(const_iterator __pos, const_iterator __before, + const_iterator __last); // Called by forward_list(n). void diff --git a/libstdc++-v3/include/bits/forward_list.tcc b/libstdc++-v3/include/bits/forward_list.tcc index 99fa3a0..3c9f238 100644 --- a/libstdc++-v3/include/bits/forward_list.tcc +++ b/libstdc++-v3/include/bits/forward_list.tcc @@ -223,22 +223,37 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER template typename forward_list<_Tp, _Alloc>::iterator forward_list<_Tp, _Alloc>:: - _M_splice_after(const_iterator __pos, forward_list&& __list) + _M_splice_after(const_iterator __pos, + const_iterator __before, const_iterator __last) { _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node); - iterator __before = __list.before_begin(); - return iterator(__tmp->_M_transfer_after(__before._M_node)); + _Node_base* __b = const_cast<_Node_base*>(__before._M_node); + _Node_base* __end = __b; + + while (__end && __end->_M_next != __last._M_node) + __end = __end->_M_next; + + if (__b != __end) + return iterator(__tmp->_M_transfer_after(__b, __end)); + else + return iterator(__tmp); } template void forward_list<_Tp, _Alloc>:: splice_after(const_iterator __pos, forward_list&&, - const_iterator __before, const_iterator __last) + const_iterator __i) { + const_iterator __j = __i; + ++__j; + + if (__pos == __i || __pos == __j) + return; + _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node); - __tmp->_M_transfer_after(const_cast<_Node_base*>(__before._M_node), - const_cast<_Node_base*>(__last._M_node)); + __tmp->_M_transfer_after(const_cast<_Node_base*>(__i._M_node), + const_cast<_Node_base*>(__j._M_node)); } template @@ -249,7 +264,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER if (__n) { forward_list __tmp(__n, __val, get_allocator()); - return _M_splice_after(__pos, std::move(__tmp)); + return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end()); } else return iterator(const_cast<_Node_base*>(__pos._M_node)); @@ -264,26 +279,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { forward_list __tmp(__first, __last, get_allocator()); if (!__tmp.empty()) - return _M_splice_after(__pos, std::move(__tmp)); + return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end()); else return iterator(const_cast<_Node_base*>(__pos._M_node)); } template - typename forward_list<_Tp, _Alloc>::iterator - forward_list<_Tp, _Alloc>:: - insert_after(const_iterator __pos, std::initializer_list<_Tp> __il) - { - if (__il.size()) - { - forward_list __tmp(__il, get_allocator()); - return _M_splice_after(__pos, std::move(__tmp)); - } - else - return iterator(const_cast<_Node_base*>(__pos._M_node)); - } - - template void forward_list<_Tp, _Alloc>:: remove(const _Tp& __val) diff --git a/libstdc++-v3/include/debug/forward_list b/libstdc++-v3/include/debug/forward_list index 1f887da..e21eb4a 100644 --- a/libstdc++-v3/include/debug/forward_list +++ b/libstdc++-v3/include/debug/forward_list @@ -418,6 +418,10 @@ namespace __debug } void + splice_after(const_iterator __pos, forward_list& __list) + { splice_after(__pos, std::move(__list)); } + + void splice_after(const_iterator __pos, forward_list&& __list, const_iterator __i) { @@ -440,6 +444,11 @@ namespace __debug } void + splice_after(const_iterator __pos, forward_list& __list, + const_iterator __i) + { splice_after(__pos, std::move(__list), __i); } + + void splice_after(const_iterator __pos, forward_list&& __list, const_iterator __before, const_iterator __last) { @@ -485,6 +494,11 @@ namespace __debug } void + splice_after(const_iterator __pos, forward_list& __list, + const_iterator __before, const_iterator __last) + { splice_after(__pos, std::move(__list), __before, __last); } + + void remove(const _Tp& __val) { _Base_iterator __x = _Base::before_begin(); @@ -565,6 +579,10 @@ namespace __debug } } + void + merge(forward_list& __list) + { merge(std::move(__list)); } + template void merge(forward_list&& __list, _Comp __comp) @@ -584,6 +602,11 @@ namespace __debug } } + template + void + merge(forward_list& __list, _Comp __comp) + { merge(std::move(__list), __comp); } + using _Base::sort; using _Base::reverse; diff --git a/libstdc++-v3/testsuite/23_containers/forward_list/modifiers/6.cc b/libstdc++-v3/testsuite/23_containers/forward_list/modifiers/6.cc new file mode 100644 index 0000000..e160381 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/forward_list/modifiers/6.cc @@ -0,0 +1,94 @@ +// { dg-options "-std=gnu++11" } + +// Copyright (C) 2012 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 3, 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 COPYING3. If not see +// . + +#include + +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + std::forward_list fl1(1, 5), fl2(1, 4), fl3(1, 3), + fl4(1, 2), fl5(1, 1), fl6(1, 0); + + fl1.splice_after(fl1.before_begin(), fl2); + + auto it = fl1.begin(); + + VERIFY( *it == 4 ); + + ++it; + + VERIFY( *it == 5 ); + + fl3.splice_after(fl3.before_begin(), fl4, fl4.before_begin()); + + it = fl3.begin(); + + VERIFY( *it == 2 ); + + ++it; + + VERIFY( *it == 3 ); + + fl5.splice_after(fl5.before_begin(), fl6, fl6.before_begin(), fl6.end()); + + it = fl5.begin(); + + VERIFY( *it == 0 ); + + ++it; + + VERIFY( *it == 1 ); + + fl1.merge(fl2); + + it = fl1.begin(); + + VERIFY( *it == 4 ); + + ++it; + + VERIFY( *it == 5 ); + + fl1.merge(fl3, std::less()); + + it = fl1.begin(); + + VERIFY( *it == 2 ); + + ++it; + + VERIFY( *it == 3 ); + + ++it; + + VERIFY( *it == 4 ); + + ++it; + + VERIFY( *it == 5 ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/23_containers/forward_list/operations/1.cc b/libstdc++-v3/testsuite/23_containers/forward_list/operations/1.cc index 5a996f3..4a9e364 100644 --- a/libstdc++-v3/testsuite/23_containers/forward_list/operations/1.cc +++ b/libstdc++-v3/testsuite/23_containers/forward_list/operations/1.cc @@ -1,6 +1,6 @@ // { dg-options "-std=gnu++0x" } -// Copyright (C) 2008, 2009 Free Software Foundation, Inc. +// Copyright (C) 2008, 2009, 2012 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 @@ -68,7 +68,7 @@ test02() VERIFY(*befy == 10.0); ++befy; - VERIFY(*befy == 15.0); + VERIFY(*befy == 14.0); } // This test verifies the following: -- 2.7.4