From 459ea690e5d991bb184acbe08ff6ef9eb930a6f8 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 11 Oct 2022 23:01:31 +0200 Subject: [PATCH] more on DPos/VPos --- src/include/ranges/xfor/pos_type.cc.h | 36 +++++- src/include/ranges/xfor/pos_type.h | 8 +- src/include/ranges/xfor/vpos_type.cc.h | 154 +++++++++++++++---------- src/include/ranges/xfor/vpos_type.h | 24 ++-- src/tests/xfor_unit_test.cc | 12 +- 5 files changed, 151 insertions(+), 83 deletions(-) diff --git a/src/include/ranges/xfor/pos_type.cc.h b/src/include/ranges/xfor/pos_type.cc.h index 564788f..710e2d7 100644 --- a/src/include/ranges/xfor/pos_type.cc.h +++ b/src/include/ranges/xfor/pos_type.cc.h @@ -69,6 +69,12 @@ namespace CNORXZ return MPos,PosT>(*this,a); } + template + constexpr SPos::operator UPos() const + { + return UPos(N); + } + /************ * UPos * ************/ @@ -315,20 +321,32 @@ namespace CNORXZ template inline DPos::DPos(const PosT& a) : - ObjHandle(std::make_unique>(a)) + ObjHandle + (std::make_unique::type>>(a)) { - static_assert(is_pos_type::value, + static_assert(is_pos_type::type>::value, "DPos can only be constructed from pos types"); } template inline DPos::DPos(PosT&& a) : - ObjHandle(std::make_unique>(a)) + ObjHandle + (std::make_unique::type>>(a)) { - static_assert(is_pos_type::value, + static_assert(is_pos_type::type>::value, "DPos can only be constructed from pos types"); } + inline const VPosBase* DPos::get() const + { + return mC->get(); + } + + inline bool DPos::F() const + { + return mC->F(); + } + inline SizeT DPos::size() const { return mC->vsize(); @@ -376,6 +394,16 @@ namespace CNORXZ inline DPosRef::DPosRef(const VPosBase* p) : mP(p) {} + inline const VPosBase* DPosRef::get() const + { + return mP->get(); + } + + inline bool DPosRef::F() const + { + return mP->F(); + } + inline SizeT DPosRef::size() const { return mP->vsize(); diff --git a/src/include/ranges/xfor/pos_type.h b/src/include/ranges/xfor/pos_type.h index af28abb..485b9e0 100644 --- a/src/include/ranges/xfor/pos_type.h +++ b/src/include/ranges/xfor/pos_type.h @@ -37,6 +37,8 @@ namespace CNORXZ template constexpr auto extend(const PosT& a) const; + + explicit constexpr operator UPos() const; }; class UPos @@ -167,6 +169,8 @@ namespace CNORXZ template inline explicit DPos(PosT&& a); + inline const VPosBase* get() const; + inline bool F() const; inline SizeT size() const; inline SizeT val() const; inline DPosRef next() const; @@ -194,7 +198,9 @@ namespace CNORXZ DEFAULT_MEMBERS(DPosRef); explicit DPosRef(const VPosBase* p); - + + inline const VPosBase* get() const; + inline bool F() const; inline SizeT size() const; inline SizeT val() const; inline DPosRef next() const; diff --git a/src/include/ranges/xfor/vpos_type.cc.h b/src/include/ranges/xfor/vpos_type.cc.h index 5e7e6e9..3ac2147 100644 --- a/src/include/ranges/xfor/vpos_type.cc.h +++ b/src/include/ranges/xfor/vpos_type.cc.h @@ -11,17 +11,57 @@ namespace CNORXZ ****************/ template - Uptr VPosBase::vextend(const SPos& a) const + inline Uptr VPosBase::vextend(const SPos& a) const { return this->vextend(UPos(N)); } template - Uptr VPosBase::vextend(const SFPos& a) const + inline Uptr VPosBase::vextend(const SFPos& a) const { return this->vextend(static_cast(a)); } + template + inline Uptr VPosBase::vextend(const MPos& a) const + { + return this->vextend(static_cast(a))->vextend(a.next()); + } + + inline Uptr vextend(const DPos& a) const + { + Uptr out = nullptr; + if(a.F()){ + out = this->vextend(dynamic_cast(a.get())); + } + else { + out = this->vextend(dynamic_cast(a.get())); + } + if(a.size() > 1){ + return out->vextend(a.next()); + } + else { + return out; + } + } + + inline Uptr vextend(const DPosRef& a) const + { + Uptr out = nullptr; + if(a.F()){ + out = this->vextend(dynamic_cast(a.get())); + } + else { + out = this->vextend(dynamic_cast(a.get())); + } + if(a.size() > 1){ + return out->vextend(a.next()); + } + else { + return out; + } + } + /************ * VPos * ************/ @@ -37,16 +77,27 @@ namespace CNORXZ return std::make_unique>(*this); } + template + bool VPos::F() const + { + if constexpr(typename std::is_same::value){ + return true; + } + else { + return false; + } + } + template SizeT VPos::vsize() const { - return PosT::THIS().size(); + return this->size(); } template SizeT VPos::vval() const { - return PosT::THIS().val(); + return this->val(); } template @@ -76,20 +127,6 @@ namespace CNORXZ return std::make_unique>( (*this)(UPos(a->vval())) ); } - template - Uptr VPos::vextend(const DPos& a) const - { - typedef decltype(this->extend(a)) OPosT; - return std::make_unique>(this->extend(a)); - } - - template - Uptr VPos::vextend(const DPosRef& a) const - { - typedef decltype(this->extend(a)) OPosT; - return std::make_unique>(this->extend(a)); - } - template Uptr VPos::vextend(const UPos& a) const { @@ -111,7 +148,7 @@ namespace CNORXZ template VPos>::VPos(const MPos& a) : - MPos(a.THIS()), + MPos(a), mNRef(&this->next()) {} @@ -121,6 +158,17 @@ namespace CNORXZ return std::make_unique>>(*this); } + template + bool VPos>::F() const + { + if constexpr(typename std::is_same::value){ + return true; + } + else { + return false; + } + } + template SizeT VPos>::vsize() const { @@ -160,20 +208,6 @@ namespace CNORXZ return std::make_unique>( (*this)(UPos(a->vval())) ); } - template - Uptr VPos>::vextend(const DPos& a) const - { - typedef decltype(this->extend(a)) OPosT; - return std::make_unique>(this->extend(a)); - } - - template - Uptr VPos>::vextend(const DPosRef& a) const - { - typedef decltype(this->extend(a)) OPosT; - return std::make_unique>(this->extend(a)); - } - template Uptr VPos>::vextend(const UPos& a) const { @@ -194,7 +228,7 @@ namespace CNORXZ template VPosRef::VPosRef(const PosT* c) : - mC(&c) + mC(c) {} template @@ -203,6 +237,17 @@ namespace CNORXZ return std::make_unique>(*mC); } + template + bool VPosRef::F() const + { + if constexpr(typename std::is_same::value){ + return true; + } + else { + return false; + } + } + template SizeT VPosRef::vsize() const { @@ -242,20 +287,6 @@ namespace CNORXZ return std::make_unique>( (*mC)(UPos(a->vval())) ); } - template - Uptr VPosRef::vextend(const DPos& a) const - { - typedef decltype(mC->extend(a)) OPosT; - return std::make_unique>(mC->extend(a)); - } - - template - Uptr VPosRef::vextend(const DPosRef& a) const - { - typedef decltype(mC->extend(a)) OPosT; - return std::make_unique>(mC->extend(a)); - } - template Uptr VPosRef::vextend(const UPos& a) const { @@ -276,7 +307,7 @@ namespace CNORXZ template VPosRef>::VPosRef(const MPos* c) : - mC(c), mNRef(&c->next()) + mC(c), mNRef(c->vnext()) {} template @@ -285,6 +316,17 @@ namespace CNORXZ return std::make_unique>>(*mC); } + template + bool VPosRef>::F() const + { + if constexpr(typename std::is_same::value){ + return true; + } + else { + return false; + } + } + template SizeT VPosRef>::vsize() const { @@ -323,20 +365,6 @@ namespace CNORXZ typedef decltype( (*mC)(UPos(a->vval())) ) OPosT; return std::make_unique>( (*mC)(UPos(a->vval())) ); } - - template - Uptr VPosRef>::vextend(const DPos& a) const - { - typedef decltype(mC->extend(a)) OPosT; - return std::make_unique>(mC->extend(a)); - } - - template - Uptr VPosRef>::vextend(const DPosRef& a) const - { - typedef decltype(mC->extend(a)) OPosT; - return std::make_unique>(mC->extend(a)); - } template Uptr VPosRef>::vextend(const UPos& a) const diff --git a/src/include/ranges/xfor/vpos_type.h b/src/include/ranges/xfor/vpos_type.h index 1fb7069..33bec21 100644 --- a/src/include/ranges/xfor/vpos_type.h +++ b/src/include/ranges/xfor/vpos_type.h @@ -12,22 +12,28 @@ namespace CNORXZ DEFAULT_MEMBERS(VPosBase); virtual Uptr copy() const = 0; + virtual bool F() const = 0; virtual SizeT vsize() const = 0; virtual SizeT vval() const = 0; + virtual const VPosBase* vget() const = 0; // strip away all MPos<...> virtual const VPosBase* vnext() const = 0; virtual Uptr vplus(const VPosBase* a) const = 0; virtual Uptr vtimes(const VPosBase* a) const = 0; virtual Uptr vexec(const VPosBase* a) const = 0; - virtual Uptr vextend(const DPos& a) const = 0; - virtual Uptr vextend(const DPosRef& a) const = 0; virtual Uptr vextend(const UPos& a) const = 0; virtual Uptr vextend(const FPos& a) const = 0; template - Uptr vextend(const SPos& a) const; + inline Uptr vextend(const SPos& a) const; template - Uptr vextend(const SFPos& a) const; + inline Uptr vextend(const SFPos& a) const; + + template + inline Uptr vextend(const MPos& a) const; + + inline Uptr vextend(const DPos& a) const; + inline Uptr vextend(const DPosRef& a) const; }; template @@ -39,14 +45,13 @@ namespace CNORXZ VPos(const PosT& a); virtual Uptr copy() const override final; + virtual bool F() const override final; virtual SizeT vsize() const override final; virtual SizeT vval() const override final; virtual const VPosBase* vnext() const override final; virtual Uptr vplus(const VPosBase* a) const override final; virtual Uptr vtimes(const VPosBase* a) const override final; virtual Uptr vexec(const VPosBase* a) const override final; - virtual Uptr vextend(const DPos& a) const override final; - virtual Uptr vextend(const DPosRef& a) const override final; virtual Uptr vextend(const UPos& a) const override final; virtual Uptr vextend(const FPos& a) const override final; }; @@ -62,14 +67,13 @@ namespace CNORXZ VPos(const MPos& a); virtual Uptr copy() const override final; + virtual bool F() const override final; virtual SizeT vsize() const override final; virtual SizeT vval() const override final; virtual const VPosBase* vnext() const override final; virtual Uptr vplus(const VPosBase* a) const override final; virtual Uptr vtimes(const VPosBase* a) const override final; virtual Uptr vexec(const VPosBase* a) const override final; - virtual Uptr vextend(const DPos& a) const override final; - virtual Uptr vextend(const DPosRef& a) const override final; virtual Uptr vextend(const UPos& a) const override final; virtual Uptr vextend(const FPos& a) const override final; }; @@ -85,14 +89,13 @@ namespace CNORXZ VPosRef(const PosT* c); virtual Uptr copy() const override final; + virtual bool F() const override final; virtual SizeT vsize() const override final; virtual SizeT vval() const override final; virtual const VPosBase* vnext() const override final; virtual Uptr vplus(const VPosBase* a) const override final; virtual Uptr vtimes(const VPosBase* a) const override final; virtual Uptr vexec(const VPosBase* a) const override final; - virtual Uptr vextend(const DPos& a) const override final; - virtual Uptr vextend(const DPosRef& a) const override final; virtual Uptr vextend(const UPos& a) const override final; virtual Uptr vextend(const FPos& a) const override final; @@ -110,6 +113,7 @@ namespace CNORXZ VPosRef(const MPos* c); virtual Uptr copy() const override final; + virtual bool F() const override final; virtual SizeT vsize() const override final; virtual SizeT vval() const override final; virtual const VPosBase* vnext() const override final; diff --git a/src/tests/xfor_unit_test.cc b/src/tests/xfor_unit_test.cc index c95b44b..8a471fe 100644 --- a/src/tests/xfor_unit_test.cc +++ b/src/tests/xfor_unit_test.cc @@ -97,24 +97,25 @@ namespace EXPECT_EQ(mp5.val(), mUp2.val() * mUp1.val()); EXPECT_EQ(mp5.next().val(), mS4p.val() * mUp1.val()); } - /* + TEST_F(Pos_Test, Dyn) { - DPos dp01(mS2p); + DPos dp01(static_cast(mS2p)); DPos dp02(mUp1); + /* DPos dp1(mkMPos(mS2p, mUp1)); DPos dp2(mkMPos(mUp2, mS4p)); auto dp3a = dp1 + dp2; auto dp3b = dp2 + dp1; auto dp4 = dp1 * dp01; auto dp5 = dp2 * dp02; - + */ EXPECT_EQ(dp01.size(), 1); EXPECT_EQ(dp02.size(), 1); EXPECT_EQ(dp01.val(), mS2p.val()); EXPECT_EQ(dp02.val(), mUp1.val()); - + /* EXPECT_EQ(dp1.size(), 2); EXPECT_EQ(dp2.size(), 2); @@ -135,8 +136,9 @@ namespace EXPECT_EQ(dp4.next().val(), mUp1.val() * mS2p.val()); EXPECT_EQ(dp5.first().val(), mUp2.val() * mUp1.val()); EXPECT_EQ(dp5.next().val(), mS4p.val() * mUp1.val()); + */ } - */ + } int main(int argc, char** argv)