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{
35 static_assert(soa_names_unique<TYPES...>::value, "NamedSlot names must be unique within an ArrayAllocator");
36
37public:
40
43
46
49
52
55
57 uint32_t Alloc();
58
60 void EraseIndex(const uint32_t id);
62 void EraseIndexSwap(const uint32_t id);
64 void EraseRange(const uint32_t start, const uint32_t end);
65
67 template <int MEMBER>
68 tuple_array_t<MEMBER, TYPES...>& Get(const uint32_t index);
69
71 template <int MEMBER>
72 const tuple_array_t<MEMBER, TYPES...>& ConstGet(const uint32_t index) const;
73
75 template <int MEMBER>
76 void Set(const uint32_t index, const tuple_array_t<MEMBER, TYPES...>& type);
77
79 template <int MEMBER>
80 const Util::Array<tuple_array_t<MEMBER, TYPES...>>& GetArray() const;
81
83 template <int MEMBER>
84 Util::Array<tuple_array_t<MEMBER, TYPES...>>& GetArray();
85
87 void Set(const uint32_t index, TYPES...);
88
90 const uint32_t Size() const;
91
93 void Reserve(uint32_t size);
94
96 void SetSize(uint32_t size);
97
99 void Clear();
100
104
105protected:
106 uint32_t size;
107 std::tuple<Util::soa_storage_t<TYPES>...> objects;
108};
109
110//------------------------------------------------------------------------------
113template<class ... TYPES>
115 size(0)
116{
117 // empty
118}
119
120//------------------------------------------------------------------------------
123template<class ...TYPES>
125{
126 this->objects = rhs.objects;
127 this->size = rhs.size;
128 rhs.Clear();
129}
130
131//------------------------------------------------------------------------------
134template<class ...TYPES>
136{
137 this->objects = rhs.objects;
138 this->size = rhs.size;
139}
140
141//------------------------------------------------------------------------------
144template<class ...TYPES>
146{
147 // empty
148}
149
150//------------------------------------------------------------------------------
153template<class ...TYPES>
154inline void
156{
157 this->objects = rhs.objects;
158 this->size = rhs.size;
159}
160
161//------------------------------------------------------------------------------
164template<class ...TYPES>
165inline void
167{
168 this->objects = rhs.objects;
169 this->size = rhs.size;
170 rhs.Clear();
171}
172
173//------------------------------------------------------------------------------
176template<class ...TYPES>
178{
180 return this->size++;
181}
182
183//------------------------------------------------------------------------------
186template<class ...TYPES> inline void
188{
190 this->size--;
191}
192
193//------------------------------------------------------------------------------
196template<class ...TYPES> inline void
198{
200 this->size--;
201}
202
203//------------------------------------------------------------------------------
206template<class ...TYPES> inline void
207ArrayAllocator<TYPES...>::EraseRange(const uint32_t start, const uint32_t end)
208{
209 erase_range_for_each_in_tuple(this->objects, start, end);
210 this->size -= (end + 1) - start;
211}
212
213//------------------------------------------------------------------------------
216template<class ...TYPES>
217inline const uint32_t
219{
220 return this->size;
221}
222
223//------------------------------------------------------------------------------
226template<class ...TYPES>
227inline void
229{
231 // Size is still the same.
232}
233
234//------------------------------------------------------------------------------
237template<class ...TYPES>
238inline void
240{
242 this->size = size;
243}
244
245//------------------------------------------------------------------------------
248template<class ...TYPES>
249inline void
255
256//------------------------------------------------------------------------------
259template<class ...TYPES>
260template<int MEMBER>
261inline tuple_array_t<MEMBER, TYPES...>&
262ArrayAllocator<TYPES...>::Get(const uint32_t index)
263{
264 return std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects)[index];
265}
266
267//------------------------------------------------------------------------------
270template<class ...TYPES>
271template<int MEMBER>
272inline const tuple_array_t<MEMBER, TYPES...>&
273ArrayAllocator<TYPES...>::ConstGet(const uint32_t index) const
274{
275 return std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects)[index];
276}
277
278//------------------------------------------------------------------------------
281template<class ...TYPES>
282template<int MEMBER>
283inline void
285{
286 std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects)[index] = type;
287}
288
289//------------------------------------------------------------------------------
292template<class ...TYPES>
293template<int MEMBER>
294inline const Util::Array<tuple_array_t<MEMBER, TYPES...>>&
296{
297 return std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects);
298}
299
300//------------------------------------------------------------------------------
303template<class ...TYPES>
304template<int MEMBER>
305inline Util::Array<tuple_array_t<MEMBER, TYPES...>>&
307{
308 return std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects);
309}
310
311//------------------------------------------------------------------------------
314template<class ...TYPES> void
315ArrayAllocator<TYPES...>::Set(const uint32_t index, TYPES... values)
316{
317 set_for_each_in_tuple(this->objects, index, values...);
318}
319
320//------------------------------------------------------------------------------
323template<class ...TYPES> void
325{
326 this->size = this->GetArray<0>().Size();
327}
328
329} // namespace Util
void EraseIndex(const uint32_t id)
Erase element for each.
Definition arrayallocator.h:187
void UpdateSize()
Any reserve and direct array access might mess with the size.
Definition arrayallocator.h:324
void operator=(const ArrayAllocator< TYPES... > &rhs)
assign operator
Definition arrayallocator.h:155
std::tuple< Util::soa_storage_t< TYPES >... > objects
Definition arrayallocator.h:107
void Reserve(uint32_t size)
grow capacity of arrays to size
Definition arrayallocator.h:228
const uint32_t Size() const
get number of used indices
Definition arrayallocator.h:218
void SetSize(uint32_t size)
set size of arrays to param size
Definition arrayallocator.h:239
~ArrayAllocator()
destructor
Definition arrayallocator.h:145
uint32_t size
Definition arrayallocator.h:106
const tuple_array_t< MEMBER, TYPES... > & ConstGet(const uint32_t index) const
same as 32 bit get, but const
Definition arrayallocator.h:273
tuple_array_t< MEMBER, TYPES... > & Get(const uint32_t index)
get single item from resource
Definition arrayallocator.h:262
void Set(const uint32_t index, const tuple_array_t< MEMBER, TYPES... > &type)
set single item
Definition arrayallocator.h:284
void Clear()
clear entire allocator and start from scratch.
Definition arrayallocator.h:250
ArrayAllocator()
constructor
Definition arrayallocator.h:114
void EraseRange(const uint32_t start, const uint32_t end)
erase range
Definition arrayallocator.h:207
void EraseIndexSwap(const uint32_t id)
Erase element for each.
Definition arrayallocator.h:197
uint32_t Alloc()
allocate a new resource
Definition arrayallocator.h:177
const Util::Array< tuple_array_t< MEMBER, TYPES... > > & GetArray() const
get array const reference
Definition arrayallocator.h:295
Nebula's dynamic array class.
Definition array.h:61
Set()
default constructor
Definition set.h:79
A quad tree designed to return regions of free 2D space.
Definition Random.cs:5
void erase_range_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t start, uint32_t end, std::index_sequence< Is... >)
Definition tupleutility.h:414
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:368
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:455
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:307
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:327
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:435
constexpr int soa_tuple_index_v
Definition tupleutility.h:167
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:392
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:475
soa_value_type_t< std::tuple_element_t< soa_tuple_index_v< MEMBER, TYPES... >, std::tuple< TYPES... > > > tuple_array_t
Get type of contained element in Util::Array stored in std::tuple.
Definition tupleutility.h:495
Definition tupleutility.h:121