SolidUtils
FixedSet.hpp
Go to the documentation of this file.
1 
30 #ifndef SOLIDUTILS_INCLUDE_FIXEDSET_HPP
31 #define SOLIDUTILS_INCLUDE_FIXEDSET_HPP
32 
33 
34 #include "Array.hpp"
35 
36 
37 namespace sl
38 {
39 
40 
49 template <typename T>
50 class FixedSet
51 {
52  public:
53  static constexpr T const NULL_INDEX = static_cast<T>(-1);
54 
61  size_t const size) :
62  m_size(0),
63  m_data(size),
64  m_index(size, NULL_INDEX)
65  {
66  // do nothing
67  }
68 
69 
77  bool has(
78  T const element) const noexcept
79  {
80  size_t const index = static_cast<size_t>(element);
81 
82  ASSERT_LESS(index, m_index.size());
83 
84  return m_index[index] != NULL_INDEX;
85  }
86 
92  void add(
93  T const element) noexcept
94  {
95  size_t const index = static_cast<size_t>(element);
96 
97  ASSERT_LESS(index, m_index.size());
98  ASSERT_EQUAL(m_index[index], NULL_INDEX);
99 
100  m_data[m_size] = element;
101  m_index[index] = m_size;
102 
103  ++m_size;
104  }
105 
106 
112  void remove(
113  T const element) noexcept
114  {
115  size_t const index = static_cast<size_t>(element);
116 
117  ASSERT_LESS(index, m_index.size());
118  ASSERT_NOTEQUAL(m_index[index], NULL_INDEX);
119 
120  --m_size;
121  T const swap = m_data[m_size];
122  size_t const place = m_index[index];
123  m_data[place] = swap;
124  m_index[swap] = place;
125  m_index[index] = NULL_INDEX;
126  }
127 
128 
134  T * data() noexcept
135  {
136  return m_data.data();
137  }
138 
139 
145  T const * data() const noexcept
146  {
147  return m_data.data();
148  }
149 
150 
156  size_t size() const noexcept
157  {
158  return m_size;
159  }
160 
161 
167  T const * begin() const noexcept
168  {
169  return m_data.begin();
170  }
171 
172 
178  T const * end() const noexcept
179  {
180  // we want to return the set's size, and not the array's.
181  return m_data.begin() + m_size;
182  }
183 
184 
190  T * begin() noexcept
191  {
192  return m_data.begin();
193  }
194 
195 
201  T * end() noexcept
202  {
203  // we want to return the set's size, and not the array's.
204  return m_data.begin() + m_size;
205  }
206 
207 
208  private:
209  size_t m_size;
210  Array<T> m_data;
211  Array<T> m_index;
212 };
213 
214 }
215 
216 #endif
T * end() noexcept
Get the end iterator (mutable).
Definition: FixedSet.hpp:201
T const * data() const noexcept
Get the underlying array.
Definition: FixedSet.hpp:145
The FixedSet class provides a set 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).
Definition: FixedSet.hpp:50
Definition: Alloc.hpp:40
T const * begin() const noexcept
Get the beginning iterator.
Definition: FixedSet.hpp:167
FixedSet(size_t const size)
Create a new empty fixed set.
Definition: FixedSet.hpp:60
T * data() noexcept
Get the underlying array.
Definition: FixedSet.hpp:134
size_t size() const noexcept
Get the number of elements in the set.
Definition: FixedSet.hpp:156
The Array class provides functionality similar to std::vector, except that it does not construct or d...
Definition: Array.hpp:53
bool has(T const element) const noexcept
Check if an element exists in this set.
Definition: FixedSet.hpp:77
T * begin() noexcept
Get the beginning iterator (mutable).
Definition: FixedSet.hpp:190
A mutable array structure for storing self-allocated memory.
T const * end() const noexcept
Get the end iterator.
Definition: FixedSet.hpp:178
void add(T const element) noexcept
Add an element to this set.
Definition: FixedSet.hpp:92