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