ArrayBase-Impl.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_ARRAY_BASE_IMPL_HPP
12 #define CUBBYFLOW_ARRAY_BASE_IMPL_HPP
13 
14 namespace CubbyFlow
15 {
16 template <typename T, size_t N, typename D>
17 size_t ArrayBase<T, N, D>::Index(size_t i) const
18 {
19  return i;
20 }
21 
22 template <typename T, size_t N, typename D>
23 template <typename... Args>
24 size_t ArrayBase<T, N, D>::Index(size_t i, Args... args) const
25 {
26  static_assert(sizeof...(args) == N - 1, "Invalid number of indices.");
27 
28  return i + m_size[0] * IndexInternal(1, args...);
29 }
30 
31 template <typename T, size_t N, typename D>
32 template <size_t... I>
34 {
35  return IndexInternal(idx, std::make_index_sequence<N>{});
36 }
37 
38 template <typename T, size_t N, typename D>
40 {
41  return m_ptr;
42 }
43 
44 template <typename T, size_t N, typename D>
45 const T* ArrayBase<T, N, D>::data() const
46 {
47  return m_ptr;
48 }
49 
50 template <typename T, size_t N, typename D>
52 {
53  return m_size;
54 }
55 
56 template <typename T, size_t N, typename D>
57 template <size_t M>
58 std::enable_if_t<(M > 0), size_t> ArrayBase<T, N, D>::Width() const
59 {
60  return m_size.x;
61 }
62 
63 template <typename T, size_t N, typename D>
64 template <size_t M>
65 std::enable_if_t<(M > 1), size_t> ArrayBase<T, N, D>::Height() const
66 {
67  return m_size.y;
68 }
69 
70 template <typename T, size_t N, typename D>
71 template <size_t M>
72 std::enable_if_t<(M > 2), size_t> ArrayBase<T, N, D>::Depth() const
73 {
74  return m_size.z;
75 }
76 
77 template <typename T, size_t N, typename D>
79 {
80  return Length() == 0;
81 }
82 
83 template <typename T, size_t N, typename D>
85 {
86  return Product<size_t, N>(m_size, 1);
87 }
88 
89 template <typename T, size_t N, typename D>
91 {
92  return m_ptr;
93 }
94 
95 template <typename T, size_t N, typename D>
97 {
98  return m_ptr;
99 }
100 
101 template <typename T, size_t N, typename D>
103 {
104  return m_ptr + Length();
105 }
106 
107 template <typename T, size_t N, typename D>
109 {
110  return m_ptr + Length();
111 }
112 
113 template <typename T, size_t N, typename D>
115 {
116  return end() - 1;
117 }
118 
119 template <typename T, size_t N, typename D>
121 {
122  return end() - 1;
123 }
124 
125 template <typename T, size_t N, typename D>
127 {
128  return begin() - 1;
129 }
130 
131 template <typename T, size_t N, typename D>
133 {
134  return begin() - 1;
135 }
136 
137 template <typename T, size_t N, typename D>
139 {
140  return m_ptr[i];
141 }
142 
143 template <typename T, size_t N, typename D>
144 const T& ArrayBase<T, N, D>::At(size_t i) const
145 {
146  return m_ptr[i];
147 }
148 
149 template <typename T, size_t N, typename D>
150 template <typename... Args>
151 T& ArrayBase<T, N, D>::At(size_t i, Args... args)
152 {
153  return data()[Index(i, args...)];
154 }
155 
156 template <typename T, size_t N, typename D>
157 template <typename... Args>
158 const T& ArrayBase<T, N, D>::At(size_t i, Args... args) const
159 {
160  return m_ptr[Index(i, args...)];
161 }
162 
163 template <typename T, size_t N, typename D>
165 {
166  return data()[Index(idx)];
167 }
168 
169 template <typename T, size_t N, typename D>
170 const T& ArrayBase<T, N, D>::At(const Vector<size_t, N>& idx) const
171 {
172  return data()[Index(idx)];
173 }
174 
175 template <typename T, size_t N, typename D>
177 {
178  return At(i);
179 }
180 
181 template <typename T, size_t N, typename D>
182 const T& ArrayBase<T, N, D>::operator[](size_t i) const
183 {
184  return At(i);
185 }
186 
187 template <typename T, size_t N, typename D>
188 template <typename... Args>
189 T& ArrayBase<T, N, D>::operator()(size_t i, Args... args)
190 {
191  return At(i, args...);
192 }
193 
194 template <typename T, size_t N, typename D>
195 template <typename... Args>
196 const T& ArrayBase<T, N, D>::operator()(size_t i, Args... args) const
197 {
198  return At(i, args...);
199 }
200 
201 template <typename T, size_t N, typename D>
203 {
204  return At(idx);
205 }
206 
207 template <typename T, size_t N, typename D>
209 {
210  return At(idx);
211 }
212 
213 template <typename T, size_t N, typename D>
215 {
216  // Do nothing
217 }
218 
219 template <typename T, size_t N, typename D>
221 {
222  SetPtrAndSize(other.m_ptr, other.m_size);
223 }
224 
225 template <typename T, size_t N, typename D>
227 {
228  *this = std::move(other);
229 }
230 
231 template <typename T, size_t N, typename D>
233 {
234  SetPtrAndSize(other.data(), other.Size());
235 
236  return *this;
237 }
238 
239 template <typename T, size_t N, typename D>
241 {
242  SetPtrAndSize(other.data(), other.Size());
243  other.SetPtrAndSize(nullptr, Vector<size_t, N>{});
244 
245  return *this;
246 }
247 
248 template <typename T, size_t N, typename D>
249 template <typename... Args>
250 void ArrayBase<T, N, D>::SetPtrAndSize(T* ptr, size_t ni, Args... args)
251 {
252  SetPtrAndSize(ptr, Vector<size_t, N>{ ni, args... });
253 }
254 
255 template <typename T, size_t N, typename D>
257 {
258  m_ptr = data;
259  m_size = size;
260 }
261 
262 template <typename T, size_t N, typename D>
264 {
265  SetPtrAndSize(nullptr, Vector<size_t, N>{});
266 }
267 
268 template <typename T, size_t N, typename D>
270 {
271  std::swap(m_ptr, other.m_ptr);
272  std::swap(m_size, other.m_size);
273 }
274 
275 template <typename T, size_t N, typename D>
276 template <typename... Args>
277 size_t ArrayBase<T, N, D>::IndexInternal(size_t d, size_t i, Args... args) const
278 {
279  return i + m_size[d] * IndexInternal(d + 1, args...);
280 }
281 
282 template <typename T, size_t N, typename D>
283 size_t ArrayBase<T, N, D>::IndexInternal(size_t, size_t i) const
284 {
285  return i;
286 }
287 
288 template <typename T, size_t N, typename D>
289 template <size_t... I>
291  std::index_sequence<I...>) const
292 {
293  return Index(idx[I]...);
294 }
295 } // namespace CubbyFlow
296 
297 #endif
Reference operator[](size_t i)
Definition: ArrayBase-Impl.hpp:176
Definition: ArrayBase.hpp:19
Iterator end()
Definition: ArrayBase-Impl.hpp:102
Pointer m_ptr
Definition: ArrayBase.hpp:124
Vector< size_t, N > m_size
Definition: ArrayBase.hpp:125
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
Iterator begin()
Definition: ArrayBase-Impl.hpp:90
size_t Length() const
Definition: ArrayBase-Impl.hpp:84
Reference operator()(size_t i, Args... args)
Iterator rbegin()
Definition: ArrayBase-Impl.hpp:114
Pointer data()
Definition: ArrayBase-Impl.hpp:39
ArrayBase()
Definition: ArrayBase-Impl.hpp:214
bool IsEmpty() const
Definition: ArrayBase-Impl.hpp:78
const T * ConstIterator
Definition: ArrayBase.hpp:29
const Vector< size_t, N > & Size() const
Definition: ArrayBase-Impl.hpp:51
size_t Index(size_t i) const
Definition: ArrayBase-Impl.hpp:17
Iterator rend()
Definition: ArrayBase-Impl.hpp:126
Reference At(size_t i)
Definition: ArrayBase-Impl.hpp:138
T * Iterator
Definition: ArrayBase.hpp:28
void SetPtrAndSize(Pointer ptr, size_t ni, Args... args)
Definition: ArrayBase-Impl.hpp:250