Nebula
Loading...
Searching...
No Matches
fixedpool.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/types.h"
13#include "util/array.h"
14#include <functional>
15
16//------------------------------------------------------------------------------
17namespace Util
18{
19template<class TYPE> class FixedPool
20{
21public:
23 typedef TYPE* Iterator;
24
28 FixedPool(SizeT s, std::function<void(TYPE& val, IndexT idx)> setupFunc);
31
33 void operator=(const FixedArray<TYPE>& rhs);
35 TYPE& operator[](const IndexT elem);
36
38 TYPE& Alloc();
40 void Free(const TYPE& elem);
42 void Free(const Iterator iter);
43
45 SizeT Size() const;
47 SizeT NumFree() const;
49 SizeT NumUsed() const;
51 void Resize(SizeT newSize);
53 bool IsFull() const;
55 bool IsEmpty() const;
57 void Clear();
58
60 void Reset();
61
63 void SetSetupFunc(const std::function<void(TYPE& val, IndexT idx)>& func);
64
67
68private:
69 std::function<void(TYPE& val, IndexT idx)> setupFunc;
73};
74
75//------------------------------------------------------------------------------
78template<class TYPE>
79inline
81 size(0)
82{
83 // empty
84}
85
86//------------------------------------------------------------------------------
89template<class TYPE>
90inline
92{
93 this->freeValues.Clear();
94 this->usedValues.Clear();
95 this->setupFunc = nullptr;
96}
97
98//------------------------------------------------------------------------------
101template<class TYPE>
102inline
103Util::FixedPool<TYPE>::FixedPool(SizeT s, std::function<void(TYPE& val, IndexT idx)> setupFunc) :
104 size(s)
105{
106 this->freeValues.Reserve(s);
107 this->usedValues.Reserve(s);
108 this->setupFunc = setupFunc;
109
110 IndexT i;
111 for (i = 0; i < this->size; i++)
112 {
113 this->freeValues.Append(TYPE());
114 if (this->setupFunc != nullptr) this->setupFunc(this->freeValues[i], i);
115 }
116}
117
118//------------------------------------------------------------------------------
121template<class TYPE>
122inline void
124{
125 this->freeValues = rhs.freeValues;
126 this->usedValues = rhs.usedValues;
127}
128
129//------------------------------------------------------------------------------
132template<class TYPE>
133TYPE&
135{
136#if NEBULA_BOUNDSCHECKS
137 n_assert(this->usedValues.Size() > elem);
138#endif
139 return this->usedValues[elem];
140}
141
142//------------------------------------------------------------------------------
145template<class TYPE>
146inline TYPE&
148{
149 n_assert(!this->freeValues.IsEmpty());
150 TYPE elem = this->freeValues.PopFront();
151 this->usedValues.Append(elem);
152 return this->usedValues.Back();
153}
154
155//------------------------------------------------------------------------------
158template<class TYPE>
159inline void
161{
162 IndexT idx = this->usedValues.FindIndex(elem);
163 n_assert(idx != InvalidIndex);
164 this->usedValues.EraseIndex(idx);
165 this->freeValues.Append(elem);
166}
167
168//------------------------------------------------------------------------------
171template<class TYPE>
172inline void
174{
175 this->usedValues.Erase(iter);
176 this->freeValues.Append(&iter);
177}
178
179//------------------------------------------------------------------------------
182template<class TYPE>
183inline SizeT
185{
186 return this->size;
187}
188
189//------------------------------------------------------------------------------
192template<class TYPE>
193inline SizeT
195{
196 return this->freeValues.Size();
197}
198
199//------------------------------------------------------------------------------
202template<class TYPE>
203inline SizeT
205{
206 return this->usedValues.Size();
207}
208
209//------------------------------------------------------------------------------
212template<class TYPE>
213inline void
215{
216 this->size = newSize;
217 this->usedValues.Clear();
218 this->freeValues.Clear();
219 this->freeValues.Reserve(newSize);
220
221 IndexT i;
222 for (i = 0; i < this->size; i++)
223 {
224 this->freeValues.Append(TYPE());
225 if (this->setupFunc != nullptr) this->setupFunc(this->freeValues[i], i);
226 }
227}
228
229//------------------------------------------------------------------------------
232template<class TYPE>
233inline bool
235{
236 return this->freeValues.IsEmpty();
237}
238
239//------------------------------------------------------------------------------
242template<class TYPE>
243inline bool
245{
246 return this->usedValues.IsEmpty();
247}
248
249//------------------------------------------------------------------------------
252template<class TYPE>
253inline void
255{
256 this->usedValues.Clear();
257 this->freeValues.Clear();
258 this->freeValues.Reserve(this->size);
259
260 IndexT i;
261 for (i = 0; i < this->size; i++)
262 {
263 this->freeValues.Append(TYPE());
264 if (this->setupFunc != nullptr) this->setupFunc(this->freeValues[i], i);
265 }
266}
267
268//------------------------------------------------------------------------------
271template<class TYPE>
272inline void
274{
275 IndexT i;
276 for (i = 0; i < this->usedValues.Size(); i++)
277 {
278 this->freeValues.Append(this->usedValues[i]);
279 }
280 this->usedValues.Clear();
281}
282
283//------------------------------------------------------------------------------
286template<class TYPE>
287inline void
288Util::FixedPool<TYPE>::SetSetupFunc(const std::function<void(TYPE& val, IndexT idx)>& func)
289{
290 this->setupFunc = func;
291}
292
293//------------------------------------------------------------------------------
296template<class TYPE>
297inline const Util::Array<TYPE>&
302
303}
Nebula's dynamic array class.
Definition array.h:60
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
void operator=(const FixedArray< TYPE > &rhs)
assignment operator
Definition fixedpool.h:123
Util::Array< TYPE > freeValues
Definition fixedpool.h:71
void Free(const TYPE &elem)
free element allocated from pool
Definition fixedpool.h:160
TYPE & operator[](const IndexT elem)
access operator
Definition fixedpool.h:134
FixedPool(SizeT s, std::function< void(TYPE &val, IndexT idx)> setupFunc)
constructor with fixed size
Definition fixedpool.h:103
std::function< void(uint32_t &val, IndexT idx)> setupFunc
Definition fixedpool.h:69
bool IsEmpty() const
returns true if the pool is empty
Definition fixedpool.h:244
const Util::Array< TYPE > & GetAllocated()
get allocated values as array
Definition fixedpool.h:298
Util::Array< TYPE > usedValues
Definition fixedpool.h:72
FixedPool()
default constructor
Definition fixedpool.h:80
~FixedPool()
destructor
Definition fixedpool.h:91
TYPE & Alloc()
allocate an element in the pool
Definition fixedpool.h:147
SizeT NumUsed() const
get number of used elements
Definition fixedpool.h:204
void Clear()
clear all pool values
Definition fixedpool.h:254
SizeT Size() const
get number of elements
Definition fixedpool.h:184
void Free(const Iterator iter)
free element using iterator
Definition fixedpool.h:173
void SetSetupFunc(const std::function< void(TYPE &val, IndexT idx)> &func)
set optional setup value
Definition fixedpool.h:288
void Reset()
resets all used indices without clearing contents
Definition fixedpool.h:273
SizeT size
Definition fixedpool.h:70
void Resize(SizeT newSize)
reset pool and resize pool
Definition fixedpool.h:214
SizeT NumFree() const
get number of free elements
Definition fixedpool.h:194
bool IsFull() const
returns true if no more free values are available
Definition fixedpool.h:234
TYPE * Iterator
define iterator
Definition fixedpool.h:23
#define n_assert(exp)
Definition debug.h:50
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
static const int InvalidIndex
Definition types.h:54
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48