Nebula
Loading...
Searching...
No Matches
idpool.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
19//------------------------------------------------------------------------------
20#include <cstdint>
21#include "core/types.h"
22#include "util/array.h"
23#include "math/scalar.h"
24#include <functional>
25
26namespace Ids
27{
28class IdPool
29{
30public:
32 IdPool();
34 IdPool(const uint max, const uint grow = 512);
36 ~IdPool();
37
39 uint Alloc();
41 void Dealloc(uint id);
43 void Clear();
45 void Reserve(uint numIds);
47 uint GetNumUsed() const;
49 uint GetNumFree() const;
51 void ForEachFree(const std::function<void(uint, uint)> fun, SizeT num);
53 void Move(uint lhs, uint rhs);
55 const uint GetGrow() const;
56private:
57
61};
62
63//------------------------------------------------------------------------------
66inline
68 maxId(0xFFFFFFFF), // int max
69 grow(512)
70{
71 // empty
72}
73
74//------------------------------------------------------------------------------
77inline
78IdPool::IdPool(const uint max, const uint grow) :
79 maxId(max),
80 grow(grow)
81{
82 // empty
83}
84
85//------------------------------------------------------------------------------
88inline
90{
91 // empty
92}
93
94//------------------------------------------------------------------------------
97inline uint
99{
100 // if we're out of ids, allocate more, but with a controlled grow (not log2 as per Array)
101 if (this->free.Size() == 0)
102 {
103 // calculate how many more indices we are allowed to get
104 SizeT growTo = Math::min(this->maxId, this->grow);
105 SizeT oldCapacity = this->free.Capacity();
106
107 // make sure we don't allocate too many indices
108 n_assert2((uint)(oldCapacity + growTo) < this->maxId, "Pool is full! Be careful with how much you allocate!\n");
109
110 // reserve more space for new Ids
111 this->free.Reserve(oldCapacity + growTo);
112 IndexT i;
113 for (i = this->free.Capacity()-1; i >= oldCapacity; i--)
114 {
115 // count backwards from the max id
116 this->free.Append(this->maxId - i);
117 }
118 }
119
120 // if we do an inverse erase, we don't have to move elements, and since we know the max id, subtract it
121 uint id = this->maxId - this->free.Back();
122 this->free.EraseIndex(this->free.Size() - 1);
123 return id;
124}
125
126//------------------------------------------------------------------------------
129inline void
131{
132 this->free.Append(this->maxId - id);
133}
134
135//------------------------------------------------------------------------------
138inline void
140{
141 this->free.Clear();
142 this->maxId = 0xFFFFFFFF;
143}
144
145//------------------------------------------------------------------------------
148inline void
150{
151 this->maxId = numIds;
152 this->free.Reserve(numIds);
153 for (int i = numIds - 1; i >= 0; i--)
154 {
155 this->free.Append(this->maxId - i);
156 }
157}
158
159//------------------------------------------------------------------------------
162inline uint
164{
165 return this->free.Capacity() - this->free.Size();
166}
167
168//------------------------------------------------------------------------------
171inline uint
173{
174 return this->free.Size();
175}
176
177//------------------------------------------------------------------------------
180inline void
181IdPool::ForEachFree(const std::function<void(uint, uint)> fun, SizeT num)
182{
183 SizeT size = this->free.Size();
184 for (IndexT i = size - 1; i >= 0; i--)
185 {
186 const uint id = this->maxId - this->free[i];
187 if (id < (uint)num)
188 {
189 fun(id, i);
190 num--;
191 }
192 }
193}
194
195//------------------------------------------------------------------------------
198inline void
200{
201 this->free.Append(this->maxId - id);
202 this->free.EraseIndex(idx);
203}
204
205//------------------------------------------------------------------------------
208inline const uint
210{
211 return this->grow;
212}
213
214} // namespace Ids
~IdPool()
destructor
Definition idpool.h:89
void Move(uint lhs, uint rhs)
frees up lhs and erases rhs
Definition idpool.h:199
Util::Array< uint > free
Definition idpool.h:58
IdPool()
constructor
Definition idpool.h:67
uint GetNumFree() const
get number of free elements
Definition idpool.h:172
const uint GetGrow() const
get grow
Definition idpool.h:209
uint maxId
Definition idpool.h:59
uint GetNumUsed() const
get number of active ids
Definition idpool.h:163
void ForEachFree(const std::function< void(uint, uint)> fun, SizeT num)
iterate free indices
Definition idpool.h:181
uint Alloc()
get new id
Definition idpool.h:98
uint grow
Definition idpool.h:60
void Reserve(uint numIds)
reserve ids
Definition idpool.h:149
void Dealloc(uint id)
free id
Definition idpool.h:130
void Clear()
Reset to no free ids and start counting anew.
Definition idpool.h:139
Nebula's dynamic array class.
Definition array.h:61
void Reserve(SizeT num)
increase capacity to fit N more elements into the array.
Definition array.h:916
TYPE & Back() const
return reference to last element
Definition array.h:1047
const SizeT Capacity() const
get overall allocated size of array in number of elements
Definition array.h:954
void Clear()
clear array (calls destructors)
Definition array.h:1288
void Append(const TYPE &first, const ELEM_TYPE &... elements)
Append multiple elements to the end of the array.
Definition array.h:1415
void EraseIndex(IndexT index)
erase element at index, keep sorting intact
Definition array.h:1090
const SizeT Size() const
get number of elements in array
Definition array.h:934
#define n_assert2(exp, msg)
Definition debug.h:59
This simple Id pool implements a set of free and used consecutive integers.
Definition id.h:135
__forceinline TYPE min(TYPE a, TYPE b)
Definition scalar.h:399
Nebula's scalar datatype.
int SizeT
Definition types.h:50
unsigned int uint
Definition types.h:41
int IndexT
Definition types.h:49