gwnavruntime/containers/iterablepool.h Source File

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