gwnavgeneration/common/boxoflists.h Source File

boxoflists.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 
9 // primary contact: GUAL - secondary contact: NOBODY
10 #ifndef GwNavGen_BoxOfLists_H
11 #define GwNavGen_BoxOfLists_H
12 
13 
15 
16 
17 namespace Kaim
18 {
19 
20 
21 template <class T, KyUInt32 NbEl = 8>
22 class BoxOfLists
23 {
24  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_NavDataGen)
25 public:
26  typedef GrowingListPool<T, NbEl> Pool;
27  typedef typename Pool::List Column;
28 
29 public:
30  explicit BoxOfLists(MemoryHeap* heap, KyUInt32 byteCountInChunk = 0) :
31  m_countX(0), m_countY(0), m_columns(KY_NULL), m_pool(heap, byteCountInChunk), m_heap(heap) {}
32 
33  void Init(KyUInt32 countX, KyUInt32 countY);
34 
35  KyUInt32 CountX() { return m_countX; }
36  KyUInt32 CountY() { return m_countY; }
37  KyUInt32 ColumnsCount() { return m_countX * m_countY; }
38 
39  T* GetNew(KyInt32 x, KyInt32 y) { return GetNew(y * m_countX + x); }
40  T* GetNew(KyInt32 rowMajorIdx) { return GetOrCreateColumn(rowMajorIdx)->GetNew(); }
41 
42  Column* GetOrCreateColumn(KyInt32 x, KyInt32 y) { return GetOrCreateColumn(y * m_countX + x); }
43  Column* GetOrCreateColumn(KyInt32 rowMajorIdx);
44 
45  const Column* GetColumn(KyInt32 x, KyInt32 y) const { return m_columns[y * m_countX + x]; }
46  const Column* GetColumn(KyInt32 rowMajorIdx) const { return m_columns[rowMajorIdx]; }
47 
48  void Clear();
49 
50  void Release();
51 
52  KyUInt32 ByteCountAllocated() const { return m_pool.m_byteCountAllocated; }
53 
54  KyUInt32 ElementCount() const { return m_pool.ElementCount(); }
55 
56 private:
57  KyUInt32 m_countX;
58  KyUInt32 m_countY;
59  Column** m_columns; // row major
60  Pool m_pool;
61  MemoryHeap* m_heap;
62 };
63 
64 
65 
66 // --------------------------------- inline implementation ---------------------------------
67 
68 template <class T, KyUInt32 NbEl>
69 inline void BoxOfLists<T, NbEl>::Init(KyUInt32 countX, KyUInt32 countY)
70 {
71  Clear();
72  m_countX = countX;
73  m_countY = countY;
74  m_columns = KY_HEAP_MALLOC(m_heap, Column*, countX * countY, Stat_Default_Mem);
75  memset(m_columns, 0, countX * countY * sizeof(Column*));
76 }
77 
78 
79 template <class T, KyUInt32 NbEl>
80 inline typename BoxOfLists<T, NbEl>::Column* BoxOfLists<T, NbEl>::GetOrCreateColumn(KyInt32 rowMajorIdx)
81 {
82  Column*& column = m_columns[rowMajorIdx];
83  if (column == KY_NULL)
84  column = m_pool.GetNewList();
85  return column;
86 }
87 
88 
89 template <class T, KyUInt32 NbEl>
90 inline void BoxOfLists<T, NbEl>::Clear()
91 {
92  if (m_columns != KY_NULL)
93  KY_FREE(m_columns);
94  m_columns = KY_NULL;
95 
96  m_countX = 0;
97  m_countY = 0;
98 
99  m_pool.Clear();
100 }
101 
102 
103 template <class T, KyUInt32 NbEl>
104 inline void BoxOfLists<T, NbEl>::Release()
105 {
106  Clear();
107  m_pool.Release();
108 }
109 
110 
111 }
112 
113 
114 #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