gwnavruntime/containers/pool.h Source File

pool.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #ifndef Navigation_Pool_H
8 #define Navigation_Pool_H
9 
14 
15 namespace Kaim
16 {
17 
18 
19 // PoolHandle
20 // - allows Delete(Handle handle) in O(1)
21 // - has sizeof() = 8 on 32 bits
22 // - has sizeof() = 16 on 64 bits
23 // - points directly to the value
24 template <typename T>
25 class PoolHandle
26 {
27  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
28 
29 public:
30  KY_INLINE PoolHandle() : m_chunkIdx(KyUInt32MAXVAL), m_ptr(KY_NULL) {}
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) {}
33 
34  KY_INLINE PoolHandle& operator=(const PoolHandle& rhs) { m_chunkIdx = rhs.m_chunkIdx; m_ptr = rhs.m_ptr; return *this; }
35 
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; }
39 
40  KY_INLINE void Invalidate() { m_chunkIdx = KyUInt32MAXVAL; m_ptr = KY_NULL; }
41  KY_INLINE bool IsValid() const { return m_ptr != KY_NULL; }
42 
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; }
45 
46 public:
47  KyUInt32 m_chunkIdx;
48  T* m_ptr;
49 };
50 
51 
52 // PoolKey
53 // - allows Delete(Key compactKey) in O(1)
54 // - has sizeof() = 4
55 // - requires 2 indirections to get the value
56 template <typename T>
57 class PoolKey
58 {
59  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
60 
61 public:
62  KY_INLINE PoolKey() : m_chunkIdx(KyUInt16MAXVAL), m_idxInChunk(KyUInt16MAXVAL) {}
63 
64  KY_INLINE PoolKey(KyUInt32 chunkIdx, KyUInt32 idxInChunk) :
65  m_chunkIdx(KyUInt16(chunkIdx)), m_idxInChunk(KyUInt16(idxInChunk)) {}
66 
67  KY_INLINE PoolKey(const PoolKey& rhs) : m_chunkIdx(rhs.m_chunkIdx), m_idxInChunk(rhs.m_idxInChunk) {}
68 
69  KY_INLINE PoolKey& operator=(const PoolKey& rhs) { m_chunkIdx = rhs.m_chunkIdx; m_idxInChunk = rhs.m_idxInChunk; return *this; }
70 
71  KY_INLINE void Invalidate() { m_chunkIdx = KyUInt16MAXVAL; m_idxInChunk = KyUInt16MAXVAL; }
72  KY_INLINE bool IsValid() const { return m_chunkIdx != KyUInt16MAXVAL && m_idxInChunk != KyUInt16MAXVAL; }
73 
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); }
76 
77 public:
78  KyUInt16 m_chunkIdx;
79  KyUInt16 m_idxInChunk;
80 };
81 
82 
83 struct PoolChunkSize
84 {
85  enum Mode { ByteSize, SlotCount, WaitInit };
86 };
87 
88 
89 template <typename T>
90 class Pool
91 {
92  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
94  typedef PoolChunk<T> ChunkT;
95 
96 public:
97  typedef PoolHandle<T> Handle;
98  typedef PoolKey<T> Key;
99 
100 public:
101  explicit Pool(MemoryHeap* heap, KyInt32 memStat, PoolChunkSize::Mode chunkSizeMode = PoolChunkSize::ByteSize, KyUInt32 byteOrSlotCount = 256);
102 
103  void Init(PoolChunkSize::Mode chunkSizeMode, KyUInt32 byteOrSlotCount);
104 
105  ~Pool();
106 
107  KY_INLINE KyUInt32 GetCount() const { return m_count; }
108 
109  // -------- Key access --------
110  KY_INLINE Key New_CompactKey();
111  KY_INLINE Key New_CompactKey(const T& data);
112 
113  KY_INLINE void New_CompactKeyAndPtr(Key& key, T*& ptr);
114 
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); }
117 
118  KY_INLINE void Delete(Key key) { --m_count; m_chunks[key.m_chunkIdx]->Delete(key.m_idxInChunk); }
119 
120  // -------- Handle access --------
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); }
124 
125  // -------- ptr access Delete() is in O(nbChunks) --------
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); }
129 
130  // --------
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)); }
133 
134  KY_INLINE KyUInt32 GetChunkCount() const { return m_chunks.GetCount(); }
135  KY_INLINE KyUInt32 GetNbSlotsByChunk() const { return m_nbSlotsByChunk; }
136 
137  KyUInt32 Ptr2ChunkIdx(T* ptr) const; // O(NbChunks)
138  KY_INLINE KyUInt32 Ptr2IdxInChunk(KyUInt32 chunkIdx, T* ptr) const { return m_chunks[chunkIdx]->GetIdxInChunk(ptr); }
139 
140  KY_INLINE T& Get(KyUInt32 chunkIdx, KyUInt32 idxInChunk) const { return *m_chunks[chunkIdx]->Get(idxInChunk); }
141 
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); }
145 
146 private:
147  ChunkT* PushBackNewChunk();
148 
149  struct Slot
150  {
151  void Set(KyUInt32 chunkIdx, ChunkT* chunk, KyUInt32 idxInChunk) { m_chunkIdx = chunkIdx; m_idxInchunk = idxInChunk; m_chunk = chunk; m_value = chunk->Get(idxInChunk); }
152  KyUInt32 m_chunkIdx; KyUInt32 m_idxInchunk; ChunkT* m_chunk; T* m_value;
153  };
154  void NewSlot(Slot& slot);
155  void Slot2CompactKey(const Slot& slot, Key& key);
156 
157 private:
158  KyArrayDH_POD<ChunkT*> m_chunks;
159  KyUInt32 m_chunkByteSize;
160  KyUInt32 m_lastAllocChunkIdx;
161  KyUInt32 m_nbSlotsByChunk;
162  KyUInt32 m_count;
163  PoolChunkSize::Mode m_chunkSizeMode;
164  MemoryHeap* m_heap;
165  KyInt32 m_memStat;
166 };
167 
168 } // namespace Kaim
169 
171 
172 #endif
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