gwnavgeneration/common/growinglistpool.h Source File

growinglistpool.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 
8 // primary contact: GUAL - secondary contact: NOBODY
9 #ifndef GwNavGen_GrowingListPool_H
10 #define GwNavGen_GrowingListPool_H
11 
15 
16 
17 namespace Kaim
18 {
19 
20 // forward declare
21 template<class T, KyUInt32 NbEl> class GrowingListPool;
22 
23 
24 // ---------------------------------------------------------------------
25 template<class T, KyUInt32 NbEl = 8>
26 class GrowingListNode
27 {
28  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
29 public:
30  GrowingListNode() : m_next(KY_NULL) {}
31 
32 public:
33  T m_values[NbEl];
34  GrowingListNode<T, NbEl>* m_next;
35 };
36 
37 
38 // ---------------------------------------------------------------------
39 template<class T, KyUInt32 NbEl = 8>
40 class GrowingList
41 {
42  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
43 public:
44  typedef GrowingListPool<T, NbEl> Pool;
45  typedef GrowingListNode<T, NbEl> Node;
46 
47 public:
48  GrowingList();
49 
50  void Init(Pool* pool);
51 
52  T* GetNew();
53 
54  void PushBack(const T& value) { *GetNew() = value; }
55 
56  KyInt32 Count() const { return m_count; }
57 
58  void ToArray(T* values) const;
59 
60 private:
61  Pool* m_pool;
62  KyUInt32 m_count;
63  Node* m_first;
64  Node* m_last;
65  KyUInt32 m_countInLast;
66 };
67 
68 
69 // ---------------------------------------------------------------------
70 template<class T, KyUInt32 NbEl = 8>
71 class GrowingListPool
72 {
73  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
74 public:
75  typedef GrowingListNode<T, NbEl> Node;
76  typedef GrowingList<T, NbEl> List;
77 
78  friend class GrowingList<T, NbEl>;
79 public:
80  explicit GrowingListPool(MemoryHeap* heap, KyUInt32 byteCountInChunk = 0) :
81  m_lists(heap, byteCountInChunk), m_nodes(heap, byteCountInChunk), m_elementCount(0), m_heap(heap) {}
82 
83  List* GetNewList();
84 
85  KyUInt32 GetListChunkCount() { return m_lists.GetChunkCount(); }
86 
87  void Clear();
88 
89  void Release();
90 
91  KyUInt32 ByteCountAllocated() const;
92 
93  KyUInt32 ElementCount() const { return m_elementCount; }
94 
95 private:
96  Node* GetNewNode();
97 private:
98  GrowingPool<List> m_lists;
99  GrowingPool<Node> m_nodes;
100  KyUInt32 m_elementCount;
101  MemoryHeap* m_heap;
102 };
103 
104 
105 template<class T, KyUInt32 NbEl>
106 GrowingList<T, NbEl>::GrowingList()
107 {
108  Init(KY_NULL);
109 }
110 
111 
112 template<class T, KyUInt32 NbEl>
113 void GrowingList<T, NbEl>::Init(Pool* pool)
114 {
115  m_pool = pool;
116  m_count = 0;
117  m_first = KY_NULL;
118  m_last = KY_NULL;
119  m_countInLast = 0;
120 }
121 
122 
123 template<class T, KyUInt32 NbEl>
124 void GrowingList<T, NbEl>::ToArray(T* values) const
125 {
126  if (m_count == 0)
127  return;
128 
129  KyUInt32 idx = 0;
130  for (GrowingListNode<T, NbEl>* node = m_first; node != m_last; node = node->m_next)
131  {
132  for (KyUInt32 i = 0; i < NbEl; ++i, ++idx)
133  values[idx] = node->m_values[i];
134  }
135  for (KyUInt32 i = 0; i < m_countInLast; ++i, ++idx)
136  values[idx] = m_last->m_values[i];
137 }
138 
139 
140 template<class T, KyUInt32 NbEl>
141 T* GrowingList<T, NbEl>::GetNew()
142 {
143  if (m_first == KY_NULL)
144  {
145  m_last = m_first = m_pool->GetNewNode();
146  }
147  else if (m_countInLast == NbEl)
148  {
149  GrowingListNode<T, NbEl>* newNode = m_pool->GetNewNode();
150  m_last->m_next = newNode;
151  m_last = newNode;
152  m_countInLast = 0;
153  }
154 
155 
156  KY_DEBUG_ASSERTN(m_countInLast >= 0 && m_countInLast < NbEl, ("Can not get a new element"));
157 
158  T* value = &m_last->m_values[m_countInLast];
159 
160  ++m_countInLast;
161  ++m_count;
162  ++m_pool->m_elementCount;
163 
164  return value;
165 }
166 
167 
168 template<class T, KyUInt32 NbEl>
169 GrowingList<T, NbEl>* GrowingListPool<T, NbEl>::GetNewList()
170 {
171  List* list = m_lists.GetNew();
172  list->Init(this);
173  return list;
174 }
175 
176 
177 template<class T, KyUInt32 NbEl>
178 GrowingListNode<T, NbEl>* GrowingListPool<T, NbEl>::GetNewNode()
179 {
180  Node* node = m_nodes.GetNew();
181  return node;
182 }
183 
184 
185 template<class T, KyUInt32 NbEl>
186 void GrowingListPool<T, NbEl>::Clear()
187 {
188  m_lists.Clear();
189  m_nodes.Clear();
190  m_elementCount = 0;
191 }
192 
193 template<class T, KyUInt32 NbEl>
194 void GrowingListPool<T, NbEl>::Release()
195 {
196  m_lists.Release();
197  m_nodes.Release();
198  m_elementCount = 0;
199 }
200 
201 
202 template<class T, KyUInt32 NbEl>
203 KyUInt32 GrowingListPool<T, NbEl>::ByteCountAllocated() const
204 {
205  return m_lists.ByteCountAllocated() + m_nodes.ByteCountAllocated();
206 }
207 
208 
209 }
210 
211 
212 #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
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