From ed6c90b95cb24a586ef2ce69f8204759f2f746c2 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 11 Oct 2022 18:36:23 +0200 Subject: [PATCH] new DPos/VPos + further macros --- src/include/base/macros.h | 14 ++- src/include/base/obj_handle.cc.h | 3 - src/include/base/obj_handle.h | 1 - src/include/base/types.h | 10 -- src/include/ranges/xfor/pos_type.cc.h | 117 +++++++++++++++++++ src/include/ranges/xfor/pos_type.h | 69 ++++++++++- src/include/ranges/xfor/vpos_type.cc.h | 154 +++++++++++++++---------- src/include/ranges/xfor/vpos_type.h | 29 +++-- 8 files changed, 303 insertions(+), 94 deletions(-) diff --git a/src/include/base/macros.h b/src/include/base/macros.h index 47f3d0c..0bebe57 100644 --- a/src/include/base/macros.h +++ b/src/include/base/macros.h @@ -19,8 +19,20 @@ #define DEFAULT_MOVE_A(__class_name__) __class_name__& operator=(__class_name__&& a) = default #define DEFAULT_COPY(__class_name__) DEFAULT_COPY_C(__class_name__); DEFAULT_COPY_A(__class_name__) #define DEFAULT_MOVE(__class_name__) DEFAULT_MOVE_C(__class_name__); DEFAULT_MOVE_A(__class_name__) - #define DEFAULT_MEMBERS_X(__class_name__) DEFAULT_COPY(__class_name__); DEFAULT_MOVE(__class_name__) #define DEFAULT_MEMBERS(__class_name__) DEFAULT_C(__class_name__); DEFAULT_MEMBERS_X(__class_name__) +#define SP_DEFAULT_C(__spec__,__class_name__) __spec__ __class_name__() = default +#define SP_DEFAULT_COPY_C(__spec__,__class_name__) __spec__ __class_name__(const __class_name__& a) = default +#define SP_DEFAULT_COPY_A(__spec__,__class_name__) __spec__ __class_name__& operator=(const __class_name__& a) = default +#define SP_DEFAULT_MOVE_C(__spec__,__class_name__) __spec__ __class_name__(__class_name__&& a) = default +#define SP_DEFAULT_MOVE_A(__spec__,__class_name__) __spec__ __class_name__& operator=(__class_name__&& a) = default +#define SP_DEFAULT_COPY(__spec__,__class_name__) SP_DEFAULT_COPY_C(__spec__,__class_name__); SP_DEFAULT_COPY_A(__spec__,__class_name__) +#define SP_DEFAULT_MOVE(__spec__,__class_name__) SP_DEFAULT_MOVE_C(__spec__,__class_name__); SP_DEFAULT_MOVE_A(__spec__,__class_name__) +#define SP_DEFAULT_MEMBERS_X(__spec__,__class_name__) SP_DEFAULT_COPY(__spec__,__class_name__); SP_DEFAULT_MOVE(__spec__,__class_name__) +#define SP_DEFAULT_MEMBERS(__spec__,__class_name__) SP_DEFAULT_C(__spec__,__class_name__); SP_DEFAULT_MEMBERS_X(__spec__,__class_name__) + +#define CXZ_CVAL_FALSE static constexpr bool value = false +#define CXZ_CVAL_TRUE static constexpr bool value = true + #endif diff --git a/src/include/base/obj_handle.cc.h b/src/include/base/obj_handle.cc.h index f9fe826..17a58e8 100644 --- a/src/include/base/obj_handle.cc.h +++ b/src/include/base/obj_handle.cc.h @@ -10,9 +10,6 @@ namespace CNORXZ template ObjHandle::ObjHandle() {} - template - ObjHandle::ObjHandle(const T& a) : mC(std::make_unique(a)) {} - template ObjHandle::ObjHandle(Uptr&& a) : mC(std::forward>(a)) {} diff --git a/src/include/base/obj_handle.h b/src/include/base/obj_handle.h index cf140f2..fe12ed1 100644 --- a/src/include/base/obj_handle.h +++ b/src/include/base/obj_handle.h @@ -18,7 +18,6 @@ namespace CNORXZ public: ObjHandle(); - ObjHandle(const T& a); ObjHandle(Uptr&& a); ObjHandle(const ObjHandle& a); ObjHandle(ObjHandle&& a); diff --git a/src/include/base/types.h b/src/include/base/types.h index aed952f..674f58b 100644 --- a/src/include/base/types.h +++ b/src/include/base/types.h @@ -120,16 +120,6 @@ namespace CNORXZ template class MPos; - // definition: ranges/xfor/pos_type.h - template - class FMPos; - - // definition: ranges/xfor/pos_type.h - class UPos; - - // definition: ranges/xfor/pos_type.h - class FPos; - // definition: ranges/range_base.h class RangeBase; diff --git a/src/include/ranges/xfor/pos_type.cc.h b/src/include/ranges/xfor/pos_type.cc.h index 4f1ab08..564788f 100644 --- a/src/include/ranges/xfor/pos_type.cc.h +++ b/src/include/ranges/xfor/pos_type.cc.h @@ -216,6 +216,12 @@ namespace CNORXZ return MPos,PosT>(*this,a); } + template + constexpr SFPos::operator FPos() const + { + return FPos(N, &sMs[0]); + } + /************ * MPos * ************/ @@ -299,6 +305,117 @@ namespace CNORXZ return mNext.extend(a); } + /************ + * DPos * + ************/ + + inline DPos::DPos(Uptr&& a) : + ObjHandle(std::forward>(a)) + {} + + template + inline DPos::DPos(const PosT& a) : + ObjHandle(std::make_unique>(a)) + { + static_assert(is_pos_type::value, + "DPos can only be constructed from pos types"); + } + + template + inline DPos::DPos(PosT&& a) : + ObjHandle(std::make_unique>(a)) + { + static_assert(is_pos_type::value, + "DPos can only be constructed from pos types"); + } + + inline SizeT DPos::size() const + { + return mC->vsize(); + } + + inline SizeT DPos::val() const + { + return mC->vval(); + } + + inline DPosRef DPos::next() const + { + return DPosRef(mC->vnext()); + } + + template + inline DPos DPos::operator+(const PosT& a) const + { + return DPos(mC->vplus( VPosRef(&a) )); + } + + template + inline DPos DPos::operator*(const PosT& a) const + { + + return DPos(mC->vtimes( VPosRef(&a) )); + } + + // same as operator*, except for FPos/SFPos, where map is executed + template + inline DPos DPos::operator()(const PosT& a) const + { + return DPos(mC->vexec( VPosRef(&a) )); + } + + template + inline DPos DPos::extend(const PosT& a) const + { + return DPos(mC->vextend( a )); + } + + /*************** + * DPosRef * + ***************/ + + inline DPosRef::DPosRef(const VPosBase* p) : mP(p) {} + + inline SizeT DPosRef::size() const + { + return mP->vsize(); + } + + inline SizeT DPosRef::val() const + { + return mP->vval(); + } + + inline DPosRef DPosRef::next() const + { + return DPosRef(mP->vnext()); + } + + template + inline DPos DPosRef::operator+(const PosT& a) const + { + return DPos(mP->vplus( VPosRef(&a) )); + } + + template + inline DPos DPosRef::operator*(const PosT& a) const + { + return DPos(mP->vtimes( VPosRef(&a) )); + } + + // same as operator*, except for FPos/SFPos, where map is executed + template + inline DPos DPosRef::operator()(const PosT& a) const + { + return DPos(mP->vexec( VPosRef(&a) )); + } + + template + inline DPos DPosRef::extend(const PosT& a) const + { + return DPos(mP->vextend( a )); + } + } #endif diff --git a/src/include/ranges/xfor/pos_type.h b/src/include/ranges/xfor/pos_type.h index f839e97..af28abb 100644 --- a/src/include/ranges/xfor/pos_type.h +++ b/src/include/ranges/xfor/pos_type.h @@ -6,9 +6,6 @@ namespace CNORXZ { - // shift to base.h -#define CXZ_CVAL_FALSE static constexpr bool value = false -#define CXZ_CVAL_TRUE static constexpr bool value = true template struct is_pos_type { CXZ_CVAL_FALSE; }; @@ -93,6 +90,7 @@ namespace CNORXZ template constexpr auto extend(const PosT1& a) const; + }; template @@ -119,6 +117,8 @@ namespace CNORXZ template constexpr auto extend(const PosT& a) const; + + explicit constexpr operator FPos() const; }; template @@ -154,6 +154,65 @@ namespace CNORXZ constexpr auto extend(const PosT& a) const; }; + class DPos : public ObjHandle + { + public: + DEFAULT_MEMBERS(DPos); + + inline DPos(Uptr&& a); + + template + inline explicit DPos(const PosT& a); + + template + inline explicit DPos(PosT&& a); + + inline SizeT size() const; + inline SizeT val() const; + inline DPosRef next() const; + + template + inline DPos operator+(const PosT& a) const; + + template + inline DPos operator*(const PosT& a) const; + + // same as operator*, except for FPos/SFPos, where map is executed + template + inline DPos operator()(const PosT& a) const; + + template + inline DPos extend(const PosT& a) const; + }; + + class DPosRef + { + private: + const VPosBase* mP = nullptr; + + public: + DEFAULT_MEMBERS(DPosRef); + + explicit DPosRef(const VPosBase* p); + + inline SizeT size() const; + inline SizeT val() const; + inline DPosRef next() const; + + template + inline DPos operator+(const PosT& a) const; + + template + inline DPos operator*(const PosT& a) const; + + // same as operator*, except for FPos/SFPos, where map is executed + template + inline DPos operator()(const PosT& a) const; + + template + inline DPos extend(const PosT& a) const; + }; + template struct is_pos_type> { CXZ_CVAL_TRUE; }; template struct is_scalar_pos_type> { CXZ_CVAL_TRUE; }; template <> struct is_pos_type { CXZ_CVAL_TRUE; }; @@ -163,7 +222,9 @@ namespace CNORXZ template struct is_pos_type> { CXZ_CVAL_TRUE; }; template struct is_scalar_pos_type> { CXZ_CVAL_TRUE; }; template struct is_pos_type> { CXZ_CVAL_TRUE; }; - + template <> struct is_pos_type { CXZ_CVAL_TRUE; }; + template <> struct is_pos_type { CXZ_CVAL_TRUE; }; + } // end namespace CNORXZInternal diff --git a/src/include/ranges/xfor/vpos_type.cc.h b/src/include/ranges/xfor/vpos_type.cc.h index d0d2815..5e7e6e9 100644 --- a/src/include/ranges/xfor/vpos_type.cc.h +++ b/src/include/ranges/xfor/vpos_type.cc.h @@ -16,13 +16,19 @@ namespace CNORXZ return this->vextend(UPos(N)); } + template + Uptr VPosBase::vextend(const SFPos& a) const + { + return this->vextend(static_cast(a)); + } + /************ * VPos * ************/ template - VPos::VPos(const CPosInterface& a) : - PosT(a.THIS()) + VPos::VPos(const PosT& a) : + PosT(a) {} template @@ -43,12 +49,6 @@ namespace CNORXZ return PosT::THIS().val(); } - template - const VPosBase* VPos::vget() const - { - return this; - } - template const VPosBase* VPos::vnext() const { @@ -58,37 +58,50 @@ namespace CNORXZ template Uptr VPos::vplus(const VPosBase* a) const { - return std::make_unique>((*this) + DPosRef(a)); + typedef decltype((*this) + UPos(a->vval())) OPosT; + return std::make_unique>((*this) + UPos(a->vval())); } template Uptr VPos::vtimes(const VPosBase* a) const { - return std::make_unique>((*this) * UPos(a->vval())); + typedef decltype((*this) * UPos(a->vval())) OPosT; + return std::make_unique>((*this) * UPos(a->vval())); } - + + template + Uptr VPos::vexec(const VPosBase* a) const + { + typedef decltype((*this)(UPos(a->vval()))) OPosT; + return std::make_unique>( (*this)(UPos(a->vval())) ); + } + template Uptr VPos::vextend(const DPos& a) const { - return std::make_unique>(*this,a); + typedef decltype(this->extend(a)) OPosT; + return std::make_unique>(this->extend(a)); } template Uptr VPos::vextend(const DPosRef& a) const { - return std::make_unique>(*this,a); + typedef decltype(this->extend(a)) OPosT; + return std::make_unique>(this->extend(a)); } template Uptr VPos::vextend(const UPos& a) const { - return std::make_unique>(*this,a); + typedef decltype(this->extend(a)) OPosT; + return std::make_unique>(this->extend(a)); } template Uptr VPos::vextend(const FPos& a) const { - return std::make_unique>(*this,a); + typedef decltype(this->extend(a)) OPosT; + return std::make_unique>(this->extend(a)); } // .... probably I need to define a static instanciation limit... @@ -97,9 +110,9 @@ namespace CNORXZ ******************/ template - VPos>::VPos(const CPosInterface>& a) : + VPos>::VPos(const MPos& a) : MPos(a.THIS()), - mFRef(&this->first()), mNRef(&this->next()) + mNRef(&this->next()) {} template @@ -120,12 +133,6 @@ namespace CNORXZ return this->val(); } - template - const VPosBase* VPos>::vget() const - { - return &mFRef; - } - template const VPosBase* VPos>::vnext() const { @@ -135,37 +142,50 @@ namespace CNORXZ template Uptr VPos>::vplus(const VPosBase* a) const { - return std::make_unique>((*this) + DPosRef(a)); + typedef decltype((*this) + DPosRef(a)) OPosT; + return std::make_unique>((*this) + DPosRef(a)); } template Uptr VPos>::vtimes(const VPosBase* a) const { - return std::make_unique>((*this) * UPos(a->vval())); + typedef decltype((*this) * UPos(a->vval())) OPosT; + return std::make_unique>((*this) * UPos(a->vval())); } - + + template + Uptr VPos>::vexec(const VPosBase* a) const + { + typedef decltype( (*this)(UPos(a->vval())) ) OPosT; + return std::make_unique>( (*this)(UPos(a->vval())) ); + } + template Uptr VPos>::vextend(const DPos& a) const { - return std::make_unique>(*this,a); + typedef decltype(this->extend(a)) OPosT; + return std::make_unique>(this->extend(a)); } template Uptr VPos>::vextend(const DPosRef& a) const { - return std::make_unique>(*this,a); + typedef decltype(this->extend(a)) OPosT; + return std::make_unique>(this->extend(a)); } template Uptr VPos>::vextend(const UPos& a) const { - return std::make_unique>(*this,a); + typedef decltype(this->extend(a)) OPosT; + return std::make_unique>(this->extend(a)); } template Uptr VPos>::vextend(const FPos& a) const { - return std::make_unique>(*this,a); + typedef decltype(this->extend(a)) OPosT; + return std::make_unique>(this->extend(a)); } /*************** @@ -173,8 +193,8 @@ namespace CNORXZ ***************/ template - VPosRef::VPosRef(const CPosInterface* c) : - mC(&c->THIS()) + VPosRef::VPosRef(const PosT* c) : + mC(&c) {} template @@ -195,12 +215,6 @@ namespace CNORXZ return mC->val(); } - template - const VPosBase* VPosRef::vget() const - { - return this; - } - template const VPosBase* VPosRef::vnext() const { @@ -210,37 +224,50 @@ namespace CNORXZ template Uptr VPosRef::vplus(const VPosBase* a) const { - return std::make_unique>((*mC) + DPosRef(a)); + typedef decltype((*mC) + UPos(a->vval())) OPosT; + return std::make_unique>((*mC) + UPos(a->vval())); } template Uptr VPosRef::vtimes(const VPosBase* a) const { - return std::make_unique>((*mC) * UPos(a->vval())); + typedef decltype((*mC) * UPos(a->vval())) OPosT; + return std::make_unique>((*mC) * UPos(a->vval())); } - + + template + Uptr VPosRef::vexec(const VPosBase* a) const + { + typedef decltype( (*mC)(UPos(a->vval())) ) OPosT; + return std::make_unique>( (*mC)(UPos(a->vval())) ); + } + template Uptr VPosRef::vextend(const DPos& a) const { - return std::make_unique>(*mC,a); + typedef decltype(mC->extend(a)) OPosT; + return std::make_unique>(mC->extend(a)); } template Uptr VPosRef::vextend(const DPosRef& a) const { - return std::make_unique>(*mC,a); + typedef decltype(mC->extend(a)) OPosT; + return std::make_unique>(mC->extend(a)); } template Uptr VPosRef::vextend(const UPos& a) const { - return std::make_unique>(*mC,a); + typedef decltype(mC->extend(a)) OPosT; + return std::make_unique>(mC->extend(a)); } template Uptr VPosRef::vextend(const FPos& a) const { - return std::make_unique>(*mC,a); + typedef decltype(mC->extend(a)) OPosT; + return std::make_unique>(mC->extend(a)); } /********************* @@ -248,14 +275,14 @@ namespace CNORXZ *********************/ template - VPosRef>::VPosRef(const CPosInterface>* c) : - mC(c), mFRef(&c->get()), mNRef(&c->next()) + VPosRef>::VPosRef(const MPos* c) : + mC(c), mNRef(&c->next()) {} template Uptr VPosRef>::copy() const { - return std::make_unique>>(*mFRef,*mNRef); + return std::make_unique>>(*mC); } template @@ -270,12 +297,6 @@ namespace CNORXZ return mC->val(); } - template - const VPosBase* VPosRef>::vget() const - { - return mFRef; - } - template const VPosBase* VPosRef>::vnext() const { @@ -285,37 +306,50 @@ namespace CNORXZ template Uptr VPosRef>::vplus(const VPosBase* a) const { - return std::make_unique>((*mC) + DPosRef(a)); + typedef decltype((*mC) + DPosRef(a)) OPosT; + return std::make_unique>((*mC) + DPosRef(a)); } template Uptr VPosRef>::vtimes(const VPosBase* a) const { - return std::make_unique>((*mC) * UPos(a->vval())); + typedef decltype((*mC) * UPos(a->vval())) OPosT; + return std::make_unique>((*mC) * UPos(a->vval())); + } + + template + Uptr VPosRef>::vexec(const VPosBase* a) const + { + typedef decltype( (*mC)(UPos(a->vval())) ) OPosT; + return std::make_unique>( (*mC)(UPos(a->vval())) ); } template Uptr VPosRef>::vextend(const DPos& a) const { - return std::make_unique>(*mC,a); + typedef decltype(mC->extend(a)) OPosT; + return std::make_unique>(mC->extend(a)); } template Uptr VPosRef>::vextend(const DPosRef& a) const { - return std::make_unique>(*mC,a); + typedef decltype(mC->extend(a)) OPosT; + return std::make_unique>(mC->extend(a)); } template Uptr VPosRef>::vextend(const UPos& a) const { - return std::make_unique>(*mC,a); + typedef decltype(mC->extend(a)) OPosT; + return std::make_unique>(mC->extend(a)); } template Uptr VPosRef>::vextend(const FPos& a) const { - return std::make_unique>(*mC,a); + typedef decltype(mC->extend(a)) OPosT; + return std::make_unique>(mC->extend(a)); } } diff --git a/src/include/ranges/xfor/vpos_type.h b/src/include/ranges/xfor/vpos_type.h index 658ac93..1fb7069 100644 --- a/src/include/ranges/xfor/vpos_type.h +++ b/src/include/ranges/xfor/vpos_type.h @@ -14,10 +14,10 @@ namespace CNORXZ virtual Uptr copy() const = 0; virtual SizeT vsize() const = 0; virtual SizeT vval() const = 0; - virtual const VPosBase* vget() const = 0; 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; @@ -25,6 +25,9 @@ namespace CNORXZ template Uptr vextend(const SPos& a) const; + + template + Uptr vextend(const SFPos& a) const; }; template @@ -33,15 +36,15 @@ namespace CNORXZ public: DEFAULT_MEMBERS(VPos); - VPos(const CPosInterface& a); + VPos(const PosT& a); virtual Uptr copy() const override final; virtual SizeT vsize() const override final; virtual SizeT vval() const override final; - virtual const VPosBase* vget() 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; @@ -52,21 +55,19 @@ namespace CNORXZ class VPos> : public VPosBase, public MPos { private: - VPosRef mFRef; VPosRef mNRef; + public: - typedef MPos MPosT; - DEFAULT_MEMBERS(VPos); - VPos(const CPosInterface>& a); + VPos(const MPos& a); virtual Uptr copy() const override final; virtual SizeT vsize() const override final; virtual SizeT vval() const override final; - virtual const VPosBase* vget() 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; @@ -81,15 +82,15 @@ namespace CNORXZ public: DEFAULT_MEMBERS(VPosRef); - VPosRef(const CPosInterface* c); + VPosRef(const PosT* c); virtual Uptr copy() const override final; virtual SizeT vsize() const override final; virtual SizeT vval() const override final; - virtual const VPosBase* vget() 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; @@ -102,21 +103,19 @@ namespace CNORXZ { private: const MPos* mC = nullptr; - VPosRef mFRef; VPosRef mNRef; - public: - typedef MPos MPosT; + public: DEFAULT_MEMBERS(VPosRef); - VPosRef(const CPosInterface>* c); + VPosRef(const MPos* c); virtual Uptr copy() const override final; virtual SizeT vsize() const override final; virtual SizeT vval() const override final; - virtual const VPosBase* vget() 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;