SolidUtils
FixedMap.hpp
Go to the documentation of this file.
1 
29 #ifndef SOLIDUTILS_INCLUDE_FIXEDMAP_HPP
30 #define SOLIDUTILS_INCLUDE_FIXEDMAP_HPP
31 
32 
33 #include "Array.hpp"
34 #include "ConstArray.hpp"
35 
36 namespace sl
37 {
38 
49 template <typename K, typename V>
50 class FixedMap
51 {
52  public:
53  static constexpr K const NULL_INDEX = static_cast<K>(-1);
54 
61  size_t const size) :
62  m_size(0),
63  m_keys(size),
64  m_values(size),
65  m_index(size, NULL_INDEX)
66  {
67  // do nothing
68  }
69 
70 
78  bool has(
79  K const key) const noexcept
80  {
81  size_t const index = static_cast<size_t>(key);
82 
83  ASSERT_LESS(index, m_index.size());
84 
85  return m_index[index] != NULL_INDEX;
86  }
87 
88 
96  V get(
97  K const key) const noexcept
98  {
99  size_t const index = static_cast<size_t>(key);
100 
101  ASSERT_LESS(index, m_index.size());
102 
103  return m_values[m_index[index]];
104  }
105 
112  void add(
113  K const key,
114  V const value) noexcept
115  {
116  size_t const index = static_cast<size_t>(key);
117 
118  ASSERT_LESS(index, m_index.size());
119  ASSERT_EQUAL(m_index[index], NULL_INDEX);
120 
121  m_keys[m_size] = key;
122  m_values[m_size] = value;
123 
124  m_index[index] = m_size;
125 
126  ++m_size;
127  }
128 
129 
135  void remove(
136  K const key) noexcept
137  {
138  size_t const index = static_cast<size_t>(key);
139 
140  ASSERT_LESS(index, m_index.size());
141  ASSERT_NOTEQUAL(m_index[index], NULL_INDEX);
142 
143  --m_size;
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;
151  }
152 
153 
159  size_t size() const noexcept
160  {
161  return m_size;
162  }
163 
164 
170  size_t maxSize() const noexcept
171  {
172  return m_keys.size();
173  }
174 
175 
181  ConstArray<K> keys() const noexcept
182  {
183  ASSERT_LESS(m_size, m_keys.size());
184  return ConstArray<K>(m_keys.data(), m_size);
185  }
186 
192  ConstArray<V> values() const noexcept
193  {
194  ASSERT_LESS(m_size, m_values.size());
195  return ConstArray<V>(m_values.data(), m_size);
196  }
197 
198 
199  private:
200  size_t m_size;
201  Array<K> m_keys;
202  Array<V> m_values;
203  Array<K> m_index;
204 };
205 
206 }
207 
208 
209 #endif
size_t maxSize() const noexcept
Get the maximum size of this map.
Definition: FixedMap.hpp:170
Definition: Alloc.hpp:40
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