7 #ifndef Navigation_Pool_H
8 #define Navigation_Pool_H
31 KY_INLINE PoolHandle(
KyUInt32 chunkIdx, T* value) : m_chunkIdx(chunkIdx), m_ptr(value) {}
32 KY_INLINE PoolHandle(
const PoolHandle& rhs) : m_chunkIdx(rhs.m_chunkIdx), m_ptr(rhs.m_ptr) {}
34 KY_INLINE PoolHandle& operator=(
const PoolHandle& rhs) { m_chunkIdx = rhs.m_chunkIdx; m_ptr = rhs.m_ptr;
return *
this; }
36 KY_INLINE T& Get()
const {
return *m_ptr; }
37 KY_INLINE T&
operator*() {
return *m_ptr; }
38 KY_INLINE T* operator->() {
return m_ptr; }
41 KY_INLINE
bool IsValid()
const {
return m_ptr !=
KY_NULL; }
43 KY_INLINE
bool operator==(
const PoolHandle& rhs)
const {
return m_ptr == rhs.m_ptr; }
44 KY_INLINE
bool operator!=(
const PoolHandle& rhs)
const {
return m_ptr != rhs.m_ptr; }
62 KY_INLINE PoolKey() : m_chunkIdx(
KyUInt16MAXVAL), m_idxInChunk(KyUInt16MAXVAL) {}
67 KY_INLINE PoolKey(
const PoolKey& rhs) : m_chunkIdx(rhs.m_chunkIdx), m_idxInChunk(rhs.m_idxInChunk) {}
69 KY_INLINE PoolKey& operator=(
const PoolKey& rhs) { m_chunkIdx = rhs.m_chunkIdx; m_idxInChunk = rhs.m_idxInChunk;
return *
this; }
74 KY_INLINE
bool operator==(
const PoolKey& rhs)
const {
return m_chunkIdx == rhs.m_chunkIdx && m_idxInChunk == rhs.m_idxInChunk; }
75 KY_INLINE
bool operator!=(
const PoolKey& rhs)
const {
return !(*
this == rhs); }
85 enum Mode { ByteSize, SlotCount, WaitInit };
94 typedef PoolChunk<T> ChunkT;
97 typedef PoolHandle<T> Handle;
98 typedef PoolKey<T> Key;
101 explicit Pool(MemoryHeap* heap,
KyInt32 memStat, PoolChunkSize::Mode chunkSizeMode = PoolChunkSize::ByteSize,
KyUInt32 byteOrSlotCount = 256);
103 void Init(PoolChunkSize::Mode chunkSizeMode,
KyUInt32 byteOrSlotCount);
107 KY_INLINE
KyUInt32 GetCount()
const {
return m_count; }
110 KY_INLINE Key New_CompactKey();
111 KY_INLINE Key New_CompactKey(
const T& data);
113 KY_INLINE
void New_CompactKeyAndPtr(Key& key, T*& ptr);
115 KY_INLINE
const T& Get(Key key)
const {
return *m_chunks[key.m_chunkIdx]->Get(key.m_idxInChunk); }
116 KY_INLINE T& Get(Key key) {
return *m_chunks[key.m_chunkIdx]->Get(key.m_idxInChunk); }
118 KY_INLINE
void Delete(Key key) { --m_count; m_chunks[key.m_chunkIdx]->Delete(key.m_idxInChunk); }
121 KY_INLINE Handle New_Handle();
122 KY_INLINE Handle New_Handle(
const T& data);
123 KY_INLINE
void Delete(
const Handle& handle) { --m_count;
return m_chunks[handle.m_chunkIdx]->Delete(handle.m_ptr); }
126 KY_INLINE T* New_Ptr();
127 KY_INLINE T* New_Ptr(
const T& data);
128 KY_INLINE
void Delete(T* ptr) { --m_count;
return m_chunks[Ptr2ChunkIdx(ptr)]->Delete(ptr); }
131 KY_INLINE Key Handle2CompactKey(
const Handle& h)
const {
return Key(h.m_chunkIdx, Ptr2IdxInChunk(h.m_chunkIdx, h.m_ptr)); }
132 KY_INLINE Handle CompactKey2Handle(Key key)
const {
return Handle(key.m_chunkIdx, m_chunks[key.m_chunkIdx]->Get(key.m_idxInChunk)); }
134 KY_INLINE
KyUInt32 GetChunkCount()
const {
return m_chunks.GetCount(); }
135 KY_INLINE
KyUInt32 GetNbSlotsByChunk()
const {
return m_nbSlotsByChunk; }
137 KyUInt32 Ptr2ChunkIdx(T* ptr)
const;
138 KY_INLINE
KyUInt32 Ptr2IdxInChunk(
KyUInt32 chunkIdx, T* ptr)
const {
return m_chunks[chunkIdx]->GetIdxInChunk(ptr); }
140 KY_INLINE T& Get(
KyUInt32 chunkIdx,
KyUInt32 idxInChunk)
const {
return *m_chunks[chunkIdx]->Get(idxInChunk); }
142 KY_INLINE
KyUInt32 GetGlobalIdx(
const Handle& handle) {
return handle.m_chunkIdx * m_nbSlotsByChunk + Ptr2IdxInChunk(handle.m_chunkIdx, handle.m_ptr); }
143 KY_INLINE
KyUInt32 GetGlobalIdx(Key key) {
return key.m_chunkIdx * m_nbSlotsByChunk + key.m_idxInChunk; }
144 KY_INLINE
KyUInt32 GetGlobalIdx(T* ptr) {
KyUInt32 chunkIdx = Ptr2ChunkIdx(ptr);
return chunkIdx * m_nbSlotsByChunk + Ptr2IdxInChunk(chunkIdx, ptr); }
147 ChunkT* PushBackNewChunk();
151 void Set(
KyUInt32 chunkIdx, ChunkT* chunk,
KyUInt32 idxInChunk) { m_chunkIdx = chunkIdx; m_idxInchunk = idxInChunk; m_chunk = chunk; m_value = chunk->Get(idxInChunk); }
154 void NewSlot(Slot& slot);
155 void Slot2CompactKey(
const Slot& slot, Key& key);
158 KyArrayDH_POD<ChunkT*> m_chunks;
163 PoolChunkSize::Mode m_chunkSizeMode;
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
#define KY_NULL
Null value.
Definition: types.h:247
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:387
Definition: gamekitcrowddispersion.h:20
Vec2f operator*(KyFloat32 s, const Vec2f &v)
Multiplies the X and Y coordinates of v by s.
Definition: vec2f.h:184
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
#define KyUInt16MAXVAL
The maximum value that can be stored in the KyUInt16 variable type.
Definition: types.h:230
unsigned short KyUInt16
Type used internally to represent an unsigned 16-bit integer.
Definition: types.h:40
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
#define KyUInt32MAXVAL
The maximum value that can be stored in the KyUInt32 variable type.
Definition: types.h:226