From 0066cfef54c885e2705d821a445558e9426b49e1 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 15 Mar 2017 22:54:48 +0100 Subject: [PATCH] further corrections -> first application compiles --- src/base_def.h | 4 +++ src/multi_array.cc | 5 +-- src/multi_array.h | 4 +-- src/multi_array_operation.cc | 60 ++++++++++++++++++++++++++----- src/multi_array_operation.h | 29 ++++++++++----- src/multi_range.cc | 37 ++++++++++---------- src/multi_range.h | 9 +++-- src/name.cc | 68 +++++++++++++++++++----------------- src/name.h | 4 +-- src/single_range.cc | 7 +++- src/single_range.h | 17 +++++++-- src/slice.cc | 50 ++++++++++++++++++++++++-- src/slice.h | 35 +++++++++++++++++-- 13 files changed, 245 insertions(+), 84 deletions(-) diff --git a/src/base_def.h b/src/base_def.h index d2865c6..56d9d71 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -93,6 +93,10 @@ namespace MultiArrayTools template class Slice; + // slice.h + template + class ConstSlice; + // manipulator.h template class ManipulatorBase; diff --git a/src/multi_array.cc b/src/multi_array.cc index 69179c1..15b2f50 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -18,13 +18,13 @@ namespace MultiArrayTools } template - auto MultiArrayBase::begin() -> decltype(Range().begin()) + auto MultiArrayBase::begin() const -> decltype(Range().begin()) { return mRange->begin(); } template - auto MultiArrayBase::end() -> decltype(Range().end()) + auto MultiArrayBase::end() const -> decltype(Range().end()) { return mRange->end(); } @@ -119,6 +119,7 @@ namespace MultiArrayTools mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end()); } assert(mCont.size() == MAB::mRange->size()); + return *this; } template diff --git a/src/multi_array.h b/src/multi_array.h index 6631971..6ea374d 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -33,8 +33,8 @@ namespace MultiArrayTools virtual size_t size() const; virtual bool isSlice() const = 0; - virtual auto begin() -> decltype(Range().begin()); - virtual auto end() -> decltype(Range().end()); + virtual auto begin() const -> decltype(Range().begin()); + virtual auto end() const -> decltype(Range().end()); virtual const Range& range() const; diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 17aadc4..21043e3 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -16,7 +16,7 @@ namespace MultiArrayTools } template - IndefinitIndexBase& MultiArrayOperationBase::index() + const IndefinitIndexBase& MultiArrayOperationBase::index() const { return *mIibPtr; } @@ -43,9 +43,22 @@ namespace MultiArrayTools MultiArrayOperationRoot::makeSlice(MultiArrayOperationRoot& in) { Slice& sl = dynamic_cast&>( mArrayRef ); - sl.set(in.mArrayRef, name(), dynamic_cast( in.index() ), in.name()); + sl.set(in.mArrayRef, name(), dynamic_cast( in.index() ), in.name()); return *this; } + + template + template + const MultiArrayOperationRoot& + MultiArrayOperationRoot::makeConstSlice(const MultiArrayOperationRoot& in) + { + ConstSlice& sl = dynamic_cast&>( mArrayRef ); + sl.set(in.mArrayRef, name(), dynamic_cast( in.index() ), in.name()); + return *this; + } + + + // CONST SLICE !!!!! template MultiArrayOperationRoot:: @@ -92,6 +105,18 @@ namespace MultiArrayTools performAssignment(in); return *this; } + + template + template + const MultiArrayOperationRoot& + MultiArrayOperationRoot::operator=(const MultiArrayOperationRoot& in) + { + if(mArrayRef.isSlice() and not mArrayRef.isInit()){ + return makeConstSlice(in); + } + performAssignment(in); + return *this; + } template template @@ -107,18 +132,18 @@ namespace MultiArrayTools return *this; } - template + /* template template MultiArrayOperation, MAOps...> - MultiArrayOperationRoot::operator()(Operation& op, const MAOps&... secs) + MultiArrayOperationRoot::operator()(Operation& op, const MAOps&... secs) const { return MultiArrayOperation, MAOps...>(op, *this, secs...); - } + }*/ template template MultiArrayOperation, MAOps...> - MultiArrayOperationRoot::operator()(const Operation& op, const MAOps&... secs) + MultiArrayOperationRoot::operator()(const Operation& op, const MAOps&... secs) const { return MultiArrayOperation, MAOps...>(op, *this, secs...); } @@ -192,6 +217,18 @@ namespace MultiArrayTools { return (*this) = copyThis() / sec; } + + template + const MultiArrayBase& MultiArrayOperationRoot::operator*() const + { + return mArrayRef; + } + + template + MultiArrayBase const* MultiArrayOperationRoot::operator->() const + { + return &mArrayRef; + } template size_t MultiArrayOperationRoot::argNum() const @@ -231,6 +268,13 @@ namespace MultiArrayTools mIndex.copyPos(ind); return *this; } + + template + const MultiArrayOperationRoot& MultiArrayOperationRoot::operator[](const IndexType& ind) const + { + mIndex.copyPos(ind); + return *this; + } /***************************** * MultiArrayOperation * @@ -308,7 +352,7 @@ namespace MultiArrayTools template template MultiArrayOperation,MAOps2...> - MultiArrayOperation::operator()(Operation2& op, const MAOps2&... secs) + MultiArrayOperation::operator()(Operation2& op, const MAOps2&... secs) const { return MultiArrayOperation, MAOps2...>(op, *this, secs...); @@ -317,7 +361,7 @@ namespace MultiArrayTools template template MultiArrayOperation,MAOps2...> - MultiArrayOperation::operator()(const Operation2& op, const MAOps2&... secs) + MultiArrayOperation::operator()(const Operation2& op, const MAOps2&... secs) const { return MultiArrayOperation, MAOps2...>(op, *this, secs...); diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 1abebb1..fa3f988 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -21,7 +21,7 @@ namespace MultiArrayTools virtual ~MultiArrayOperationBase(); virtual size_t argNum() const = 0; - IndefinitIndexBase& index(); + const IndefinitIndexBase& index() const; virtual void linkIndicesTo(IndefinitIndexBase* target) const = 0; virtual T& get() = 0; @@ -37,8 +37,9 @@ namespace MultiArrayTools public: typedef MultiArrayOperationBase MAOB; - typedef decltype(MultiArray().begin()) IndexType; - + typedef typename Range::IndexType IndexType; + //typedef decltype(MultiArray().begin()) IndexType; + MultiArrayOperationRoot(MultiArrayBase& ma, const Name& nm); MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot& in); @@ -46,17 +47,20 @@ namespace MultiArrayTools template MultiArrayOperationRoot& operator=(MultiArrayOperationRoot& in); + + template + const MultiArrayOperationRoot& operator=(const MultiArrayOperationRoot& in); template MultiArrayOperationRoot& operator=(const MultiArrayOperation& in); - template - MultiArrayOperation, MAOps...> - operator()(Operation& op, const MAOps&... secs); + //template + //MultiArrayOperation, MAOps...> + //operator()(Operation& op, const MAOps&... secs) const; template MultiArrayOperation, MAOps...> - operator()(const Operation& op, const MAOps&... secs); + operator()(const Operation& op, const MAOps&... secs) const; template auto operator+(const MAOp& sec) -> decltype(operator()(std::plus(), sec)); @@ -83,11 +87,15 @@ namespace MultiArrayTools template MultiArrayOperationRoot& operator/=(const MAOp& sec); + + const MultiArrayBase& operator*() const; + MultiArrayBase const* operator->() const; virtual size_t argNum() const override; // set index -> implement !!!!! MultiArrayOperationRoot& operator[](const IndexType& ind); + const MultiArrayOperationRoot& operator[](const IndexType& ind) const; virtual void linkIndicesTo(IndefinitIndexBase* target) const override; @@ -105,6 +113,9 @@ namespace MultiArrayTools template MultiArrayOperationRoot& makeSlice(MultiArrayOperationRoot& in); + + template + const MultiArrayOperationRoot& makeConstSlice(const MultiArrayOperationRoot& in); MultiArrayBase& mArrayRef; mutable IndexType mIndex; @@ -124,11 +135,11 @@ namespace MultiArrayTools template MultiArrayOperation,MAOps2...> - operator()(Operation2& op, const MAOps2&... secs); + operator()(Operation2& op, const MAOps2&... secs) const; template MultiArrayOperation,MAOps2...> - operator()(const Operation2& op, const MAOps2&... secs); + operator()(const Operation2& op, const MAOps2&... secs) const; template auto operator+(const MAOp2& sec) -> decltype(operator()(std::plus(), sec)); diff --git a/src/multi_range.cc b/src/multi_range.cc index cc1ceac..334891b 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -236,8 +236,8 @@ namespace MultiArrayTools struct MetaPosGetter { template - static void getMetaPos(typename MultiIndex::MetaType& target, - const typename MultiIndex::IndexPack& source) + static void getMetaPos(std::tuple& target, + const std::tuple& source) { std::get(target) = std::get(source).getMetaPos(); MetaPosGetter::getMetaPos(target, source); @@ -248,8 +248,8 @@ namespace MultiArrayTools struct MetaPosGetter<0> { template - static void getMetaPos(typename MultiIndex::MetaType& target, - const typename MultiIndex::IndexPack& source) + static void getMetaPos(std::tuple& target, + const std::tuple& source) { std::get<0>(target) = std::get<0>(source).getMetaPos(); } @@ -614,33 +614,26 @@ namespace MultiArrayTools } template - struct MetaTypePrinter + struct TuplePrinter { - template - static void print(std::ostream& os, typename MultiIndex::MetaType& meta) + template + static void print(std::ostream& os, const std::tuple& meta) { - MetaTypePrinter::print(os, meta); + TuplePrinter::print(os, meta); os << std::get(meta) << '\t'; } }; template <> - struct MetaTypePrinter<0> + struct TuplePrinter<0> { - template - static void print(std::ostream& os, typename MultiIndex::MetaType& meta) + template + static void print(std::ostream& os, const std::tuple& meta) { os << std::get<0>(meta) << '\t'; } }; - template - std::ostream& operator<<(std::ostream& os, typename MultiIndex::MetaType& meta) - { - MetaTypePrinter::print(os, meta); - return os; - } - /****************** * MultiRange * ******************/ @@ -701,3 +694,11 @@ namespace MultiArrayTools return ++MultiIndex(this, is); } } + +template +std::ostream& operator<<(std::ostream& os, + const std::tuple& meta) +{ + MultiArrayTools::TuplePrinter::print(os, meta); + return os; +} diff --git a/src/multi_range.h b/src/multi_range.h index 722d88b..27c1c64 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -92,9 +92,6 @@ namespace MultiArrayTools //virtual void assignRange(RangeBase > const* range) override; }; - template - std::ostream& operator<<(std::ostream& os, typename MultiIndex::MetaType& meta); - /***************************** * IndexGetter Functions * ****************************/ @@ -106,6 +103,7 @@ namespace MultiArrayTools public: typedef std::tuple SpaceType; + typedef typename RangeBase >::IndexType IndexType; DEFAULT_MEMBERS(MultiRange); @@ -134,6 +132,11 @@ namespace MultiArrayTools } +template +std::ostream& operator<<(std::ostream& os, + const std::tuple& meta); + + #include "multi_range.cc" #endif diff --git a/src/name.cc b/src/name.cc index 2f3d710..1c26432 100644 --- a/src/name.cc +++ b/src/name.cc @@ -6,39 +6,40 @@ namespace MultiArrayTools namespace { - - void giveNames(std::vector& nvec) + struct Naming { - nvec.clear(); - } - - void giveNames(std::vector& nvec, const Name& name) - { - nvec.push_back(name); - } - - template - void giveNames(std::vector& nvec, const Name& name1, const Name& name2, const NameTypes&... names) - { - nvec.push_back(name1); - giveNames(nvec, name2, names...); - } - - + static void giveNames(std::vector& nvec) + { + nvec.clear(); + } + + static void giveNames(std::vector& nvec, const Name& name) + { + nvec.push_back(name); + } + + template + static void giveNames(std::vector& nvec, const Name& name1, + const Name& name2, const NameTypes&... names) + { + nvec.push_back(name1); + giveNames(nvec, name2, names...); + } + }; } template Name::Name(const std::string& mainName, const NameTypes&... names) : mMain(mainName) { mSub.reserve(sizeof...(NameTypes)); - giveNames(mSub, names...); + Naming::giveNames(mSub, names...); } template Name::Name(char const* mainName, const NameTypes&... names) : mMain(mainName) { mSub.reserve(sizeof...(NameTypes)); - giveNames(mSub, names...); + Naming::giveNames(mSub, names...); } const std::string& Name::own() const @@ -67,17 +68,18 @@ namespace MultiArrayTools return mSub.size(); } - std::ostream& operator<<(std::ostream& os, const Name& name) - { - if(name.size() != 0){ - for(size_t i = 0; i != name.size(); ++i){ - os << name.get(i) << '\t'; - } - } - else { - os << name.own(); - } - return os; - } - } + +std::ostream& operator<<(std::ostream& os, const MultiArrayTools::Name& name) +{ + if(name.size() != 0){ + for(size_t i = 0; i != name.size(); ++i){ + os << name.get(i) << '\t'; + } + } + else { + os << name.own(); + } + return os; +} + diff --git a/src/name.h b/src/name.h index 0e466de..490def5 100644 --- a/src/name.h +++ b/src/name.h @@ -33,11 +33,11 @@ namespace MultiArrayTools std::string mMain; std::vector mSub; }; - - std::ostream& operator<<(std::ostream& os, const Name& name); } +std::ostream& operator<<(std::ostream& os, const MultiArrayTools::Name& name); + #include "name.cc" #endif diff --git a/src/single_range.cc b/src/single_range.cc index 779e97c..f6b5d71 100644 --- a/src/single_range.cc +++ b/src/single_range.cc @@ -288,7 +288,7 @@ namespace MultiArrayTools } template - const U& SingleIndex::getMetaPos() const + U SingleIndex::getMetaPos() const { return dynamic_cast const*>( IB::mRange )->get(IIB::pos()); } @@ -346,6 +346,11 @@ namespace MultiArrayTools } } + std::ostream& operator<<(std::ostream& os, VET vet) + { + os << ( (vet == VET::VALUE) ? std::string("VALUE") : std::string("ERROR") ); + return os; + } /* template diff --git a/src/single_range.h b/src/single_range.h index 40f1e98..ab38d9e 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -43,7 +43,7 @@ namespace MultiArrayTools virtual MultiRangeType rangeType() const override; - virtual const U& getMetaPos() const; + virtual U getMetaPos() const; virtual SingleIndex& atMeta(const U& metaPos); virtual size_t dim() const override; // = 1 @@ -65,12 +65,15 @@ namespace MultiArrayTools class SingleRange : public RangeBase > { public: + typedef typename RangeBase >::IndexType IndexType; + DEFAULT_MEMBERS(SingleRange); SingleRange(const std::vector& space); virtual size_t size() const override; - + + //U get(size_t pos) const; const U& get(size_t pos) const; size_t getMeta(const U& metaPos) const; @@ -89,6 +92,8 @@ namespace MultiArrayTools class SingleRange : public RangeBase > { public: + typedef typename RangeBase >::IndexType IndexType; + DEFAULT_MEMBERS(SingleRange); SingleRange(int begin, int end); @@ -112,6 +117,8 @@ namespace MultiArrayTools class SingleRange : public RangeBase > { public: + typedef typename RangeBase >::IndexType IndexType; + DEFAULT_MEMBERS(SingleRange); SingleRange(size_t num); @@ -136,10 +143,14 @@ namespace MultiArrayTools ERROR = 1 }; + std::ostream& operator<<(std::ostream& os, VET vet); + template <> class SingleRange : public RangeBase > { public: + typedef typename RangeBase >::IndexType IndexType; + DEFAULT_MEMBERS(SingleRange); virtual size_t size() const override; @@ -157,6 +168,8 @@ namespace MultiArrayTools class SingleRange : public RangeBase > { public: + typedef typename RangeBase >::IndexType IndexType; + DEFAULT_MEMBERS(SingleRange); virtual size_t size() const override; diff --git a/src/slice.cc b/src/slice.cc index 437822a..7d7ac40 100644 --- a/src/slice.cc +++ b/src/slice.cc @@ -32,13 +32,13 @@ namespace MultiArrayTools } template - auto Slice::begin() -> decltype(Range().begin()) + auto Slice::begin() const -> decltype(Range().begin()) { return MAB::mRange->begin(); } template - auto Slice::end() -> decltype(Range().end()) + auto Slice::end() const -> decltype(Range().end()) { return MAB::mRange->end(); } @@ -58,4 +58,50 @@ namespace MultiArrayTools //mOwnIdx = i.pos(); return (*mMultiArrayPtr)[ mMAIdx ]; } + + template + ConstSlice:: + ConstSlice(const Range& range) : + MultiArrayBase(range) {} + + template + bool ConstSlice::isSlice() const + { + return true; + } + + template + void ConstSlice::set(const MultiArrayBase& multiArrayRef, + const Name& ownNm, // for correct linkage + const typename MARange::IndexType& MAIdx, // for desired slice position + const Name& MANm) // for correct linkage) + { + MAB::mInit = true; + mMultiArrayPtr = &multiArrayRef; + mMAIdx = MAIdx; + mOwnIdx = MAB::mRange->begin(); + mMAIdx.name(MANm); + mOwnIdx.name(ownNm); + mMAIdx.linkTo(&mOwnIdx); + } + + template + auto ConstSlice::begin() const -> decltype(Range().begin()) + { + return MAB::mRange->begin(); + } + + template + auto ConstSlice::end() const -> decltype(Range().end()) + { + return MAB::mRange->end(); + } + + template + const T& ConstSlice::operator[](const typename Range::IndexType& i) const + { + mOwnIdx.copyPos(i); + //mOwnIdx = i.pos(); + return (*mMultiArrayPtr)[ mMAIdx ]; + } } diff --git a/src/slice.h b/src/slice.h index de08ebe..09c66f8 100644 --- a/src/slice.h +++ b/src/slice.h @@ -29,8 +29,8 @@ namespace MultiArrayTools //Slice& setSlicePos(const Index& slicePos); // link given Index to mMAPtr which is index of total array - virtual auto begin() -> decltype(Range().begin()) override; - virtual auto end() -> decltype(Range().end()) override; + virtual auto begin() const -> decltype(Range().begin()) override; + virtual auto end() const -> decltype(Range().end()) override; virtual bool isSlice() const override; @@ -45,6 +45,37 @@ namespace MultiArrayTools mutable typename Range::IndexType mOwnIdx; mutable typename MARange::IndexType mMAIdx; }; + + template + class ConstSlice : public MultiArrayBase // yes, 'Range' is correct !!! + { + public: + + typedef MultiArrayBase MAB; + + ConstSlice(const Range& range); + + virtual const T& operator[](const typename Range::IndexType& i) const override; + + //Slice& setSlicePos(const Index& slicePos); + + // link given Index to mMAPtr which is index of total array + virtual auto begin() const -> decltype(Range().begin()) override; + virtual auto end() const -> decltype(Range().end()) override; + + virtual bool isSlice() const override; + + void set(const MultiArrayBase& multiArrayRef, + const Name& ownNm, + const typename MARange::IndexType& MAIdx, + const Name& MANm); + + private: + + MultiArrayBase const* mMultiArrayPtr = nullptr; + mutable typename Range::IndexType mOwnIdx; + mutable typename MARange::IndexType mMAIdx; + }; }