Skip to content

fix const reference return by iterator #41

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 4 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
32 changes: 25 additions & 7 deletions include/tsl/ordered_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class bucket_entry {
*/
template <class ValueType, class KeySelect, class ValueSelect, class Hash,
class KeyEqual, class Allocator, class ValueTypeContainer,
class IndexType>
class IndexType, class ValueTypeIt>
class ordered_hash : private Hash, private KeyEqual {
private:
template <typename U>
Expand Down Expand Up @@ -334,6 +334,12 @@ class ordered_hash : private Hash, private KeyEqual {

using values_container_type = ValueTypeContainer;

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*;

public:
template <bool IsConst>
class ordered_iterator {
Expand All @@ -348,10 +354,16 @@ class ordered_hash : private Hash, private KeyEqual {

public:
using iterator_category = std::random_access_iterator_tag;
using value_type = const typename ordered_hash::value_type;
using value_type = typename ordered_hash::value_type_it;
using difference_type = typename iterator::difference_type;
using reference = value_type&;
using pointer = value_type*;
using reference =
typename std::conditional<IsConst,
typename ordered_hash::const_reference_it,
typename ordered_hash::reference_it>::type;
using pointer =
typename std::conditional<IsConst,
typename ordered_hash::const_pointer_it,
typename ordered_hash::pointer_it>::type;

ordered_iterator() noexcept {}

Expand Down Expand Up @@ -384,8 +396,12 @@ class ordered_hash : private Hash, private KeyEqual {
return U()(*m_iterator);
}

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

ordered_iterator& operator++() {
++m_iterator;
Expand All @@ -407,7 +423,9 @@ class ordered_hash : private Hash, private KeyEqual {
return tmp;
}

reference operator[](difference_type n) const { return m_iterator[n]; }
reference operator[](difference_type n) const {
return reinterpret_cast<reference>(m_iterator[n]);
}

ordered_iterator& operator+=(difference_type n) {
m_iterator += n;
Expand Down
23 changes: 18 additions & 5 deletions include/tsl/ordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ class ordered_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 @@ -111,10 +120,9 @@ class ordered_map {
}
};

using ht =
detail_ordered_hash::ordered_hash<std::pair<Key, T>, KeySelect,
ValueSelect, Hash, KeyEqual, Allocator,
ValueTypeContainer, IndexType>;
using ht = detail_ordered_hash::ordered_hash<
std::pair<Key, T>, KeySelect, ValueSelect, Hash, KeyEqual, Allocator,
ValueTypeContainer, IndexType, std::pair<const Key, T>>;

public:
using key_type = typename ht::key_type;
Expand Down Expand Up @@ -409,7 +417,7 @@ class ordered_map {
* It can still be cleared and destroyed without leaking memory.
*/
template <class Predicate>
friend size_type erase_if(ordered_map &map, Predicate pred) {
friend size_type erase_if(ordered_map& map, Predicate pred) {
return map.m_ht.erase_if(pred);
}

Expand Down Expand Up @@ -991,6 +999,11 @@ class ordered_map {
ht m_ht;
};

template <class Key, class T, class Hash = std::hash<Key>>
using vector_map = tsl::ordered_map<Key, T, std::hash<Key>, std::equal_to<Key>,
std::allocator<std::pair<Key, T>>,
std::vector<std::pair<Key, T>>>;

} // end namespace tsl

#endif
13 changes: 9 additions & 4 deletions include/tsl/ordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ class ordered_set {
key_type& operator()(Key& key) noexcept { return key; }
};

using ht = detail_ordered_hash::ordered_hash<Key, KeySelect, void, Hash,
KeyEqual, Allocator,
ValueTypeContainer, IndexType>;
using ht =
detail_ordered_hash::ordered_hash<Key, KeySelect, void, Hash, KeyEqual,
Allocator, ValueTypeContainer,
IndexType, Key>;

public:
using key_type = typename ht::key_type;
Expand Down Expand Up @@ -334,7 +335,7 @@ class ordered_set {
* It can still be cleared and destroyed without leaking memory.
*/
template <class Predicate>
friend size_type erase_if(ordered_set &set, Predicate pred) {
friend size_type erase_if(ordered_set& set, Predicate pred) {
return set.m_ht.erase_if(pred);
}

Expand Down Expand Up @@ -826,6 +827,10 @@ class ordered_set {
ht m_ht;
};

template <class Key, class Hash = std::hash<Key>>
using vector_set = tsl::ordered_set<Key, std::hash<Key>, std::equal_to<Key>,
std::allocator<Key>, std::vector<Key>>;

} // end namespace tsl

#endif
18 changes: 9 additions & 9 deletions tests/ordered_map_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ BOOST_AUTO_TEST_CASE(test_insert_at_position) {
{"Key12", 12}}));

auto it = map.insert_at_position(map.nth(2), {"Key3", 3});
BOOST_CHECK(*it.first == (std::pair<std::string, int>("Key3", 3)));
BOOST_CHECK(*it.first == (std::pair<const std::string, int>("Key3", 3)));
BOOST_CHECK(it.second);
BOOST_CHECK(utils::test_is_equal(
map, tsl::ordered_map<std::string, int>{{"Key1", 1},
Expand Down Expand Up @@ -413,7 +413,7 @@ BOOST_AUTO_TEST_CASE(test_insert_at_position) {
{"Key7", 7}}));

it = map.insert_at_position(map.nth(3), {"Key8", 8});
BOOST_CHECK(*it.first == (std::pair<std::string, int>("Key8", 8)));
BOOST_CHECK(*it.first == (std::pair<const std::string, int>("Key8", 8)));
BOOST_CHECK(!it.second);
BOOST_CHECK(utils::test_is_equal(
map, tsl::ordered_map<std::string, int>{{"Key1", 1},
Expand All @@ -436,7 +436,7 @@ BOOST_AUTO_TEST_CASE(test_insert_at_position_high_collisions) {
map.insert({{0, 0}, {32, -32}, {64, -64}, {96, -96}, {128, -128}});

auto it = map.insert_at_position(map.begin(), {160, -160});
BOOST_CHECK(*it.first == (std::pair<int, int>(160, -160)));
BOOST_CHECK(*it.first == (std::pair<const int, int>(160, -160)));
BOOST_CHECK(it.second);
BOOST_CHECK(utils::test_is_equal(
map,
Expand Down Expand Up @@ -465,7 +465,7 @@ BOOST_AUTO_TEST_CASE(test_try_emplace_at_position) {
{"Key12", 12}}));

auto it = map.try_emplace_at_position(map.nth(2), "Key3", 3);
BOOST_CHECK(*it.first == (std::pair<std::string, int>("Key3", 3)));
BOOST_CHECK(*it.first == (std::pair<const std::string, int>("Key3", 3)));
BOOST_CHECK(it.second);
BOOST_CHECK(utils::test_is_equal(
map, tsl::ordered_map<std::string, int>{{"Key1", 1},
Expand Down Expand Up @@ -509,7 +509,7 @@ BOOST_AUTO_TEST_CASE(test_try_emplace_at_position) {
{"Key7", 7}}));

it = map.try_emplace_at_position(map.nth(3), "Key8", 8);
BOOST_CHECK(*it.first == (std::pair<std::string, int>("Key8", 8)));
BOOST_CHECK(*it.first == (std::pair<const std::string, int>("Key8", 8)));
BOOST_CHECK(!it.second);
BOOST_CHECK(utils::test_is_equal(
map, tsl::ordered_map<std::string, int>{{"Key1", 1},
Expand Down Expand Up @@ -567,7 +567,7 @@ BOOST_AUTO_TEST_CASE(test_range_erase) {
if (i >= 10 && i < 220) {
continue;
}
BOOST_CHECK(*it == (std::pair<std::string, std::int64_t>(
BOOST_CHECK(*it == (std::pair<const std::string, std::int64_t>(
utils::get_key<std::string>(i),
utils::get_value<std::int64_t>(i))));
++it;
Expand Down Expand Up @@ -1457,13 +1457,13 @@ BOOST_AUTO_TEST_CASE(test_nth) {
map.insert({0, 0});

BOOST_REQUIRE(map.nth(0) != map.end());
BOOST_CHECK(*map.nth(0) == (std::pair<std::int64_t, std::int64_t>(1, 10)));
BOOST_CHECK(*map.nth(0) == (std::pair<const std::int64_t, std::int64_t>(1, 10)));

BOOST_REQUIRE(map.nth(1) != map.end());
BOOST_CHECK(*map.nth(1) == (std::pair<std::int64_t, std::int64_t>(2, 20)));
BOOST_CHECK(*map.nth(1) == (std::pair<const std::int64_t, std::int64_t>(2, 20)));

BOOST_REQUIRE(map.nth(2) != map.end());
BOOST_CHECK(*map.nth(2) == (std::pair<std::int64_t, std::int64_t>(0, 0)));
BOOST_CHECK(*map.nth(2) == (std::pair<const std::int64_t, std::int64_t>(0, 0)));

BOOST_REQUIRE(map.nth(3) == map.end());

Expand Down