Nebula
Loading...
Searching...
No Matches
arrayallocator.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
24//------------------------------------------------------------------------------
25#include "core/types.h"
26#include "util/array.h"
27#include <tuple>
28#include "tupleutility.h"
29namespace Util
30{
31
32template <class ... TYPES>
34{
35public:
38
41
44
47
50
53
55 uint32_t Alloc();
56
58 void EraseIndex(const uint32_t id);
60 void EraseIndexSwap(const uint32_t id);
62 void EraseRange(const uint32_t start, const uint32_t end);
63
65 template <int MEMBER>
66 tuple_array_t<MEMBER, TYPES...>& Get(const uint32_t index);
67
69 template <int MEMBER>
70 const tuple_array_t<MEMBER, TYPES...>& ConstGet(const uint32_t index) const;
71
73 template <int MEMBER>
74 void Set(const uint32_t index, const tuple_array_t<MEMBER, TYPES...>& type);
75
77 template <int MEMBER>
78 const Util::Array<tuple_array_t<MEMBER, TYPES...>>& GetArray() const;
79
81 template <int MEMBER>
82 Util::Array<tuple_array_t<MEMBER, TYPES...>>& GetArray();
83
85 void Set(const uint32_t index, TYPES...);
86
88 const uint32_t Size() const;
89
91 void Reserve(uint32_t size);
92
94 void SetSize(uint32_t size);
95
97 void Clear();
98
102
103protected:
104 uint32_t size;
105 std::tuple<Util::Array<TYPES>...> objects;
106};
107
108//------------------------------------------------------------------------------
111template<class ... TYPES>
113 size(0)
114{
115 // empty
116}
117
118//------------------------------------------------------------------------------
121template<class ...TYPES>
123{
124 this->objects = rhs.objects;
125 this->size = rhs.size;
126 rhs.Clear();
127}
128
129//------------------------------------------------------------------------------
132template<class ...TYPES>
134{
135 this->objects = rhs.objects;
136 this->size = rhs.size;
137}
138
139//------------------------------------------------------------------------------
142template<class ...TYPES>
144{
145 // empty
146}
147
148//------------------------------------------------------------------------------
151template<class ...TYPES>
152inline void
154{
155 this->objects = rhs.objects;
156 this->size = rhs.size;
157}
158
159//------------------------------------------------------------------------------
162template<class ...TYPES>
163inline void
165{
166 this->objects = rhs.objects;
167 this->size = rhs.size;
168 rhs.Clear();
169}
170
171//------------------------------------------------------------------------------
174template<class ...TYPES>
176{
177 alloc_for_each_in_tuple(this->objects);
178 return this->size++;
179}
180
181//------------------------------------------------------------------------------
184template<class ...TYPES> inline void
186{
187 erase_index_for_each_in_tuple(this->objects, id);
188 this->size--;
189}
190
191//------------------------------------------------------------------------------
194template<class ...TYPES> inline void
196{
197 erase_index_swap_for_each_in_tuple(this->objects, id);
198 this->size--;
199}
200
201//------------------------------------------------------------------------------
204template<class ...TYPES> inline void
205ArrayAllocator<TYPES...>::EraseRange(const uint32_t start, const uint32_t end)
206{
207 erase_range_for_each_in_tuple(this->objects, start, end);
208 this->size -= (end + 1) - start;
209}
210
211//------------------------------------------------------------------------------
214template<class ...TYPES>
215inline const uint32_t
217{
218 return this->size;
219}
220
221//------------------------------------------------------------------------------
224template<class ...TYPES>
225inline void
227{
228 reserve_for_each_in_tuple(this->objects, num);
229 // Size is still the same.
230}
231
232//------------------------------------------------------------------------------
235template<class ...TYPES>
236inline void
238{
239 set_size_for_each_in_tuple(this->objects, size);
240 this->size = size;
241}
242
243//------------------------------------------------------------------------------
246template<class ...TYPES>
247inline void
249{
250 clear_for_each_in_tuple(this->objects);
251 this->size = 0;
252}
253
254//------------------------------------------------------------------------------
257template<class ...TYPES>
258template<int MEMBER>
259inline tuple_array_t<MEMBER, TYPES...>&
260ArrayAllocator<TYPES...>::Get(const uint32_t index)
261{
262 return std::get<MEMBER>(this->objects)[index];
263}
264
265//------------------------------------------------------------------------------
268template<class ...TYPES>
269template<int MEMBER>
270inline const tuple_array_t<MEMBER, TYPES...>&
271ArrayAllocator<TYPES...>::ConstGet(const uint32_t index) const
272{
273 return std::get<MEMBER>(this->objects)[index];
274}
275
276//------------------------------------------------------------------------------
279template<class ...TYPES>
280template<int MEMBER>
281inline void
283{
284 std::get<MEMBER>(this->objects)[index] = type;
285}
286
287//------------------------------------------------------------------------------
290template<class ...TYPES>
291template<int MEMBER>
292inline const Util::Array<tuple_array_t<MEMBER, TYPES...>>&
294{
295 return std::get<MEMBER>(this->objects);
296}
297
298//------------------------------------------------------------------------------
301template<class ...TYPES>
302template<int MEMBER>
303inline Util::Array<tuple_array_t<MEMBER, TYPES...>>&
305{
306 return std::get<MEMBER>(this->objects);
307}
308
309//------------------------------------------------------------------------------
312template<class ...TYPES> void
313ArrayAllocator<TYPES...>::Set(const uint32_t index, TYPES... values)
314{
315 set_for_each_in_tuple(this->objects, index, values...);
316}
317
318//------------------------------------------------------------------------------
321template<class ...TYPES> void
323{
324 this->size = this->GetArray<0>().Size();
325}
326
327} // namespace Util
The ArrayAllocator provides a variadic list of types which is to be contained in the allocator and fe...
Definition arrayallocator.h:34
void Set(const uint32_t index, TYPES...)
set for each in tuple
Definition arrayallocator.h:313
void EraseIndex(const uint32_t id)
Erase element for each.
Definition arrayallocator.h:185
void UpdateSize()
Any reserve and direct array access might mess with the size.
Definition arrayallocator.h:322
void operator=(const ArrayAllocator< TYPES... > &rhs)
assign operator
Definition arrayallocator.h:153
void Reserve(uint32_t size)
grow capacity of arrays to size
Definition arrayallocator.h:226
const uint32_t Size() const
get number of used indices
Definition arrayallocator.h:216
void SetSize(uint32_t size)
set size of arrays to param size
Definition arrayallocator.h:237
~ArrayAllocator()
destructor
Definition arrayallocator.h:143
void operator=(ArrayAllocator< TYPES... > &&rhs)
move operator
Definition arrayallocator.h:164
uint32_t size
Definition arrayallocator.h:104
ArrayAllocator(ArrayAllocator< TYPES... > &&rhs)
move constructor
Definition arrayallocator.h:122
const tuple_array_t< MEMBER, TYPES... > & ConstGet(const uint32_t index) const
same as 32 bit get, but const
Definition arrayallocator.h:271
std::tuple< Util::Array< TYPES >... > objects
Definition arrayallocator.h:105
tuple_array_t< MEMBER, TYPES... > & Get(const uint32_t index)
get single item from resource
Definition arrayallocator.h:260
void Set(const uint32_t index, const tuple_array_t< MEMBER, TYPES... > &type)
set single item
Definition arrayallocator.h:282
void Clear()
clear entire allocator and start from scratch.
Definition arrayallocator.h:248
ArrayAllocator()
constructor
Definition arrayallocator.h:112
Util::Array< tuple_array_t< MEMBER, TYPES... > > & GetArray()
get array
Definition arrayallocator.h:304
void EraseRange(const uint32_t start, const uint32_t end)
erase range
Definition arrayallocator.h:205
void EraseIndexSwap(const uint32_t id)
Erase element for each.
Definition arrayallocator.h:195
uint32_t Alloc()
allocate a new resource
Definition arrayallocator.h:175
ArrayAllocator(const ArrayAllocator< TYPES... > &rhs)
copy constructor
Definition arrayallocator.h:133
const Util::Array< tuple_array_t< MEMBER, TYPES... > > & GetArray() const
get array const reference
Definition arrayallocator.h:293
Nebula's dynamic array class.
Definition array.h:60
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
void erase_range_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t start, uint32_t end, std::index_sequence< Is... >)
Definition tupleutility.h:253
void erase_index_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t i, std::index_sequence< Is... >)
Entry point for erasing an element.
Definition tupleutility.h:207
void reserve_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t size, std::index_sequence< Is... >)
Entry point for reserving in each array.
Definition tupleutility.h:294
void alloc_for_each_in_tuple(std::tuple< Ts... > &tuple, std::index_sequence< Is... >)
Unpacks allocations for each member in a tuble.
Definition tupleutility.h:146
void clear_for_each_in_tuple(std::tuple< Ts... > &tuple, std::index_sequence< Is... >)
Unpacks allocations for each member in a tuple.
Definition tupleutility.h:166
void set_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t i, std::index_sequence< Is... >, TYPES const &... values)
Entry point for setting values in each array at an index.
Definition tupleutility.h:274
get_template_type_t< std::tuple_element_t< MEMBER, std::tuple< Util::Array< TYPES >... > > > tuple_array_t
Get type of contained element in Util::Array stored in std::tuple.
Definition tupleutility.h:334
void erase_index_swap_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t i, std::index_sequence< Is... >)
Entry point for erasing an element by swapping with the last and reducing size.
Definition tupleutility.h:231
void set_size_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t size, std::index_sequence< Is... >)
Entry point for reserving in each array.
Definition tupleutility.h:314