Skip to content

Commit

Permalink
slot_map: Rename key_size_type to key_index_type.
Browse files Browse the repository at this point in the history
Merges part of WG21-SG14#145. Thanks to @p-groake for the patch!
  • Loading branch information
Quuxplusone committed May 19, 2019
1 parent 98591c4 commit 3acd437
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
18 changes: 9 additions & 9 deletions SG14/slot_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ class slot_map
#if __cplusplus >= 201703L
static constexpr auto get_index(const Key& k) { const auto& [idx, gen] = k; return idx; }
static constexpr auto get_generation(const Key& k) { const auto& [idx, gen] = k; return gen; }
template<class Integral> static constexpr void set_index(Key& k, Integral value) { auto& [idx, gen] = k; idx = static_cast<key_size_type>(value); }
template<class Integral> static constexpr void set_index(Key& k, Integral value) { auto& [idx, gen] = k; idx = static_cast<key_index_type>(value); }
static constexpr void increment_generation(Key& k) { auto& [idx, gen] = k; ++gen; }
#else
static constexpr auto get_index(const Key& k) { using std::get; return get<0>(k); }
static constexpr auto get_generation(const Key& k) { using std::get; return get<1>(k); }
template<class Integral> static constexpr void set_index(Key& k, Integral value) { using std::get; get<0>(k) = static_cast<key_size_type>(value); }
template<class Integral> static constexpr void set_index(Key& k, Integral value) { using std::get; get<0>(k) = static_cast<key_index_type>(value); }
static constexpr void increment_generation(Key& k) { using std::get; ++get<1>(k); }
#endif

Expand All @@ -84,7 +84,7 @@ class slot_map
using key_type = Key;
using mapped_type = T;

using key_size_type = decltype(slot_map::get_index(std::declval<Key>()));
using key_index_type = decltype(slot_map::get_index(std::declval<Key>()));
using key_generation_type = decltype(slot_map::get_generation(std::declval<Key>()));

using container_type = Container<mapped_type>;
Expand Down Expand Up @@ -223,7 +223,7 @@ class slot_map
slot_map_detail::reserve_if_possible(slots_, n);
while (slots_.size() < n) {
auto idx = next_available_slot_index_;
next_available_slot_index_ = static_cast<key_size_type>(slots_.size());
next_available_slot_index_ = static_cast<key_index_type>(slots_.size());
slots_.emplace_back(key_type{idx, key_generation_type{}});
}
}
Expand Down Expand Up @@ -292,7 +292,7 @@ class slot_map
slots_.clear();
values_.clear();
reverse_map_.clear();
next_available_slot_index_ = key_size_type{};
next_available_slot_index_ = key_index_type{};
}

// swap is not mentioned in P0661r1 but it should be.
Expand Down Expand Up @@ -328,21 +328,21 @@ class slot_map
*value_iter = std::move(*value_back_iter);
this->set_index(*slot_back_iter, value_index);
auto reverse_map_iter = std::next(reverse_map_.begin(), value_index);
*reverse_map_iter = static_cast<key_size_type>(std::distance(slots_.begin(), slot_back_iter));
*reverse_map_iter = static_cast<key_index_type>(std::distance(slots_.begin(), slot_back_iter));
}
values_.pop_back();
reverse_map_.pop_back();
// Expire this key.
this->set_index(*slot_iter, next_available_slot_index_);
this->increment_generation(*slot_iter);
next_available_slot_index_ = static_cast<key_size_type>(slot_index);
next_available_slot_index_ = static_cast<key_index_type>(slot_index);
return std::next(values_.begin(), value_index);
}

Container<key_type> slots_; // high_water_mark() entries
Container<key_size_type> reverse_map_; // exactly size() entries
Container<key_index_type> reverse_map_; // exactly size() entries
Container<mapped_type> values_; // exactly size() entries
key_size_type next_available_slot_index_{};
key_index_type next_available_slot_index_{};
};

template<class T, class Key, template<class...> class Container>
Expand Down
10 changes: 5 additions & 5 deletions SG14_test/slot_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ static void TypedefTests()
using SM = stdext::slot_map<int>;
static_assert(std::is_same<typename SM::key_type, std::pair<unsigned, unsigned>>::value, "");
static_assert(std::is_same<typename SM::mapped_type, int>::value, "");
static_assert(std::is_same<typename SM::key_size_type, unsigned>::value, "");
static_assert(std::is_same<typename SM::key_index_type, unsigned>::value, "");
static_assert(std::is_same<typename SM::key_generation_type, unsigned>::value, "");
static_assert(std::is_same<typename SM::container_type, std::vector<int>>::value, "");
static_assert(std::is_same<typename SM::reference, int&>::value, "");
Expand All @@ -356,7 +356,7 @@ static void TypedefTests()
using SM = stdext::slot_map<bool>;
static_assert(std::is_same<typename SM::key_type, std::pair<unsigned, unsigned>>::value, "");
static_assert(std::is_same<typename SM::mapped_type, bool>::value, "");
static_assert(std::is_same<typename SM::key_size_type, unsigned>::value, "");
static_assert(std::is_same<typename SM::key_index_type, unsigned>::value, "");
static_assert(std::is_same<typename SM::key_generation_type, unsigned>::value, "");
static_assert(std::is_same<typename SM::container_type, std::vector<bool>>::value, "");
static_assert(std::is_same<typename SM::reference, std::vector<bool>::reference>::value, "");
Expand All @@ -374,7 +374,7 @@ static void TypedefTests()
using SM = stdext::slot_map<double, TestKey::key_16_8_t>;
static_assert(std::is_same<typename SM::key_type, TestKey::key_16_8_t>::value, "");
static_assert(std::is_same<typename SM::mapped_type, double>::value, "");
static_assert(std::is_same<typename SM::key_size_type, uint16_t>::value, "");
static_assert(std::is_same<typename SM::key_index_type, uint16_t>::value, "");
static_assert(std::is_same<typename SM::key_generation_type, uint8_t>::value, "");
static_assert(std::is_same<typename SM::container_type, std::vector<double>>::value, "");
static_assert(std::is_same<typename SM::reference, double&>::value, "");
Expand All @@ -392,7 +392,7 @@ static void TypedefTests()
using SM = stdext::slot_map<int, std::pair<char, int>, TestContainer::Vector>;
static_assert(std::is_same<typename SM::key_type, std::pair<char, int>>::value, "");
static_assert(std::is_same<typename SM::mapped_type, int>::value, "");
static_assert(std::is_same<typename SM::key_size_type, char>::value, "");
static_assert(std::is_same<typename SM::key_index_type, char>::value, "");
static_assert(std::is_same<typename SM::key_generation_type, int>::value, "");
static_assert(std::is_same<typename SM::container_type, TestContainer::Vector<int>>::value, "");
static_assert(std::is_same<typename SM::reference, int&>::value, "");
Expand All @@ -411,7 +411,7 @@ static void TypedefTests()
using SM = stdext::slot_map<double, TestKey::key_11_5_t>;
static_assert(std::is_same<typename SM::key_type, TestKey::key_11_5_t>::value, "");
static_assert(std::is_same<typename SM::mapped_type, double>::value, "");
static_assert(std::is_same<typename SM::key_size_type, uint16_t>::value, "");
static_assert(std::is_same<typename SM::key_index_type, uint16_t>::value, "");
static_assert(std::is_same<typename SM::key_generation_type, uint8_t>::value, "");
static_assert(std::is_same<typename SM::container_type, std::vector<double>>::value, "");
static_assert(std::is_same<typename SM::reference, double&>::value, "");
Expand Down

0 comments on commit 3acd437

Please sign in to comment.