From e8fc6dd8c659c490aeeaf619e02e9ba87e1662d7 Mon Sep 17 00:00:00 2001 From: morzhovets Date: Mon, 25 Nov 2024 22:40:47 +0400 Subject: [PATCH] Support pair-like arguments in map::insert and map::emplace --- include/momo/stdish/map.h | 118 ++++-------------- include/momo/stdish/unordered_map.h | 74 +++-------- include/momo/stdish/unordered_multimap.h | 65 ++-------- .../libcxx/map/extra/emplace_ext.pass.cpp | 13 ++ ...map_allocator_requirement_test_templates.h | 20 +-- .../unord.map/extra/emplace_ext.pass.cpp | 11 ++ .../unord.multimap/extra/emplace_ext.pass.cpp | 15 ++- 7 files changed, 92 insertions(+), 224 deletions(-) diff --git a/include/momo/stdish/map.h b/include/momo/stdish/map.h index c46cc9a5..552a6e78 100644 --- a/include/momo/stdish/map.h +++ b/include/momo/stdish/map.h @@ -485,68 +485,18 @@ namespace internal return { lower_bound(key), upper_bound(key) }; } - //template - //momo::internal::EnableIf::value, - //std::pair> insert(Value&& value) - - //template - //momo::internal::EnableIf::value, - //iterator> insert(const_iterator hint, Value&& value) - - //std::pair insert(value_type&& value) - - //iterator insert(const_iterator hint, value_type&& value) - - //std::pair insert(const value_type& value) - - //iterator insert(const_iterator hint, const value_type& value) - - std::pair insert(std::pair&& value) - { - return ptEmplace(nullptr, std::forward_as_tuple(std::move(value.first)), - std::forward_as_tuple(std::move(value.second))); - } - - iterator insert(const_iterator hint, std::pair&& value) - { - return ptEmplace(hint, std::forward_as_tuple(std::move(value.first)), - std::forward_as_tuple(std::move(value.second))).first; - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - std::pair> insert(const std::pair& value) - { - return ptEmplace(nullptr, std::forward_as_tuple(value.first), - std::forward_as_tuple(value.second)); - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const_iterator hint, const std::pair& value) + template> + momo::internal::EnableIf::value, + std::pair> insert(ValueArg&& valueArg) { - return ptEmplace(hint, std::forward_as_tuple(value.first), - std::forward_as_tuple(value.second)).first; + return emplace(std::forward(valueArg)); } - template - momo::internal::EnableIf::value - && std::is_constructible::value, - std::pair> insert(std::pair&& value) + template> + momo::internal::EnableIf::value, + iterator> insert(const_iterator hint, ValueArg&& valueArg) { - return ptEmplace(nullptr, std::forward_as_tuple(std::forward(value.first)), - std::forward_as_tuple(std::forward(value.second))); - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const_iterator hint, std::pair&& value) - { - return ptEmplace(hint, std::forward_as_tuple(std::forward(value.first)), - std::forward_as_tuple(std::forward(value.second))).first; + return emplace_hint(hint, std::forward(valueArg)); } insert_return_type insert(node_type&& node) @@ -603,13 +553,17 @@ namespace internal template std::pair emplace(ValueArg&& valueArg) { - return insert(std::forward(valueArg)); + return ptEmplace(nullptr, + std::forward_as_tuple(std::get<0>(std::forward(valueArg))), + std::forward_as_tuple(std::get<1>(std::forward(valueArg)))); } template iterator emplace_hint(const_iterator hint, ValueArg&& valueArg) { - return insert(hint, std::forward(valueArg)); + return ptEmplace(hint, + std::forward_as_tuple(std::get<0>(std::forward(valueArg))), + std::forward_as_tuple(std::get<1>(std::forward(valueArg)))).first; } template @@ -1053,46 +1007,18 @@ class multimap : public internal::map_base&& value) - { - return BaseMap::insert(std::move(value)).first; - } - - iterator insert(const_iterator hint, std::pair&& value) - { - return BaseMap::insert(hint, std::move(value)); - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const std::pair& value) - { - return BaseMap::insert(value).first; - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const_iterator hint, const std::pair& value) - { - return BaseMap::insert(hint, value); - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(std::pair&& value) + template> + momo::internal::EnableIf::value, + iterator> insert(ValueArg&& valueArg) { - return BaseMap::insert(std::move(value)).first; + return BaseMap::insert(std::forward(valueArg)).first; } - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const_iterator hint, std::pair&& value) + template> + momo::internal::EnableIf::value, + iterator> insert(const_iterator hint, ValueArg&& valueArg) { - return BaseMap::insert(hint, std::move(value)); + return BaseMap::insert(hint, std::forward(valueArg)); } iterator insert(node_type&& node) diff --git a/include/momo/stdish/unordered_map.h b/include/momo/stdish/unordered_map.h index 3ffaa50a..a9a89796 100644 --- a/include/momo/stdish/unordered_map.h +++ b/include/momo/stdish/unordered_map.h @@ -504,68 +504,18 @@ class unordered_map return { find(key), end() }; } - //template - //momo::internal::EnableIf::value, - //std::pair> insert(Value&& value) - - //template - //momo::internal::EnableIf::value, - //iterator> insert(const_iterator hint, Value&& value) - - //std::pair insert(value_type&& value) - - //iterator insert(const_iterator hint, value_type&& value) - - //std::pair insert(const value_type& value) - - //iterator insert(const_iterator hint, const value_type& value) - - std::pair insert(std::pair&& value) - { - return pvEmplace(nullptr, std::forward_as_tuple(std::move(value.first)), - std::forward_as_tuple(std::move(value.second))); - } - - iterator insert(const_iterator hint, std::pair&& value) - { - return pvEmplace(hint, std::forward_as_tuple(std::move(value.first)), - std::forward_as_tuple(std::move(value.second))).first; - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - std::pair> insert(const std::pair& value) - { - return pvEmplace(nullptr, std::forward_as_tuple(value.first), - std::forward_as_tuple(value.second)); - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const_iterator hint, const std::pair& value) - { - return pvEmplace(hint, std::forward_as_tuple(value.first), - std::forward_as_tuple(value.second)).first; - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - std::pair> insert(std::pair&& value) + template> + momo::internal::EnableIf::value, + std::pair> insert(ValueArg&& valueArg) { - return pvEmplace(nullptr, std::forward_as_tuple(std::forward(value.first)), - std::forward_as_tuple(std::forward(value.second))); + return emplace(std::forward(valueArg)); } - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const_iterator hint, std::pair&& value) + template> + momo::internal::EnableIf::value, + iterator> insert(const_iterator hint, ValueArg&& valueArg) { - return pvEmplace(hint, std::forward_as_tuple(std::forward(value.first)), - std::forward_as_tuple(std::forward(value.second))).first; + return emplace_hint(hint, std::forward(valueArg)); } insert_return_type insert(node_type&& node) @@ -624,13 +574,17 @@ class unordered_map template std::pair emplace(ValueArg&& valueArg) { - return insert(std::forward(valueArg)); + return pvEmplace(nullptr, + std::forward_as_tuple(std::get<0>(std::forward(valueArg))), + std::forward_as_tuple(std::get<1>(std::forward(valueArg)))); } template iterator emplace_hint(const_iterator hint, ValueArg&& valueArg) { - return insert(hint, std::forward(valueArg)); + return pvEmplace(hint, + std::forward_as_tuple(std::get<0>(std::forward(valueArg))), + std::forward_as_tuple(std::get<1>(std::forward(valueArg)))).first; } template diff --git a/include/momo/stdish/unordered_multimap.h b/include/momo/stdish/unordered_multimap.h index 427bd6a4..e5d4accb 100644 --- a/include/momo/stdish/unordered_multimap.h +++ b/include/momo/stdish/unordered_multimap.h @@ -467,64 +467,18 @@ class unordered_multimap return pvEqualRange(mHashMultiMap, mHashMultiMap.Find(key)); } - //template - //momo::internal::EnableIf::value, - //iterator> insert(Value&& value) - - //template - //momo::internal::EnableIf::value, - //iterator> insert(const_iterator hint, Value&& value) - - //iterator insert(value_type&& value) - - //iterator insert(const_iterator hint, value_type&& value) - - //iterator insert(const value_type& value) - - //iterator insert(const_iterator hint, const value_type& value) - - iterator insert(std::pair&& value) + template> + momo::internal::EnableIf::value, + iterator> insert(ValueArg&& valueArg) { - return pvEmplace(std::forward_as_tuple(std::move(value.first)), - std::forward_as_tuple(std::move(value.second))); - } - - iterator insert(const_iterator, std::pair&& value) - { - return insert(std::move(value)); - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const std::pair& value) - { - return pvEmplace(std::forward_as_tuple(value.first), std::forward_as_tuple(value.second)); - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const_iterator, const std::pair& value) - { - return insert(value); - } - - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(std::pair&& value) - { - return pvEmplace(std::forward_as_tuple(std::forward(value.first)), - std::forward_as_tuple(std::forward(value.second))); + return emplace(std::forward(valueArg)); } - template - momo::internal::EnableIf::value - && std::is_constructible::value, - iterator> insert(const_iterator, std::pair&& value) + template> + momo::internal::EnableIf::value, + iterator> insert(const_iterator, ValueArg&& valueArg) { - return insert(std::move(value)); + return insert(std::forward(valueArg)); } template @@ -560,7 +514,8 @@ class unordered_multimap template iterator emplace(ValueArg&& valueArg) { - return insert(std::forward(valueArg)); + return pvEmplace(std::forward_as_tuple(std::get<0>(std::forward(valueArg))), + std::forward_as_tuple(std::get<1>(std::forward(valueArg)))); } template diff --git a/test/sources/libcxx/map/extra/emplace_ext.pass.cpp b/test/sources/libcxx/map/extra/emplace_ext.pass.cpp index 10b01bfb..03cb9d2a 100644 --- a/test/sources/libcxx/map/extra/emplace_ext.pass.cpp +++ b/test/sources/libcxx/map/extra/emplace_ext.pass.cpp @@ -47,6 +47,13 @@ void main() assert(m.size() == 3); assert(m.begin()->first == 0); assert(m.begin()->second == 0.0); + + r = m.emplace(std::make_tuple(-1, 4.5)); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 4); + assert(m.begin()->first == -1); + assert(m.begin()->second == 4.5); } { typedef std::map M; @@ -89,5 +96,11 @@ void main() assert(m.size() == 3); assert(m.begin()->first == 0); assert(m.begin()->second == 0.0); + + r = m.emplace_hint(m.end(), std::make_tuple(-1, 4.5)); + assert(r == m.begin()); + assert(m.size() == 4); + assert(m.begin()->first == -1); + assert(m.begin()->second == 4.5); } } diff --git a/test/sources/libcxx/support/map_allocator_requirement_test_templates.h b/test/sources/libcxx/support/map_allocator_requirement_test_templates.h index bc644b94..e3023f56 100644 --- a/test/sources/libcxx/support/map_allocator_requirement_test_templates.h +++ b/test/sources/libcxx/support/map_allocator_requirement_test_templates.h @@ -78,7 +78,7 @@ void testMapInsert() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif assert(c.insert(v).second); #ifdef LIBCPP_HAS_BAD_NEWS_FOR_MOMO @@ -124,7 +124,7 @@ void testMapInsert() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif assert(c.insert(std::move(v)).second); #ifdef LIBCPP_HAS_BAD_NEWS_FOR_MOMO @@ -311,7 +311,7 @@ void testMapInsertHint() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif It ret = c.insert(c.end(), v); assert(ret != c.end()); @@ -403,7 +403,7 @@ void testMapInsertHint() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif It ret = c.insert(c.end(), std::move(v)); assert(ret != c.end()); @@ -539,7 +539,7 @@ void testMapEmplace() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif assert(c.emplace(v).second); #ifdef LIBCPP_HAS_BAD_NEWS_FOR_MOMO @@ -585,7 +585,7 @@ void testMapEmplace() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif assert(c.emplace(std::move(v)).second); #ifdef LIBCPP_HAS_BAD_NEWS_FOR_MOMO @@ -810,7 +810,7 @@ void testMapEmplaceHint() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif It ret = c.emplace_hint(c.end(), v); assert(ret != c.end()); @@ -872,7 +872,7 @@ void testMapEmplaceHint() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif It ret = c.emplace_hint(c.end(), std::move(v)); assert(ret != c.end()); @@ -1138,7 +1138,7 @@ void testMultimapInsert() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif c.insert(v); #ifdef LIBCPP_HAS_BAD_NEWS_FOR_MOMO @@ -1299,7 +1299,7 @@ void testMultimapInsertHint() cc->expect(); #else cc1.expect(); - cc2.expect(); + cc2.expect(); #endif c.insert(c.begin(), v); #ifdef LIBCPP_HAS_BAD_NEWS_FOR_MOMO diff --git a/test/sources/libcxx/unord.map/extra/emplace_ext.pass.cpp b/test/sources/libcxx/unord.map/extra/emplace_ext.pass.cpp index 4521a685..a1082644 100644 --- a/test/sources/libcxx/unord.map/extra/emplace_ext.pass.cpp +++ b/test/sources/libcxx/unord.map/extra/emplace_ext.pass.cpp @@ -53,6 +53,12 @@ void main() assert(c.size() == 4); assert(r.first->first == 0); assert(r.first->second == Emplaceable()); + + r = c.emplace(std::make_tuple(6, Emplaceable(7, 8))); + assert(r.second); + assert(c.size() == 5); + assert(r.first->first == 6); + assert(r.first->second == Emplaceable(7, 8)); } { typedef std::unordered_map C; @@ -79,5 +85,10 @@ void main() assert(c.size() == 4); assert(r->first == 0); assert(r->second == Emplaceable()); + + r = c.emplace_hint(c.find(6), std::make_tuple(6, Emplaceable(7, 8))); + assert(c.size() == 5); + assert(r->first == 6); + assert(r->second == Emplaceable(7, 8)); } } diff --git a/test/sources/libcxx/unord.multimap/extra/emplace_ext.pass.cpp b/test/sources/libcxx/unord.multimap/extra/emplace_ext.pass.cpp index 9719e088..ffd5e2e0 100644 --- a/test/sources/libcxx/unord.multimap/extra/emplace_ext.pass.cpp +++ b/test/sources/libcxx/unord.multimap/extra/emplace_ext.pass.cpp @@ -49,6 +49,11 @@ void main() assert(c.size() == 4); assert(r->first == 0); assert(r->second == Emplaceable()); + + r = c.emplace(std::make_tuple(0, Emplaceable(7, 8))); + assert(c.size() == 5); + assert(r->first == 0); + assert(r->second == Emplaceable(7, 8)); } { typedef std::unordered_multimap C; @@ -65,8 +70,7 @@ void main() typedef std::unordered_multimap C; typedef C::iterator R; C c; - C::const_iterator e = c.end(); - R r = c.emplace_hint(e, std::piecewise_construct, std::forward_as_tuple(3), + R r = c.emplace_hint(c.end(), std::piecewise_construct, std::forward_as_tuple(3), std::forward_as_tuple()); assert(c.size() == 1); assert(r->first == 3); @@ -78,7 +82,7 @@ void main() assert(r->second == Emplaceable(5, 6)); assert(r == next(c.begin())); - r = c.emplace_hint(r, 3, Emplaceable(6, 7)); + r = c.emplace_hint(c.end(), 3, Emplaceable(6, 7)); assert(c.size() == 3); assert(r->first == 3); assert(r->second == Emplaceable(6, 7)); @@ -92,5 +96,10 @@ void main() assert(c.size() == 4); assert(r->first == 0); assert(r->second == Emplaceable()); + + r = c.emplace_hint(c.end(), std::make_tuple(0, Emplaceable(7, 8))); + assert(c.size() == 5); + assert(r->first == 0); + assert(r->second == Emplaceable(7, 8)); } }