gwnavgeneration/common/boxofarrays.h Source File

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