29 #ifndef SOLIDUTILS_INCLUDE_FIXEDMAP_HPP 30 #define SOLIDUTILS_INCLUDE_FIXEDMAP_HPP 49 template <
typename K,
typename V>
53 static constexpr K
const NULL_INDEX =
static_cast<K
>(-1);
65 m_index(size, NULL_INDEX)
79 K
const key)
const noexcept
81 size_t const index =
static_cast<size_t>(key);
83 ASSERT_LESS(index, m_index.
size());
85 return m_index[index] != NULL_INDEX;
97 K
const key)
const noexcept
99 size_t const index =
static_cast<size_t>(key);
101 ASSERT_LESS(index, m_index.
size());
103 return m_values[m_index[index]];
114 V
const value) noexcept
116 size_t const index =
static_cast<size_t>(key);
118 ASSERT_LESS(index, m_index.
size());
119 ASSERT_EQUAL(m_index[index], NULL_INDEX);
121 m_keys[m_size] = key;
122 m_values[m_size] = value;
124 m_index[index] = m_size;
136 K
const key) noexcept
138 size_t const index =
static_cast<size_t>(key);
140 ASSERT_LESS(index, m_index.
size());
141 ASSERT_NOTEQUAL(m_index[index], NULL_INDEX);
144 K
const swapKey = m_keys[m_size];
145 V
const swapValue = m_values[m_size];
146 size_t const place = m_index[index];
147 m_keys[place] = swapKey;
148 m_values[place] = swapValue;
149 m_index[
static_cast<size_t>(swapKey)] = place;
150 m_index[index] = NULL_INDEX;
172 return m_keys.
size();
183 ASSERT_LESS(m_size, m_keys.
size());
194 ASSERT_LESS(m_size, m_values.
size());
size_t maxSize() const noexcept
Get the maximum size of this map.
Definition: FixedMap.hpp:170
T * data() noexcept
Get the underlying memory.
Definition: Array.hpp:201
The ConstArray class provides functionality similar to std::vector, except that it does not construct...
Definition: ConstArray.hpp:54
A constant array structure for storing self-allocated or externally allocated memory.
void add(K const key, V const value) noexcept
Add an key-value pair to this set.
Definition: FixedMap.hpp:112
size_t size() const noexcept
Get the size of the underlying memory allocation.
Definition: Array.hpp:223
ConstArray< V > values() const noexcept
Get the values in this map.
Definition: FixedMap.hpp:192
The FixedMap class provides a map implementation which allows for insertion, querying, and deletion in constant time. While std::unordered_set may give constant time complexity of these operations through a hash table, this does so through fixed size dense vector (no hashing), and store the data in a contiguous chunk of memory.
Definition: FixedMap.hpp:50
bool has(K const key) const noexcept
Check if an key exists in this set.
Definition: FixedMap.hpp:78
size_t size() const noexcept
Get the number of elements in the set.
Definition: FixedMap.hpp:159
A mutable array structure for storing self-allocated memory.
FixedMap(size_t const size)
Create a new empty fixed set.
Definition: FixedMap.hpp:60
ConstArray< K > keys() const noexcept
Get the keys in this map.
Definition: FixedMap.hpp:181