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 index_type tmp = {{
static_cast< size_t >(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 index_type tmp = {{
static_cast< size_t >(idx)...}};
209 template<
typename... XIndexTypeS >
210 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK),
const XValueType& >::type
213 index_type tmp = {{
static_cast< size_t >(idx)...}};
223 template<
typename... XIndexTypeS >
224 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK), XValueType& >::type
at(XIndexTypeS... idx)
227 index_type tmp = {{
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 index_type tmp = {{
static_cast< size_t >(idx)...}};
255 throw std::out_of_range(
"MHO_NDArrayWrapper::at() indices out of range.");
262 XValueType*
GetData() {
return fData.data(); };
264 const XValueType*
GetData()
const {
return fData.data(); };
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::fill(fData.begin(), fData.end(), 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]));
343 template<
typename... XIndexTypeS >
344 typename std::enable_if< (
sizeof...(XIndexTypeS) < RANK),
348 constexpr
typename std::integral_constant< std::size_t,
sizeof...(XIndexTypeS) > nfixed_t;
349 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 tmp[i] = leading_idx[i];
359 std::size_t offset = MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(fStrides[0]), &(tmp[0]));
360 return MHO_NDArrayView< XValueType, RANK - (
sizeof...(XIndexTypeS)) >(&(fData[offset]), &(fDims[nfixed_t]),
361 &(fStrides[nfixed_t]));
375 template<
typename... XIndexTypeS >
376 typename std::enable_if< (
sizeof...(XIndexTypeS) == RANK),
378 XIndexTypeS... >::
value > >::type
381 constexpr
typename std::integral_constant<
390 for(std::size_t i = 0; i < RANK; i++)
400 std::array< std::size_t, RANK > full_idx;
401 std::vector< std::size_t > fixed_idx;
402 std::vector< std::size_t > free_idx;
408 free_idx.push_back(i);
415 fixed_idx.push_back(i);
421 std::sort(free_idx.begin(), free_idx.end());
422 std::sort(fixed_idx.begin(), fixed_idx.end());
427 std::tuple< XIndexTypeS... > input_idx = std::make_tuple(idx...);
431 std::size_t offset = MHO_NDArrayMath::OffsetFromRowMajorIndex< RANK >(&(fDims[0]), &(filler.full_idx[0]));
433 std::array< std::size_t, nfree_t > dim;
434 std::array< std::size_t, nfree_t > strides;
435 for(std::size_t i = 0; i < dim.size(); i++)
437 dim[i] = fDims[filler.free_idx[i]];
438 strides[i] = fStrides[filler.free_idx[i]];
440 return MHO_NDArrayView< XValueType, nfree_t >(&(fData[offset]), &(dim[0]), &(strides[0]));
450 template<
typename T >
451 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
452 std::is_floating_point< T >::value,
453 MHO_NDArrayWrapper& >::type
inline
456 std::size_t length = fData.size();
457 for(std::size_t i = 0; i < length; i++)
467 template<
typename T >
468 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
469 std::is_floating_point< T >::value,
473 std::size_t length = fData.size();
474 for(std::size_t i = 0; i < length; i++)
484 template<
typename T >
485 typename std::enable_if< std::is_same< XValueType, T >::value or std::is_integral< T >::value or
486 std::is_floating_point< T >::value,
490 std::size_t length = fData.size();
491 for(std::size_t i = 0; i < length; i++)
503 if(!HaveSameNumberOfElements(
this, &anArray))
505 throw std::out_of_range(
"MHO_NDArrayWrapper::*= size mismatch.");
507 std::size_t length = fData.size();
508 for(std::size_t i = 0; i < length; i++)
510 fData[i] *= anArray.fData[i];
520 if(!HaveSameNumberOfElements(
this, &anArray))
522 throw std::out_of_range(
"MHO_NDArrayWrapper::+= size mismatch.");
524 std::size_t length = fData.size();
525 for(std::size_t i = 0; i < length; i++)
527 fData[i] += anArray.fData[i];
537 if(!HaveSameNumberOfElements(
this, &anArray))
539 throw std::out_of_range(
"MHO_NDArrayWrapper::-= size mismatch.");
541 std::size_t length = fData.size();
542 for(std::size_t i = 0; i < length; i++)
544 fData[i] -= anArray.fData[i];
551 return MHO_NDArrayMath::CheckIndexValidity< RANK >(&(fDims[0]), &(idx[0]));
556 return fData[MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(fStrides[0]), &(idx[0]))];
561 return fData[MHO_NDArrayMath::OffsetFromStrideIndex< RANK >(&(fStrides[0]), &(idx[0]))];
565 std::vector< XValueType > fData;
569 void Construct(XValueType* ptr,
const std::size_t* dim)
572 for(std::size_t i = 0; i < RANK; i++)
577 if(ptr ==
nullptr && dim ==
nullptr)
585 for(std::size_t i = 0; i < RANK; i++)
590 std::size_t length = MHO_NDArrayMath::TotalArraySize< RANK >(&(fDims[0]));
592 fData.resize(length);
596 std::copy(ptr, ptr + length, fData.begin());
601 void ComputeStrides()
603 for(std::size_t i = 0; i < RANK; i++)
606 std::size_t stride = 1;
607 std::size_t j = RANK - 1;
613 fStrides[i] = stride;
631 return iterator(fData.data(), fData.data() +
std::min(offset, fData.size()), fData.size());
645 return stride_iterator(fData.data(), fData.data(), fData.size(), stride);
650 return stride_iterator(fData.data(), fData.data() + fData.size(), fData.size(), stride);
694 template<
class XArrayType1,
class XArrayType2 >
695 static bool HaveSameRank(
const XArrayType1* ,
const XArrayType2* )
697 return (XArrayType1::rank::value == XArrayType2::rank::value);
710 template<
class XArrayType1,
class XArrayType2 >
711 static bool HaveSameNumberOfElements(
const XArrayType1* arr1,
const XArrayType2* arr2)
713 return (arr1->GetSize() == arr2->GetSize());
726 template<
class XArrayType1,
class XArrayType2 >
727 static bool HaveSameDimensions(
const XArrayType1* arr1,
const XArrayType2* arr2)
729 std::size_t shape1[XArrayType1::rank::value];
730 std::size_t shape2[XArrayType2::rank::value];
732 if(HaveSameRank(arr1, arr2))
734 size_t rank = XArrayType1::rank::value;
735 arr1->GetDimensions(shape1);
736 arr2->GetDimensions(shape2);
738 for(std::size_t i = 0; i < rank; i++)
740 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:32
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
const_iterator cbegin() const
Definition: MHO_NDArrayView.hh:483
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:658
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:549
iterator begin()
Definition: MHO_NDArrayWrapper.hh:625
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:471
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:629
MHO_NDArrayWrapper & operator-=(const MHO_NDArrayWrapper &anArray)
operator-= in place point-wise subtraction by another array
Definition: MHO_NDArrayWrapper.hh:535
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:663
const std::size_t * GetDimensions() const
get the dimensions/shape of the array
Definition: MHO_NDArrayWrapper.hh:126
void CopyFromExternalData(XValueType *ptr, const std::size_t *dim)
set data pointer to externally managed array with associated dimensions
Definition: MHO_NDArrayWrapper.hh:105
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:488
const_iterator cbegin() const
Definition: MHO_NDArrayWrapper.hh:634
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:668
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:554
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:653
stride_iterator stride_begin(std::size_t stride)
Definition: MHO_NDArrayWrapper.hh:643
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:559
const_iterator cend() const
Definition: MHO_NDArrayWrapper.hh:636
const_iterator citerator_at(std::size_t offset) const
Definition: MHO_NDArrayWrapper.hh:638
MHO_NDArrayWrapper & operator*=(const MHO_NDArrayWrapper &anArray)
operator*= in place point-wise multiplication by another array
Definition: MHO_NDArrayWrapper.hh:501
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:454
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:518
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:379
stride_iterator stride_end(std::size_t stride)
Definition: MHO_NDArrayWrapper.hh:648
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:346
virtual ~MHO_NDArrayWrapper()
Definition: MHO_NDArrayWrapper.hh:67
iterator end()
Definition: MHO_NDArrayWrapper.hh:627
std::array< std::size_t, RANK > index_type
Definition: MHO_NDArrayWrapper.hh:45
#define min(a, b)
Definition: max555.c:9
Definition: MHO_AdhocFlagging.hh:18
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