17 const char*
what() const noexcept
override {
return msg_.c_str(); }
28template <Un
signedIntegral Key,
typename Value>
31 std::vector<Key> sparse;
32 std::vector<Key> dense;
33 std::vector<Value> values;
42 throw std::length_error(
"Key exceeds the maximum size limit.");
45 if (
key >= sparse.size())
48 sparse.resize(
newSize, std::numeric_limits<Key>::max());
57 if (sparse[
key] == std::numeric_limits<Key>::max())
59 sparse[
key] =
static_cast<Key>(values.size());
61 values.push_back(value);
64 values[sparse[
key]] = value;
73 if (sparse[
key] == std::numeric_limits<Key>::max())
75 sparse[
key] =
static_cast<Key>(values.size());
77 values.push_back(std::move(value));
80 values[sparse[
key]] = std::move(value);
91 return values[sparse[
key]];
101 return values[sparse[
key]];
125 sparse[
key] = std::numeric_limits<Key>::max();
130 template <
typename Func>
133 for (
size_t i = 0;
i < values.size(); ++
i)
135 f(dense[
i], values[
i]);
141 return key < sparse.size() && sparse[
key] != std::numeric_limits<Key>::max();
144 constexpr size_t capacity()
const {
return sparse.size(); }
146 constexpr size_t size()
const {
return dense.size(); }
148 inline const std::vector<Key>&
getKeys()
const {
return dense; }
150 inline std::vector<Value>&
getValues() {
return values; }
152 inline const std::vector<Value>&
getValues()
const {
return values; }
156 constexpr size_t maxKeyVal =
static_cast<size_t>(std::numeric_limits<Key>::max());
157 return std::min({
maxKeyVal, dense.max_size(), values.max_size()});
Definition sparse_set.hpp:13
KeyNotFoundException(const std::string &key)
Definition sparse_set.hpp:15
const char * what() const noexcept override
Definition sparse_set.hpp:17
Definition sparse_set.hpp:29
const Value & operator[](const Key key) const
Definition sparse_set.hpp:104
constexpr size_t maxSize() const noexcept
Definition sparse_set.hpp:154
void forEach(Func &&f)
Definition sparse_set.hpp:131
void remove(const Key key)
Definition sparse_set.hpp:108
void set(const Key key, Value &&value)
Definition sparse_set.hpp:69
std::vector< Value > & getValues()
Definition sparse_set.hpp:150
void accommodate(const Key key)
Definition sparse_set.hpp:37
Value & operator[](const Key key)
Definition sparse_set.hpp:105
const std::vector< Key > & getKeys() const
Definition sparse_set.hpp:148
constexpr size_t size() const
Definition sparse_set.hpp:146
void set(const Key key, const Value &value)
Definition sparse_set.hpp:53
constexpr size_t capacity() const
Definition sparse_set.hpp:144
const std::vector< Value > & getValues() const
Definition sparse_set.hpp:152
Value & get(const Key key)
Definition sparse_set.hpp:95
constexpr bool contains(const Key key) const
Definition sparse_set.hpp:139
const Value & get(const Key key) const
Definition sparse_set.hpp:85
void clear()
Definition sparse_set.hpp:160
Definition sparse_set.hpp:26