-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse_array
262 lines (234 loc) · 6.54 KB
/
sparse_array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* sparse_array
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Made by: Mattis DALLEAU
*/
#pragma once
#include <vector>
#include <optional>
#include <cstdint>
namespace hl
{
namespace silva
{
template <typename T>
class sparse_array
{
public:
using value_type = std::optional<T>;
using reference_type = value_type &;
using const_reference_type = value_type const &;
using container_t = std::vector<value_type>;
using size_type = std::size_t;
using iterator = typename container_t::iterator;
using const_iterator = typename container_t::const_iterator;
/**
* @brief Construct a new sparse array object
* @param size: The size of the array
* @retval None
*/
sparse_array(const size_type& default_size=20)
{
// assert(default_size > 0);
_data.assign(default_size, std::nullopt);
}
/**
* @brief Destroy the sparse array object
*/
~sparse_array() = default;
/**
* @brief Copy constructor
* @param other: The other sparse array to copy
* @retval None
*/
sparse_array(const sparse_array &other) = default;
/**
* @brief Move constructor
* @param other: The other sparse array to move
* @retval None
*/
sparse_array(sparse_array &&other) = default;
/**
* @brief Copy assignment operator
* @param other: The other sparse array to copy
* @retval The sparse array
*/
sparse_array &operator=(const sparse_array &other) = default;
/**
* @brief Move assignment operator
* @param other: The other sparse array to move
* @retval The sparse array
*/
sparse_array &operator=(sparse_array &&other) = default;
/**
* @brief Get the index of the component
* @retval The index of the component
*/
reference_type operator[](size_type idx)
{
return _data[idx];
}
/**
* @brief Get the index of the component
* @retval The index of the component
*/
const_reference_type operator[](size_type idx) const
{
return _data[idx];
}
/**
* @brief Get the begin of the array
* @retval The begin of the array
*/
iterator begin()
{
return _data.begin();
}
/**
* @brief Get the begin of the array
* @retval The begin of the array
*/
const_iterator begin() const
{
return _data.begin();
}
/**
* @brief Get the begin of the array
* @retval The begin of the array
*/
const_iterator cbegin() const
{
return _data.cbegin();
}
/**
* @brief Get the end of the array
* @retval The end of the array
*/
iterator end()
{
return _data.end();
}
/**
* @brief Get the end of the array
* @retval The end of the array
*/
const_iterator end() const
{
return _data.end();
}
/**
* @brief Get the end of the array
* @retval The end of the array
*/
const_iterator cend() const
{
return _data.cend();
}
/**
* @brief Get the size of the array
* @retval The size of the array
*/
size_type size() const
{
return _data.size();
}
/**
* @brief Add a new element to the array and put null in it
* @retval None
*/
void ensure_space(size_type pos)
{
for (; _data.size() <= pos; _data.push_back(std::nullopt))
;
}
/**
* @brief Add an element from the array
* @param pos: The position of the element to remove
* @param comp: Reference to the component to add
* @retval reference to the component
*/
reference_type insert(size_type pos, T const &comp)
{
ensure_space(pos);
return _data[pos] = comp;
}
/**
* @brief Add an element from the array
* @param pos: The position of the element to remove
* @param comp: Reference to the component to add
* @retval reference to the component
*/
reference_type insert(size_type pos, T &&comp)
{
ensure_space(pos);
return _data[pos] = std::move(comp);
}
/**
* @brief Generate a component for the specified index
* @param pos: The position of the element to set
* @param &&...params: optional
* @retval reference to the component
*/
template <class... Params>
reference_type emplace_at(size_type pos, Params &&...params)
{
T c = Component(std::forward<Params>(params)...);
return insert_at(pos, std::move(c));
}
/**
* @brief Remove an element from the array
* @param pos: The position of the element to remove
* @retval None
*/
void erase(size_type pos)
{
if (pos >= _data.size())
return;
_data[pos] = std::nullopt;
}
/**
* @brief Return the index of the value
* @param &val: Reference to the value
* @retval The index of the value
*/
size_type get_index(value_type const &val) const
{
return (&val >= _data.data() && &val <= _data.data() + _data.size()) ?
((reinterpret_cast<char *>(&val) -
reinterpret_cast<char *>(_data.data())) /
sizeof(value_type)) :
static_cast<size_type>(-1);
}
/**
* @brief Return true if the array is empty
* @param pos: position of the element
* @retval true if the array is empty
*/
bool non_null(size_type pos) const
{
if (pos >= _data.size())
return false;
return _data[pos] != std::nullopt;
}
private:
container_t _data;
};
}
}