1 #ifndef MHO_NDArrayView_HH__
2 #define MHO_NDArrayView_HH__
36 typedef std::integral_constant< std::size_t, RANK >
rank;
39 MHO_NDArrayView(XValueType* ptr,
const std::size_t* dim,
const std::size_t* strides) { Construct(ptr, dim, strides); };
63 for(j = 0; j < RANK; j++)
75 for(std::size_t i = 0; i <
fSize; i++)
77 MHO_NDArrayMath::IncrementIndices< RANK >(&(
fDims[0]), &(idx[0]));
83 msg_error(
"containers",
"array view copy failed due to mismatched sizes on dimension: " << j <<
"." << eom);
92 std::size_t
GetRank()
const {
return RANK; }
115 for(std::size_t i = 0; i < RANK; i++)
150 for(std::size_t i = 0; i < RANK; i++)
177 template<
typename... XIndexTypeS >
178 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK), XValueType& >::type
operator()(XIndexTypeS... idx)
180 fTmp = {{
static_cast< size_t >(idx)...}};
190 template<
typename... XIndexTypeS >
191 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK),
const XValueType& >::type
194 fTmp = {{
static_cast< size_t >(idx)...}};
204 template<
typename... XIndexTypeS >
205 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK), XValueType& >::type
at(XIndexTypeS... idx)
208 fTmp = {{
static_cast< size_t >(idx)...}};
215 throw std::out_of_range(
"MHO_NDArrayView::at() indices out of range.");
225 template<
typename... XIndexTypeS >
226 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK),
const XValueType& >::type
at(XIndexTypeS... idx)
const
229 fTmp = {{
static_cast< size_t >(idx)...}};
236 throw std::out_of_range(
"MHO_NDArrayView::at() indices out of range.");
255 auto bit = this->
begin();
256 auto eit = this->
end();
257 for(
auto it = bit; it != eit; ++it)
268 auto bit = this->
begin();
269 auto eit = this->
end();
270 for(
auto it = bit; it != eit; ++it)
281 return MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(
fStrides[0]), index);
290 MHO_NDArrayMath::RowMajorIndexFromOffset< RANK >(offset, &(
fDims[0]), &(index[0]));
301 template<
typename T >
302 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
303 std::is_floating_point< T >::value,
307 auto bit = this->
begin();
308 auto eit = this->
end();
309 for(
auto it = bit; it != eit; ++it)
319 template<
typename T >
320 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
321 std::is_floating_point< T >::value,
325 auto bit = this->
begin();
326 auto eit = this->
end();
327 for(
auto it = bit; it != eit; ++it)
337 template<
typename T >
338 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
339 std::is_floating_point< T >::value,
343 auto bit = this->
begin();
344 auto eit = this->
end();
345 for(
auto it = bit; it != eit; ++it)
357 if(!HaveSameNumberOfElements(
this, &anArray))
359 throw std::out_of_range(
"MHO_NDArrayView::*= size mismatch.");
361 auto bit1 = this->
begin();
362 auto bit2 = anArray.
cbegin();
365 for(std::size_t i = 0; i <
fSize; i++)
379 if(!HaveSameNumberOfElements(
this, &anArray))
381 throw std::out_of_range(
"MHO_NDArrayView::+= size mismatch.");
383 auto bit1 = this->
begin();
384 auto bit2 = anArray.
cbegin();
387 for(std::size_t i = 0; i <
fSize; i++)
401 if(!HaveSameNumberOfElements(
this, &anArray))
403 throw std::out_of_range(
"MHO_NDArrayView::-= size mismatch.");
405 auto bit1 = this->
begin();
406 auto bit2 = anArray.
cbegin();
409 for(std::size_t i = 0; i <
fSize; i++)
420 return MHO_NDArrayMath::CheckIndexValidity< RANK >(&(
fDims[0]), &(idx[0]));
425 return fDataPtr[MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(
fStrides[0]), &(idx[0]))];
430 return fDataPtr[MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(
fStrides[0]), &(idx[0]))];
443 void Construct(XValueType* ptr,
const std::size_t* dim,
const std::size_t* strides)
446 for(std::size_t i = 0; i < RANK; i++)
453 if(ptr ==
nullptr || dim ==
nullptr || strides ==
nullptr)
455 msg_error(
"containers",
"cannot construct array slice." << eom);
461 for(std::size_t i = 0; i < RANK; i++)
466 fSize = MHO_NDArrayMath::TotalArraySize< RANK >(&(
fDims[0]));
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
Class MHO_BidirectionalIndexedIterator.
Definition: MHO_BidirectionalIndexedIterator.hh:26
MHO_NDArrayView is a class to represent a view (slice) of a n-dimensional array Thu 13 Aug 2020 02:53...
Definition: MHO_NDArrayView.hh:32
const std::size_t * GetStrides() const
get element strides
Definition: MHO_NDArrayView.hh:141
MHO_NDArrayView & operator*=(const MHO_NDArrayView &anArray)
operator*= in place point-wise multiplication by another array
Definition: MHO_NDArrayView.hh:355
index_type GetIndicesForOffset(std::size_t offset)
invert (memory) offset into array to indexes of the associated element
Definition: MHO_NDArrayView.hh:287
MHO_NDArrayView & operator=(const MHO_NDArrayView &rhs)
Definition: MHO_NDArrayView.hh:241
index_type GetStrideArray() const
Getter for stride array.
Definition: MHO_NDArrayView.hh:161
XValueType * fDataPtr
Definition: MHO_NDArrayView.hh:434
MHO_NDArrayView(XValueType *ptr, const std::size_t *dim, const std::size_t *strides)
Definition: MHO_NDArrayView.hh:39
std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or std::is_floating_point< T >::value, MHO_NDArrayView & >::type operator-=(T aScalar)
operator-= in place subtraction by a scalar amount
Definition: MHO_NDArrayView.hh:341
std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or std::is_floating_point< T >::value, MHO_NDArrayView & >::type operator*=(T aScalar)
operator*= in place multiplication by a scalar factor
Definition: MHO_NDArrayView.hh:305
index_type fTmp
Definition: MHO_NDArrayView.hh:438
std::enable_if<(sizeof...(XIndexTypeS)==RANK), XValueType & >::type operator()(XIndexTypeS... idx)
access operator, accepts multiple indices (,,...,) but does no bounds checking
Definition: MHO_NDArrayView.hh:178
std::enable_if<(sizeof...(XIndexTypeS)==RANK), const XValueType & >::type at(XIndexTypeS... idx) const
at(): same as const operator(...) but with bounds checking with bounds checking
Definition: MHO_NDArrayView.hh:226
const XValueType & ValueAt(const index_type &idx) const
Definition: MHO_NDArrayView.hh:428
std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or std::is_floating_point< T >::value, MHO_NDArrayView & >::type operator+=(T aScalar)
operator+= in place addition by a scalar amount
Definition: MHO_NDArrayView.hh:323
MHO_BidirectionalIndexedIterator< XValueType, RANK > iterator
Definition: MHO_NDArrayView.hh:471
void Copy(const MHO_NDArrayView &rhs)
copy functionality, calling array view must have same shape as rhs
Definition: MHO_NDArrayView.hh:58
MHO_NDArrayView * Clone()
clone functionality - Creates a deep copy of this MHO_NDArrayView object.
Definition: MHO_NDArrayView.hh:51
std::size_t GetOffsetForIndices(const std::size_t *index)
compute (memory) offset into array from a set of indexes
Definition: MHO_NDArrayView.hh:279
iterator begin()
Definition: MHO_NDArrayView.hh:474
void ZeroArray()
set all elements in the array to zero
Definition: MHO_NDArrayView.hh:266
index_type fDims
Definition: MHO_NDArrayView.hh:435
void SetArray(const XValueType &obj)
set all elements in the array to a certain value
Definition: MHO_NDArrayView.hh:253
void GetDimensions(std::size_t *dim) const
get the dimensions/shape of the array
Definition: MHO_NDArrayView.hh:113
const std::size_t * GetDimensions() const
get the dimensions/shape of the array
Definition: MHO_NDArrayView.hh:106
std::array< std::size_t, RANK > index_type
Definition: MHO_NDArrayView.hh:35
std::size_t GetDimension(std::size_t idx) const
Getter for a single dimension dimension.
Definition: MHO_NDArrayView.hh:134
std::enable_if<(sizeof...(XIndexTypeS)==RANK), XValueType & >::type at(XIndexTypeS... idx)
at(): same as operator(...) but with bounds checking with bounds checking
Definition: MHO_NDArrayView.hh:205
bool CheckIndexValidity(const index_type &idx) const
Definition: MHO_NDArrayView.hh:418
iterator end()
Definition: MHO_NDArrayView.hh:476
const_iterator citerator_at(std::size_t offset) const
Definition: MHO_NDArrayView.hh:487
MHO_NDArrayView & operator+=(const MHO_NDArrayView &anArray)
operator+= in place point-wise addition by another array
Definition: MHO_NDArrayView.hh:377
XValueType & ValueAt(const index_type &idx)
Definition: MHO_NDArrayView.hh:423
std::size_t GetStride(std::size_t idx) const
Getter for stride at index.
Definition: MHO_NDArrayView.hh:169
const_iterator cend() const
Definition: MHO_NDArrayView.hh:485
index_type GetDimensionArray() const
get the dimensions/shape of the array as std::array
Definition: MHO_NDArrayView.hh:126
MHO_BidirectionalIndexedIterator< XValueType, RANK > const_iterator
Definition: MHO_NDArrayView.hh:472
XValueType value_type
Definition: MHO_NDArrayView.hh:34
uint64_t fSize
Definition: MHO_NDArrayView.hh:437
iterator iterator_at(std::size_t offset)
Definition: MHO_NDArrayView.hh:478
MHO_NDArrayView & operator-=(const MHO_NDArrayView &anArray)
operator-= in place point-wise subtraction by another array
Definition: MHO_NDArrayView.hh:399
virtual ~MHO_NDArrayView()
Definition: MHO_NDArrayView.hh:44
MHO_NDArrayView(const MHO_NDArrayView &obj)
Definition: MHO_NDArrayView.hh:41
std::enable_if<(sizeof...(XIndexTypeS)==RANK), const XValueType & >::type operator()(XIndexTypeS... idx) const
const reference access operator, accepts multiple indices (,,...,) but does no bounds checking
Definition: MHO_NDArrayView.hh:192
std::size_t GetSize() const
get the total size of the array view
Definition: MHO_NDArrayView.hh:99
void GetStrides(std::size_t *strd) const
Getter for strides array (fills passed array)
Definition: MHO_NDArrayView.hh:148
std::integral_constant< std::size_t, RANK > rank
Definition: MHO_NDArrayView.hh:36
const_iterator cbegin() const
Definition: MHO_NDArrayView.hh:483
index_type fStrides
Definition: MHO_NDArrayView.hh:436
std::size_t GetRank() const
Getter for rank of the array view.
Definition: MHO_NDArrayView.hh:92
#define min(a, b)
Definition: max555.c:9
Definition: MHO_AdhocFlagging.hh:18