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
Reserve
(
uint
numIds);
45
uint
GetNumUsed
()
const
;
47
uint
GetNumFree
()
const
;
49
void
ForEachFree
(
const
std::function<
void
(
uint
,
uint
)> fun,
SizeT
num);
51
void
Move
(
uint
lhs,
uint
rhs);
53
const
uint
GetGrow
()
const
;
54
private
:
55
56
Util::Array<uint>
free
;
57
uint
maxId
;
58
uint
grow
;
59
};
60
61
//------------------------------------------------------------------------------
64
inline
65
IdPool::IdPool
() :
66
maxId
(0xFFFFFFFF),
// int max
67
grow
(512)
68
{
69
// empty
70
}
71
72
//------------------------------------------------------------------------------
75
inline
76
IdPool::IdPool
(
const
uint
max,
const
uint
grow
) :
77
maxId
(max),
78
grow
(
grow
)
79
{
80
// empty
81
}
82
83
//------------------------------------------------------------------------------
86
inline
87
IdPool::~IdPool
()
88
{
89
// empty
90
}
91
92
//------------------------------------------------------------------------------
95
inline
uint
96
IdPool::Alloc
()
97
{
98
// if we're out of ids, allocate more, but with a controlled grow (not log2 as per Array)
99
if
(this->
free
.Size() == 0)
100
{
101
// calculate how many more indices we are allowed to get
102
SizeT
growTo =
Math::min
(this->
maxId
, this->
grow
);
103
SizeT
oldCapacity = this->
free
.Capacity();
104
105
// make sure we don't allocate too many indices
106
n_assert2
((
uint
)(oldCapacity + growTo) < this->
maxId
,
"Pool is full! Be careful with how much you allocate!\n"
);
107
108
// reserve more space for new Ids
109
this->
free
.Reserve(oldCapacity + growTo);
110
IndexT
i;
111
for
(i = this->
free
.Capacity()-1; i >= oldCapacity; i--)
112
{
113
// count backwards from the max id
114
this->
free
.Append(this->
maxId
- i);
115
}
116
}
117
118
// if we do an inverse erase, we don't have to move elements, and since we know the max id, subtract it
119
uint
id
= this->
maxId
- this->
free
.Back();
120
this->
free
.EraseIndex(this->
free
.Size() - 1);
121
return
id;
122
}
123
124
//------------------------------------------------------------------------------
127
inline
void
128
IdPool::Dealloc
(
uint
id
)
129
{
130
this->
free
.Append(this->
maxId
-
id
);
131
}
132
133
//------------------------------------------------------------------------------
136
inline
void
137
IdPool::Reserve
(
uint
numIds)
138
{
139
this->
maxId
= numIds;
140
this->
free
.Reserve(numIds);
141
for
(
int
i = numIds - 1; i >= 0; i--)
142
{
143
this->
free
.Append(this->
maxId
- i);
144
}
145
}
146
147
//------------------------------------------------------------------------------
150
inline
uint
151
IdPool::GetNumUsed
()
const
152
{
153
return
this->
free
.Capacity() - this->
free
.Size();
154
}
155
156
//------------------------------------------------------------------------------
159
inline
uint
160
IdPool::GetNumFree
()
const
161
{
162
return
this->
free
.Size();
163
}
164
165
//------------------------------------------------------------------------------
168
inline
void
169
IdPool::ForEachFree
(
const
std::function<
void
(
uint
,
uint
)> fun,
SizeT
num)
170
{
171
SizeT
size = this->
free
.Size();
172
for
(
IndexT
i = size - 1; i >= 0; i--)
173
{
174
const
uint
id
= this->
maxId
- this->
free
[i];
175
if
(
id
< (
uint
)num)
176
{
177
fun(
id
, i);
178
num--;
179
}
180
}
181
}
182
183
//------------------------------------------------------------------------------
186
inline
void
187
IdPool::Move
(
uint
idx,
uint
id
)
188
{
189
this->
free
.Append(this->
maxId
-
id
);
190
this->
free
.EraseIndex(idx);
191
}
192
193
//------------------------------------------------------------------------------
196
inline
const
uint
197
IdPool::GetGrow
()
const
198
{
199
return
this->
grow
;
200
}
201
202
}
// namespace Ids
array.h
Ids::IdPool::~IdPool
~IdPool()
destructor
Definition
idpool.h:87
Ids::IdPool::Move
void Move(uint lhs, uint rhs)
frees up lhs and erases rhs
Definition
idpool.h:187
Ids::IdPool::free
Util::Array< uint > free
Definition
idpool.h:56
Ids::IdPool::IdPool
IdPool()
constructor
Definition
idpool.h:65
Ids::IdPool::GetNumFree
uint GetNumFree() const
get number of free elements
Definition
idpool.h:160
Ids::IdPool::GetGrow
const uint GetGrow() const
get grow
Definition
idpool.h:197
Ids::IdPool::maxId
uint maxId
Definition
idpool.h:57
Ids::IdPool::GetNumUsed
uint GetNumUsed() const
get number of active ids
Definition
idpool.h:151
Ids::IdPool::ForEachFree
void ForEachFree(const std::function< void(uint, uint)> fun, SizeT num)
iterate free indices
Definition
idpool.h:169
Ids::IdPool::Alloc
uint Alloc()
get new id
Definition
idpool.h:96
Ids::IdPool::grow
uint grow
Definition
idpool.h:58
Ids::IdPool::Reserve
void Reserve(uint numIds)
reserve ids
Definition
idpool.h:137
Ids::IdPool::Dealloc
void Dealloc(uint id)
free id
Definition
idpool.h:128
Util::Array
Nebula's dynamic array class.
Definition
array.h:61
n_assert2
#define n_assert2(exp, msg)
Definition
debug.h:51
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:42
uint
unsigned int uint
Definition
types.h:33
IndexT
int IndexT
Definition
types.h:41
code
foundation
ids
idpool.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.