From 3426a860395c079de81548e4af815f048170f7d3 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 5 Jan 2016 19:32:41 +0000 Subject: [PATCH] First half of LWG#2354: 'Unnecessary copying when inserting into maps with braced-init syntax' git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@256859 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/__tree | 51 +++++++++++++++++++ include/map | 24 +++++++++ .../map/map.modifiers/insert_iter_rv.pass.cpp | 32 +++++++++++- .../map/map.modifiers/insert_rv.pass.cpp | 37 +++++++++++++- .../insert_iter_rv.pass.cpp | 33 +++++++++++- .../multimap.modifiers/insert_rv.pass.cpp | 32 +++++++++++- test/support/MoveOnly.h | 30 +++++++++++ 7 files changed, 235 insertions(+), 4 deletions(-) diff --git a/include/__tree b/include/__tree index cb58a9078..94565bc3f 100644 --- a/include/__tree +++ b/include/__tree @@ -909,6 +909,13 @@ public: iterator __insert_multi(const value_type& __v); iterator __insert_multi(const_iterator __p, const value_type& __v); +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + pair __insert_unique( value_type&& __v); + iterator __insert_unique(const_iterator __p, value_type&& __v); + iterator __insert_multi( value_type&& __v); + iterator __insert_multi(const_iterator __p, value_type&& __v); +#endif + pair __node_insert_unique(__node_pointer __nd); iterator __node_insert_unique(const_iterator __p, __node_pointer __nd); @@ -1729,6 +1736,28 @@ __tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p, #endif // _LIBCPP_HAS_NO_VARIADICS +template +pair::iterator, bool> +__tree<_Tp, _Compare, _Allocator>::__insert_unique(value_type&& __v) +{ + __node_holder __h = __construct_node(_VSTD::forward(__v)); + pair __r = __node_insert_unique(__h.get()); + if (__r.second) + __h.release(); + return __r; +} + +template +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, value_type&& __v) +{ + __node_holder __h = __construct_node(_VSTD::forward(__v)); + iterator __r = __node_insert_unique(__p, __h.get()); + if (__r.__ptr_ == __h.get()) + __h.release(); + return __r; +} + template template pair::iterator, bool> @@ -1753,6 +1782,28 @@ __tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _Vp&& __v return __r; } +template +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__insert_multi(value_type&& __v) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf_high(__parent, __v); + __node_holder __h = __construct_node(_VSTD::forward(__v)); + __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); + return iterator(__h.release()); +} + +template +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, value_type&& __v) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf(__p, __parent, __v); + __node_holder __h = __construct_node(_VSTD::forward(__v)); + __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); + return iterator(__h.release()); +} + template template typename __tree<_Tp, _Compare, _Allocator>::iterator diff --git a/include/map b/include/map index 2416205d6..87add437a 100644 --- a/include/map +++ b/include/map @@ -126,9 +126,11 @@ public: template iterator emplace_hint(const_iterator position, Args&&... args); pair insert(const value_type& v); + pair insert( value_type&& v); // C++17 template pair insert(P&& p); iterator insert(const_iterator position, const value_type& v); + iterator insert(const_iterator position, value_type&& v); // C++17 template iterator insert(const_iterator position, P&& p); template @@ -336,9 +338,11 @@ public: template iterator emplace_hint(const_iterator position, Args&&... args); iterator insert(const value_type& v); + iterator insert( value_type&& v); // C++17 template iterator insert(P&& p); iterator insert(const_iterator position, const value_type& v); + iterator insert(const_iterator position, value_type&& v); // C++17 template iterator insert(const_iterator position, P&& p); template @@ -1089,6 +1093,17 @@ public: insert(const_iterator __p, const value_type& __v) {return __tree_.__insert_unique(__p.__i_, __v);} +#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) + _LIBCPP_INLINE_VISIBILITY + pair + insert( value_type&& __v) {return __tree_.__insert_unique(_VSTD::forward(__v));} + + _LIBCPP_INLINE_VISIBILITY + iterator + insert(const_iterator __p, value_type&& __v) + {return __tree_.__insert_unique(__p.__i_, _VSTD::forward(__v));} +#endif + template _LIBCPP_INLINE_VISIBILITY void insert(_InputIterator __f, _InputIterator __l) @@ -1940,6 +1955,15 @@ public: iterator insert(const_iterator __p, const value_type& __v) {return __tree_.__insert_multi(__p.__i_, __v);} +#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) + _LIBCPP_INLINE_VISIBILITY + iterator insert( value_type&& __v) {return __tree_.__insert_multi(_VSTD::forward(__v));} + + _LIBCPP_INLINE_VISIBILITY + iterator insert(const_iterator __p, value_type&& __v) + {return __tree_.__insert_multi(__p.__i_, _VSTD::forward(__v));} +#endif + template _LIBCPP_INLINE_VISIBILITY void insert(_InputIterator __f, _InputIterator __l) diff --git a/test/std/containers/associative/map/map.modifiers/insert_iter_rv.pass.cpp b/test/std/containers/associative/map/map.modifiers/insert_iter_rv.pass.cpp index 42b41fd7b..fabafd332 100644 --- a/test/std/containers/associative/map/map.modifiers/insert_iter_rv.pass.cpp +++ b/test/std/containers/associative/map/map.modifiers/insert_iter_rv.pass.cpp @@ -52,7 +52,7 @@ int main() assert(r->first == 3); assert(r->second == 3); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::map, min_allocator>> M; typedef std::pair P; @@ -83,5 +83,35 @@ int main() assert(r->second == 3); } #endif +#if TEST_STD_VER > 14 + { + typedef std::map M; + typedef M::iterator R; + M m; + R r = m.insert(m.end(), {2, MoveOnly(2)}); + assert(r == m.begin()); + assert(m.size() == 1); + assert(r->first == 2); + assert(r->second == 2); + + r = m.insert(m.end(), {1, MoveOnly(1)}); + assert(r == m.begin()); + assert(m.size() == 2); + assert(r->first == 1); + assert(r->second == 1); + + r = m.insert(m.end(), {3, MoveOnly(3)}); + assert(r == prev(m.end())); + assert(m.size() == 3); + assert(r->first == 3); + assert(r->second == 3); + + r = m.insert(m.end(), {3, MoveOnly(3)}); + assert(r == prev(m.end())); + assert(m.size() == 3); + assert(r->first == 3); + assert(r->second == 3); + } #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES +#endif } diff --git a/test/std/containers/associative/map/map.modifiers/insert_rv.pass.cpp b/test/std/containers/associative/map/map.modifiers/insert_rv.pass.cpp index a9d3277e6..813e425e5 100644 --- a/test/std/containers/associative/map/map.modifiers/insert_rv.pass.cpp +++ b/test/std/containers/associative/map/map.modifiers/insert_rv.pass.cpp @@ -11,6 +11,7 @@ // class map +// pair insert( value_type&& v); // C++17 and later // template // pair insert(P&& p); @@ -55,7 +56,7 @@ int main() assert(r.first->first == 3); assert(r.first->second == 3); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::map, min_allocator>> M; typedef std::pair R; @@ -89,5 +90,39 @@ int main() assert(r.first->second == 3); } #endif +#if TEST_STD_VER > 14 + { + typedef std::map M; + typedef std::pair R; + M m; + R r = m.insert({2, MoveOnly(2)}); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 1); + assert(r.first->first == 2); + assert(r.first->second == 2); + + r = m.insert({1, MoveOnly(1)}); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 2); + assert(r.first->first == 1); + assert(r.first->second == 1); + + r = m.insert({3, MoveOnly(3)}); + assert(r.second); + assert(r.first == prev(m.end())); + assert(m.size() == 3); + assert(r.first->first == 3); + assert(r.first->second == 3); + + r = m.insert({3, MoveOnly(3)}); + assert(!r.second); + assert(r.first == prev(m.end())); + assert(m.size() == 3); + assert(r.first->first == 3); + assert(r.first->second == 3); + } +#endif #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES } diff --git a/test/std/containers/associative/multimap/multimap.modifiers/insert_iter_rv.pass.cpp b/test/std/containers/associative/multimap/multimap.modifiers/insert_iter_rv.pass.cpp index b44f46429..de5aea614 100644 --- a/test/std/containers/associative/multimap/multimap.modifiers/insert_iter_rv.pass.cpp +++ b/test/std/containers/associative/multimap/multimap.modifiers/insert_iter_rv.pass.cpp @@ -52,7 +52,7 @@ int main() assert(r->first == 3); assert(r->second == 2); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::multimap, min_allocator>> M; typedef std::pair P; @@ -83,5 +83,36 @@ int main() assert(r->second == 2); } #endif +#if TEST_STD_VER > 14 + { + typedef std::multimap M; + typedef std::pair P; + typedef M::iterator R; + M m; + R r = m.insert(m.cend(), {2, MoveOnly(2)}); + assert(r == m.begin()); + assert(m.size() == 1); + assert(r->first == 2); + assert(r->second == 2); + + r = m.insert(m.cend(), {1, MoveOnly(1)}); + assert(r == m.begin()); + assert(m.size() == 2); + assert(r->first == 1); + assert(r->second == 1); + + r = m.insert(m.cend(), {3, MoveOnly(3)}); + assert(r == prev(m.end())); + assert(m.size() == 3); + assert(r->first == 3); + assert(r->second == 3); + + r = m.insert(m.cend(), {3, MoveOnly(2)}); + assert(r == prev(m.end())); + assert(m.size() == 4); + assert(r->first == 3); + assert(r->second == 2); + } +#endif #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES } diff --git a/test/std/containers/associative/multimap/multimap.modifiers/insert_rv.pass.cpp b/test/std/containers/associative/multimap/multimap.modifiers/insert_rv.pass.cpp index b1c043586..86fc6cef0 100644 --- a/test/std/containers/associative/multimap/multimap.modifiers/insert_rv.pass.cpp +++ b/test/std/containers/associative/multimap/multimap.modifiers/insert_rv.pass.cpp @@ -51,7 +51,7 @@ int main() assert(r->first == 3); assert(r->second == 3); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::multimap, min_allocator>> M; typedef M::iterator R; @@ -81,5 +81,35 @@ int main() assert(r->second == 3); } #endif +#if TEST_STD_VER > 14 + { + typedef std::multimap M; + typedef M::iterator R; + M m; + R r = m.insert({2, MoveOnly(2)}); + assert(r == m.begin()); + assert(m.size() == 1); + assert(r->first == 2); + assert(r->second == 2); + + r = m.insert({1, MoveOnly(1)}); + assert(r == m.begin()); + assert(m.size() == 2); + assert(r->first == 1); + assert(r->second == 1); + + r = m.insert({3, MoveOnly(3)}); + assert(r == prev(m.end())); + assert(m.size() == 3); + assert(r->first == 3); + assert(r->second == 3); + + r = m.insert({3, MoveOnly(3)}); + assert(r == prev(m.end())); + assert(m.size() == 4); + assert(r->first == 3); + assert(r->second == 3); + } +#endif #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES } diff --git a/test/support/MoveOnly.h b/test/support/MoveOnly.h index e4d9f6495..b9653516c 100644 --- a/test/support/MoveOnly.h +++ b/test/support/MoveOnly.h @@ -17,6 +17,7 @@ class MoveOnly { + friend class MoveOnly2; MoveOnly(const MoveOnly&); MoveOnly& operator=(const MoveOnly&); @@ -34,6 +35,29 @@ public: bool operator< (const MoveOnly& x) const {return data_ < x.data_;} }; +class MoveOnly2 +{ + MoveOnly2(const MoveOnly&); + MoveOnly2& operator=(const MoveOnly2&); + + int data_; +public: + MoveOnly2(int data = 1) : data_(data) {} + MoveOnly2(MoveOnly2&& x) + : data_(x.data_) {x.data_ = 0;} + MoveOnly2& operator=(MoveOnly2&& x) + {data_ = x.data_; x.data_ = 0; return *this;} + MoveOnly2(MoveOnly&& x) + : data_(x.data_) {x.data_ = 0;} + MoveOnly2& operator=(MoveOnly&& x) + {data_ = x.data_; x.data_ = 0; return *this;} + + int get() const {return data_;} + + bool operator==(const MoveOnly2& x) const {return data_ == x.data_;} + bool operator< (const MoveOnly2& x) const {return data_ < x.data_;} +}; + namespace std { template <> @@ -43,6 +67,12 @@ struct hash std::size_t operator()(const MoveOnly& x) const {return x.get();} }; +template <> +struct hash + : public std::unary_function +{ + std::size_t operator()(const MoveOnly2& x) const {return x.get();} +}; } #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES