Nebula
Toggle main menu visibility
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
26
namespace
Ids
27
{
28
class
IdPool
29
{
30
public
:
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
;
56
private
:
57
58
Util::Array<uint>
free
;
59
uint
maxId
;
60
uint
grow
;
61
};
62
63
//------------------------------------------------------------------------------
66
inline
67
IdPool::IdPool
() :
68
maxId
(0xFFFFFFFF),
// int max
69
grow
(512)
70
{
71
// empty
72
}
73
74
//------------------------------------------------------------------------------
77
inline
78
IdPool::IdPool
(
const
uint
max,
const
uint
grow
) :
79
maxId
(max),
80
grow
(
grow
)
81
{
82
// empty
83
}
84
85
//------------------------------------------------------------------------------
88
inline
89
IdPool::~IdPool
()
90
{
91
// empty
92
}
93
94
//------------------------------------------------------------------------------
97
inline
uint
98
IdPool::Alloc
()
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
//------------------------------------------------------------------------------
129
inline
void
130
IdPool::Dealloc
(
uint
id
)
131
{
132
this->
free
.
Append
(this->
maxId
-
id
);
133
}
134
135
//------------------------------------------------------------------------------
138
inline
void
139
IdPool::Clear
()
140
{
141
this->
free
.
Clear
();
142
this->
maxId
= 0xFFFFFFFF;
143
}
144
145
//------------------------------------------------------------------------------
148
inline
void
149
IdPool::Reserve
(
uint
numIds)
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
//------------------------------------------------------------------------------
162
inline
uint
163
IdPool::GetNumUsed
()
const
164
{
165
return
this->
free
.
Capacity
() - this->
free
.
Size
();
166
}
167
168
//------------------------------------------------------------------------------
171
inline
uint
172
IdPool::GetNumFree
()
const
173
{
174
return
this->
free
.
Size
();
175
}
176
177
//------------------------------------------------------------------------------
180
inline
void
181
IdPool::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
//------------------------------------------------------------------------------
198
inline
void
199
IdPool::Move
(
uint
idx,
uint
id
)
200
{
201
this->
free
.
Append
(this->
maxId
-
id
);
202
this->
free
.
EraseIndex
(idx);
203
}
204
205
//------------------------------------------------------------------------------
208
inline
const
uint
209
IdPool::GetGrow
()
const
210
{
211
return
this->
grow
;
212
}
213
214
}
// namespace Ids
array.h
Ids::IdPool::~IdPool
~IdPool()
destructor
Definition
idpool.h:89
Ids::IdPool::Move
void Move(uint lhs, uint rhs)
frees up lhs and erases rhs
Definition
idpool.h:199
Ids::IdPool::free
Util::Array< uint > free
Definition
idpool.h:58
Ids::IdPool::IdPool
IdPool()
constructor
Definition
idpool.h:67
Ids::IdPool::GetNumFree
uint GetNumFree() const
get number of free elements
Definition
idpool.h:172
Ids::IdPool::GetGrow
const uint GetGrow() const
get grow
Definition
idpool.h:209
Ids::IdPool::maxId
uint maxId
Definition
idpool.h:59
Ids::IdPool::GetNumUsed
uint GetNumUsed() const
get number of active ids
Definition
idpool.h:163
Ids::IdPool::ForEachFree
void ForEachFree(const std::function< void(uint, uint)> fun, SizeT num)
iterate free indices
Definition
idpool.h:181
Ids::IdPool::Alloc
uint Alloc()
get new id
Definition
idpool.h:98
Ids::IdPool::grow
uint grow
Definition
idpool.h:60
Ids::IdPool::Reserve
void Reserve(uint numIds)
reserve ids
Definition
idpool.h:149
Ids::IdPool::Dealloc
void Dealloc(uint id)
free id
Definition
idpool.h:130
Ids::IdPool::Clear
void Clear()
Reset to no free ids and start counting anew.
Definition
idpool.h:139
Util::Array
Nebula's dynamic array class.
Definition
array.h:61
Util::Array::Reserve
void Reserve(SizeT num)
increase capacity to fit N more elements into the array.
Definition
array.h:916
Util::Array::Back
TYPE & Back() const
return reference to last element
Definition
array.h:1047
Util::Array::Capacity
const SizeT Capacity() const
get overall allocated size of array in number of elements
Definition
array.h:954
Util::Array::Clear
void Clear()
clear array (calls destructors)
Definition
array.h:1288
Util::Array::Append
void Append(const TYPE &first, const ELEM_TYPE &... elements)
Append multiple elements to the end of the array.
Definition
array.h:1415
Util::Array::EraseIndex
void EraseIndex(IndexT index)
erase element at index, keep sorting intact
Definition
array.h:1090
Util::Array::Size
const SizeT Size() const
get number of elements in array
Definition
array.h:934
n_assert2
#define n_assert2(exp, msg)
Definition
debug.h:59
Ids
This simple Id pool implements a set of free and used consecutive integers.
Definition
id.h:135
Math::min
__forceinline TYPE min(TYPE a, TYPE b)
Definition
scalar.h:399
scalar.h
Nebula's scalar datatype.
types.h
SizeT
int SizeT
Definition
types.h:50
uint
unsigned int uint
Definition
types.h:41
IndexT
int IndexT
Definition
types.h:49
code
foundation
ids
idpool.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.