1 #ifndef MHO_NDArrayView_HH__
2 #define MHO_NDArrayView_HH__
37 typedef std::integral_constant< std::size_t, RANK >
rank;
40 MHO_NDArrayView(XValueType* ptr,
const std::size_t* dim,
const std::size_t* strides) { Construct(ptr, dim, strides); };
64 for(j = 0; j < RANK; j++)
76 for(std::size_t i = 0; i <
fSize; i++)
78 MHO_NDArrayMath::IncrementIndices< RANK >(&(
fDims[0]), &(idx[0]));
84 msg_error(
"containers",
"array view copy failed due to mismatched sizes on dimension: " << j <<
"." << eom);
93 std::size_t
GetRank()
const {
return RANK; }
116 for(std::size_t i = 0; i < RANK; i++)
151 for(std::size_t i = 0; i < RANK; i++)
179 template<
typename... XIndexTypeS >
180 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK), XValueType& >::type
operator()(XIndexTypeS... idx)
182 fTmp = {{
static_cast< size_t >(idx)...}};
192 template<
typename... XIndexTypeS >
193 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK),
const XValueType& >::type
196 fTmp = {{
static_cast< size_t >(idx)...}};
206 template<
typename... XIndexTypeS >
207 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK), XValueType& >::type
at(XIndexTypeS... idx)
210 fTmp = {{
static_cast< size_t >(idx)...}};
217 throw std::out_of_range(
"MHO_NDArrayView::at() indices out of range.");
227 template<
typename... XIndexTypeS >
228 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK),
const XValueType& >::type
at(XIndexTypeS... idx)
const
231 fTmp = {{
static_cast< size_t >(idx)...}};
238 throw std::out_of_range(
"MHO_NDArrayView::at() indices out of range.");
257 auto bit = this->
begin();
258 auto eit = this->
end();
259 for(
auto it = bit; it != eit; ++it)
270 auto bit = this->
begin();
271 auto eit = this->
end();
272 for(
auto it = bit; it != eit; ++it)
283 return MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(
fStrides[0]), index);
292 MHO_NDArrayMath::RowMajorIndexFromOffset< RANK >(offset, &(
fDims[0]), &(index[0]));
303 template<
typename T >
304 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
305 std::is_floating_point< T >::value,
309 auto bit = this->
begin();
310 auto eit = this->
end();
311 for(
auto it = bit; it != eit; ++it)
321 template<
typename T >
322 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
323 std::is_floating_point< T >::value,
327 auto bit = this->
begin();
328 auto eit = this->
end();
329 for(
auto it = bit; it != eit; ++it)
339 template<
typename T >
340 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
341 std::is_floating_point< T >::value,
345 auto bit = this->
begin();
346 auto eit = this->
end();
347 for(
auto it = bit; it != eit; ++it)
359 if(!HaveSameNumberOfElements(
this, &anArray))
361 throw std::out_of_range(
"MHO_NDArrayView::*= size mismatch.");
363 auto bit1 = this->
begin();
364 auto bit2 = anArray.
begin();
367 for(std::size_t i = 0; i <
fSize; i++)
381 if(!HaveSameNumberOfElements(
this, &anArray))
383 throw std::out_of_range(
"MHO_NDArrayView::+= size mismatch.");
385 auto bit1 = this->
begin();
386 auto bit2 = anArray.
begin();
389 for(std::size_t i = 0; i <
fSize; i++)
403 if(!HaveSameNumberOfElements(
this, &anArray))
405 throw std::out_of_range(
"MHO_NDArrayView::-= size mismatch.");
407 auto bit1 = this->
begin();
408 auto bit2 = anArray.
begin();
411 for(std::size_t i = 0; i <
fSize; i++)
422 return MHO_NDArrayMath::CheckIndexValidity< RANK >(&(
fDims[0]), &(idx[0]));
427 return fDataPtr[MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(
fStrides[0]), &(idx[0]))];
432 return fDataPtr[MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(
fStrides[0]), &(idx[0]))];
445 void Construct(XValueType* ptr,
const std::size_t* dim,
const std::size_t* strides)
448 for(std::size_t i = 0; i < RANK; i++)
455 if(ptr ==
nullptr || dim ==
nullptr || strides ==
nullptr)
457 msg_error(
"containers",
"cannot construct array slice." << eom);
463 for(std::size_t i = 0; i < RANK; i++)
468 fSize = MHO_NDArrayMath::TotalArraySize< RANK >(&(
fDims[0]));
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
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:33
const std::size_t * GetStrides() const
get element strides
Definition: MHO_NDArrayView.hh:142
MHO_NDArrayView & operator*=(const MHO_NDArrayView &anArray)
operator*= in place point-wise multiplication by another array
Definition: MHO_NDArrayView.hh:357
index_type GetIndicesForOffset(std::size_t offset)
invert (memory) offset into array to indexes of the associated element
Definition: MHO_NDArrayView.hh:289
MHO_NDArrayView & operator=(const MHO_NDArrayView &rhs)
Definition: MHO_NDArrayView.hh:243
index_type GetStrideArray() const
Getter for stride array.
Definition: MHO_NDArrayView.hh:162
XValueType * fDataPtr
Definition: MHO_NDArrayView.hh:436
MHO_NDArrayView(XValueType *ptr, const std::size_t *dim, const std::size_t *strides)
Definition: MHO_NDArrayView.hh:40
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:343
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:307
index_type fTmp
Definition: MHO_NDArrayView.hh:440
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:180
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:228
const XValueType & ValueAt(const index_type &idx) const
Definition: MHO_NDArrayView.hh:430
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:325
MHO_BidirectionalIndexedIterator< XValueType, RANK > iterator
Definition: MHO_NDArrayView.hh:473
void Copy(const MHO_NDArrayView &rhs)
copy functionality, calling array view must have same shape as rhs
Definition: MHO_NDArrayView.hh:59
MHO_NDArrayView * Clone()
clone functionality - Creates a deep copy of this MHO_NDArrayView object.
Definition: MHO_NDArrayView.hh:52
std::size_t GetOffsetForIndices(const std::size_t *index)
compute (memory) offset into array from a set of indexes
Definition: MHO_NDArrayView.hh:281
iterator begin()
Definition: MHO_NDArrayView.hh:476
void ZeroArray()
set all elements in the array to zero
Definition: MHO_NDArrayView.hh:268
index_type fDims
Definition: MHO_NDArrayView.hh:437
void SetArray(const XValueType &obj)
set all elements in the array to a certain value
Definition: MHO_NDArrayView.hh:255
void GetDimensions(std::size_t *dim) const
get the dimensions/shape of the array
Definition: MHO_NDArrayView.hh:114
const std::size_t * GetDimensions() const
get the dimensions/shape of the array
Definition: MHO_NDArrayView.hh:107
std::array< std::size_t, RANK > index_type
Definition: MHO_NDArrayView.hh:36
std::size_t GetDimension(std::size_t idx) const
Getter for a single dimension dimension.
Definition: MHO_NDArrayView.hh:135
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:207
bool CheckIndexValidity(const index_type &idx) const
Definition: MHO_NDArrayView.hh:420
iterator end()
Definition: MHO_NDArrayView.hh:478
const_iterator citerator_at(std::size_t offset) const
Definition: MHO_NDArrayView.hh:489
MHO_NDArrayView & operator+=(const MHO_NDArrayView &anArray)
operator+= in place point-wise addition by another array
Definition: MHO_NDArrayView.hh:379
XValueType & ValueAt(const index_type &idx)
Definition: MHO_NDArrayView.hh:425
std::size_t GetStride(std::size_t idx) const
Getter for stride at index.
Definition: MHO_NDArrayView.hh:170
const_iterator cend() const
Definition: MHO_NDArrayView.hh:487
index_type GetDimensionArray() const
get the dimensions/shape of the array as std::array
Definition: MHO_NDArrayView.hh:127
MHO_BidirectionalIndexedIterator< XValueType, RANK > const_iterator
Definition: MHO_NDArrayView.hh:474
XValueType value_type
Definition: MHO_NDArrayView.hh:35
uint64_t fSize
Definition: MHO_NDArrayView.hh:439
iterator iterator_at(std::size_t offset)
Definition: MHO_NDArrayView.hh:480
MHO_NDArrayView & operator-=(const MHO_NDArrayView &anArray)
operator-= in place point-wise subtraction by another array
Definition: MHO_NDArrayView.hh:401
virtual ~MHO_NDArrayView()
Definition: MHO_NDArrayView.hh:45
MHO_NDArrayView(const MHO_NDArrayView &obj)
Definition: MHO_NDArrayView.hh:42
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:194
std::size_t GetSize() const
get the total size of the array view
Definition: MHO_NDArrayView.hh:100
void GetStrides(std::size_t *strd) const
Getter for strides array (fills passed array)
Definition: MHO_NDArrayView.hh:149
std::integral_constant< std::size_t, RANK > rank
Definition: MHO_NDArrayView.hh:37
const_iterator cbegin() const
Definition: MHO_NDArrayView.hh:485
index_type fStrides
Definition: MHO_NDArrayView.hh:438
std::size_t GetRank() const
Getter for rank of the array view.
Definition: MHO_NDArrayView.hh:93
#define min(a, b)
Definition: max555.c:9
Definition: MHO_ChannelLabeler.hh:17