From a4fd33dcda0b4e49134ecacd0bd640c3683ef611 Mon Sep 17 00:00:00 2001 From: Dietoad Date: Tue, 23 May 2023 00:00:42 +0800 Subject: [PATCH 1/3] fix const value return by emplace iterator with const this code will not compile tsl::sparse_map spp; const auto it=spp.emplace(1,1); it.first->second=1; --- include/tsl/sparse_hash.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/tsl/sparse_hash.h b/include/tsl/sparse_hash.h index 3547c24..090cd18 100644 --- a/include/tsl/sparse_hash.h +++ b/include/tsl/sparse_hash.h @@ -1070,7 +1070,7 @@ class sparse_hash : private Allocator, public: using iterator_category = std::forward_iterator_tag; - using value_type = const typename sparse_hash::value_type; + using value_type = typename sparse_hash::value_type; using difference_type = std::ptrdiff_t; using reference = value_type &; using pointer = value_type *; From 3aa93834ccafb8d4fa11589a5ca4be71883c9709 Mon Sep 17 00:00:00 2001 From: dietoad Date: Sat, 27 May 2023 23:18:49 +0800 Subject: [PATCH 2/3] fix const reference return by iterator --- include/tsl/sparse_hash.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/tsl/sparse_hash.h b/include/tsl/sparse_hash.h index 090cd18..da6f8fd 100644 --- a/include/tsl/sparse_hash.h +++ b/include/tsl/sparse_hash.h @@ -1072,8 +1072,13 @@ class sparse_hash : private Allocator, using iterator_category = std::forward_iterator_tag; using value_type = typename sparse_hash::value_type; using difference_type = std::ptrdiff_t; - using reference = value_type &; - using pointer = value_type *; + using reference = + typename std::conditional::type; + using pointer = + typename std::conditional::type; sparse_iterator() noexcept {} From fcc567f72ae828c3bd83acc4f254fba2b4966181 Mon Sep 17 00:00:00 2001 From: dietoad Date: Sat, 3 Jun 2023 22:27:41 +0800 Subject: [PATCH 3/3] map iterator with std::pair --- include/tsl/sparse_hash.h | 27 +++++++++++++++++++-------- include/tsl/sparse_map.h | 12 +++++++++++- include/tsl/sparse_set.h | 7 +++---- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/include/tsl/sparse_hash.h b/include/tsl/sparse_hash.h index da6f8fd..504a4a4 100644 --- a/include/tsl/sparse_hash.h +++ b/include/tsl/sparse_hash.h @@ -993,7 +993,7 @@ class sparse_array { template + tsl::sh::probing Probing, class ValueTypeIt> class sparse_hash : private Allocator, private Hash, private KeyEqual, @@ -1027,6 +1027,12 @@ class sparse_hash : private Allocator, using iterator = sparse_iterator; using const_iterator = sparse_iterator; + using value_type_it = ValueTypeIt; + using reference_it = value_type_it &; + using const_reference_it = const value_type_it &; + using pointer_it = value_type_it *; + using const_pointer_it = const value_type_it *; + private: using sparse_array = tsl::detail_sparse_hash::sparse_array; @@ -1070,15 +1076,16 @@ class sparse_hash : private Allocator, public: using iterator_category = std::forward_iterator_tag; - using value_type = typename sparse_hash::value_type; + using value_type = typename sparse_hash::value_type_it; using difference_type = std::ptrdiff_t; using reference = typename std::conditional::type; + typename sparse_hash::const_reference_it, + typename sparse_hash::reference_it>::type; using pointer = - typename std::conditional::type; + typename std::conditional::type; sparse_iterator() noexcept {} @@ -1112,9 +1119,13 @@ class sparse_hash : private Allocator, return U()(*m_sparse_array_it); } - reference operator*() const { return *m_sparse_array_it; } + reference operator*() const { + return reinterpret_cast(*m_sparse_array_it); + } - pointer operator->() const { return std::addressof(*m_sparse_array_it); } + pointer operator->() const { + return reinterpret_cast(std::addressof(*m_sparse_array_it)); + } sparse_iterator &operator++() { tsl_sh_assert(m_sparse_array_it != nullptr); diff --git a/include/tsl/sparse_map.h b/include/tsl/sparse_map.h index 601742d..1fbe622 100644 --- a/include/tsl/sparse_map.h +++ b/include/tsl/sparse_map.h @@ -103,6 +103,15 @@ class sparse_map { key_type &operator()(std::pair &key_value) noexcept { return key_value.first; } + + const key_type &operator()( + const std::pair &key_value) const noexcept { + return key_value.first; + } + + const key_type &operator()(std::pair &key_value) noexcept { + return key_value.first; + } }; class ValueSelect { @@ -121,7 +130,8 @@ class sparse_map { using ht = detail_sparse_hash::sparse_hash< std::pair, KeySelect, ValueSelect, Hash, KeyEqual, Allocator, - GrowthPolicy, ExceptionSafety, Sparsity, tsl::sh::probing::quadratic>; + GrowthPolicy, ExceptionSafety, Sparsity, tsl::sh::probing::quadratic, + std::pair>; public: using key_type = typename ht::key_type; diff --git a/include/tsl/sparse_set.h b/include/tsl/sparse_set.h index 3ce6a58..d313723 100644 --- a/include/tsl/sparse_set.h +++ b/include/tsl/sparse_set.h @@ -100,10 +100,9 @@ class sparse_set { key_type &operator()(Key &key) noexcept { return key; } }; - using ht = - detail_sparse_hash::sparse_hash; + using ht = detail_sparse_hash::sparse_hash< + Key, KeySelect, void, Hash, KeyEqual, Allocator, GrowthPolicy, + ExceptionSafety, Sparsity, tsl::sh::probing::quadratic, Key>; public: using key_type = typename ht::key_type;