Nebula
Loading...
Searching...
No Matches
idgenerationpool.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
19#include "util/array.h"
20#include "id.h"
21#include "util/queue.h"
22
23const uint32_t ID_BITS = 24;
24const uint32_t GENERATION_BITS = 8;
25const uint32_t ID_MASK = (1<<ID_BITS) - 1;
26const uint32_t GENERATION_MASK = (1<<GENERATION_BITS) - 1;
27
28#if GENERATION_BITS <= 8
29typedef uint8_t generation_t;
30#elif GENERATION_BITS <= 16
31typedef uint16_t generation_t;
32#else
33typedef 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//------------------------------------------------------------------------------
40namespace Ids
41{
43{
44public:
49
51 bool Allocate(Id32& id);
53 void Deallocate(Id32 id);
55 bool IsValid(Id32 id) const;
56
57private:
60
64};
65
66//------------------------------------------------------------------------------
69constexpr static Id24
70Index(const Id32 id)
71{
72 return id & ID_MASK;
73}
74
75//------------------------------------------------------------------------------
78constexpr static generation_t
80{
81 return (id >> ID_BITS) & GENERATION_MASK;
82}
83
84//------------------------------------------------------------------------------
87static Id32
88CreateId(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//------------------------------------------------------------------------------
Provides a system for creating array friendly id numbers with reuse and generations.
Definition idgenerationpool.h:43
void Deallocate(Id32 id)
remove an id
Definition idgenerationpool.cc:54
bool IsValid(Id32 id) const
check if valid
Definition idgenerationpool.cc:66
Util::Queue< Id32 > freeIds
stores freed indices
Definition idgenerationpool.h:62
IdGenerationPool()
constructor
Definition idgenerationpool.cc:14
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
SizeT freeIdsSize
Definition idgenerationpool.h:63
Util::Array< generation_t > generations
array containing generation value for every index
Definition idgenerationpool.h:59
~IdGenerationPool()
destructor
Definition idgenerationpool.cc:24
Nebula's dynamic array class.
Definition array.h:60
Nebula's queue class (a FIFO container).
Definition queue.h:28
#define n_assert2(exp, msg)
Definition debug.h:51
const uint32_t GENERATION_MASK
Definition idgenerationpool.h:26
uint8_t generation_t
Definition idgenerationpool.h:29
const uint32_t ID_BITS
Definition idgenerationpool.h:23
const uint32_t GENERATION_BITS
Definition idgenerationpool.h:24
const uint32_t ID_MASK
Definition idgenerationpool.h:25
This simple Id pool implements a set of free and used consecutive integers.
Definition id.h:135
uint32_t Id24
Definition id.h:139
static constexpr Id24 Index(const Id32 id)
Definition idgenerationpool.h:70
static Id32 CreateId(const Id24 index, generation_t generation)
Definition idgenerationpool.h:88
uint32_t Id32
Definition id.h:138
static constexpr generation_t Generation(const Id32 id)
Definition idgenerationpool.h:79
int SizeT
Definition types.h:49