1 #ifndef MHO_NDArrayWrapper_HH__
2 #define MHO_NDArrayWrapper_HH__
39 template<
typename XValueType, std::
size_t RANK >
46 typedef std::integral_constant< std::size_t, RANK >
rank;
61 Construct(
nullptr, &(obj.fDims[0]));
62 std::copy(obj.fData.begin(), obj.fData.end(), fData.begin());
83 virtual void Resize(
const std::size_t* dim) { Construct(
nullptr, dim); }
92 template<
typename... XDimSizeTypeS >
93 typename std::enable_if< (
sizeof...(XDimSizeTypeS) == RANK),
void >::type
Resize(XDimSizeTypeS... dim)
95 fTmp = {{
static_cast< size_t >(dim)...}};
105 void SetExternalData(XValueType* ptr,
const std::size_t* dim) { Construct(ptr, dim); }
119 std::size_t
GetSize()
const {
return fData.size(); };
135 for(std::size_t i = 0; i < RANK; i++)
162 throw std::out_of_range(
"MHO_NDArrayWrapper::GetDimension() index out of range.");
171 const std::size_t*
GetStrides()
const {
return &(fStrides[0]); }
180 for(std::size_t i = 0; i < RANK; i++)
182 strd[i] = fStrides[i];
188 std::size_t
GetStride(std::size_t idx)
const {
return fStrides[idx]; }
196 template<
typename... XIndexTypeS >
197 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK), XValueType& >::type
operator()(XIndexTypeS... idx)
199 fTmp = {{
static_cast< size_t >(idx)...}};
209 template<
typename... XIndexTypeS >
210 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK),
const XValueType& >::type
213 fTmp = {{
static_cast< size_t >(idx)...}};
223 template<
typename... XIndexTypeS >
224 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK), XValueType& >::type
at(XIndexTypeS... idx)
227 fTmp = {{
static_cast< size_t >(idx)...}};
234 throw std::out_of_range(
"MHO_NDArrayWrapper::at() indices out of range.");
244 template<
typename... XIndexTypeS >
245 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK),
const XValueType& >::type
at(XIndexTypeS... idx)
const
248 fTmp = {{
static_cast< size_t >(idx)...}};
255 throw std::out_of_range(
"MHO_NDArrayWrapper::at() indices out of range.");
262 XValueType*
GetData() {
return &(fData[0]); };
264 const XValueType*
GetData()
const {
return &(fData[0]); };
271 const XValueType&
operator[](std::size_t i)
const {
return fData[i]; }
278 Construct(
nullptr, &(rhs.fDims[0]));
279 std::copy(rhs.fData.begin(), rhs.fData.end(), this->fData.begin());
289 for(
auto it = fData.begin(); it != fData.end(); it++)
298 void ZeroArray() { std::memset(&(fData[0]), 0, (fData.size()) *
sizeof(XValueType)); }
305 Construct(
nullptr, &(rhs.fDims[0]));
306 std::copy(rhs.fData.begin(), rhs.fData.end(), this->fData.begin());
314 Construct(
nullptr, &(dims[0]));
315 std::copy(rhs.
cbegin(), rhs.
cend(), this->fData.begin());
323 return MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(fStrides[0]), index);
332 MHO_NDArrayMath::RowMajorIndexFromOffset< RANK >(offset, &(fDims[0]), &(index[0]));
344 template<
typename... XIndexTypeS >
345 typename std::enable_if< (
sizeof...(XIndexTypeS) < RANK),
349 constexpr
typename std::integral_constant< std::size_t,
sizeof...(XIndexTypeS) > nfixed_t;
350 std::array< std::size_t,
sizeof...(XIndexTypeS) > leading_idx = {{
static_cast< size_t >(idx)...}};
351 for(std::size_t i = 0; i < RANK; i++)
355 for(std::size_t i = 0; i < leading_idx.size(); i++)
357 fTmp[i] = leading_idx[i];
359 std::size_t offset = MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(fStrides[0]), &(fTmp[0]));
360 return MHO_NDArrayView< XValueType, RANK - (
sizeof...(XIndexTypeS)) >(&(fData[offset]), &(fDims[nfixed_t]),
361 &(fStrides[nfixed_t]));
377 template<
typename... XIndexTypeS >
378 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK),
380 XIndexTypeS... >::
value > >::type
383 constexpr
typename std::integral_constant<
392 for(std::size_t i = 0; i < RANK; i++)
402 std::array< std::size_t, RANK > full_idx;
403 std::vector< std::size_t > fixed_idx;
404 std::vector< std::size_t > free_idx;
410 free_idx.push_back(i);
417 fixed_idx.push_back(i);
423 std::sort(free_idx.begin(), free_idx.end());
424 std::sort(fixed_idx.begin(), fixed_idx.end());
429 std::tuple< XIndexTypeS... > input_idx = std::make_tuple(idx...);
433 std::size_t offset = MHO_NDArrayMath::OffsetFromRowMajorIndex< RANK >(&(fDims[0]), &(filler.full_idx[0]));
435 std::array< std::size_t, nfree_t > dim;
436 std::array< std::size_t, nfree_t > strides;
437 for(std::size_t i = 0; i < dim.size(); i++)
439 dim[i] = fDims[filler.free_idx[i]];
440 strides[i] = fStrides[filler.free_idx[i]];
442 return MHO_NDArrayView< XValueType, nfree_t >(&(fData[offset]), &(dim[0]), &(strides[0]));
452 template<
typename T >
453 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
454 std::is_floating_point< T >::value,
455 MHO_NDArrayWrapper& >::type
inline
458 std::size_t length = fData.size();
459 for(std::size_t i = 0; i < length; i++)
469 template<
typename T >
470 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
471 std::is_floating_point< T >::value,
475 std::size_t length = fData.size();
476 for(std::size_t i = 0; i < length; i++)
486 template<
typename T >
487 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
488 std::is_floating_point< T >::value,
492 std::size_t length = fData.size();
493 for(std::size_t i = 0; i < length; i++)
505 if(!HaveSameNumberOfElements(
this, &anArray))
507 throw std::out_of_range(
"MHO_NDArrayWrapper::*= size mismatch.");
509 std::size_t length = fData.size();
510 for(std::size_t i = 0; i < length; i++)
512 fData[i] *= anArray.fData[i];
522 if(!HaveSameNumberOfElements(
this, &anArray))
524 throw std::out_of_range(
"MHO_NDArrayWrapper::+= size mismatch.");
526 std::size_t length = fData.size();
527 for(std::size_t i = 0; i < length; i++)
529 fData[i] += anArray.fData[i];
540 if(!HaveSameNumberOfElements(
this, &anArray))
542 throw std::out_of_range(
"MHO_NDArrayWrapper::-= size mismatch.");
544 std::size_t length = fData.size();
545 for(std::size_t i = 0; i < length; i++)
547 fData[i] -= anArray.fData[i];
554 return MHO_NDArrayMath::CheckIndexValidity< RANK >(&(fDims[0]), &(idx[0]));
559 return fData[MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(fStrides[0]), &(idx[0]))];
564 return fData[MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(fStrides[0]), &(idx[0]))];
568 std::vector< XValueType > fData;
571 mutable index_type fTmp;
573 void Construct(XValueType* ptr,
const std::size_t* dim)
576 for(std::size_t i = 0; i < RANK; i++)
581 if(ptr ==
nullptr && dim ==
nullptr)
589 for(std::size_t i = 0; i < RANK; i++)
594 std::size_t length = MHO_NDArrayMath::TotalArraySize< RANK >(&(fDims[0]));
596 fData.resize(length);
600 std::memcpy(&(fData[0]), ptr, length *
sizeof(XValueType));
604 void ComputeStrides()
606 for(std::size_t i = 0; i < RANK; i++)
609 std::size_t stride = 1;
610 std::size_t j = RANK - 1;
616 fStrides[i] = stride;
634 return iterator(&(fData[0]), &(fData[0]) +
std::min(offset, fData.size()), fData.size());
648 return stride_iterator(&(fData[0]), &(fData[0]), fData.size(), stride);
653 return stride_iterator(&(fData[0]), &(fData[0]) + fData.size(), fData.size(), stride);
697 template<
class XArrayType1,
class XArrayType2 >
698 static bool HaveSameRank(
const XArrayType1* ,
const XArrayType2* )
700 return (XArrayType1::rank::value == XArrayType2::rank::value);
713 template<
class XArrayType1,
class XArrayType2 >
714 static bool HaveSameNumberOfElements(
const XArrayType1* arr1,
const XArrayType2* arr2)
716 return (arr1->GetSize() == arr2->GetSize());
729 template<
class XArrayType1,
class XArrayType2 >
730 static bool HaveSameDimensions(
const XArrayType1* arr1,
const XArrayType2* arr2)
732 std::size_t shape1[XArrayType1::rank::value];
733 std::size_t shape2[XArrayType2::rank::value];
735 if(HaveSameRank(arr1, arr2))
737 size_t rank = XArrayType1::rank::value;
738 arr1->GetDimensions(shape1);
739 arr2->GetDimensions(shape2);
741 for(std::size_t i = 0; i < rank; i++)
743 if(shape1[i] != shape2[i])
Class MHO_BidirectionalConstIterator.
Definition: MHO_BidirectionalIterator.hh:144
Class MHO_BidirectionalConstStrideIterator.
Definition: MHO_BidirectionalStrideIterator.hh:139
Class MHO_BidirectionalIterator.
Definition: MHO_BidirectionalIterator.hh:22
Class MHO_BidirectionalStrideIterator.
Definition: MHO_BidirectionalStrideIterator.hh:22
Class MHO_ExtensibleElement.
Definition: MHO_ExtensibleElement.hh:60
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_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
const_iterator cbegin() const
Definition: MHO_NDArrayView.hh:485
Class MHO_NDArrayWrapper.
Definition: MHO_NDArrayWrapper.hh:42
std::size_t GetSize() const
get the total size of the array
Definition: MHO_NDArrayWrapper.hh:119
const_stride_iterator cstride_begin(std::size_t stride) const
Definition: MHO_NDArrayWrapper.hh:661
std::size_t GetRank() const
Getter for the rank (dimensionality) of the array.
Definition: MHO_NDArrayWrapper.hh:112
MHO_NDArrayWrapper()
Definition: MHO_NDArrayWrapper.hh:50
std::enable_if<(sizeof...(XIndexTypeS)==RANK), XValueType & >::type operator()(XIndexTypeS... idx)
access operator, accepts multiple indices (,,...,) but does no bounds checking
Definition: MHO_NDArrayWrapper.hh:197
void GetStrides(std::size_t *strd) const
Getter for strides (along each dimension), fills passed array.
Definition: MHO_NDArrayWrapper.hh:178
std::integral_constant< std::size_t, RANK > rank
Definition: MHO_NDArrayWrapper.hh:46
bool CheckIndexValidity(const index_type &idx) const
Definition: MHO_NDArrayWrapper.hh:552
iterator begin()
Definition: MHO_NDArrayWrapper.hh:628
index_type GetStrideArray() const
Definition: MHO_NDArrayWrapper.hh:186
std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or std::is_floating_point< T >::value, MHO_NDArrayWrapper & >::type operator+=(T aScalar)
operator+= in place addition by a scalar amount
Definition: MHO_NDArrayWrapper.hh:473
MHO_NDArrayWrapper * Clone()
clone functionality - creates a deep copy of this MHO_NDArrayWrapper object.
Definition: MHO_NDArrayWrapper.hh:74
void GetDimensions(std::size_t *dim) const
Getter for dimensions, fills passed array.
Definition: MHO_NDArrayWrapper.hh:133
XValueType * GetData()
access to underlying data pointer (unsafe)
Definition: MHO_NDArrayWrapper.hh:262
MHO_NDArrayWrapper & operator=(const MHO_NDArrayWrapper &rhs)
Definition: MHO_NDArrayWrapper.hh:274
iterator iterator_at(std::size_t offset)
Definition: MHO_NDArrayWrapper.hh:632
MHO_NDArrayWrapper & operator-=(const MHO_NDArrayWrapper &anArray)
operator-= in place point-wise subtraction by another array
Definition: MHO_NDArrayWrapper.hh:538
XValueType value_type
Definition: MHO_NDArrayWrapper.hh:44
const XValueType * GetData() const
Definition: MHO_NDArrayWrapper.hh:264
const_stride_iterator cstride_end(std::size_t stride) const
Definition: MHO_NDArrayWrapper.hh:666
const std::size_t * GetDimensions() const
get the dimensions/shape of the array
Definition: MHO_NDArrayWrapper.hh:126
std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or std::is_floating_point< T >::value, MHO_NDArrayWrapper & >::type operator-=(T aScalar)
operator+= in place addition by a scalar amount
Definition: MHO_NDArrayWrapper.hh:490
const_iterator cbegin() const
Definition: MHO_NDArrayWrapper.hh:637
std::enable_if<(sizeof...(XIndexTypeS)==RANK), XValueType & >::type at(XIndexTypeS... idx)
at(): same as operator(...) but with bounds checking with bounds checking
Definition: MHO_NDArrayWrapper.hh:224
const_stride_iterator cstride_iterator_at(std::size_t offset, std::size_t stride) const
Definition: MHO_NDArrayWrapper.hh:671
virtual void Resize(const std::size_t *dim)
Destroys contents and resizes using passed dimensions.
Definition: MHO_NDArrayWrapper.hh:83
XValueType & ValueAt(const index_type &idx)
Definition: MHO_NDArrayWrapper.hh:557
MHO_NDArrayWrapper(const MHO_NDArrayWrapper &obj)
Definition: MHO_NDArrayWrapper.hh:59
const std::size_t * GetStrides() const
Getter for element strides (along each dimension)
Definition: MHO_NDArrayWrapper.hh:171
void SetArray(const XValueType &obj)
set all elements in the array to a certain value
Definition: MHO_NDArrayWrapper.hh:287
void ZeroArray()
set all elements in the array to zero
Definition: MHO_NDArrayWrapper.hh:298
std::enable_if<(sizeof...(XDimSizeTypeS)==RANK), void >::type Resize(XDimSizeTypeS... dim)
Resize function that destroys contents and resizes according to dimension arguments.
Definition: MHO_NDArrayWrapper.hh:93
virtual void Copy(const MHO_NDArrayView< XValueType, RANK > &rhs)
Definition: MHO_NDArrayWrapper.hh:311
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_NDArrayWrapper.hh:211
stride_iterator stride_iterator_at(std::size_t offset, std::size_t stride)
Definition: MHO_NDArrayWrapper.hh:656
stride_iterator stride_begin(std::size_t stride)
Definition: MHO_NDArrayWrapper.hh:646
MHO_NDArrayWrapper(XValueType *ptr, const std::size_t *dim)
Definition: MHO_NDArrayWrapper.hh:56
const XValueType & ValueAt(const index_type &idx) const
Definition: MHO_NDArrayWrapper.hh:562
const_iterator cend() const
Definition: MHO_NDArrayWrapper.hh:639
const_iterator citerator_at(std::size_t offset) const
Definition: MHO_NDArrayWrapper.hh:641
MHO_NDArrayWrapper & operator*=(const MHO_NDArrayWrapper &anArray)
operator*= in place point-wise multiplication by another array
Definition: MHO_NDArrayWrapper.hh:503
std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or std::is_floating_point< T >::value, MHO_NDArrayWrapper & >::type operator*=(T aScalar)
operator*= in place multiplication by a scalar factor
Definition: MHO_NDArrayWrapper.hh:456
std::size_t GetDimension(std::size_t idx) const
Getter for dimension.
Definition: MHO_NDArrayWrapper.hh:154
MHO_NDArrayWrapper & operator+=(const MHO_NDArrayWrapper &anArray)
operator+= in place point-wise addition by another array
Definition: MHO_NDArrayWrapper.hh:520
std::enable_if<(sizeof...(XIndexTypeS)==RANK), MHO_NDArrayView< XValueType, count_instances_of_type< const char *, sizeof...(XIndexTypeS) - 1, XIndexTypeS... >::value > >::type SliceView(XIndexTypeS... idx)
creates a slice-view of the array (given n < RANK indexes), return the remaining chunk of the array w...
Definition: MHO_NDArrayWrapper.hh:381
stride_iterator stride_end(std::size_t stride)
Definition: MHO_NDArrayWrapper.hh:651
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_NDArrayWrapper.hh:245
index_type GetDimensionArray() const
Getter for dimension array.
Definition: MHO_NDArrayWrapper.hh:146
std::size_t GetStride(std::size_t idx) const
Definition: MHO_NDArrayWrapper.hh:188
virtual void Copy(const MHO_NDArrayWrapper &rhs)
Definition: MHO_NDArrayWrapper.hh:301
XValueType & operator[](std::size_t i)
Definition: MHO_NDArrayWrapper.hh:269
MHO_NDArrayWrapper(const std::size_t *dim)
Definition: MHO_NDArrayWrapper.hh:53
index_type GetIndicesForOffset(std::size_t offset)
invert (memory) offset into array to indexes of the associated element
Definition: MHO_NDArrayWrapper.hh:329
std::size_t GetOffsetForIndices(const std::size_t *index)
compute (memory) offset into array from a set of indexes
Definition: MHO_NDArrayWrapper.hh:321
const XValueType & operator[](std::size_t i) const
Definition: MHO_NDArrayWrapper.hh:271
std::enable_if<(sizeof...(XIndexTypeS)< RANK), MHO_NDArrayView< XValueType, RANK -(sizeof...(XIndexTypeS)) > >::type SubView(XIndexTypeS... idx)
creates a sub-view of the array (given n < RANK leading indexes), return the remaining chunk of the a...
Definition: MHO_NDArrayWrapper.hh:347
virtual ~MHO_NDArrayWrapper()
Definition: MHO_NDArrayWrapper.hh:67
void SetExternalData(XValueType *ptr, const std::size_t *dim)
set data pointer to externally managed array with associated dimensions
Definition: MHO_NDArrayWrapper.hh:105
iterator end()
Definition: MHO_NDArrayWrapper.hh:630
std::array< std::size_t, RANK > index_type
Definition: MHO_NDArrayWrapper.hh:45
#define min(a, b)
Definition: max555.c:9
Definition: MHO_ChannelLabeler.hh:17
Class count_instances_of_type - utility to count the instances of a particular type in a parameter pa...
Definition: MHO_Meta.hh:98
static void visit(XTupleType &tup, XFunctorType &functor)
Applies a functor to all elements of an XTupleType tuple and recursively visits the next type.
Definition: MHO_Meta.hh:193