From 305f45103cffa65f13dc7d4a3bbf5630824fc209 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Mon, 29 Oct 2018 14:19:42 +0100 Subject: [PATCH] dynamic range test works --- src/include/ranges/anonymous_range.h | 4 +- src/include/ranges/dynamic_meta.h | 43 +++++++++ src/include/ranges/dynamic_range.h | 83 ++++++++++------- src/include/ranges/rbase_def.h | 6 ++ src/include/ranges/x_to_string.h | 20 ++++ src/lib/CMakeLists.txt | 1 + src/lib/ranges/dynamic_range.cc | 117 +++++++++++++++++------- src/tests/ranges/anonymous_unit_test.cc | 34 +++++++ 8 files changed, 240 insertions(+), 68 deletions(-) create mode 100644 src/include/ranges/dynamic_meta.h diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index ac60aeb..ca18f9f 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -16,8 +16,8 @@ namespace MultiArrayTools typedef SingleIndex AnonymousIndex; - template - using SIZET = size_t; + //template + //using SIZET = size_t; typedef SingleRange AnonymousRange; diff --git a/src/include/ranges/dynamic_meta.h b/src/include/ranges/dynamic_meta.h new file mode 100644 index 0000000..522da4f --- /dev/null +++ b/src/include/ranges/dynamic_meta.h @@ -0,0 +1,43 @@ + +#ifndef __dynamic_meta_h__ +#define __dynamic_meta_h__ + +#include +#include + +namespace MultiArrayTools +{ + + typedef std::pair DynamicMetaElem; + + class DynamicMetaT + { + private: + std::vector mMeta; + + public: + DynamicMetaT() = default; + DynamicMetaT(const DynamicMetaT& in) = default; + DynamicMetaT(DynamicMetaT&& in) = default; + DynamicMetaT& operator=(const DynamicMetaT& in) = default; + DynamicMetaT& operator=(DynamicMetaT&& in) = default; + + template + DynamicMetaT(const std::tuple& meta) + { + + } + + bool operator==(const DynamicMetaT& in) const; + bool operator!=(const DynamicMetaT& in) const; + + size_t size() const; + + DynamicMetaElem& operator[](size_t pos); + const DynamicMetaElem& operator[](size_t pos) const; + }; + + +} // namespace MultiArrayTools + +#endif diff --git a/src/include/ranges/dynamic_range.h b/src/include/ranges/dynamic_range.h index 31604e8..9995fc7 100644 --- a/src/include/ranges/dynamic_range.h +++ b/src/include/ranges/dynamic_range.h @@ -6,33 +6,20 @@ #include "ranges/range_base.h" #include "ranges/index_base.h" +#include "xfor/xfor.h" + +#include +#include "ranges/rpheader.h" +#include "ranges/x_to_string.h" +#include "ranges/type_map.h" + +#include "ranges/dynamic_meta.h" + namespace MultiArrayTools { - typedef std::pair DynamicMetaElem; + + using MultiArrayHelper::DynamicalExpression; - class DynamicMetaT - { - private: - std::vector mMeta; - - public: - DynamicMetaT() = default; - DynamicMetaT(const DynamicMetaT& in) = default; - DynamicMetaT(DynamicMetaT&& in) = default; - DynamicMetaT& operator=(const DynamicMetaT& in) = default; - DynamicMetaT& operator=(DynamicMetaT&& in) = default; - - template - DynamicMetaT(const std::tuple& meta); - - bool operator==(const DynamicMetaT& in) const; - bool operator!=(const DynamicMetaT& in) const; - - DynamicMetaElem& operator[](size_t pos); - const DynamicMetaElem& operator[](size_t pos) const; - }; - - class IndexWrapperBase { public: @@ -53,13 +40,15 @@ namespace MultiArrayTools virtual int mm(std::intptr_t idxPtrNum) = 0; virtual std::string stringMeta() const = 0; - virtual DynamicMetaT meta() const = 0; + //virtual DynamicMetaT meta() const = 0; virtual const DynamicMetaT* metaPtr() const = 0; //virtual IndexWrapperBase& at(const U& metaPos) = 0; //virtual size_t posAt(const U& metaPos) const = 0; //virtual bool isMeta(const U& metaPos) const = 0; - + + virtual size_t pos() const = 0; + virtual size_t max() const = 0; virtual size_t dim() const = 0; virtual bool last() const = 0; virtual bool first() const = 0; @@ -67,7 +56,8 @@ namespace MultiArrayTools virtual std::shared_ptr range() const = 0; virtual size_t getStepSize(size_t n) const = 0; - + virtual size_t getStepSizeComp(std::intptr_t j) const = 0; + virtual std::intptr_t get() const = 0; virtual DynamicalExpression ifor(size_t step, DynamicalExpression ex) const = 0; @@ -99,6 +89,9 @@ namespace MultiArrayTools virtual IndexWrapperBase& operator++() final { ++(*mI); return *this; } virtual IndexWrapperBase& operator--() final { --(*mI); return *this; } + virtual size_t pos() const final { return mI->pos(); } + virtual size_t max() const final { return mI->max(); } + virtual int pp(std::intptr_t idxPtrNum) final { return mI->pp(idxPtrNum); } virtual int mm(std::intptr_t idxPtrNum) final { return mI->mm(idxPtrNum); } @@ -117,7 +110,8 @@ namespace MultiArrayTools virtual std::shared_ptr range() const final { return mI->range(); } virtual size_t getStepSize(size_t n) const final { return mI->getStepSize(n); } - + virtual size_t getStepSizeComp(std::intptr_t j) const final { return getStepSize(*this, j); } + virtual std::intptr_t get() const final { return reinterpret_cast(mI.get()); } virtual DynamicalExpression ifor(size_t step, DynamicalExpression ex) const final { return mI->ifor(step, ex); } @@ -130,7 +124,9 @@ namespace MultiArrayTools class DynamicIndex : public IndexInterface { private: - std::vector,size_t>> mIVec; + typedef std::vector,size_t>> IVecT; + + IVecT mIVec; inline DynamicalExpression mkFor(size_t i, size_t step, DynamicalExpression ex, bool hidden = false) const; @@ -155,6 +151,8 @@ namespace MultiArrayTools DynamicIndex& operator++(); DynamicIndex& operator--(); + DynamicIndex& operator()(const IVecT& ivec); + int pp(std::intptr_t idxPtrNum); int mm(std::intptr_t idxPtrNum); @@ -166,17 +164,19 @@ namespace MultiArrayTools //bool isMeta(const MetaType& metaPos) const; - size_t dim(); - bool last(); - bool first(); + size_t dim() const; + bool last() const; + bool first() const; + + const IndexW& get(size_t n) const; std::shared_ptr range(); template void getPtr(); - size_t getStepSize(size_t n); - + size_t getStepSize(size_t n) const; + std::string id() const; void print(size_t offset); @@ -357,6 +357,21 @@ namespace MultiArrayHelper } } + template <> + inline size_t getStepSize(const DynamicIndex& ii, std::intptr_t j) + { + size_t ss = 0; + size_t sx = 1; + for(size_t i = ii.dim(); i != 0; --i){ + const auto& ni = ii.get(i); + const size_t max = ni.max(); + const size_t tmp = ni.getStepSizeComp(j); + ss += tmp * ii.getStepSize(i); + sx *= max; + } + return ss; + } + } namespace MultiArrayTools diff --git a/src/include/ranges/rbase_def.h b/src/include/ranges/rbase_def.h index b9f7d41..80284c5 100644 --- a/src/include/ranges/rbase_def.h +++ b/src/include/ranges/rbase_def.h @@ -3,8 +3,14 @@ #ifndef __ranges_base_def_h__ #define __ranges_base_def_h__ +#include + namespace MultiArrayTools { + + template + using SIZET = size_t; + /*********************** * Provided Types * ***********************/ diff --git a/src/include/ranges/x_to_string.h b/src/include/ranges/x_to_string.h index d3e6ac2..8d00dd4 100644 --- a/src/include/ranges/x_to_string.h +++ b/src/include/ranges/x_to_string.h @@ -7,6 +7,8 @@ #include #include +#include "ranges/dynamic_meta.h" + namespace MultiArrayHelper { template @@ -19,6 +21,11 @@ namespace MultiArrayHelper template <> inline std::string xToString(const std::string& x); + using MultiArrayTools::DynamicMetaT; + + template <> + inline std::string xToString(const DynamicMetaT& x); + template inline std::string xToString(const std::vector& x); @@ -69,6 +76,19 @@ namespace MultiArrayHelper return x; } + template <> + inline std::string xToString(const DynamicMetaT& x) + { + std::string out = "[ "; + for(size_t i = 0; i != x.size(); ++i){ + out += x[i].first; + out += " , "; + } + out.pop_back(); + out.back() = ']'; + return out; + } + template inline std::string xToString(const std::vector& x) { diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index fcbfd9f..0bda828 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -2,6 +2,7 @@ set(libmultiarray_a_SOURCES ${CMAKE_SOURCE_DIR}/src/lib/ranges/range_base.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/anonymous_range.cc + ${CMAKE_SOURCE_DIR}/src/lib/ranges/dynamic_range.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/multi_range_factory_product_map.cc ${CMAKE_SOURCE_DIR}/src/lib/map_range_factory_product_map.cc ) diff --git a/src/lib/ranges/dynamic_range.cc b/src/lib/ranges/dynamic_range.cc index 98caa88..f3d31e8 100644 --- a/src/lib/ranges/dynamic_range.cc +++ b/src/lib/ranges/dynamic_range.cc @@ -1,16 +1,54 @@ #include "ranges/dynamic_range.h" +#include "ranges/dynamic_meta.h" namespace MultiArrayTools { - /********************** + /********************* + * DynamicMetaT * + *********************/ + + bool DynamicMetaT::operator==(const DynamicMetaT& in) const + { + if(in.size() != mMeta.size()) { return false; } + for(size_t i = 0; i != mMeta.size(); ++i){ + if(in[i].second != mMeta[i].second) { return false; } + for(size_t j = 0; j != mMeta[i].second; ++j){ + if(in[i].first[j] != mMeta[i].first[j]) { return false; } + } + } + return true; + } + + bool DynamicMetaT::operator!=(const DynamicMetaT& in) const + { + return not operator==(in); + } + + size_t DynamicMetaT::size() const + { + return mMeta.size(); + } + + DynamicMetaElem& DynamicMetaT::operator[](size_t pos) + { + return mMeta[pos]; + } + + const DynamicMetaElem& DynamicMetaT::operator[](size_t pos) const + { + return mMeta[pos]; + } + + + /********************* * DynamicIndex * - **********************/ + *********************/ DynamicIndex::DynamicIndex(const std::shared_ptr& range) : - IndexInterface(range, 0), - mExplicitRangePtr(std::dynamic_pointer_cast(IB::mRangePtr)), + IndexInterface(range, 0)//, + //mExplicitRangePtr(std::dynamic_pointer_cast(IB::mRangePtr)), {} @@ -25,21 +63,38 @@ namespace MultiArrayTools IB::mPos = pos; return *this; } - DynamicIndex& DynamicIndex::operator++() { - // !!! + size_t ipos = mIVec.size()-1; + auto& ii = mIVec[ipos].first; + auto& jj = mIVec[ipos-1].first; + while(ii->pos() == ii->max()-1 and ipos != 0) { + (*ii) = 0; + ++(*jj); + --ipos; + } return *this; } - DynamicIndex& DynamicIndex::operator--() { - // !!! + size_t ipos = mIVec.size()-1; + auto& ii = mIVec[ipos].first; + auto& jj = mIVec[ipos-1].first; + while(ii->pos() == 0 and ipos != 0) { + (*ii) = ii->max()-1; + --(*jj); + --ipos; + } return *this; } + DynamicIndex& DynamicIndex::operator()(const IVecT& ivec) + { + mIVec = ivec; + return *this; + } int DynamicIndex::pp(std::intptr_t idxPtrNum) { @@ -61,13 +116,13 @@ namespace MultiArrayTools } - MetaType DynamicIndex::meta() const + DynamicIndex::MetaType DynamicIndex::meta() const { - return mExplicitRangePtr->meta(IB::mPos); + return std::dynamic_pointer_cast( IB::mRangePtr )->get(IB::mPos); } - - const MetaType* DynamicIndex::metaPtr() const + + const DynamicIndex::MetaType* DynamicIndex::metaPtr() const { return nullptr; } @@ -86,29 +141,33 @@ namespace MultiArrayTools } - size_t DynamicIndex::posAt(const U& metaPos) const + size_t DynamicIndex::posAt(const MetaType& metaPos) const { return std::dynamic_pointer_cast( IB::mRangePtr )->getMeta( metaPos ); } - size_t DynamicIndex::dim() // = 1 + size_t DynamicIndex::dim() const // = 1 { return 1; } - bool DynamicIndex::last() + bool DynamicIndex::last() const { return IB::mPos == IB::mMax - 1; } - bool DynamicIndex::first() + bool DynamicIndex::first() const { return IB::mPos == 0; } + const IndexW& DynamicIndex::get(size_t n) const + { + return *mIVec[n].first; + } std::shared_ptr DynamicIndex::range() { @@ -120,12 +179,11 @@ namespace MultiArrayTools void DynamicIndex::getPtr() {} - size_t DynamicIndex::getStepSize(size_t n) + size_t DynamicIndex::getStepSize(size_t n) const { - return 1; + return mIVec[n].second; } - std::string DynamicIndex::id() const { return std::string("dyn") + std::to_string(IB::mId); @@ -139,7 +197,7 @@ namespace MultiArrayTools } for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } std::cout << id() << "[" << reinterpret_cast(this) - << "](" << IB::mRangePtr << "): " << meta() << std::endl; + << "](" << IB::mRangePtr << "): " /*<< meta().first*/ << std::endl; } @@ -197,14 +255,14 @@ namespace MultiArrayTools * DynamicRange * ***********************/ - size_t DynamicRange::get(size_t pos) const + DynamicRange::MetaType DynamicRange::get(size_t pos) const { - return pos; + return MetaType(); // !!! } - size_t DynamicRange::getMeta(size_t metaPos) const + size_t DynamicRange::getMeta(const MetaType& metaPos) const { - return metaPos; + return 0; // !!! } size_t DynamicRange::size() const @@ -214,7 +272,7 @@ namespace MultiArrayTools size_t DynamicRange::dim() const { - return 1; + return mOrig.size(); } SpaceType DynamicRange::spaceType() const @@ -258,11 +316,6 @@ namespace MultiArrayTools return out; } - size_t DynamicRange::anonymousDim() const - { - return mOrig.size(); - } - typename DynamicRange::IndexType DynamicRange::begin() const { DynamicIndex i @@ -296,7 +349,7 @@ namespace MultiArrayTools /***************** * Functions * *****************/ - + /* std::shared_ptr defaultRange(size_t size ) { DynamicRangeFactory arf @@ -304,7 +357,7 @@ namespace MultiArrayTools (DynamicRange::factory().create() ) ); return std::dynamic_pointer_cast( arf.create() ); } - + */ } // end namespace MultiArrayTools diff --git a/src/tests/ranges/anonymous_unit_test.cc b/src/tests/ranges/anonymous_unit_test.cc index 80e6f80..dd77c81 100644 --- a/src/tests/ranges/anonymous_unit_test.cc +++ b/src/tests/ranges/anonymous_unit_test.cc @@ -123,6 +123,40 @@ namespace { EXPECT_EQ(mr->template getPtr<1>()->size(), m3rptr->size() * sr2ptr->size()); } + TEST_F(AnonymousTest, DCast1) + { + DynamicRangeFactory arf1(sr1ptr,m3rptr); + auto ar1a = std::dynamic_pointer_cast( arf1.create() ); + + arf1.append(sr2ptr); + + auto ar1b = std::dynamic_pointer_cast( arf1.create() ); + + DynamicRangeFactory arf2(sr1ptr,m3rptr,sr2ptr); + auto ar2 = std::dynamic_pointer_cast( arf2.create() ); + + EXPECT_EQ(ar1b.get(), ar2.get()); + EXPECT_EQ(ar1a->size(), sr1ptr->size() * m3rptr->size()); + EXPECT_EQ(ar2->size(), sr1ptr->size() * m3rptr->size() * sr2ptr->size()); + + EXPECT_EQ(ar1a->sub(0)->size(), sr1ptr->size()); + EXPECT_EQ(ar1a->sub(1)->size(), m3rptr->size()); + + EXPECT_EQ(ar2->sub(0)->size(), sr1ptr->size()); + EXPECT_EQ(ar2->sub(1)->size(), m3rptr->size()); + EXPECT_EQ(ar2->sub(2)->size(), sr2ptr->size()); + } + + TEST_F(AnonymousTest, DCast2) + { + DynamicRangeFactory arf2(sr1ptr,m3rptr,sr2ptr); + auto ar = std::dynamic_pointer_cast( arf2.create() ); + auto mr = ar->template scast(1,2); + + EXPECT_EQ(mr->template getPtr<0>()->size(), sr1ptr->size()); + EXPECT_EQ(mr->template getPtr<1>()->size(), m3rptr->size() * sr2ptr->size()); + } + TEST_F(AnonymousTest, ToString1) { std::vector vv = sr1ptr->data();