Skip to content

fix const value return by emplace iterator #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions include/tsl/sparse_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ class sparse_array {
template <class ValueType, class KeySelect, class ValueSelect, class Hash,
class KeyEqual, class Allocator, class GrowthPolicy,
tsl::sh::exception_safety ExceptionSafety, tsl::sh::sparsity Sparsity,
tsl::sh::probing Probing>
tsl::sh::probing Probing, class ValueTypeIt>
class sparse_hash : private Allocator,
private Hash,
private KeyEqual,
Expand Down Expand Up @@ -1027,6 +1027,12 @@ class sparse_hash : private Allocator,
using iterator = sparse_iterator<false>;
using const_iterator = sparse_iterator<true>;

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<ValueType, Allocator, Sparsity>;
Expand Down Expand Up @@ -1070,10 +1076,16 @@ 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_it;
using difference_type = std::ptrdiff_t;
using reference = value_type &;
using pointer = value_type *;
using reference =
typename std::conditional<IsConst,
typename sparse_hash::const_reference_it,
typename sparse_hash::reference_it>::type;
using pointer =
typename std::conditional<IsConst,
typename sparse_hash::const_pointer_it,
typename sparse_hash::pointer_it>::type;

sparse_iterator() noexcept {}

Expand Down Expand Up @@ -1107,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<reference>(*m_sparse_array_it);
}

pointer operator->() const { return std::addressof(*m_sparse_array_it); }
pointer operator->() const {
return reinterpret_cast<pointer>(std::addressof(*m_sparse_array_it));
}

sparse_iterator &operator++() {
tsl_sh_assert(m_sparse_array_it != nullptr);
Expand Down
12 changes: 11 additions & 1 deletion include/tsl/sparse_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class sparse_map {
key_type &operator()(std::pair<Key, T> &key_value) noexcept {
return key_value.first;
}

const key_type &operator()(
const std::pair<const Key, T> &key_value) const noexcept {
return key_value.first;
}

const key_type &operator()(std::pair<const Key, T> &key_value) noexcept {
return key_value.first;
}
};

class ValueSelect {
Expand All @@ -121,7 +130,8 @@ class sparse_map {

using ht = detail_sparse_hash::sparse_hash<
std::pair<Key, T>, KeySelect, ValueSelect, Hash, KeyEqual, Allocator,
GrowthPolicy, ExceptionSafety, Sparsity, tsl::sh::probing::quadratic>;
GrowthPolicy, ExceptionSafety, Sparsity, tsl::sh::probing::quadratic,
std::pair<const Key, T>>;

public:
using key_type = typename ht::key_type;
Expand Down
7 changes: 3 additions & 4 deletions include/tsl/sparse_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ class sparse_set {
key_type &operator()(Key &key) noexcept { return key; }
};

using ht =
detail_sparse_hash::sparse_hash<Key, KeySelect, void, Hash, KeyEqual,
Allocator, GrowthPolicy, ExceptionSafety,
Sparsity, tsl::sh::probing::quadratic>;
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;
Expand Down