gwnavgeneration/common/boxoflists.h Source File

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