29 KY_INLINE PoolHandle() : m_chunkIdx(
KyUInt32MAXVAL), m_ptr(
nullptr) {}
30 KY_INLINE PoolHandle(
KyUInt32 chunkIdx, T* value) : m_chunkIdx(chunkIdx), m_ptr(value) {}
31 KY_INLINE PoolHandle(
const PoolHandle& rhs) : m_chunkIdx(rhs.m_chunkIdx), m_ptr(rhs.m_ptr) {}
33 KY_INLINE PoolHandle& operator=(
const PoolHandle& rhs) { m_chunkIdx = rhs.m_chunkIdx; m_ptr = rhs.m_ptr;
return *
this; }
35 KY_INLINE T& Get()
const {
return *m_ptr; }
36 KY_INLINE T&
operator*() {
return *m_ptr; }
37 KY_INLINE T* operator->() {
return m_ptr; }
39 KY_INLINE
void Invalidate() { m_chunkIdx =
KyUInt32MAXVAL; m_ptr =
nullptr; }
40 KY_INLINE
bool IsValid()
const {
return m_ptr !=
nullptr; }
42 KY_INLINE
bool operator==(
const PoolHandle& rhs)
const {
return m_ptr == rhs.m_ptr; }
43 KY_INLINE
bool operator!=(
const PoolHandle& rhs)
const {
return m_ptr != rhs.m_ptr; }
60 KY_INLINE PoolKey() : m_chunkIdx(
KyUInt16MAXVAL), m_idxInChunk(KyUInt16MAXVAL) {}
64 KY_INLINE PoolKey(
const PoolKey& rhs) : m_chunkIdx(rhs.m_chunkIdx), m_idxInChunk(rhs.m_idxInChunk) {}
66 KY_INLINE PoolKey& operator=(
const PoolKey& rhs) { m_chunkIdx = rhs.m_chunkIdx; m_idxInChunk = rhs.m_idxInChunk;
return *
this; }
71 KY_INLINE
bool operator==(
const PoolKey& rhs)
const {
return m_chunkIdx == rhs.m_chunkIdx && m_idxInChunk == rhs.m_idxInChunk; }
72 KY_INLINE
bool operator!=(
const PoolKey& rhs)
const {
return !(*
this == rhs); }
82 enum Mode { ByteSize, SlotCount, WaitInit };
86 struct PoolChunkElement
89 PoolChunkElement(PoolChunk<T>* chunk) : m_chunk(chunk), m_next(
KyUInt32MAXVAL) {}
90 PoolChunk<T>* m_chunk;
100 typedef PoolChunk<T> ChunkT;
101 typedef PoolChunkElement<T> ChunkElemT;
104 typedef PoolHandle<T> Handle;
108 explicit Pool(MemoryHeap* heap,
KyInt32 memStat, PoolChunkSize::Mode chunkSizeMode = PoolChunkSize::ByteSize,
KyUInt32 byteOrSlotCount = 256);
110 void Init(PoolChunkSize::Mode chunkSizeMode,
KyUInt32 byteOrSlotCount);
114 KY_INLINE
KyUInt32 GetCount()
const {
return m_count; }
117 KY_INLINE Key New_CompactKey();
118 KY_INLINE Key New_CompactKey(
const T& data);
120 KY_INLINE
void New_CompactKeyAndPtr(Key& key, T*& ptr);
122 KY_INLINE
const T& Get(Key key)
const {
return *GetChunk(key).Get(key.m_idxInChunk); }
123 KY_INLINE T& Get(Key key) {
return *GetChunk(key).Get(key.m_idxInChunk); }
125 KY_INLINE
void Delete(Key key) { Delete(key.m_chunkIdx, key.m_idxInChunk); }
128 KY_INLINE Handle New_Handle();
129 KY_INLINE Handle New_Handle(
const T& data);
130 KY_INLINE
void Delete(
const Handle& handle) { Delete(handle.m_chunkIdx, GetChunk(handle).GetIdxInChunk(handle.m_ptr)); }
133 KY_INLINE T* New_Ptr();
134 KY_INLINE T* New_Ptr(
const T& data);
135 KY_INLINE
void Delete(T* ptr)
137 KyUInt32 chunkIdx = Ptr2ChunkIdx(ptr);
138 Delete(chunkIdx, GetChunk(chunkIdx).GetIdxInChunk(ptr));
142 KY_INLINE Key Handle2CompactKey(
const Handle& h)
const {
return Key(h.m_chunkIdx, Ptr2IdxInChunk(h.m_chunkIdx, h.m_ptr)); }
143 KY_INLINE Handle CompactKey2Handle(Key key)
const {
return Handle(key.m_chunkIdx, GetChunk(key).Get(key.m_idxInChunk)); }
145 KY_INLINE
KyUInt32 GetChunkCount()
const {
return m_chunkElems.GetCount(); }
146 KY_INLINE
KyUInt32 GetNbSlotsByChunk()
const {
return m_nbSlotsByChunk; }
148 KyUInt32 Ptr2ChunkIdx(T* ptr)
const;
149 KY_INLINE
KyUInt32 Ptr2IdxInChunk(
KyUInt32 chunkIdx, T* ptr)
const {
return GetChunk(chunkIdx).GetIdxInChunk(ptr); }
151 KY_INLINE T& Get(
KyUInt32 chunkIdx,
KyUInt32 idxInChunk)
const {
return *GetChunk(chunkIdx).Get(idxInChunk); }
153 KY_INLINE
KyUInt32 GetGlobalIdx(
const Handle& handle) {
return handle.m_chunkIdx * m_nbSlotsByChunk + Ptr2IdxInChunk(handle.m_chunkIdx, handle.m_ptr); }
154 KY_INLINE
KyUInt32 GetGlobalIdx(Key key) {
return key.m_chunkIdx * m_nbSlotsByChunk + key.m_idxInChunk; }
155 KY_INLINE
KyUInt32 GetGlobalIdx(T* ptr) {
KyUInt32 chunkIdx = Ptr2ChunkIdx(ptr);
return chunkIdx * m_nbSlotsByChunk + Ptr2IdxInChunk(chunkIdx, ptr); }
158 void PushBackNewChunk();
162 void Set(
KyUInt32 chunkIdx, ChunkT* chunk,
KyUInt32 idxInChunk) { m_chunkIdx = chunkIdx; m_idxInchunk = idxInChunk; m_chunk = chunk; m_value = chunk->Get(idxInChunk); }
166 void NewSlot(Slot& slot);
169 void Slot2CompactKey(
const Slot& slot, Key& key);
171 ChunkT& GetChunk(
const Handle& handle) {
return *m_chunkElems[handle.m_chunkIdx].m_chunk; }
172 ChunkT& GetChunk(Key key) {
return *m_chunkElems[key.m_chunkIdx].m_chunk; }
173 ChunkT& GetChunk(Key key)
const {
return *m_chunkElems[key.m_chunkIdx].m_chunk; }
174 ChunkT& GetChunk(T* ptr) {
return *m_chunkElems[Ptr2ChunkIdx(ptr)].m_chunk; }
175 ChunkT& GetChunk(
KyUInt32 idx) {
return *m_chunkElems[idx].m_chunk; }
176 ChunkT& GetChunk(
KyUInt32 idx)
const {
return *m_chunkElems[idx].m_chunk; }
179 KyArrayDH_POD<ChunkElemT> m_chunkElems;
184 PoolChunkSize::Mode m_chunkSizeMode;
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:196
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
std::uint16_t KyUInt16
uint16_t
Definition: types.h:28
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
Vec2f operator*(KyFloat32 s, const Vec2f &v)
scalar * vec operator
Definition: vec2f.h:120
#define KyUInt16MAXVAL
KyUInt16 max value
Definition: types.h:67
std::int32_t KyInt32
int32_t
Definition: types.h:24
#define KyUInt32MAXVAL
KyUInt32 max value
Definition: types.h:68