gwnavruntime/containers/iterablepool.h Source File

iterablepool.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_IterablePool_H
8 #define Navigation_IterablePool_H
9 
13 
14 namespace Kaim
15 {
16 
17 
18 template <typename T> class IterablePool;
19 
20 // MyPool<MyObject>::Iterator it(myPool);
21 // while (it.Next())
22 // { MyObject& o = it.Get(); }
23 template<typename T>
24 class IterablePool_Iterator
25 {
26  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
27 
28 public:
29  KY_INLINE IterablePool_Iterator();
30  KY_INLINE IterablePool_Iterator(const IterablePool<T>* iterablePool) { Init(iterablePool); }
31  KY_INLINE void Init(const IterablePool<T>* iterablePool);
32  KY_INLINE bool Next();
33  KY_INLINE T& Get() const { return *m_ptr; }
34 
35 public:
36  const IterablePool<T>* m_iterablePool;
37  KyUInt32 m_usedSlotsCount;
38  KyUInt32 m_usedSlotsIdx;
39  KyUInt32 m_chunkCount;
40  KyUInt32 m_chunkIdx;
41  KyUInt32 m_idxInChunk;
42  KyUInt32 m_globalIdx;
43  T* m_ptr;
44 };
45 
46 
47 // Same Pool but
48 // - has a BitField to indicate what slots are filled/free, this allows to iterate on all elements in the Pool
49 // - destructor deletes all elements that are in the Pool
50 template <typename T>
51 class IterablePool
52 {
53  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
54  KY_CLASS_WITHOUT_COPY(IterablePool)
55 
56 public:
57  typedef PoolHandle<T> Handle;
58  typedef PoolKey<T> Key;
59  typedef IterablePool_Iterator<T> Iterator;
60 
61 private:
62  // struct that allows to update the bitfield easily when a new element is added
63  struct OnNew
64  {
65  OnNew(IterablePool* pool) : m_pool(pool), m_oldChunkCount(pool->GetChunkCount()) {}
66  Key Do(Key compactKey);
67  Handle Do(Handle handle);
68  T* Do(T* ptr);
69  IterablePool* m_pool;
70  KyUInt32 m_oldChunkCount;
71  };
72 
73 public:
74  explicit IterablePool(MemoryHeap* heap, KyInt32 memStat, PoolChunkSize::Mode chunkSizeMode = PoolChunkSize::ByteSize, KyUInt32 byteOrSlotCount = 256) :
75  m_basePool(heap, memStat, chunkSizeMode, byteOrSlotCount) {}
76 
77  void Init(PoolChunkSize::Mode chunkSizeMode, KyUInt32 byteOrSlotCount) { m_basePool.Init(chunkSizeMode, byteOrSlotCount); }
78 
79  ~IterablePool() { Clear(); }
80 
81  KY_INLINE KyUInt32 GetCount() const { return m_basePool.GetCount(); }
82 
83  void Clear();
84 
85  // -------- Key access --------
86  KY_INLINE Key New_CompactKey() { OnNew onNew(this); return onNew.Do(m_basePool.New_CompactKey()); }
87  KY_INLINE Key New_CompactKey(const T& data) { OnNew onNew(this); return onNew.Do(m_basePool.New_CompactKey(data)); }
88 
89  KY_INLINE void New_CompactKeyAndPtr(Key& key, T*& ptr) { OnNew onNew(this); m_basePool.New_CompactKeyAndPtr(key, ptr); onNew.Do(key); }
90 
91  KY_INLINE const T& Get(Key key) const { return m_basePool.Get(key); }
92  KY_INLINE T& Get(Key key) { return m_basePool.Get(key); }
93 
94  KY_INLINE void Delete(Key key) { m_basePool.Delete(key); m_bitField.UnsetBit(GetGlobalIdx(key)); }
95 
96  // -------- Handle access --------
97  KY_INLINE Handle New_Handle() { OnNew onNew(this); return onNew.Do(m_basePool.New_Handle()); }
98  KY_INLINE Handle New_Handle(const T& data) { OnNew onNew(this); return onNew.Do(m_basePool.New_Handle(data)); }
99  KY_INLINE void Delete(const Handle& handle) { m_basePool.Delete(handle); m_bitField.UnsetBit(GetGlobalIdx(handle)); }
100 
101  // -------- ptr access Delete() is in O(nbChunks) --------
102  KY_INLINE T* New_Ptr() { OnNew onNew(this); return onNew.Do(m_basePool.New_Ptr()); }
103  KY_INLINE T* New_Ptr(const T& data) { OnNew onNew(this); return onNew.Do(m_basePool.New_Ptr()); }
104  KY_INLINE void Delete(T* ptr) { m_basePool.Delete(ptr); m_bitField.UnsetBit(GetGlobalIdx(ptr)); }
105 
106  // -------- internal stuff --------
107  KY_INLINE Key Handle2CompactKey(const Handle& h) const { return m_basePool.Handle2CompactKey(h); }
108  KY_INLINE Handle CompactKey2Handle(Key key) const { return m_basePool.CompactKey2Handle(key); }
109 
110  KY_INLINE KyUInt32 GetChunkCount() const { return m_basePool.GetChunkCount(); }
111  KY_INLINE KyUInt32 GetNbSlotsByChunk() const { return m_basePool.GetNbSlotsByChunk(); }
112 
113  KY_INLINE KyUInt32 Ptr2ChunkIdx(T* ptr) const { return m_basePool.Ptr2ChunkIdx(ptr); } // O(NbChunks)
114  KY_INLINE KyUInt32 Ptr2IdxInChunk(KyUInt32 chunkIdx, T* ptr) const { return m_basePool.Ptr2IdxInChunk(chunkIdx, ptr); }
115 
116  KY_INLINE T& Get(KyUInt32 chunkIdx, KyUInt32 idxInChunk) const { return m_basePool.Get(chunkIdx, idxInChunk); }
117 
118  KY_INLINE KyUInt32 GetGlobalIdx(const Handle& handle) { return m_basePool.GetGlobalIdx(handle); }
119  KY_INLINE KyUInt32 GetGlobalIdx(Key key) { return m_basePool.GetGlobalIdx(key); }
120  KY_INLINE KyUInt32 GetGlobalIdx(T* ptr) { return m_basePool.GetGlobalIdx(ptr); }
121 
122  // internal
123  KY_INLINE bool Next(Iterator& it) const;
124 
125 public:
126  Pool<T> m_basePool;
127  BitField m_bitField;
128 };
129 
130 } // namespace Kaim
131 
133 
134 #endif // Navigation_IterablePool_H
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:387
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36