gwnavgeneration/common/boxofarrays.h Source File

boxofarrays.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_BoxOfArrays_H
10 #define GwNavGen_BoxOfArrays_H
11 
12 
14 
15 
16 namespace Kaim
17 {
18 
19 /* So T must be a POD with a default constructor.
20  T() constructor is called when InitColumn() is called.
21  ~T() destructor is never called. */
22 template <class T>
23 class BoxOfArrays
24 {
25  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
26 public:
27  class Column
28  {
29  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
30  public:
31  void Init(KyUInt32 count_, T* values_); // T() constructor called
32  public:
33  KyUInt32 m_count;
34  T* m_values;
35  };
36 
37 public:
38  explicit BoxOfArrays(MemoryHeap* heap, KyUInt32 byteCountInChunk = 0) :
39  m_countX(0), m_countY(0), m_columns(KY_NULL), m_pool(heap, byteCountInChunk), m_heap(heap) {}
40 
41  ~BoxOfArrays() { Release(); }
42 
43  void Init(KyUInt32 countX, KyUInt32 countY);
44 
45  KyUInt32 CountX() const { return m_countX; }
46  KyUInt32 CountY() const { return m_countY; }
47  KyUInt32 ColumnsCount() const { return m_countX * m_countY; }
48 
49  T* InitColumn(const Vec2i& pos, KyUInt32 count) { return InitColumn(pos.x, pos.y, count); }
50  T* InitColumn(KyInt32 x, KyInt32 y, KyUInt32 count) { return InitColumn(y * m_countX + x, count); }
51  T* InitColumn(KyInt32 columnIdx, KyUInt32 count);
52 
53  Column& GetColumn(const Vec2i& pos) { return GetColumn(pos.x, pos.y); }
54  Column& GetColumn(KyInt32 x, KyInt32 y) { return GetColumn(y * m_countX + x); }
55  Column& GetColumn(KyInt32 rowMajorIdx) { return m_columns[rowMajorIdx]; }
56 
57  const Column& GetColumn(const Vec2i& pos) const { return GetColumn(pos.x, pos.y); }
58  const Column& GetColumn(KyInt32 x, KyInt32 y) const { return GetColumn(y * m_countX + x); }
59  const Column& GetColumn(KyInt32 rowMajorIdx) const { return m_columns[rowMajorIdx]; }
60 
61  const Column* GetColumns() const { return m_columns; }
62 
63  void Clear();
64 
65  void Release();
66 
67  KyUInt32 ByteCountAllocated() const { return m_pool.ByteCountAllocated(); }
68 
69  BoxOfArrays<T>& operator=(const BoxOfArrays<T>& other);
70 
71 private:
72  KyUInt32 m_countX;
73  KyUInt32 m_countY;
74  Column* m_columns; // row major
75  GrowingSmallBufferPool m_pool;
76  MemoryHeap* m_heap;
77 
78 private:
79  BoxOfArrays<T>(const BoxOfArrays<T>& other);
80 };
81 
82 
83 
84 // --------------------------------- inline implementation ---------------------------------
85 
86 template <class T>
87 void BoxOfArrays<T>::Column::Init(KyUInt32 count_, T* values_)
88 {
89  m_count = count_;
90  m_values = values_;
91 
92  // call constructor on each element of the new buffer
93  for (KyUInt32 i = 0; i < m_count; ++i)
94  ::new(m_values + i) T();
95 }
96 
97 
98 template <class T>
99 void BoxOfArrays<T>::Init(KyUInt32 countX, KyUInt32 countY)
100 {
101  Clear();
102  m_countX = countX;
103  m_countY = countY;
104  m_columns = KY_HEAP_MALLOC(m_heap, Column, countX * countY, MemStat_NavDataGen);
105  memset(m_columns, 0, countX * countY * sizeof(Column)); // sets m_columns[i].count = 0 and m_columns[i].values = KY_NULL
106 }
107 
108 template <class T>
109 T* BoxOfArrays<T>::InitColumn(KyInt32 columnIdx, KyUInt32 count)
110 {
111  T* values = (T*)m_pool.GetNewBuffer(count * sizeof(T));
112  GetColumn(columnIdx).Init(count, values);
113  return values;
114 }
115 
116 template <class T>
117 void BoxOfArrays<T>::Clear()
118 {
119  if (m_columns != KY_NULL)
120  KY_FREE(m_columns);
121  m_columns = KY_NULL;
122 
123  m_countX = 0;
124  m_countY = 0;
125 
126  m_pool.Clear();
127 }
128 
129 template <class T>
130 void BoxOfArrays<T>::Release()
131 {
132  Clear();
133  m_pool.Release();
134 }
135 
136 
137 template <class T>
138 BoxOfArrays<T>& BoxOfArrays<T>::operator=(const BoxOfArrays<T>& other)
139 {
140  Init(other.CountX(), other.CountY());
141 
142  const Column* otherColumns = other.GetColumns();
143  KyUInt32 columnsCount = ColumnsCount();
144 
145  for (KyUInt32 columnIdx = 0; columnIdx < columnsCount; ++columnIdx)
146  {
147  const Column& otherColumn = otherColumns[columnIdx];
148  T* values = InitColumn(columnIdx, otherColumn.m_count);
149  memcpy(values, otherColumn.m_values, otherColumn.m_count * sizeof(T));
150  }
151  return *this;
152 }
153 
154 
155 }
156 
157 
158 #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