// -*- C++ -*- #ifndef __multi_array_h__ #define __multi_array_h__ #include "multi_array_base.h" namespace MultiArrayTools { template struct ArrayCatter; template struct ArrayCatter { template static auto cat(const MultiArray& ma) -> MultiArray { return ma; } }; template class MultiArray : public MutableMultiArrayBase { public: typedef ContainerRange CRange; typedef MultiArrayBase MAB; typedef ContainerIndex IndexType; DEFAULT_MEMBERS(MultiArray); MultiArray(const std::shared_ptr&... ranges); MultiArray(const std::shared_ptr&... ranges, const std::vector& vec); MultiArray(const std::shared_ptr&... ranges, std::vector&& vec); MultiArray(const typename CRange::SpaceType& space); MultiArray(const typename CRange::SpaceType& space, std::vector&& vec); // Only if ALL ranges have default extensions: //MultiArray(const std::vector& vec); //MultiArray(std::vector&& vec); // template // MultiArray(const MultiArray,Range3> in); // implement contstructor using FunctionalMultiArray as Input !!! //template //MultiArray& operator=(const MultiArray,Range3> in); virtual T& operator[](const IndexType& i) override; virtual const T& operator[](const IndexType& i) const override; virtual T& at(const typename IndexType::MetaType& meta) override; virtual const T& at(const typename IndexType::MetaType& meta) const override; virtual bool isConst() const override; virtual bool isSlice() const override; template MultiArray format(const std::shared_ptr&... nrs); // reformat array using 'nr' which in // total must have the same size as mRange virtual const T* data() const override; virtual T* data() override; virtual std::vector& vdata() { return mCont; } virtual const std::vector& vdata() const { return mCont; } auto cat() const -> decltype(ArrayCatter::cat(*this)); operator T() const; template friend class MultiArray; private: std::vector mCont; }; template using Scalar = MultiArray; template struct ArrayCatter > { template static auto cat(const MultiArray,Ranges...>& ma) -> MultiArray { auto sma = *ma.begin(); const size_t smas = sma.size(); const size_t mas = ma.size(); auto cr = ma.range()->cat(sma.range()); std::vector ov; ov.reserve(mas * smas); for(auto& x: ma){ assert(x.size() == smas); ov.insert(ov.end(), x.vdata().begin(), x.vdata().end()); } return MultiArray(cr->space(), std::move(ov)); } }; } /* ========================= * * --- TEMPLATE CODE --- * * ========================= */ namespace MultiArrayTools { /******************* * MultiArray * *******************/ template MultiArray::MultiArray(const typename CRange::SpaceType& space) : MutableMultiArrayBase(space), mCont(MAB::mRange->size()) { MAB::mInit = true; } template MultiArray::MultiArray(const typename CRange::SpaceType& space, std::vector&& vec) : MutableMultiArrayBase(space), mCont(vec) { MAB::mInit = true; if(mCont.size() > MAB::mRange->size()){ mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); } } template MultiArray::MultiArray(const std::shared_ptr&... ranges) : MutableMultiArrayBase(ranges...), mCont(MAB::mRange->size()) { MAB::mInit = true; } template MultiArray::MultiArray(const std::shared_ptr&... ranges, const std::vector& vec) : MutableMultiArrayBase(ranges...), mCont(vec) { MAB::mInit = true; if(mCont.size() > MAB::mRange->size()){ mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); } } template MultiArray::MultiArray(const std::shared_ptr&... ranges, std::vector&& vec) : MutableMultiArrayBase(ranges...), mCont(vec) { MAB::mInit = true; if(mCont.size() > MAB::mRange->size()){ mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); } } /* template template MultiArray::MultiArray(const MultiArray,Range3> in) : MutableMultiArrayBase(merge(in.range(), in[ in.beginIndex() ].range())) // assert that Range2 has always same extension { MAB::mInit = true; mCont.clear(); for(auto i = in.beginIndex(); i != in.endIndex(); ++i){ mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end()); } assert(mCont.size() == MAB::mRange->size()); } */ /* template template MultiArray& MultiArray::operator=(const MultiArray,Range3> in) { MAB::mRange.reset(new Range(merge(in.range(), in[ in.beginIndex() ].range()))); // assert that Range2 has always same extension mCont.clear(); for(auto i = in.beginIndex(); i != in.endIndex(); ++i){ mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end()); } assert(mCont.size() == MAB::mRange->size()); return *this; } */ template T& MultiArray::operator[](const IndexType& i) { return mCont[ i.pos() ]; } template const T& MultiArray::operator[](const IndexType& i) const { return mCont[ i.pos() ]; } template T& MultiArray::at(const typename IndexType::MetaType& meta) { return mCont[ MAB::beginIndex().at(meta).pos() ]; } template const T& MultiArray::at(const typename IndexType::MetaType& meta) const { return mCont[ MAB::beginIndex().at(meta).pos() ]; } template bool MultiArray::isConst() const { return false; } template bool MultiArray::isSlice() const { return false; } template template MultiArray MultiArray::format(const std::shared_ptr&... nrs) { return MultiArray( nrs... , std::move(mCont) ); } template const T* MultiArray::data() const { return mCont.data(); } template T* MultiArray::data() { return mCont.data(); } template MultiArray::operator T() const { static_assert( sizeof...(SRanges) == 0, "try to cast non-scalar type into scalar" ); return mCont[0]; } template auto MultiArray::cat() const -> decltype(ArrayCatter::cat(*this)) { return ArrayCatter::cat(*this); } } #endif