Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
idgenerationpool.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
17
18
19
#include "
util/array.h
"
20
#include "
id.h
"
21
#include "
util/queue.h
"
22
23
const
uint32_t
ID_BITS
= 24;
24
const
uint32_t
GENERATION_BITS
= 8;
25
const
uint32_t
ID_MASK
= (1<<
ID_BITS
) - 1;
26
const
uint32_t
GENERATION_MASK
= (1<<
GENERATION_BITS
) - 1;
27
28
#if GENERATION_BITS <= 8
29
typedef
uint8_t
generation_t
;
30
#elif GENERATION_BITS <= 16
31
typedef
uint16_t
generation_t
;
32
#else
33
typedef
uint32_t
generation_t
;
34
#endif
35
#if ID_BITS + GENERATION_BITS > 32
36
#error bits in game id generation exceeds 32
37
#endif
38
39
//------------------------------------------------------------------------------
40
namespace
Ids
41
{
42
class
IdGenerationPool
43
{
44
public
:
46
IdGenerationPool
();
48
~IdGenerationPool
();
49
51
bool
Allocate
(
Id32
&
id
);
53
void
Deallocate
(
Id32
id
);
55
bool
IsValid
(
Id32
id
)
const
;
56
57
private
:
59
Util::Array<generation_t>
generations
;
60
62
Util::Queue<Id32>
freeIds
;
63
SizeT
freeIdsSize
;
64
};
65
66
//------------------------------------------------------------------------------
69
constexpr
static
Id24
70
Index
(
const
Id32
id
)
71
{
72
return
id
&
ID_MASK
;
73
}
74
75
//------------------------------------------------------------------------------
78
constexpr
static
generation_t
79
Generation
(
const
Id32
id
)
80
{
81
return
(
id
>>
ID_BITS
) &
GENERATION_MASK
;
82
}
83
84
//------------------------------------------------------------------------------
87
static
Id32
88
CreateId
(
const
Id24
index,
generation_t
generation)
89
{
90
Id32
id;
91
id
= (generation <<
ID_BITS
) | index;
92
n_assert2
(index < (1 <<
ID_BITS
),
"index overflow"
);
93
return
id;
94
}
95
96
}
// namespace Ids
97
//------------------------------------------------------------------------------
array.h
Ids::IdGenerationPool::Deallocate
void Deallocate(Id32 id)
remove an id
Definition
idgenerationpool.cc:54
Ids::IdGenerationPool::IsValid
bool IsValid(Id32 id) const
check if valid
Definition
idgenerationpool.cc:66
Ids::IdGenerationPool::freeIds
Util::Queue< Id32 > freeIds
stores freed indices
Definition
idgenerationpool.h:62
Ids::IdGenerationPool::IdGenerationPool
IdGenerationPool()
constructor
Definition
idgenerationpool.cc:14
Ids::IdGenerationPool::Allocate
bool Allocate(Id32 &id)
allocate a new id, returns true if a new id was created, and false if an id was reused but with new g...
Definition
idgenerationpool.cc:33
Ids::IdGenerationPool::freeIdsSize
SizeT freeIdsSize
Definition
idgenerationpool.h:63
Ids::IdGenerationPool::generations
Util::Array< generation_t > generations
array containing generation value for every index
Definition
idgenerationpool.h:59
Ids::IdGenerationPool::~IdGenerationPool
~IdGenerationPool()
destructor
Definition
idgenerationpool.cc:24
Util::Array
Nebula's dynamic array class.
Definition
array.h:61
Util::Queue
Nebula's queue class (a FIFO container).
Definition
queue.h:22
n_assert2
#define n_assert2(exp, msg)
Definition
debug.h:51
GENERATION_MASK
const uint32_t GENERATION_MASK
Definition
idgenerationpool.h:26
generation_t
uint8_t generation_t
Definition
idgenerationpool.h:29
ID_BITS
const uint32_t ID_BITS
Definition
idgenerationpool.h:23
GENERATION_BITS
const uint32_t GENERATION_BITS
Definition
idgenerationpool.h:24
ID_MASK
const uint32_t ID_MASK
Definition
idgenerationpool.h:25
id.h
Ids
This simple Id pool implements a set of free and used consecutive integers.
Definition
id.h:135
Ids::Id24
uint32_t Id24
Definition
id.h:139
Ids::Index
static constexpr Id24 Index(const Id32 id)
Definition
idgenerationpool.h:70
Ids::CreateId
static Id32 CreateId(const Id24 index, generation_t generation)
Definition
idgenerationpool.h:88
Ids::Id32
uint32_t Id32
Definition
id.h:138
Ids::Generation
static constexpr generation_t Generation(const Id32 id)
Definition
idgenerationpool.h:79
queue.h
SizeT
int SizeT
Definition
types.h:42
code
foundation
ids
idgenerationpool.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.