Array3-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Array3-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk, Dongmin Kim
5 > Purpose: 3-D array class.
6 > Created Time: 2017/01/25
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_ARRAY3_IMPL_H
10 #define CUBBYFLOW_ARRAY3_IMPL_H
11 
12 #include <algorithm>
13 #include <cassert>
14 
15 namespace CubbyFlow
16 {
17  template <typename T>
19  {
20  // Do nothing
21  }
22 
23  template <typename T>
24  Array<T, 3>::Array(const Size3& size, const T& initVal)
25  {
26  Resize(size, initVal);
27  }
28 
29  template <typename T>
30  Array<T, 3>::Array(size_t width, size_t height, size_t depth, const T& initVal)
31  {
32  Resize(width, height, depth, initVal);
33  }
34 
35  template <typename T>
36  Array<T, 3>::Array(const std::initializer_list<std::initializer_list<std::initializer_list<T>>>& list)
37  {
38  Set(list);
39  }
40 
41  template <typename T>
42  Array<T, 3>::Array(const Array& other)
43  {
44  Set(other);
45  }
46 
47  template <typename T>
49  {
50  *this = std::move(other);
51  }
52 
53  template <typename T>
54  void Array<T, 3>::Set(const T& value)
55  {
56  for (auto& v : m_data)
57  {
58  v = value;
59  }
60  }
61 
62  template <typename T>
63  void Array<T, 3>::Set(const Array& other)
64  {
65  m_data.resize(other.m_data.size());
66  std::copy(other.m_data.begin(), other.m_data.end(), m_data.begin());
67  m_size = other.m_size;
68  }
69 
70  template <typename T>
71  void Array<T, 3>::Set(const std::initializer_list<std::initializer_list<std::initializer_list<T>>>& list)
72  {
73  size_t depth = list.size();
74  size_t height = (depth > 0) ? list.begin()->size() : 0;
75  size_t width = (height > 0) ? list.begin()->begin()->size() : 0;
76 
77  Resize(Size3(width, height, depth));
78 
79  auto depthIter = list.begin();
80  for (size_t k = 0; k < depth; ++k)
81  {
82  assert(height == depthIter->size());
83 
84  auto heightIter = depthIter->begin();
85  for (size_t j = 0; j < height; ++j)
86  {
87  assert(width == heightIter->size());
88 
89  auto widthIter = heightIter->begin();
90  for (size_t i = 0; i < width; ++i)
91  {
92  (*this)(i, j, k) = *widthIter;
93  ++widthIter;
94  }
95  ++heightIter;
96  }
97  ++depthIter;
98  }
99  }
100 
101  template <typename T>
103  {
104  m_size = Size3(0, 0, 0);
105  m_data.clear();
106  }
107 
108  template <typename T>
109  void Array<T, 3>::Resize(const Size3& size, const T& initVal)
110  {
111  Array grid;
112  grid.m_data.resize(size.x * size.y * size.z, initVal);
113  grid.m_size = size;
114 
115  size_t iMin = std::min(size.x, m_size.x);
116  size_t jMin = std::min(size.y, m_size.y);
117  size_t kMin = std::min(size.z, m_size.z);
118 
119  for (size_t k = 0; k < kMin; ++k)
120  {
121  for (size_t j = 0; j < jMin; ++j)
122  {
123  for (size_t i = 0; i < iMin; ++i)
124  {
125  grid(i, j, k) = At(i, j, k);
126  }
127  }
128  }
129 
130  Swap(grid);
131  }
132 
133  template <typename T>
134  void Array<T, 3>::Resize(size_t width, size_t height, size_t depth, const T& initVal)
135  {
136  Resize(Size3(width, height, depth), initVal);
137  }
138 
139  template <typename T>
140  T& Array<T, 3>::At(size_t i)
141  {
142  assert(i < Width() * Height() * Depth());
143  return m_data[i];
144  }
145 
146  template <typename T>
147  const T& Array<T, 3>::At(size_t i) const
148  {
149  assert(i < Width() * Height() * Depth());
150  return m_data[i];
151  }
152 
153  template <typename T>
154  T& Array<T, 3>::At(const Point3UI& pt)
155  {
156  return At(pt.x, pt.y, pt.z);
157  }
158 
159  template <typename T>
160  const T& Array<T, 3>::At(const Point3UI& pt) const
161  {
162  return At(pt.x, pt.y, pt.z);
163  }
164 
165  template <typename T>
166  T& Array<T, 3>::At(size_t i, size_t j, size_t k)
167  {
168  assert(i < Width() && j < Height() && k < Depth());
169  return m_data[i + Width() * j + Width() * Height() * k];
170  }
171 
172  template <typename T>
173  const T& Array<T, 3>::At(size_t i, size_t j, size_t k) const
174  {
175  assert(i < Width() && j < Height() && k < Depth());
176  return m_data[i + Width() * j + Width() * Height() * k];
177  }
178 
179  template <typename T>
181  {
182  return m_size;
183  }
184 
185  template <typename T>
186  size_t Array<T, 3>::Width() const
187  {
188  return m_size.x;
189  }
190 
191  template <typename T>
192  size_t Array<T, 3>::Height() const
193  {
194  return m_size.y;
195  }
196 
197  template <typename T>
198  size_t Array<T, 3>::Depth() const
199  {
200  return m_size.z;
201  }
202 
203  template <typename T>
205  {
206  return m_data.data();
207  }
208 
209  template <typename T>
210  const T* const Array<T, 3>::data() const
211  {
212  return m_data.data();
213  }
214 
215  template <typename T>
217  {
218  return m_data.begin();
219  }
220 
221  template <typename T>
223  {
224  return m_data.cbegin();
225  }
226 
227  template <typename T>
229  {
230  return m_data.end();
231  }
232 
233  template <typename T>
235  {
236  return m_data.cend();
237  }
238 
239  template <typename T>
241  {
242  return ArrayAccessor3<T>(size(), data());
243  }
244 
245  template <typename T>
247  {
248  return ConstArrayAccessor3<T>(size(), data());
249  }
250 
251  template <typename T>
253  {
254  std::swap(other.m_data, m_data);
255  std::swap(other.m_size, m_size);
256  }
257 
258  template <typename T>
259  template <typename Callback>
260  void Array<T, 3>::ForEach(Callback func) const
261  {
262  ConstAccessor().ForEach(func);
263  }
264 
265  template <typename T>
266  template <typename Callback>
267  void Array<T, 3>::ForEachIndex(Callback func) const
268  {
269  ConstAccessor().ForEachIndex(func);
270  }
271 
272  template <typename T>
273  template <typename Callback>
274  void Array<T, 3>::ParallelForEach(Callback func)
275  {
276  Accessor().ParallelForEach(func);
277  }
278 
279  template <typename T>
280  template <typename Callback>
281  void Array<T, 3>::ParallelForEachIndex(Callback func) const
282  {
283  ConstAccessor().ParallelForEachIndex(func);
284  }
285 
286  template <typename T>
288  {
289  return m_data[i];
290  }
291 
292  template <typename T>
293  const T& Array<T, 3>::operator[](size_t i) const
294  {
295  return m_data[i];
296  }
297 
298  template <typename T>
299  T& Array<T, 3>::operator()(size_t i, size_t j, size_t k)
300  {
301  assert(i < Width() && j < Height() && k < Depth());
302  return m_data[i + Width() * j + Width() * Height() * k];
303  }
304 
305  template <typename T>
306  const T& Array<T, 3>::operator()(size_t i, size_t j, size_t k) const
307  {
308  assert(i < Width() && j < Height() && k < Depth());
309  return m_data[i + Width() * j + Width() * Height() * k];
310  }
311 
312  template <typename T>
314  {
315  assert(pt.x < Width() && pt.y < Height() && pt.z < Depth());
316  return m_data[pt.x + Width() * pt.y + Width() * Height() * pt.z];
317  }
318 
319  template <typename T>
320  const T& Array<T, 3>::operator()(const Point3UI &pt) const
321  {
322  assert(pt.x < Width() && pt.y < Height() && pt.z < Depth());
323  return m_data[pt.x + Width() * pt.y + Width() * Height() * pt.z];
324  }
325 
326  template <typename T>
328  {
329  Set(value);
330  return *this;
331  }
332 
333  template <typename T>
335  {
336  Set(other);
337  return *this;
338  }
339 
340  template <typename T>
342  {
343  m_data = std::move(other.m_data);
344  m_size = other.m_size;
345  other.m_size = Size3();
346  return *this;
347  }
348 
349  template <typename T>
350  Array<T, 3>& Array<T, 3>::operator=(const std::initializer_list<std::initializer_list<std::initializer_list<T>>>& list)
351  {
352  Set(list);
353  return *this;
354  }
355 
356  template <typename T>
358  {
359  return Accessor();
360  }
361 
362  template <typename T>
364  {
365  return ConstAccessor();
366  }
367 }
368 
369 #endif
3-D array accessor class.
Definition: ArrayAccessor3.h:31
3-D read-only array accessor class.
Definition: ArrayAccessor3.h:269
3-D point class.
Definition: Point3.h:26
Definition: pybind11Utils.h:24
Generic N-dimensional array class interface.
Definition: Array.h:28
T z
Z (or the third) component of the point.
Definition: Point3.h:38
T y
Y (or the second) component of the point.
Definition: Point3.h:35
T x
X (or the first) component of the point.
Definition: Point3.h:29
3-D array class.
Definition: Array3.h:45
Point3< size_t > Size3
Definition: Size3.h:16