From 13063abce500aeb5fa6ea28b281156a4278a0dc1 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 23 Feb 2017 20:10:40 +0000 Subject: [PATCH 01/17] Move friend function outside of class The use of std::pair was causing issues with the sun compiler. --- test/objects/exception.hpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/objects/exception.hpp b/test/objects/exception.hpp index da11cf56..d5f0f5db 100644 --- a/test/objects/exception.hpp +++ b/test/objects/exception.hpp @@ -123,21 +123,21 @@ class object : private counted_object return object(::test::generate(x, g), ::test::generate(x, g)); } - friend std::pair generate( - std::pair const*, random_generator g) - { - int* x = 0; - return std::make_pair( - object(::test::generate(x, g), ::test::generate(x, g)), - object(::test::generate(x, g), ::test::generate(x, g))); - } - friend std::ostream& operator<<(std::ostream& out, object const& o) { return out << "(" << o.tag1_ << "," << o.tag2_ << ")"; } }; +std::pair generate( + std::pair const*, random_generator g) +{ + int* x = 0; + return std::make_pair( + object(::test::generate(x, g), ::test::generate(x, g)), + object(::test::generate(x, g), ::test::generate(x, g))); +} + class hash { int tag_; From 223158603325144b01f26d202bbafb03a0b14c52 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 23 Feb 2017 20:14:26 +0000 Subject: [PATCH 02/17] Remove duplicate includes --- include/boost/unordered/detail/implementation.hpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index 78850bba..65592760 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -13,11 +13,9 @@ #include #include #include -#include #include #include #include -#include #include #include #include @@ -26,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -40,7 +37,6 @@ #include #include #include -#include #include #include #include From ddee1b686a1876379e1dd984f38786855a59342c Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 23 Feb 2017 20:14:26 +0000 Subject: [PATCH 03/17] Move config and declaration to the start of implementataion.hpp --- .../boost/unordered/detail/implementation.hpp | 176 ++++++++---------- 1 file changed, 75 insertions(+), 101 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index 65592760..d1d11977 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -46,10 +46,76 @@ #include #endif +#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) +#include +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Configuration +// +// Unless documented elsewhere these configuration macros should be considered +// an implementation detail, I'll try not to break them, but you never know. + +// BOOST_UNORDERED_EMPLACE_LIMIT = The maximum number of parameters in emplace +// (not including things like hints). Don't set it to a lower value, as that +// might break something. + +#if !defined BOOST_UNORDERED_EMPLACE_LIMIT +#define BOOST_UNORDERED_EMPLACE_LIMIT 11 +#endif + +// BOOST_UNORDERED_USE_ALLOCATOR_TRAITS - Pick which version of +// allocator_traits to use. +// +// 0 = Own partial implementation +// 1 = std::allocator_traits +// 2 = boost::container::allocator_traits + +#if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS) +#if !defined(BOOST_NO_CXX11_ALLOCATOR) +#define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 1 +#elif defined(BOOST_MSVC) +#if BOOST_MSVC < 1400 +// Use container's allocator_traits for older versions of Visual +// C++ as I don't test with them. +#define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 2 +#endif +#endif +#endif + +#if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS) +#define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 0 +#endif + +namespace boost { +namespace unordered { +namespace iterator_detail { +template struct iterator; +template struct c_iterator; +template struct l_iterator; +template struct cl_iterator; +} +} +} + namespace boost { namespace unordered { namespace detail { +template struct table; +template struct bucket; +struct ptr_bucket; +template struct table_impl; +template struct grouped_table_impl; + +template struct unique_node; +template struct ptr_node; +template struct table_impl; + +template struct grouped_node; +template struct grouped_ptr_node; +template struct grouped_table_impl; + static const float minimum_max_load_factor = 1e-3f; static const std::size_t default_bucket_count = 11; struct move_tag @@ -270,9 +336,6 @@ struct compressed // move_assign explicit. compressed& operator=(compressed const&); }; -} -} -} #if defined(BOOST_MSVC) #pragma warning(push) @@ -282,13 +345,6 @@ struct compressed // will be default-initialized. #endif -// Maximum number of arguments supported by emplace + 1. -#define BOOST_UNORDERED_EMPLACE_LIMIT 11 - -namespace boost { -namespace unordered { -namespace detail { - //////////////////////////////////////////////////////////////////////////// // Bits and pieces for implementing traits @@ -483,33 +539,6 @@ BOOST_PP_REPEAT_FROM_TO( #undef BOOST_UNORDERED_EARGS_MEMBER #undef BOOST_UNORDERED_EARGS_INIT -#endif -} -} -} - -//////////////////////////////////////////////////////////////////////////////// -// -// Pick which version of allocator_traits to use -// -// 0 = Own partial implementation -// 1 = std::allocator_traits -// 2 = boost::container::allocator_traits - -#if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS) -#if !defined(BOOST_NO_CXX11_ALLOCATOR) -#define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 1 -#elif defined(BOOST_MSVC) -#if BOOST_MSVC < 1400 -// Use container's allocator_traits for older versions of Visual -// C++ as I don't test with them. -#define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 2 -#endif -#endif -#endif - -#if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS) -#define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 0 #endif //////////////////////////////////////////////////////////////////////////////// @@ -517,14 +546,6 @@ BOOST_PP_REPEAT_FROM_TO( // Some utilities for implementing allocator_traits, but useful elsewhere so // they're always defined. -#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) -#include -#endif - -namespace boost { -namespace unordered { -namespace detail { - //////////////////////////////////////////////////////////////////////////// // Integral_constrant, true_type, false_type // @@ -1167,6 +1188,10 @@ struct rebind_wrap : boost::container::allocator_traits< #endif +//////////////////////////////////////////////////////////////////////////// +// Functions used to construct nodes. Emulates variadic construction, +// piecewise construction etc. + namespace boost { namespace unordered { namespace detail { @@ -1688,19 +1713,6 @@ construct_node_pair(Alloc& alloc, BOOST_FWD_REF(Key) k, BOOST_FWD_REF(Mapped) m) #pragma warning(pop) #endif -namespace boost { -namespace unordered { -namespace detail { - -template struct table; -template struct bucket; -struct ptr_bucket; -template struct table_impl; -template struct grouped_table_impl; -} -} -} - // The 'iterator_detail' namespace was a misguided attempt at avoiding ADL // in the detail namespace. It didn't work because the template parameters // were in detail. I'm not changing it at the moment to be safe. I might @@ -1714,15 +1726,6 @@ namespace iterator_detail { // // all no throw -template struct iterator; -template struct c_iterator; -template struct l_iterator; -template struct cl_iterator; - -// Local Iterators -// -// all no throw - template struct l_iterator : public std::iterator::type #endif -} -} -} #if defined(BOOST_MSVC) #pragma warning(push) #pragma warning(disable : 4127) // conditional expression is constant #endif -namespace boost { -namespace unordered { -namespace detail { - //////////////////////////////////////////////////////////////////////////// // convert double to std::size_t @@ -3122,18 +3118,12 @@ inline void table::reserve(std::size_t num_elements) rehash(static_cast( std::ceil(static_cast(num_elements) / mlf_))); } -} -} -} #if defined(BOOST_MSVC) #pragma warning(pop) #endif -namespace boost { -namespace unordered { -namespace detail { - +//////////////////////////////////////////////////////////////////////// // key extractors // // no throw @@ -3276,17 +3266,9 @@ template struct map_extractor BOOST_UNORDERED_KEY_FROM_TUPLE(std::) #endif }; -} -} -} -namespace boost { -namespace unordered { -namespace detail { - -template struct unique_node; -template struct ptr_node; -template struct table_impl; +//////////////////////////////////////////////////////////////////////// +// Unique nodes template struct unique_node : boost::unordered::detail::value_base @@ -3913,17 +3895,9 @@ struct table_impl : boost::unordered::detail::table } } }; -} -} -} -namespace boost { -namespace unordered { -namespace detail { - -template struct grouped_node; -template struct grouped_ptr_node; -template struct grouped_table_impl; +//////////////////////////////////////////////////////////////////////// +// Grouped nodes template struct grouped_node : boost::unordered::detail::value_base From c0b72d97b3eccafa691ff34dfab2aa47653c57b0 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 23 Feb 2017 20:14:27 +0000 Subject: [PATCH 04/17] Stricter about rebinding the allocator --- .../boost/unordered/detail/implementation.hpp | 6 +- include/boost/unordered/detail/map.hpp | 19 ++++-- include/boost/unordered/detail/set.hpp | 19 ++++-- include/boost/unordered/unordered_map.hpp | 12 ++-- include/boost/unordered/unordered_set.hpp | 12 ++-- test/objects/minimal.hpp | 68 +++++++++++++++++++ test/unordered/compile_map.cpp | 8 +-- test/unordered/compile_set.cpp | 4 +- 8 files changed, 113 insertions(+), 35 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index d1d11977..4b669fd5 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -2484,10 +2484,10 @@ struct table : boost::unordered::detail::functions::type node_allocator; - typedef typename boost::unordered::detail::rebind_wrap::type bucket_allocator; typedef boost::unordered::detail::allocator_traits node_allocator_traits; diff --git a/include/boost/unordered/detail/map.hpp b/include/boost/unordered/detail/map.hpp index 15b18e71..5770e9ec 100644 --- a/include/boost/unordered/detail/map.hpp +++ b/include/boost/unordered/detail/map.hpp @@ -13,14 +13,17 @@ template struct map { typedef boost::unordered::detail::map types; - typedef A allocator; typedef std::pair value_type; typedef H hasher; typedef P key_equal; typedef K key_type; - typedef boost::unordered::detail::allocator_traits traits; - typedef boost::unordered::detail::pick_node pick; + typedef typename ::boost::unordered::detail::rebind_wrap::type value_allocator; + typedef boost::unordered::detail::allocator_traits + value_allocator_traits; + + typedef boost::unordered::detail::pick_node pick; typedef typename pick::node node; typedef typename pick::bucket bucket; typedef typename pick::link_pointer link_pointer; @@ -44,15 +47,17 @@ struct multimap { typedef boost::unordered::detail::multimap types; - typedef A allocator; typedef std::pair value_type; typedef H hasher; typedef P key_equal; typedef K key_type; - typedef boost::unordered::detail::allocator_traits traits; - typedef boost::unordered::detail::pick_grouped_node - pick; + typedef typename ::boost::unordered::detail::rebind_wrap::type value_allocator; + typedef boost::unordered::detail::allocator_traits + value_allocator_traits; + + typedef boost::unordered::detail::pick_grouped_node pick; typedef typename pick::node node; typedef typename pick::bucket bucket; typedef typename pick::link_pointer link_pointer; diff --git a/include/boost/unordered/detail/set.hpp b/include/boost/unordered/detail/set.hpp index 024cc259..2eb34207 100644 --- a/include/boost/unordered/detail/set.hpp +++ b/include/boost/unordered/detail/set.hpp @@ -13,14 +13,17 @@ template struct set { typedef boost::unordered::detail::set types; - typedef A allocator; typedef T value_type; typedef H hasher; typedef P key_equal; typedef T key_type; - typedef boost::unordered::detail::allocator_traits traits; - typedef boost::unordered::detail::pick_node pick; + typedef typename ::boost::unordered::detail::rebind_wrap::type value_allocator; + typedef boost::unordered::detail::allocator_traits + value_allocator_traits; + + typedef boost::unordered::detail::pick_node pick; typedef typename pick::node node; typedef typename pick::bucket bucket; typedef typename pick::link_pointer link_pointer; @@ -42,15 +45,17 @@ template struct multiset { typedef boost::unordered::detail::multiset types; - typedef A allocator; typedef T value_type; typedef H hasher; typedef P key_equal; typedef T key_type; - typedef boost::unordered::detail::allocator_traits traits; - typedef boost::unordered::detail::pick_grouped_node - pick; + typedef typename ::boost::unordered::detail::rebind_wrap::type value_allocator; + typedef boost::unordered::detail::allocator_traits + value_allocator_traits; + + typedef boost::unordered::detail::pick_grouped_node pick; typedef typename pick::node node; typedef typename pick::bucket bucket; typedef typename pick::link_pointer link_pointer; diff --git a/include/boost/unordered/unordered_map.hpp b/include/boost/unordered/unordered_map.hpp index 9200f95d..3b842d54 100644 --- a/include/boost/unordered/unordered_map.hpp +++ b/include/boost/unordered/unordered_map.hpp @@ -50,12 +50,12 @@ template class unordered_map private: typedef boost::unordered::detail::map types; - typedef typename types::traits allocator_traits; + typedef typename types::value_allocator_traits value_allocator_traits; typedef typename types::table table; public: - typedef typename allocator_traits::pointer pointer; - typedef typename allocator_traits::const_pointer const_pointer; + typedef typename value_allocator_traits::pointer pointer; + typedef typename value_allocator_traits::const_pointer const_pointer; typedef value_type& reference; typedef value_type const& const_reference; @@ -462,12 +462,12 @@ template class unordered_multimap private: typedef boost::unordered::detail::multimap types; - typedef typename types::traits allocator_traits; + typedef typename types::value_allocator_traits value_allocator_traits; typedef typename types::table table; public: - typedef typename allocator_traits::pointer pointer; - typedef typename allocator_traits::const_pointer const_pointer; + typedef typename value_allocator_traits::pointer pointer; + typedef typename value_allocator_traits::const_pointer const_pointer; typedef value_type& reference; typedef value_type const& const_reference; diff --git a/include/boost/unordered/unordered_set.hpp b/include/boost/unordered/unordered_set.hpp index 9137fbc1..513ded75 100644 --- a/include/boost/unordered/unordered_set.hpp +++ b/include/boost/unordered/unordered_set.hpp @@ -47,12 +47,12 @@ template class unordered_set private: typedef boost::unordered::detail::set types; - typedef typename types::traits allocator_traits; + typedef typename types::value_allocator_traits value_allocator_traits; typedef typename types::table table; public: - typedef typename allocator_traits::pointer pointer; - typedef typename allocator_traits::const_pointer const_pointer; + typedef typename value_allocator_traits::pointer pointer; + typedef typename value_allocator_traits::const_pointer const_pointer; typedef value_type& reference; typedef value_type const& const_reference; @@ -447,12 +447,12 @@ template class unordered_multiset private: typedef boost::unordered::detail::multiset types; - typedef typename types::traits allocator_traits; + typedef typename types::value_allocator_traits value_allocator_traits; typedef typename types::table table; public: - typedef typename allocator_traits::pointer pointer; - typedef typename allocator_traits::const_pointer const_pointer; + typedef typename value_allocator_traits::pointer pointer; + typedef typename value_allocator_traits::const_pointer const_pointer; typedef value_type& reference; typedef value_type const& const_reference; diff --git a/test/objects/minimal.hpp b/test/objects/minimal.hpp index 818560a6..c1fa5b4f 100644 --- a/test/objects/minimal.hpp +++ b/test/objects/minimal.hpp @@ -441,6 +441,74 @@ template class allocator #endif }; +template class allocator +{ + public: + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + typedef void_ptr void_pointer; + typedef void_const_ptr const_void_pointer; + // Maybe these two should be const_ptr + typedef ptr pointer; + typedef const_ptr const_pointer; + typedef T const& reference; + typedef T const& const_reference; + typedef T const value_type; + + template struct rebind + { + typedef allocator other; + }; + + allocator() {} + template allocator(allocator const&) {} + allocator(allocator const&) {} + ~allocator() {} + + const_pointer address(const_reference r) { return const_pointer(&r); } + + pointer allocate(size_type n) + { + return pointer(static_cast(::operator new(n * sizeof(T)))); + } + + template pointer allocate(size_type n, const_ptr) + { + return pointer(static_cast(::operator new(n * sizeof(T)))); + } + + void deallocate(pointer p, size_type) { ::operator delete((void*)p.ptr_); } + + void construct(T const* p, T const& t) { new ((void*)p) T(t); } + +#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + template + void construct(T const* p, BOOST_FWD_REF(Args)... args) + { + new ((void*)p) T(boost::forward(args)...); + } +#endif + + void destroy(T const* p) { p->~T(); } + + size_type max_size() const { return 1000; } + +#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) || \ + BOOST_WORKAROUND(BOOST_MSVC, <= 1300) + public: + allocator& operator=(allocator const&) { return *this; } +#else + private: + allocator& operator=(allocator const&); +#endif +#if BOOST_UNORDERED_CHECK_ADDR_OPERATOR_NOT_USED + ampersand_operator_used operator&() const + { + return ampersand_operator_used(); + } +#endif +}; + template inline bool operator==(allocator const&, allocator const&) { diff --git a/test/unordered/compile_map.cpp b/test/unordered/compile_map.cpp index b149ad24..7db54e44 100644 --- a/test/unordered/compile_map.cpp +++ b/test/unordered/compile_map.cpp @@ -20,19 +20,19 @@ // Explicit instantiation to catch compile-time errors template class boost::unordered_map, - std::equal_to, test::minimal::allocator > >; + std::equal_to, test::minimal::allocator >; template class boost::unordered_multimap, - std::equal_to, test::minimal::allocator > >; + std::equal_to, test::minimal::allocator >; template class boost::unordered_map, test::minimal::equal_to, - test::minimal::allocator >; + test::minimal::allocator >; template class boost::unordered_multimap, test::minimal::equal_to, - test::minimal::allocator >; + test::minimal::allocator >; UNORDERED_AUTO_TEST(test0) { diff --git a/test/unordered/compile_set.cpp b/test/unordered/compile_set.cpp index 60f0f80f..78e8d7a0 100644 --- a/test/unordered/compile_set.cpp +++ b/test/unordered/compile_set.cpp @@ -27,11 +27,11 @@ template class boost::unordered_multiset, template class boost::unordered_set, test::minimal::equal_to, - test::minimal::allocator >; + test::minimal::allocator >; template class boost::unordered_multiset, test::minimal::equal_to, - test::minimal::allocator >; + test::minimal::allocator >; UNORDERED_AUTO_TEST(test0) { From 96602df8a8edb318cedc9c069a3cb64918c52bcb Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 23 Feb 2017 20:14:27 +0000 Subject: [PATCH 05/17] Remove const volatile when picking bucket policy Could possibly do this in a more portable manner by using some sort of function overload. --- .../boost/unordered/detail/implementation.hpp | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index 4b669fd5..331cbe9f 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -2179,8 +2179,8 @@ template <> struct pick_policy_impl<64, 2> }; template -struct pick_policy : pick_policy_impl::digits, - std::numeric_limits::radix> +struct pick_policy2 : pick_policy_impl::digits, + std::numeric_limits::radix> { }; @@ -2190,39 +2190,44 @@ struct pick_policy : pick_policy_impl::digits, // prime policy for integeral types. But not the smaller ones, as they // don't have enough unique values for this to be an issue. -template <> struct pick_policy +template <> struct pick_policy2 { typedef prime_policy type; }; -template <> struct pick_policy +template <> struct pick_policy2 { typedef prime_policy type; }; -template <> struct pick_policy +template <> struct pick_policy2 { typedef prime_policy type; }; -template <> struct pick_policy +template <> struct pick_policy2 { typedef prime_policy type; }; // TODO: Maybe not if std::size_t is smaller than long long. #if !defined(BOOST_NO_LONG_LONG) -template <> struct pick_policy +template <> struct pick_policy2 { typedef prime_policy type; }; -template <> struct pick_policy +template <> struct pick_policy2 { typedef prime_policy type; }; #endif +template +struct pick_policy : pick_policy2::type> +{ +}; + //////////////////////////////////////////////////////////////////////////// // Functions From 81aefde94e67b7cbe606165b6a6f28c43ff346a9 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 23 Feb 2017 20:14:27 +0000 Subject: [PATCH 06/17] Use const_key_type internally --- .../boost/unordered/detail/implementation.hpp | 50 +++++++++---------- include/boost/unordered/detail/map.hpp | 10 ++-- include/boost/unordered/detail/set.hpp | 4 +- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index 331cbe9f..7e1be8cd 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -2473,7 +2473,7 @@ struct table : boost::unordered::detail::functionshash_function(), k); } @@ -3055,13 +3055,13 @@ struct table : boost::unordered::detail::functions(this)->find_node_impl( key_hash, k, this->key_eq()); } - node_pointer find_node(key_type const& k) const + node_pointer find_node(const_key_type& k) const { return static_cast(this)->find_node_impl( hash(k), k, this->key_eq()); @@ -3379,7 +3379,7 @@ struct table_impl : boost::unordered::detail::table typedef typename table::link_pointer link_pointer; typedef typename table::hasher hasher; typedef typename table::key_equal key_equal; - typedef typename table::key_type key_type; + typedef typename table::const_key_type const_key_type; typedef typename table::node_constructor node_constructor; typedef typename table::node_tmp node_tmp; typedef typename table::extractor extractor; @@ -3453,12 +3453,12 @@ struct table_impl : boost::unordered::detail::table } } - std::size_t count(key_type const& k) const + std::size_t count(const_key_type& k) const { return this->find_node(k) ? 1 : 0; } - value_type& at(key_type const& k) const + value_type& at(const_key_type& k) const { if (this->size_) { node_pointer n = this->find_node(k); @@ -3470,7 +3470,7 @@ struct table_impl : boost::unordered::detail::table std::out_of_range("Unable to find key in unordered_map.")); } - std::pair equal_range(key_type const& k) const + std::pair equal_range(const_key_type& k) const { node_pointer n = this->find_node(k); return std::make_pair(iterator(n), iterator(n ? next_node(n) : n)); @@ -3530,7 +3530,7 @@ struct table_impl : boost::unordered::detail::table return this->add_node(b.release(), key_hash); } - value_type& operator[](key_type const& k) + value_type& operator[](const_key_type& k) { std::size_t key_hash = this->hash(k); node_pointer pos = this->find_node(key_hash, k); @@ -3623,7 +3623,7 @@ struct table_impl : boost::unordered::detail::table template iterator emplace_hint_impl( - c_iterator hint, key_type const& k, BOOST_UNORDERED_EMPLACE_ARGS) + c_iterator hint, const_key_type& k, BOOST_UNORDERED_EMPLACE_ARGS) { if (hint.node_ && this->key_eq()(k, this->get_key(*hint))) { return iterator(hint.node_); @@ -3633,7 +3633,7 @@ struct table_impl : boost::unordered::detail::table } template - emplace_return emplace_impl(key_type const& k, BOOST_UNORDERED_EMPLACE_ARGS) + emplace_return emplace_impl(const_key_type& k, BOOST_UNORDERED_EMPLACE_ARGS) { std::size_t key_hash = this->hash(k); node_pointer pos = this->find_node(key_hash, k); @@ -3656,7 +3656,7 @@ struct table_impl : boost::unordered::detail::table node_tmp b(boost::unordered::detail::func::construct_node_from_args( this->node_alloc(), BOOST_UNORDERED_EMPLACE_FORWARD), this->node_alloc()); - key_type const& k = this->get_key(b.node_->value()); + const_key_type& k = this->get_key(b.node_->value()); if (hint.node_ && this->key_eq()(k, this->get_key(*hint))) { return iterator(hint.node_); } @@ -3675,7 +3675,7 @@ struct table_impl : boost::unordered::detail::table node_tmp b(boost::unordered::detail::func::construct_node_from_args( this->node_alloc(), BOOST_UNORDERED_EMPLACE_FORWARD), this->node_alloc()); - key_type const& k = this->get_key(b.node_->value()); + const_key_type& k = this->get_key(b.node_->value()); std::size_t key_hash = this->hash(k); node_pointer pos = this->find_node(key_hash, k); if (pos) { @@ -3700,7 +3700,7 @@ struct table_impl : boost::unordered::detail::table } template - void insert_range_impl(key_type const& k, InputIt i, InputIt j) + void insert_range_impl(const_key_type& k, InputIt i, InputIt j) { insert_range_impl2(k, i, j); @@ -3718,7 +3718,7 @@ struct table_impl : boost::unordered::detail::table } template - void insert_range_impl2(key_type const& k, InputIt i, InputIt j) + void insert_range_impl2(const_key_type& k, InputIt i, InputIt j) { // No side effects in this initial code std::size_t key_hash = this->hash(k); @@ -3748,7 +3748,7 @@ struct table_impl : boost::unordered::detail::table a.alloc_, a.node_->value_ptr(), *i); node_tmp b(a.release(), a.alloc_); - key_type const& k = this->get_key(b.node_->value()); + const_key_type& k = this->get_key(b.node_->value()); std::size_t key_hash = this->hash(k); node_pointer pos = this->find_node(key_hash, k); @@ -3768,7 +3768,7 @@ struct table_impl : boost::unordered::detail::table // // no throw - std::size_t erase_key(key_type const& k) + std::size_t erase_key(const_key_type& k) { if (!this->size_) return 0; @@ -4012,7 +4012,7 @@ struct grouped_table_impl : boost::unordered::detail::table typedef typename table::link_pointer link_pointer; typedef typename table::hasher hasher; typedef typename table::key_equal key_equal; - typedef typename table::key_type key_type; + typedef typename table::const_key_type const_key_type; typedef typename table::node_constructor node_constructor; typedef typename table::node_tmp node_tmp; typedef typename table::extractor extractor; @@ -4091,7 +4091,7 @@ struct grouped_table_impl : boost::unordered::detail::table } } - std::size_t count(key_type const& k) const + std::size_t count(const_key_type& k) const { node_pointer n = this->find_node(k); if (!n) @@ -4107,7 +4107,7 @@ struct grouped_table_impl : boost::unordered::detail::table return x; } - std::pair equal_range(key_type const& k) const + std::pair equal_range(const_key_type& k) const { node_pointer n = this->find_node(k); return std::make_pair(iterator(n), iterator(n ? next_group(n) : n)); @@ -4315,7 +4315,7 @@ struct grouped_table_impl : boost::unordered::detail::table iterator emplace_impl(node_pointer n) { node_tmp a(n, this->node_alloc()); - key_type const& k = this->get_key(a.node_->value()); + const_key_type& k = this->get_key(a.node_->value()); std::size_t key_hash = this->hash(k); node_pointer position = this->find_node(key_hash, k); this->reserve_for_insert(this->size_ + 1); @@ -4325,7 +4325,7 @@ struct grouped_table_impl : boost::unordered::detail::table iterator emplace_hint_impl(c_iterator hint, node_pointer n) { node_tmp a(n, this->node_alloc()); - key_type const& k = this->get_key(a.node_->value()); + const_key_type& k = this->get_key(a.node_->value()); if (hint.node_ && this->key_eq()(k, this->get_key(*hint))) { this->reserve_for_insert(this->size_ + 1); return iterator(this->add_using_hint(a.release(), hint.node_)); @@ -4340,7 +4340,7 @@ struct grouped_table_impl : boost::unordered::detail::table void emplace_impl_no_rehash(node_pointer n) { node_tmp a(n, this->node_alloc()); - key_type const& k = this->get_key(a.node_->value()); + const_key_type& k = this->get_key(a.node_->value()); std::size_t key_hash = this->hash(k); node_pointer position = this->find_node(key_hash, k); this->add_node(a.release(), key_hash, position); @@ -4391,7 +4391,7 @@ struct grouped_table_impl : boost::unordered::detail::table // // no throw - std::size_t erase_key(key_type const& k) + std::size_t erase_key(const_key_type& k) { if (!this->size_) return 0; diff --git a/include/boost/unordered/detail/map.hpp b/include/boost/unordered/detail/map.hpp index 5770e9ec..dc23931b 100644 --- a/include/boost/unordered/detail/map.hpp +++ b/include/boost/unordered/detail/map.hpp @@ -16,7 +16,7 @@ template struct map typedef std::pair value_type; typedef H hasher; typedef P key_equal; - typedef K key_type; + typedef K const const_key_type; typedef typename ::boost::unordered::detail::rebind_wrap::type value_allocator; @@ -29,8 +29,7 @@ template struct map typedef typename pick::link_pointer link_pointer; typedef boost::unordered::detail::table_impl table; - typedef boost::unordered::detail::map_extractor - extractor; + typedef boost::unordered::detail::map_extractor extractor; typedef typename boost::unordered::detail::pick_policy::type policy; @@ -50,7 +49,7 @@ struct multimap typedef std::pair value_type; typedef H hasher; typedef P key_equal; - typedef K key_type; + typedef K const const_key_type; typedef typename ::boost::unordered::detail::rebind_wrap::type value_allocator; @@ -63,8 +62,7 @@ struct multimap typedef typename pick::link_pointer link_pointer; typedef boost::unordered::detail::grouped_table_impl table; - typedef boost::unordered::detail::map_extractor - extractor; + typedef boost::unordered::detail::map_extractor extractor; typedef typename boost::unordered::detail::pick_policy::type policy; diff --git a/include/boost/unordered/detail/set.hpp b/include/boost/unordered/detail/set.hpp index 2eb34207..0370f78f 100644 --- a/include/boost/unordered/detail/set.hpp +++ b/include/boost/unordered/detail/set.hpp @@ -16,7 +16,7 @@ template struct set typedef T value_type; typedef H hasher; typedef P key_equal; - typedef T key_type; + typedef T const const_key_type; typedef typename ::boost::unordered::detail::rebind_wrap::type value_allocator; @@ -48,7 +48,7 @@ template struct multiset typedef T value_type; typedef H hasher; typedef P key_equal; - typedef T key_type; + typedef T const const_key_type; typedef typename ::boost::unordered::detail::rebind_wrap::type value_allocator; From 79cf0c4bfb4117b3864420ff80bb4b3a5ce11d74 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 23 Feb 2017 20:14:27 +0000 Subject: [PATCH 07/17] pair_traits for getting pair types without instantiating. I was having some problems in an abandoned prototype with incomplete types, I'm not sure I will have this problem now, but I'm keeping this anyway, as it seems useful. --- .../boost/unordered/detail/implementation.hpp | 22 +++++++++++++++++-- include/boost/unordered/detail/map.hpp | 4 ++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index 7e1be8cd..225fbee2 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -337,6 +337,23 @@ struct compressed compressed& operator=(compressed const&); }; +//////////////////////////////////////////////////////////////////////////// +// pair_traits +// +// Used to get the types from a pair without instantiating it. + +template struct pair_traits +{ + typedef typename Pair::first_type first_type; + typedef typename Pair::second_type second_type; +}; + +template struct pair_traits > +{ + typedef T1 first_type; + typedef T2 second_type; +}; + #if defined(BOOST_MSVC) #pragma warning(push) #pragma warning(disable : 4512) // assignment operator could not be generated. @@ -3186,10 +3203,11 @@ template struct set_extractor #endif }; -template struct map_extractor +template struct map_extractor { typedef ValueType value_type; - typedef typename boost::remove_const::type key_type; + typedef typename boost::remove_const::first_type>::type key_type; static key_type const& extract(value_type const& v) { return v.first; } diff --git a/include/boost/unordered/detail/map.hpp b/include/boost/unordered/detail/map.hpp index dc23931b..6758036c 100644 --- a/include/boost/unordered/detail/map.hpp +++ b/include/boost/unordered/detail/map.hpp @@ -29,7 +29,7 @@ template struct map typedef typename pick::link_pointer link_pointer; typedef boost::unordered::detail::table_impl table; - typedef boost::unordered::detail::map_extractor extractor; + typedef boost::unordered::detail::map_extractor extractor; typedef typename boost::unordered::detail::pick_policy::type policy; @@ -62,7 +62,7 @@ struct multimap typedef typename pick::link_pointer link_pointer; typedef boost::unordered::detail::grouped_table_impl table; - typedef boost::unordered::detail::map_extractor extractor; + typedef boost::unordered::detail::map_extractor extractor; typedef typename boost::unordered::detail::pick_policy::type policy; From 8fa93cc55bb03459094dcf245652721ef3e95a80 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 23 Feb 2017 20:14:27 +0000 Subject: [PATCH 08/17] Update some comments for recent versions of standard --- include/boost/unordered/detail/implementation.hpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index 225fbee2..25f8ff83 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -2653,12 +2653,14 @@ struct table : boost::unordered::detail::functions count > size / mlf_ + // From insert/emplace requirements: + // + // size <= mlf_ * count + // => count >= size / mlf_ // // Or from rehash post-condition: - // count > size / mlf_ + // + // count >= size / mlf_ return policy::new_bucket_count( boost::unordered::detail::double_to_size( @@ -3100,10 +3102,7 @@ inline void table::reserve_for_insert(std::size_t size) { if (!buckets_) { create_buckets((std::max)(bucket_count_, min_buckets_for_size(size))); - } - // According to the standard this should be 'size >= max_load_', - // but I think this is better, defect report filed. - else if (size > max_load_) { + } else if (size > max_load_) { std::size_t num_buckets = min_buckets_for_size((std::max)(size, size_ + (size_ >> 1))); From 958d206bb6d51fb28e9428e0736e6cfdaa9f082a Mon Sep 17 00:00:00 2001 From: Daniel James Date: Mon, 27 Feb 2017 03:59:02 +0000 Subject: [PATCH 09/17] Implement insert_or_assign. --- .../boost/unordered/detail/implementation.hpp | 21 +++ include/boost/unordered/unordered_map.hpp | 31 ++++ test/unordered/compile_tests.hpp | 7 +- test/unordered/insert_tests.cpp | 135 ++++++++++++++++++ 4 files changed, 193 insertions(+), 1 deletion(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index 25f8ff83..60b831cb 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -3704,6 +3704,27 @@ struct table_impl : boost::unordered::detail::table } } + template + emplace_return insert_or_assign_impl( + BOOST_FWD_REF(Key) k, BOOST_FWD_REF(M) obj) + { + std::size_t key_hash = this->hash(k); + node_pointer pos = this->find_node(key_hash, k); + + if (pos) { + pos->value().second = boost::forward(obj); + return emplace_return(iterator(pos), false); + } else { + return emplace_return( + iterator(this->resize_and_add_node( + boost::unordered::detail::func::construct_node_pair( + this->node_alloc(), boost::forward(k), + boost::forward(obj)), + key_hash)), + true); + } + } + //////////////////////////////////////////////////////////////////////// // Insert range methods // diff --git a/include/boost/unordered/unordered_map.hpp b/include/boost/unordered/unordered_map.hpp index 3b842d54..bb3d5884 100644 --- a/include/boost/unordered/unordered_map.hpp +++ b/include/boost/unordered/unordered_map.hpp @@ -342,6 +342,37 @@ template class unordered_map return this->emplace_hint(hint, boost::move(x)); } + template + std::pair insert_or_assign( + key_type const& k, BOOST_FWD_REF(M) obj) + { + return table_.insert_or_assign_impl(k, boost::forward(obj)); + } + + template + iterator insert_or_assign( + const_iterator, key_type const& k, BOOST_FWD_REF(M) obj) + { + return table_.insert_or_assign_impl(k, boost::forward(obj)).first; + } + + template + std::pair insert_or_assign( + BOOST_RV_REF(key_type) k, BOOST_FWD_REF(M) obj) + { + return table_.insert_or_assign_impl( + boost::move(k), boost::forward(obj)); + } + + template + iterator insert_or_assign( + const_iterator, BOOST_RV_REF(key_type) k, BOOST_FWD_REF(M) obj) + { + return table_ + .insert_or_assign_impl(boost::move(k), boost::forward(obj)) + .first; + } + template void insert(InputIt, InputIt); #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) diff --git a/test/unordered/compile_tests.hpp b/test/unordered/compile_tests.hpp index 6c59860d..457b0238 100644 --- a/test/unordered/compile_tests.hpp +++ b/test/unordered/compile_tests.hpp @@ -301,13 +301,18 @@ template void unordered_equivalent_test(X& r, T const& t) } template -void unordered_map_functions(X&, Key const& k, T const&) +void unordered_map_functions(X&, Key const& k, T const& v) { typedef BOOST_DEDUCED_TYPENAME X::mapped_type mapped_type; + typedef BOOST_DEDUCED_TYPENAME X::iterator iterator; X a; test::check_return_type::equals_ref(a[k]); test::check_return_type::equals_ref(a.at(k)); + test::check_return_type >::equals( + a.insert_or_assign(k, v)); + test::check_return_type::equals( + a.insert_or_assign(a.begin(), k, v)); X const b = a; test::check_return_type::equals_ref(b.at(k)); diff --git a/test/unordered/insert_tests.cpp b/test/unordered/insert_tests.cpp index 1f477ade..86edfe24 100644 --- a/test/unordered/insert_tests.cpp +++ b/test/unordered/insert_tests.cpp @@ -505,6 +505,138 @@ template void map_tests(X*, test::random_generator generator) test::check_equivalent_keys(x); } +template void map_tests2(X*, test::random_generator generator) +{ + typedef BOOST_DEDUCED_TYPENAME X::iterator iterator; + std::cerr << "insert_or_assign\n"; + + { + test::check_instances check_; + + X x; + test::ordered tracker = test::create_ordered(x); + + test::random_values v(1000, generator); + for (BOOST_DEDUCED_TYPENAME test::random_values::iterator it = + v.begin(); + it != v.end(); ++it) { + BOOST_DEDUCED_TYPENAME X::size_type old_bucket_count = + x.bucket_count(); + float b = x.max_load_factor(); + + std::pair r = + x.insert_or_assign(it->first, it->second); + BOOST_TEST(*r.first == *it); + + tracker[it->first] = it->second; + tracker.compare_key(x, *it); + + if (static_cast(x.size()) < + b * static_cast(old_bucket_count)) + BOOST_TEST(x.bucket_count() == old_bucket_count); + } + + tracker.compare(x); + test::check_equivalent_keys(x); + } + + std::cerr << "insert_or_assign(begin)\n"; + + { + test::check_instances check_; + + X x; + test::ordered tracker = test::create_ordered(x); + + test::random_values v(1000, generator); + for (BOOST_DEDUCED_TYPENAME test::random_values::iterator it = + v.begin(); + it != v.end(); ++it) { + BOOST_DEDUCED_TYPENAME X::size_type old_bucket_count = + x.bucket_count(); + float b = x.max_load_factor(); + + iterator r = x.insert_or_assign(x.begin(), it->first, it->second); + BOOST_TEST(*r == *it); + + tracker[it->first] = it->second; + tracker.compare_key(x, *it); + + if (static_cast(x.size()) < + b * static_cast(old_bucket_count)) + BOOST_TEST(x.bucket_count() == old_bucket_count); + } + + tracker.compare(x); + test::check_equivalent_keys(x); + } + + std::cerr << "insert_or_assign(end)\n"; + + { + test::check_instances check_; + + X x; + test::ordered tracker = test::create_ordered(x); + + test::random_values v(1000, generator); + for (BOOST_DEDUCED_TYPENAME test::random_values::iterator it = + v.begin(); + it != v.end(); ++it) { + BOOST_DEDUCED_TYPENAME X::size_type old_bucket_count = + x.bucket_count(); + float b = x.max_load_factor(); + + iterator r = x.insert_or_assign(x.end(), it->first, it->second); + BOOST_TEST(*r == *it); + + tracker[it->first] = it->second; + tracker.compare_key(x, *it); + + if (static_cast(x.size()) < + b * static_cast(old_bucket_count)) + BOOST_TEST(x.bucket_count() == old_bucket_count); + } + + tracker.compare(x); + test::check_equivalent_keys(x); + } + + std::cerr << "insert_or_assign(last)\n"; + + { + test::check_instances check_; + + X x; + test::ordered tracker = test::create_ordered(x); + iterator last = x.begin(); + + test::random_values v(1000, generator); + for (BOOST_DEDUCED_TYPENAME test::random_values::iterator it = + v.begin(); + it != v.end(); ++it) { + BOOST_DEDUCED_TYPENAME X::size_type old_bucket_count = + x.bucket_count(); + float b = x.max_load_factor(); + + iterator r = x.insert_or_assign(last, it->first, it->second); + BOOST_TEST(*r == *it); + + tracker[it->first] = it->second; + tracker.compare_key(x, *it); + + if (static_cast(x.size()) < + b * static_cast(old_bucket_count)) + BOOST_TEST(x.bucket_count() == old_bucket_count); + + last = r; + } + + tracker.compare(x); + test::check_equivalent_keys(x); + } +} + // Some tests for when the range's value type doesn't match the container's // value type. @@ -600,6 +732,9 @@ UNORDERED_TEST(default_emplace_tests, UNORDERED_TEST(map_tests, ((test_map))((default_generator)(generate_collisions)(limited_range))) +UNORDERED_TEST( + map_tests2, ((test_map))((default_generator)(generate_collisions))) + UNORDERED_TEST(map_insert_range_test1, ((test_multimap_std_alloc)(test_map)(test_multimap))( (default_generator)(generate_collisions)(limited_range))) From 5f5f8ef1e4cd1458b2007639ca52516cdb44887e Mon Sep 17 00:00:00 2001 From: Daniel James Date: Mon, 27 Feb 2017 03:59:02 +0000 Subject: [PATCH 10/17] Implement try_emplace --- .../boost/unordered/detail/implementation.hpp | 103 +++++++-- include/boost/unordered/unordered_map.hpp | 195 +++++++++++++++++- test/unordered/compile_tests.hpp | 11 + test/unordered/emplace_tests.cpp | 49 +++++ test/unordered/insert_tests.cpp | 186 +++++++++++++++++ 5 files changed, 527 insertions(+), 17 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index 60b831cb..b80dbed0 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -1721,6 +1721,35 @@ construct_node_pair(Alloc& alloc, BOOST_FWD_REF(Key) k, BOOST_FWD_REF(Mapped) m) BOOST_CATCH_END return a.release(); } + +template +inline typename boost::unordered::detail::allocator_traits::pointer +construct_node_pair_from_args( + Alloc& alloc, BOOST_FWD_REF(Key) k, BOOST_UNORDERED_EMPLACE_ARGS) +{ + node_constructor a(alloc); + a.create_node(); + boost::unordered::detail::func::call_construct( + alloc, boost::unordered::detail::func::const_cast_pointer( + boost::addressof(a.node_->value_ptr()->first)), + boost::forward(k)); + BOOST_TRY + { + boost::unordered::detail::func::construct_from_args( + alloc, boost::unordered::detail::func::const_cast_pointer( + boost::addressof(a.node_->value_ptr()->second)), + BOOST_UNORDERED_EMPLACE_FORWARD); + } + BOOST_CATCH(...) + { + boost::unordered::detail::func::call_destroy( + alloc, boost::unordered::detail::func::const_cast_pointer( + boost::addressof(a.node_->value_ptr()->first))); + BOOST_RETHROW; + } + BOOST_CATCH_END + return a.release(); +} } } } @@ -3547,22 +3576,6 @@ struct table_impl : boost::unordered::detail::table return this->add_node(b.release(), key_hash); } - value_type& operator[](const_key_type& k) - { - std::size_t key_hash = this->hash(k); - node_pointer pos = this->find_node(key_hash, k); - if (pos) { - return pos->value(); - } else { - return this - ->resize_and_add_node( - boost::unordered::detail::func::construct_node_pair( - this->node_alloc(), k), - key_hash) - ->value(); - } - } - #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) emplace_return emplace(boost::unordered::detail::emplace_args1< @@ -3704,6 +3717,64 @@ struct table_impl : boost::unordered::detail::table } } + template + emplace_return try_emplace_impl(BOOST_FWD_REF(Key) k) + { + std::size_t key_hash = this->hash(k); + node_pointer pos = this->find_node(key_hash, k); + if (pos) { + return emplace_return(iterator(pos), false); + } else { + return emplace_return( + iterator(this->resize_and_add_node( + boost::unordered::detail::func::construct_node_pair( + this->node_alloc(), boost::forward(k)), + key_hash)), + true); + } + } + + template + iterator try_emplace_hint_impl(c_iterator hint, BOOST_FWD_REF(Key) k) + { + if (hint.node_ && this->key_eq()(hint->first, k)) { + return iterator(hint.node_); + } else { + return try_emplace_impl(k).first; + } + } + + template + emplace_return try_emplace_impl( + BOOST_FWD_REF(Key) k, BOOST_UNORDERED_EMPLACE_ARGS) + { + std::size_t key_hash = this->hash(k); + node_pointer pos = this->find_node(key_hash, k); + if (pos) { + return emplace_return(iterator(pos), false); + } else { + return emplace_return( + iterator(this->resize_and_add_node( + boost::unordered::detail::func:: + construct_node_pair_from_args(this->node_alloc(), + boost::forward(k), + BOOST_UNORDERED_EMPLACE_FORWARD), + key_hash)), + true); + } + } + + template + iterator try_emplace_hint_impl( + c_iterator hint, BOOST_FWD_REF(Key) k, BOOST_UNORDERED_EMPLACE_ARGS) + { + if (hint.node_ && this->key_eq()(hint->first, k)) { + return iterator(hint.node_); + } else { + return try_emplace_impl(k, BOOST_UNORDERED_EMPLACE_FORWARD).first; + } + } + template emplace_return insert_or_assign_impl( BOOST_FWD_REF(Key) k, BOOST_FWD_REF(M) obj) diff --git a/include/boost/unordered/unordered_map.hpp b/include/boost/unordered/unordered_map.hpp index bb3d5884..dbba089a 100644 --- a/include/boost/unordered/unordered_map.hpp +++ b/include/boost/unordered/unordered_map.hpp @@ -220,6 +220,37 @@ template class unordered_map { return table_.emplace_hint(hint, boost::forward(args)...); } + + template + std::pair try_emplace( + key_type const& k, BOOST_FWD_REF(Args)... args) + { + return table_.try_emplace_impl(k, boost::forward(args)...); + } + + template + iterator try_emplace( + const_iterator hint, key_type const& k, BOOST_FWD_REF(Args)... args) + { + return table_.try_emplace_hint_impl( + hint, k, boost::forward(args)...); + } + + template + std::pair try_emplace( + BOOST_RV_REF(key_type) k, BOOST_FWD_REF(Args)... args) + { + return table_.try_emplace_impl( + boost::move(k), boost::forward(args)...); + } + + template + iterator try_emplace(const_iterator hint, BOOST_RV_REF(key_type) k, + BOOST_FWD_REF(Args)... args) + { + return table_.try_emplace_hint_impl( + hint, boost::move(k), boost::forward(args)...); + } #else #if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x5100)) @@ -246,6 +277,18 @@ template class unordered_map #endif + template + std::pair try_emplace(BOOST_FWD_REF(Key) k) + { + return table_.try_emplace_impl(boost::forward(k)); + } + + template + iterator try_emplace(const_iterator hint, BOOST_FWD_REF(Key) k) + { + return table_.try_emplace_hint_impl(hint, boost::forward(k)); + } + template std::pair emplace(BOOST_FWD_REF(A0) a0) { @@ -261,6 +304,39 @@ template class unordered_map boost::forward(a0))); } + template + std::pair try_emplace( + key_type const& k, BOOST_FWD_REF(A0) a0) + { + return table_.try_emplace_impl( + k, boost::unordered::detail::create_emplace_args( + boost::forward(a0))); + } + + template + iterator try_emplace( + const_iterator hint, key_type const& k, BOOST_FWD_REF(A0) a0) + { + return table_.try_emplace_hint_impl( + hint, k, boost::unordered::detail::create_emplace_args( + boost::forward(a0))); + } + + template + std::pair try_emplace( + BOOST_RV_REF(key_type) k, BOOST_FWD_REF(A0) a0) + { + return table_.try_emplace_impl(boost::move(k), boost::forward(a0)); + } + + template + iterator try_emplace( + const_iterator hint, BOOST_RV_REF(key_type) k, BOOST_FWD_REF(A0) a0) + { + return table_.try_emplace_hint_impl( + hint, boost::move(k), boost::forward(a0)); + } + template std::pair emplace( BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1) @@ -278,6 +354,44 @@ template class unordered_map boost::forward(a0), boost::forward(a1))); } + template + std::pair try_emplace( + key_type const& k, BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1) + { + return table_.try_emplace_impl( + k, boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1))); + } + + template + iterator try_emplace(const_iterator hint, key_type const& k, + BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1) + { + return table_.try_emplace_hint_impl( + hint, k, boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1))); + } + + template + std::pair try_emplace( + BOOST_RV_REF(key_type) k, BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1) + { + return table_.try_emplace_impl( + boost::move(k), + boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1))); + } + + template + iterator try_emplace(const_iterator hint, BOOST_RV_REF(key_type) k, + BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1) + { + return table_.try_emplace_hint_impl( + hint, boost::move(k), + boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1))); + } + template std::pair emplace( BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2) @@ -297,6 +411,49 @@ template class unordered_map boost::forward(a2))); } + template + std::pair try_emplace(key_type const& k, + BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2) + { + return table_.try_emplace_impl( + k, boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1), + boost::forward(a2))); + } + + template + iterator try_emplace(const_iterator hint, key_type const& k, + BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2) + { + return table_ + .try_emplace_impl_( + hint, k, boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1), + boost::forward(a2))) + .first; + } + + template + std::pair try_emplace(BOOST_RV_REF(key_type) k, + BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2) + { + return table_.try_emplace_impl( + boost::move(k), boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1), + boost::forward(a2))); + } + + template + iterator try_emplace(const_iterator hint, BOOST_RV_REF(key_type) k, + BOOST_FWD_REF(A0) a0, BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2) + { + return table_.try_emplace_hint_impl( + hint, boost::move(k), + boost::unordered::detail::create_emplace_args( + boost::forward(a0), boost::forward(a1), + boost::forward(a2))); + } + #define BOOST_UNORDERED_EMPLACE(z, n, _) \ template \ std::pair emplace( \ @@ -313,6 +470,42 @@ template class unordered_map return table_.emplace_hint( \ hint, boost::unordered::detail::create_emplace_args( \ BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_CALL_FORWARD, a))); \ + } \ + \ + template \ + std::pair try_emplace( \ + key_type const& k, BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_FWD_PARAM, a)) \ + { \ + return table_.try_emplace_impl( \ + k, boost::unordered::detail::create_emplace_args( \ + BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_CALL_FORWARD, a))); \ + } \ + \ + template \ + iterator try_emplace(const_iterator hint, key_type const& k, \ + BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_FWD_PARAM, a)) \ + { \ + return table_.try_emplace_hint_impl(hint, k, \ + boost::unordered::detail::create_emplace_args(BOOST_PP_ENUM_##z( \ + n, BOOST_UNORDERED_CALL_FORWARD, a))); \ + } \ + \ + template \ + std::pair try_emplace(BOOST_RV_REF(key_type) k, \ + BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_FWD_PARAM, a)) \ + { \ + return table_.try_emplace_impl(boost::move(k), \ + boost::unordered::detail::create_emplace_args(BOOST_PP_ENUM_##z( \ + n, BOOST_UNORDERED_CALL_FORWARD, a))); \ + } \ + \ + template \ + iterator try_emplace(const_iterator hint, BOOST_RV_REF(key_type) k, \ + BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_FWD_PARAM, a)) \ + { \ + return table_.try_emplace_hint_impl(hint, boost::move(k), \ + boost::unordered::detail::create_emplace_args(BOOST_PP_ENUM_##z( \ + n, BOOST_UNORDERED_CALL_FORWARD, a))); \ } BOOST_PP_REPEAT_FROM_TO( @@ -1114,7 +1307,7 @@ template typename unordered_map::mapped_type& unordered_map::operator[](const key_type& k) { - return table_[k].second; + return table_.try_emplace_impl(k).first->second; } template diff --git a/test/unordered/compile_tests.hpp b/test/unordered/compile_tests.hpp index 457b0238..d0d1fd7c 100644 --- a/test/unordered/compile_tests.hpp +++ b/test/unordered/compile_tests.hpp @@ -309,10 +309,21 @@ void unordered_map_functions(X&, Key const& k, T const& v) X a; test::check_return_type::equals_ref(a[k]); test::check_return_type::equals_ref(a.at(k)); + test::check_return_type >::equals( + a.try_emplace(k, v)); + test::check_return_type >::equals( + a.try_emplace(rvalue(k), v)); + test::check_return_type::equals(a.try_emplace(a.begin(), k, v)); + test::check_return_type::equals( + a.try_emplace(a.begin(), rvalue(k), v)); test::check_return_type >::equals( a.insert_or_assign(k, v)); + test::check_return_type >::equals( + a.insert_or_assign(rvalue(k), v)); test::check_return_type::equals( a.insert_or_assign(a.begin(), k, v)); + test::check_return_type::equals( + a.insert_or_assign(a.begin(), rvalue(k), v)); X const b = a; test::check_return_type::equals_ref(b.at(k)); diff --git a/test/unordered/emplace_tests.cpp b/test/unordered/emplace_tests.cpp index 04c0253b..e86a066f 100644 --- a/test/unordered/emplace_tests.cpp +++ b/test/unordered/emplace_tests.cpp @@ -465,6 +465,55 @@ UNORDERED_AUTO_TEST(emplace_multimap) BOOST_TEST_EQ(check_.instances(), 20); BOOST_TEST_EQ(check_.constructions(), 20); } + +UNORDERED_AUTO_TEST(try_emplace) +{ + test::check_instances check_; + + typedef boost::unordered_map container; + typedef container::iterator iterator; + typedef std::pair return_type; + container x(10); + return_type r1, r2, r3; + + int k1 = 3; + emplace_value m1(414, "grr"); + r1 = x.try_emplace(3, 414, "grr"); + BOOST_TEST(r1.second); + BOOST_TEST(r1.first->first == k1); + BOOST_TEST(r1.first->second == m1); + BOOST_TEST_EQ(x.size(), 1u); + BOOST_TEST_EQ(check_.instances(), 2); + BOOST_TEST_EQ(check_.constructions(), 2); + + int k2 = 10; + emplace_value m2(25, "", 'z'); + r2 = x.try_emplace(10, 25, std::string(""), 'z'); + BOOST_TEST(r2.second); + BOOST_TEST(r2.first->first == k2); + BOOST_TEST(r2.first->second == m2); + BOOST_TEST_EQ(x.size(), 2u); + BOOST_TEST_EQ(check_.instances(), 4); + BOOST_TEST_EQ(check_.constructions(), 4); + + BOOST_TEST(x.find(k1)->second == m1); + BOOST_TEST(x.find(k2)->second == m2); + + r3 = x.try_emplace(k2, 68, "jfeoj", 'p', 49309, 2323); + BOOST_TEST(!r3.second); + BOOST_TEST(r3.first == r2.first); + BOOST_TEST(r3.first->second == m2); + BOOST_TEST_EQ(x.size(), 2u); + BOOST_TEST_EQ(check_.instances(), 4); + BOOST_TEST_EQ(check_.constructions(), 4); + + BOOST_TEST(r2.first == x.try_emplace(r2.first, k2, 808709, "what")); + BOOST_TEST( + r2.first == + x.try_emplace(r2.first, k2, 10, "xxx", 'a', 4, 5, 6, 7, 8, 9, 10)); + BOOST_TEST(r2.first->second == m2); + BOOST_TEST_EQ(x.size(), 2u); +} } RUN_TESTS() diff --git a/test/unordered/insert_tests.cpp b/test/unordered/insert_tests.cpp index 86edfe24..ae50fa18 100644 --- a/test/unordered/insert_tests.cpp +++ b/test/unordered/insert_tests.cpp @@ -637,6 +637,171 @@ template void map_tests2(X*, test::random_generator generator) } } +template void try_emplace_tests(X*, test::random_generator generator) +{ + std::cerr << "try_emplace(key, value)\n"; + + typedef BOOST_DEDUCED_TYPENAME X::iterator iterator; + + { + test::check_instances check_; + + X x; + test::ordered tracker = test::create_ordered(x); + + test::random_values v(1000, generator); + for (BOOST_DEDUCED_TYPENAME test::random_values::iterator it = + v.begin(); + it != v.end(); ++it) { + BOOST_DEDUCED_TYPENAME X::size_type old_bucket_count = + x.bucket_count(); + float b = x.max_load_factor(); + + iterator pos = x.find(it->first); + bool found = pos != x.end(); + + std::pair r = + x.try_emplace(it->first, it->second); + if (found) { + BOOST_TEST(pos == r.first); + BOOST_TEST(!r.second); + } else { + BOOST_TEST(r.second); + } + BOOST_TEST_EQ(r.first->first, it->first); + BOOST_TEST_EQ(r.first->second, it->second); + + tracker.insert(*it); + tracker.compare_key(x, *it); + + if (static_cast(x.size()) < + b * static_cast(old_bucket_count)) + BOOST_TEST(x.bucket_count() == old_bucket_count); + } + + test::check_equivalent_keys(x); + } + + std::cerr << "try_emplace(begin(), key, value)\n"; + + typedef BOOST_DEDUCED_TYPENAME X::iterator iterator; + + { + test::check_instances check_; + + X x; + test::ordered tracker = test::create_ordered(x); + + test::random_values v(1000, generator); + for (BOOST_DEDUCED_TYPENAME test::random_values::iterator it = + v.begin(); + it != v.end(); ++it) { + BOOST_DEDUCED_TYPENAME X::size_type old_bucket_count = + x.bucket_count(); + float b = x.max_load_factor(); + + iterator pos = x.find(it->first); + bool found = pos != x.end(); + + typename X::iterator r = + x.try_emplace(r.begin(), it->first, it->second); + if (found) { + BOOST_TEST(pos == r); + } + BOOST_TEST_EQ(r->first, it->first); + BOOST_TEST_EQ(r->second, it->second); + + tracker.insert(*it); + tracker.compare_key(x, *it); + + if (static_cast(x.size()) < + b * static_cast(old_bucket_count)) + BOOST_TEST(x.bucket_count() == old_bucket_count); + } + + test::check_equivalent_keys(x); + } + + std::cerr << "try_emplace(end(), key, value)\n"; + + typedef BOOST_DEDUCED_TYPENAME X::iterator iterator; + + { + test::check_instances check_; + + X x; + test::ordered tracker = test::create_ordered(x); + + test::random_values v(1000, generator); + for (BOOST_DEDUCED_TYPENAME test::random_values::iterator it = + v.begin(); + it != v.end(); ++it) { + BOOST_DEDUCED_TYPENAME X::size_type old_bucket_count = + x.bucket_count(); + float b = x.max_load_factor(); + + iterator pos = x.find(it->first); + bool found = pos != x.end(); + + typename X::iterator r = + x.try_emplace(r.end(), it->first, it->second); + if (found) { + BOOST_TEST(pos == r); + } + BOOST_TEST_EQ(r->first, it->first); + BOOST_TEST_EQ(r->second, it->second); + + tracker.insert(*it); + tracker.compare_key(x, *it); + + if (static_cast(x.size()) < + b * static_cast(old_bucket_count)) + BOOST_TEST(x.bucket_count() == old_bucket_count); + } + + test::check_equivalent_keys(x); + } + + std::cerr << "try_emplace(pos, key, value)\n"; + + typedef BOOST_DEDUCED_TYPENAME X::iterator iterator; + + { + test::check_instances check_; + + X x; + test::ordered tracker = test::create_ordered(x); + + test::random_values v(1000, generator); + for (BOOST_DEDUCED_TYPENAME test::random_values::iterator it = + v.begin(); + it != v.end(); ++it) { + BOOST_DEDUCED_TYPENAME X::size_type old_bucket_count = + x.bucket_count(); + float b = x.max_load_factor(); + + iterator pos = x.find(it->first); + bool found = pos != x.end(); + + typename X::iterator r = x.try_emplace(pos, it->first, it->second); + if (found) { + BOOST_TEST(pos == r); + } + BOOST_TEST_EQ(r->first, it->first); + BOOST_TEST_EQ(r->second, it->second); + + tracker.insert(*it); + tracker.compare_key(x, *it); + + if (static_cast(x.size()) < + b * static_cast(old_bucket_count)) + BOOST_TEST(x.bucket_count() == old_bucket_count); + } + + test::check_equivalent_keys(x); + } +} + // Some tests for when the range's value type doesn't match the container's // value type. @@ -876,6 +1041,10 @@ UNORDERED_AUTO_TEST(map_emplace_test) x.emplace(2, 3); BOOST_TEST( x.find(2) != x.end() && x.find(2)->second == overloaded_constructor(3)); + + x.try_emplace(5); + BOOST_TEST( + x.find(5) != x.end() && x.find(5)->second == overloaded_constructor()); } UNORDERED_AUTO_TEST(set_emplace_test) @@ -954,6 +1123,23 @@ UNORDERED_AUTO_TEST(map_emplace_test2) BOOST_TEST(x.find(overloaded_constructor(9, 3, 1)) != x.end() && x.find(overloaded_constructor(9, 3, 1))->second == overloaded_constructor(10)); + + x.clear(); + + x.try_emplace(overloaded_constructor()); + BOOST_TEST( + x.find(overloaded_constructor()) != x.end() && + x.find(overloaded_constructor())->second == overloaded_constructor()); + + x.try_emplace(1); + BOOST_TEST( + x.find(overloaded_constructor(1)) != x.end() && + x.find(overloaded_constructor(1))->second == overloaded_constructor()); + + x.try_emplace(overloaded_constructor(2, 3), 4, 5, 6); + BOOST_TEST(x.find(overloaded_constructor(2, 3)) != x.end() && + x.find(overloaded_constructor(2, 3))->second == + overloaded_constructor(4, 5, 6)); } UNORDERED_AUTO_TEST(set_emplace_test2) From 9c4c3a754a2c1454afdde5c4eee978272bd0b392 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Mon, 27 Feb 2017 03:59:02 +0000 Subject: [PATCH 11/17] Const methods in value_base. --- include/boost/unordered/detail/implementation.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index b80dbed0..43cab65c 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -2500,8 +2500,12 @@ template struct value_base value_type& value() { return *(ValueType*)this; } + value_type const& value() const { return *(ValueType const*)this; } + value_type* value_ptr() { return (ValueType*)this; } + value_type const* value_ptr() const { return (ValueType const*)this; } + private: value_base& operator=(value_base const&); }; From 21a24d6cd7d3e80f559186db795a838db696f13e Mon Sep 17 00:00:00 2001 From: Daniel James Date: Mon, 27 Feb 2017 03:59:02 +0000 Subject: [PATCH 12/17] Support node_handle --- doc/ref.php | 261 +++++++ doc/ref.xml | 640 +++++++++++++++++- .../boost/unordered/detail/implementation.hpp | 265 +++++++- include/boost/unordered/detail/map.hpp | 23 + include/boost/unordered/detail/set.hpp | 22 + include/boost/unordered/unordered_map.hpp | 264 +++++++- include/boost/unordered/unordered_map_fwd.hpp | 3 + include/boost/unordered/unordered_set.hpp | 261 +++++++ include/boost/unordered/unordered_set_fwd.hpp | 3 + test/Jamfile.v2 | 6 + test/helpers/equivalent.hpp | 1 + test/unordered/compile_map.cpp | 15 +- test/unordered/compile_set.cpp | 15 +- test/unordered/compile_tests.hpp | 84 ++- test/unordered/extract_tests.cpp | 140 ++++ test/unordered/insert_node_type_fail.cpp | 29 + test/unordered/node_handle_tests.cpp | 436 ++++++++++++ 17 files changed, 2419 insertions(+), 49 deletions(-) create mode 100644 test/unordered/extract_tests.cpp create mode 100644 test/unordered/insert_node_type_fail.cpp create mode 100644 test/unordered/node_handle_tests.cpp diff --git a/doc/ref.php b/doc/ref.php index 3847ede3..f6e26e91 100644 --- a/doc/ref.php +++ b/doc/ref.php @@ -189,6 +189,20 @@ EOL; A const_local_iterator object can be used to iterate through a single bucket. + + implementation-defined + + See node_handle_ for details. + + + + + implementation-defined + + Structure returned by inserting node_type. + + + size() == 0 @@ -876,6 +890,142 @@ EOL; Pointers and references to elements are never invalidated. + + + initializer_list<value_type> + + void + + Inserts a range of elements into the container. Elements are inserted if and only if there is no element in the container with an equivalent . + + + value_type is EmplaceConstructible into + X from *first. + + + When inserting a single element, if an exception is thrown by an operation other than a call to hasher the function has no effect. + + + Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor. + Pointers and references to elements are never invalidated. + + + + + const_iterator + + node_type + + Removes the element pointed to by position. + + + A node_type owning the element. + + + + + key_type const& + + node_type + + Removes an element with key equivalent to k. + + + A node_type owning the element if found, otherwise an empty node_type. + + + Only throws an exception if it is thrown by hasher or key_equal. + + + + + node_type&& + + + + If nh is empty, has no affect. + + Otherwise inserts the element owned by nh + + Otherwise inserts the element owned by nh + if and only if there is no element in the container with an equivalent . + + + + + nh is empty or nh.get_allocator() is equal to the container's allocator. + + + + If nh was empty, returns end(). + Otherwise returns an iterator pointing to the newly inserted element. + + If nh was empty, returns an insert_return_type with: + inserted equal to false, + position equal to end() and + node empty. + Otherwise if there was already an element with an equivalent key, returns an insert_return_type with: + inserted equal to false, + position pointing to a matching element and + node contains the node from nh. + Otherwise if the insertion succeeded, returns an insert_return_type with: + inserted equal to true, + position pointing to the newly inserted element and + node empty. + + + + If an exception is thrown by an operation other than a call to hasher the function has no effect. + + + Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor. + Pointers and references to elements are never invalidated. + + + + + const_iterator + + + node_type&& + + iterator + + If nh is empty, has no affect. + + Otherwise inserts the element owned by nh + + Otherwise inserts the element owned by nh + if and only if there is no element in the container with an equivalent . + + If there is already an element in the container with an equivalent + has no effect on nh (i.e. nh still contains the node.) + + hint is a suggestion to where the element should be inserted. + + + nh is empty or nh.get_allocator() is equal to the container's allocator. + + + + If nh was empty, returns end(). + Otherwise returns an iterator pointing to the newly inserted element. + + If nh was empty returns end(). + If there was already an element in the container with an equivalent + returns an iterator pointing to that. + Otherwise returns an iterator pointing to the newly inserted element. + + + + If an exception is thrown by an operation other than a call to hasher the function has no effect. + + + The standard is fairly vague on the meaning of the hint. But the only practical way to use it, and the only way that Boost.Unordered supports is to point to an existing element with the same . + Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor. + Pointers and references to elements are never invalidated. + + const_iterator @@ -1416,6 +1566,115 @@ EOL; + + + + + + An object that owns a single element extracted from an + unordered_ or an + unordered_multi, that + can then be inserted into a compatible container type. + + + + The name and template parameters of this type is implementation + defined, and should be obtained using the node_type + member typedef from the appropriate container. + + + + + + typename Container::key_type + + + typename Container::mapped_type + + + + typename Container::value_type> + + + + typename Container::allocator_type> + + + + + + + && + + + + + && + + & + + + + key_type& + + + mapped_type& + + + + value_type& + + + + allocator_type + + + + + bool + + + + & + + void + + + In C++17 is also noexcept if ator_traits::is_always_equal::value is true. + But we don't support that trait yet. + + + + + + + + & + + + & + + void + + x.swap(y) + + + + + +