Proto  3.2
Proto_Array.H
Go to the documentation of this file.
1 /* This class is only necessary because HIP doesn't support STL containers,
2  so std::array can't have any of it's member functions/operators accessed
3  on the device.
4 */
5 #pragma once
6 #ifndef PROTO_ARRAY_H
7 #define PROTO_ARRAY_H
8 
9 #include <array>
10 #include <iostream>
11 #include <cmath>
12 #include <limits>
13 
14 #include "Proto_Accel.H"
15 #include "Proto_Point.H"
16 
17 namespace Proto {
18 
19 template<typename T, size_t N>
21 template<typename T, size_t N>
23 
24 /// @brief A templated constant size array object similar to std::array, but with the ability to be used inside of device kernels
25 /// @tparam T a plain-old-data type (int, double, etc)
26 /// @tparam N static size of the array
27 template<typename T, size_t N>
28 class Array {
29 
30 public:
31 
32  /// @brief Shortcut for creating an Array filled with zeros
33  /// @return An Array of zeros
35  inline static Array<T,N> Zeros();
36 
37  /// @brief Shortcut for creating an isotropic array
38  /// @param scale - An optional scale. The output vector will be [scale, scale, scale, ...]. [Default: 1]
39  /// @return An isotropic Array multiplied by scale
41  inline static Array<T,N> Ones(T scale = 1.0);
42 
43  /// @brief Constructs an Array with uninitialized values
45  inline Array();
46 
47  /// @brief Constructs an Array with all values set to initValue
48  /// @param initValue - The initial value of all elements of the Array
50  inline Array(T initValue);
51 
52  /// @brief Constructs an Array using initializer_list syntax
53  /**
54  * Example:
55  * @code
56  * Array<int, 5> a = {1,2,3,4,5};
57  * @endcode
58  */
60  inline Array(std::initializer_list<T> list);
61 
63  inline Array(const Array<T,N> &arr);
64 
66  inline Array<T,N>& operator=(const Array<T,N> &arr);
67 
69  inline Array(Array<T,N> &&arr) = default;
70 
72  inline Array<T,N>& operator=(Array<T,N> &&arr) = default;
73 
75  inline Array<T,N>& operator=(const std::array<T,N> &arr);
76 
77  /// @brief Implicit Cast to std::array
78  /// @return
80  operator std::array<T,N>() const
81  {
82  std::array<T,N> arr;
83  for (int ii = 0; ii < N; ii++)
84  {
85  arr[ii] = (*this)[ii];
86  }
87  return arr;
88  }
89 
90  /// @brief Constructs an int-valued Array from a Point
92  inline Array(const Point& point);
93 
94  /// @brief Constructs an Array from a std::array
96  inline Array(const std::array<T,N>& arr);
97 
99  inline ~Array() = default;
100 
101  // Deprecated
102  // ACCEL_DECORATION
103  // inline void reset();
104 
105  /// @brief Sets all values of *this to value
107  inline void fill(T value);
108 
109  /// @brief Returns a raw pointer to the data buffer of *this
111  inline T* data();
112 
113  /// @brief Returns a const raw pointer to the data buffer of *this
115  inline const T* data() const;
116 
117  /// @brief Returns a reference to the element at index i
119  inline T& operator[](size_t i);
120 
121  /// @brief Returns a const reference to the element at index i
123  inline const T& operator[](size_t i) const;
124 
125  /// @brief Equality operator. Defined by value.
127  inline bool operator==(const Array<T,N> &rhs) const;
128 
129  /// @brief Inequality operator. Defined by value.
131  inline bool operator!=(const Array<T,N> &rhs) const { return !(*this==rhs); }
132 
133  /// @brief Vector addition operator. Returns a new Array of the same size as *this.
134  /// if M < N, the elements of rhs are added to the first M elements of the output
135  /// @tparam M - The number of elements in rhs
136  /// @param rhs - Another Array
137  /// @return The elementwise sum of *this with rhs on the first M elements
138  template<size_t M>
140  inline Array<T,N> operator+(const Array<T,M>& rhs) const;
141 
142  /// @brief Scalar addition operator. Returns a new Array containing the values of *this + value
144  inline Array<T,N> operator+(T value) const;
145 
146  /// @brief Vector in-place addition operator. The first M elements of rhs are added to this.
147  /// @tparam M - The number of elements in rhs
148  /// @param rhs - Another Array
149  template<size_t M>
151  inline void operator+=(const Array<T,M>& rhs);
152 
153  /// @brief Scalar in-place addition operator. Adds value to each element of *this
155  inline void operator+=(T value);
156 
157  /// @brief Vector subtraction operator. Returns a new Array of the same size as *this.
158  /// if M < N, the elements of rhs are subtracted from the first M elements of the output
159  /// @tparam M - The number of elements in rhs
160  /// @param rhs - Another Array
161  /// @return The elementwise difference of *this with rhs on the first M elements
162  template<size_t M>
164  inline Array<T,N> operator-(const Array<T,M>& rhs) const;
165 
166  /// @brief Scalar subtraction operator. Subtracts value from each element of *this
168  inline Array<T,N> operator-(T value) const;
169 
170  /// @brief Vector in-place subtraction operator. The first M elements of rhs are subtracted from this.
171  /// @tparam M - The number of elements in rhs
172  /// @param rhs - Another Array
173  template<size_t M>
175  inline void operator-=(const Array<T,M>& rhs);
176 
177  /// @brief Scalar in-place subtraction operator. Subtracts value from each element of *this
179  inline void operator-=(T value);
180 
181  /// @brief Vector product operator. Returns a new Array of the same size as *this.
182  /// if M < N, the elements of rhs are multiplied with the first M elements of the output
183  /// @tparam M - The number of elements in rhs
184  /// @param rhs - Another Array
185  /// @return The elementwise product of *this with rhs on the first M elements
186  template<size_t M>
188  inline Array<T,N> operator*(const Array<T,M>& rhs) const;
189 
190  /// @brief Scalar multiplication operator. Multiplies each element of *this by value
192  inline Array<T,N> operator*(T value) const;
193 
194  /// @brief Vector in-place multiplication operator. The first M elements of rhs are multplied into this.
195  /// @tparam M - The number of elements in rhs
196  /// @param rhs - Another Array
197  template<size_t M>
199  inline void operator*=(const Array<T,M>& rhs);
200 
201  /// @brief Scalar in-place multiplication operator. Multiplies each value of *this by value.
203  inline void operator*=(T value);
204 
205  /// @brief Vector quotient operator. Returns a new Array of the same size as *this.
206  /// if M < N, the elements of rhs are divided by the first M elements of the output.
207  /// Fails by assertion if any element of rhs contains zero
208  /// @tparam M - The number of elements in rhs
209  /// @param rhs - Another Array
210  /// @return The elementwise quotient of *this with rhs on the first M elements
211  template<size_t M>
213  inline Array<T,N> operator/(const Array<T,M>& nonZeroRhs) const;
214 
215  /// @brief Scalar quotient operator. Divides each element of *this by value.
216  /// Fails by assertion if value is zero.
218  inline Array<T,N> operator/(T nonZeroValue) const;
219 
220  /// @brief Vector in-place quotient operator. The first M elements this are divided by rhs elementwise
221  /// Fails by assertion if any element of rhs is zero.
222  /// @tparam M - The number of elements in rhs
223  /// @param rhs - Another Array
224  template<size_t M>
226  inline void operator/=(const Array<T,M>& nonZeroRhs);
227 
228  /// @brief Scalar in-place quotient operator. Divides each value of *this by value.
229  /// Fails by assertion of value is zero.
231  inline void operator/=(T nonZeroValue);
232 
233  /// @brief Returns the maximum value of this
235  inline T max() const;
236 
237  /// @brief Returns the minimum value of this
239  inline T min() const;
240 
241  /// @brief Returns the maximum value of the absolute value of this
243  inline T absMax() const;
244 
245  /// @brief Returns the sum of the elements of this
247  inline T sum() const;
248 
249  /// @brief Returns the dot product of this with rhs.
250  /// The dot product is defined as the sum of the elementwise products of the elements of the two Arrays.
252  inline T dot(const Array<T,N> &rhs) const;
253 
254  /// @brief Returns the product of the elements of this.
256  inline T product() const;
257 
258  /// @brief Returns the L2 norm of the elements of this.
259  /// The L2 norm is defined as the square root of the sum of the squared elements of this
261  inline T norm() const; //square root of sum of squares
262 
264  inline ArrayIterator<T,N> begin();
265 
267  inline ConstArrayIterator<T,N> begin() const;
268 
270  inline ArrayIterator<T,N> end();
271 
273  inline ConstArrayIterator<T,N> end() const;
274 
275  /// @brief Returns a string representation of this
276  inline std::string str() const;
277 
278  /// @brief Print a representation of this to a std::ostream [Default: std::cout]
279  inline void print(std::ostream& stream = std::cout) const;
280 
281 private:
282  T m_data[N];
283 };
284 
285 template<typename T, size_t N>
286 class ArrayIterator
287 {
288  public:
289  inline ArrayIterator(Array<T, N>& arr, int index)
290  {
291  m_data = &arr;
292  m_index = index;
293  }
294 
295  inline T& operator*() {return (*m_data)[m_index]; }
296  inline bool operator==(const ArrayIterator<T, N>& iter) const
297  {
298  if (iter.m_data != m_data) {return false;}
299  if (iter.m_index != m_index) {return false;}
300  return true;
301  }
302  inline bool operator !=(const ArrayIterator<T, N>& iter) const
303  {
304  return !(*this == iter);
305  }
306  inline ArrayIterator& operator++() {m_index++; return *this; }
307  inline ArrayIterator& operator--() {m_index--; return *this; }
308  private:
310  int m_index;
311 };
312 
313 template<typename T, size_t N>
314 class ConstArrayIterator
315 {
316  public:
317  inline ConstArrayIterator(const Array<T, N>& arr, int index)
318  {
319  m_data = &arr;
320  m_index = index;
321  }
322 
323  inline const T& operator*() {return (*m_data)[m_index]; }
324  inline bool operator==(const ConstArrayIterator<T, N>& iter) const
325  {
326  if (iter.m_data != m_data) {return false;}
327  if (iter.m_index != m_index) {return false;}
328  return true;
329  }
330  inline bool operator !=(const ConstArrayIterator<T, N>& iter) const
331  {
332  return !(*this == iter);
333  }
334  inline ConstArrayIterator& operator++() {m_index++; return *this; }
335  inline ConstArrayIterator& operator--() {m_index--; return *this; }
336  private:
338  int m_index;
339 };
340 
341 /// @brief Premultiplication by a scalar int
342 template<typename T, size_t N>
344 inline Array<T,N>& operator*(int scale, Array<T,N>& arr);
345 
346 /// @brief Premultiplication by a scalar double
347 template<typename T, size_t N>
349 inline Array<T,N>& operator*(double scale, Array<T,N>& arr);
350 
351 /// @brief Unary negation
352 template<typename T, size_t N>
354 inline Array<T,N>& operator-(Array<T,N>& arr);
355 
356 /// @brief Ostream operator
357 template<typename T, size_t N>
358 inline std::ostream& operator<<(std::ostream& stream, const Array<T,N>& arr);
359 
360 }
361 
362 #endif
ACCEL_DECORATION T max() const
Returns the maximum value of this.
ACCEL_DECORATION T sum() const
Returns the sum of the elements of this.
std::string str() const
Returns a string representation of this.
ACCEL_DECORATION Array< T, N > operator-(const Array< T, M > &rhs) const
Vector subtraction operator. Returns a new Array of the same size as *this. if M < N...
Definition: Proto_Array.H:22
ACCEL_DECORATION void operator-=(const Array< T, M > &rhs)
Vector in-place subtraction operator. The first M elements of rhs are subtracted from this...
ACCEL_DECORATION ~Array()=default
ACCEL_DECORATION void fill(T value)
Sets all values of *this to value.
ACCEL_DECORATION T & operator[](size_t i)
Returns a reference to the element at index i.
ACCEL_DECORATION T min() const
Returns the minimum value of this.
ArrayIterator & operator--()
Definition: Proto_Array.H:307
ACCEL_DECORATION void operator*=(const Array< T, M > &rhs)
Vector in-place multiplication operator. The first M elements of rhs are multplied into this...
static ACCEL_DECORATION Array< T, N > Ones(T scale=1.0)
Shortcut for creating an isotropic array.
ConstArrayIterator & operator--()
Definition: Proto_Array.H:335
ACCEL_DECORATION void operator/=(const Array< T, M > &nonZeroRhs)
Vector in-place quotient operator. The first M elements this are divided by rhs elementwise Fails by ...
ACCEL_DECORATION T * data()
Returns a raw pointer to the data buffer of *this.
ACCEL_DECORATION void operator+=(const Array< T, M > &rhs)
Vector in-place addition operator. The first M elements of rhs are added to this. ...
ACCEL_DECORATION bool operator==(const Array< T, N > &rhs) const
Equality operator. Defined by value.
ACCEL_DECORATION T product() const
Returns the product of the elements of this.
bool operator==(const ArrayIterator< T, N > &iter) const
Definition: Proto_Array.H:296
#define ACCEL_DECORATION
Definition: Proto_Accel.H:12
ACCEL_DECORATION ArrayIterator< T, N > end()
ACCEL_DECORATION Array()
Constructs an Array with uninitialized values.
bool operator==(const ConstArrayIterator< T, N > &iter) const
Definition: Proto_Array.H:324
ACCEL_DECORATION T norm() const
Returns the L2 norm of the elements of this. The L2 norm is defined as the square root of the sum of ...
static ACCEL_DECORATION Array< T, N > Zeros()
Shortcut for creating an Array filled with zeros.
void print(std::ostream &stream=std::cout) const
Print a representation of this to a std::ostream [Default: std::cout].
ACCEL_DECORATION Array< T, N > & operator=(const Array< T, N > &arr)
int m_index
Definition: Proto_Array.H:310
Definition: Proto_Array.H:17
uint64_t index
Definition: Proto_MBBoxPartition.H:15
ACCEL_DECORATION T dot(const Array< T, N > &rhs) const
Returns the dot product of this with rhs. The dot product is defined as the sum of the elementwise pr...
ACCEL_DECORATION Array< T, N > operator/(const Array< T, M > &nonZeroRhs) const
Vector quotient operator. Returns a new Array of the same size as *this. if M < N, the elements of rhs are divided by the first M elements of the output. Fails by assertion if any element of rhs contains zero.
T m_data[N]
Definition: Proto_Array.H:282
Definition: Proto_Array.H:20
Array< T, N > * m_data
Definition: Proto_Array.H:309
A templated constant size array object similar to std::array, but with the ability to be used inside ...
Definition: Proto_Array.H:28
ACCEL_DECORATION ArrayIterator< T, N > begin()
Integer Valued Vector.
Definition: Proto_Point.H:24
ConstArrayIterator(const Array< T, N > &arr, int index)
Definition: Proto_Array.H:317
ArrayIterator(Array< T, N > &arr, int index)
Definition: Proto_Array.H:289
int m_index
Definition: Proto_Array.H:338
ACCEL_DECORATION T absMax() const
Returns the maximum value of the absolute value of this.
ConstArrayIterator & operator++()
Definition: Proto_Array.H:334
ArrayIterator & operator++()
Definition: Proto_Array.H:306
T & operator*()
Definition: Proto_Array.H:295
const Array< T, N > * m_data
Definition: Proto_Array.H:337
ACCEL_DECORATION Array< T, N > operator+(const Array< T, M > &rhs) const
Vector addition operator. Returns a new Array of the same size as *this. if M < N, the elements of rhs are added to the first M elements of the output.
ACCEL_DECORATION bool operator!=(const Array< T, N > &rhs) const
Inequality operator. Defined by value.
Definition: Proto_Array.H:131
ACCEL_DECORATION Array< T, N > operator*(const Array< T, M > &rhs) const
Vector product operator. Returns a new Array of the same size as *this. if M < N, the elements of rhs...
const T & operator*()
Definition: Proto_Array.H:323