From 129c27a28d53b8203099436ba71b1cbd2096ceeb Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sat, 2 Dec 2023 18:02:34 +0100 Subject: [PATCH] index: remove xpr() from base type + add trait to check if expression is available --- TODO | 6 +- src/include/ranges/crange.h | 10 ++++ src/include/ranges/dindex.h | 3 - src/include/ranges/eindex.h | 10 ++++ src/include/ranges/index_base.cc.h | 13 +++++ src/include/ranges/index_base.h | 38 ++---------- src/include/ranges/index_traits.h | 92 ++++++++++++++++++++++++++++++ src/include/ranges/lindex.h | 10 ++++ src/include/ranges/mrange.h | 10 ++++ src/include/ranges/ranges.h | 1 + src/include/ranges/srange.h | 9 +++ src/include/ranges/urange.h | 10 ++++ src/include/ranges/xindex.cc.h | 9 --- src/include/ranges/xindex.h | 2 - src/include/ranges/yrange.h | 1 - src/lib/ranges/dindex.cc | 5 -- src/lib/ranges/yrange.cc | 6 -- 17 files changed, 170 insertions(+), 65 deletions(-) create mode 100644 src/include/ranges/index_traits.h diff --git a/TODO b/TODO index 0d70161..f76be28 100644 --- a/TODO +++ b/TODO @@ -10,14 +10,10 @@ include/ranges/index_base.cc.h@110: "if this assert never applies, remove mPtrId include/ranges/mrange.cc.h@633: "TODO: ZRange (meta and index pos static!)" [long] include/ranges/urange.cc.h@366: "else general transform using DType (better than nothing), to be implemented" [urgent] include/ranges/urange.cc.h@430: "else general transform using DType (better than nothing), to be implemented" [urgent] -include/ranges/prange.cc.h@193: "implement" [urgent] -include/ranges/prange.cc.h@325: "implement" [urgent] -include/ranges/xindex.cc.h@183: "IMPLEMENT" [urgent] include/xpr/for.cc.h@314: "check for write access" [check] include/ranges/srange.cc.h@292: "TODO: check for selected static sizes of SRange -> return SRange" [long] lib/ranges/crange.cc@114: "preliminary solution (TODO: implement xpr that simply returns PosT value)" [long] -lib/ranges/yrange.cc@348: "IMPLEMENT" [urgent] opt/hdf5/lib/h5_group.cc@166: "IMPLEMENT" [urgent] opt/hdf5/include/h5_group.cc.h@34: "not implemented" [urgent] -opt/hdf5/lib/h5_table.cc@127: "not implemented" [urgent] \ No newline at end of file +opt/hdf5/lib/h5_table.cc@127: "not implemented" [urgent] diff --git a/src/include/ranges/crange.h b/src/include/ranges/crange.h index 07f6352..e6934d6 100644 --- a/src/include/ranges/crange.h +++ b/src/include/ranges/crange.h @@ -215,6 +215,16 @@ namespace CNORXZ /** cast the range */ static Sptr func(const RangePtr& r); }; + + /** *** + CIndex can be used as expression + @see index_expression_exists + */ + template <> + struct index_expression_exists + { + static constexpr bool value = true; + }; } #endif diff --git a/src/include/ranges/dindex.h b/src/include/ranges/dindex.h index 95906bc..bdfd741 100644 --- a/src/include/ranges/dindex.h +++ b/src/include/ranges/dindex.h @@ -62,12 +62,9 @@ namespace CNORXZ String stringMeta() const; DType meta() const; DIndex& at(const DType& meta); - DXpr xpr(const Sptr& _this) const; RangePtr prange(const DIndex& end) const; Vector deepFormat() const; - //DXpr ifor(const DXpr& xpr, NoF&& f) const; - template decltype(auto) ifor(const Xpr& xpr, F&& f) const; diff --git a/src/include/ranges/eindex.h b/src/include/ranges/eindex.h index 0d8b368..f7c0bc7 100644 --- a/src/include/ranges/eindex.h +++ b/src/include/ranges/eindex.h @@ -43,6 +43,16 @@ namespace CNORXZ { static constexpr bool value = true; }; + + /** *** + EIndex can be used as expression + @see index_expression_exists + */ + template + struct index_expression_exists> + { + static constexpr bool value = true; + }; template decltype(auto) operator*(const Sptr>& a, const Sptr& b); diff --git a/src/include/ranges/index_base.cc.h b/src/include/ranges/index_base.cc.h index 38edb69..bc2dc9a 100644 --- a/src/include/ranges/index_base.cc.h +++ b/src/include/ranges/index_base.cc.h @@ -143,6 +143,19 @@ namespace CNORXZ return std::make_shared>( *i - n ); } + template + Sptr moveToPtr(I&& i) + { + return std::make_shared(std::forward(i)); + } + + template + decltype(auto) xpr(const Sptr& i) + { + static_assert(is_index::value, "got non-index type"); + static_assert(index_expression_exists::value, "expression for given index type does not exist"); + return i->xpr(i); + } } #endif diff --git a/src/include/ranges/index_base.h b/src/include/ranges/index_base.h index a246e3f..bcf6189 100644 --- a/src/include/ranges/index_base.h +++ b/src/include/ranges/index_base.h @@ -164,12 +164,6 @@ namespace CNORXZ */ I& at(const MetaType& meta) { return THIS().at(meta); } - /** create expression on this index - - @param _this pointer to this index - */ - decltype(auto) xpr(const Sptr& _this) const { return THIS().xpr(_this); } - /** create partial range starting at this index' position and ending at the position of input index. The end position is included! @param end end index @@ -210,34 +204,7 @@ namespace CNORXZ PtrId mPtrId = 0; }; - template - struct is_index - { static constexpr bool value = std::is_base_of,I>::value; }; - template - struct index_has_const_size - { static constexpr bool value = false; }; - - template - struct index_const_size - { static constexpr SizeT value = 0; }; - - template - struct index_dim - { static constexpr SizeT value = 1; }; - - template - struct has_sub - { static constexpr bool value = false; }; - - template - struct has_static_sub - { static constexpr bool value = false; }; - - template - struct index_is_multi - { static constexpr bool value = false; }; - template IndexPtr& operator++(const IndexPtr& i); @@ -252,7 +219,10 @@ namespace CNORXZ IndexPtr operator-(const IndexPtr& i, Int n); template - Sptr moveToPtr(I&& i) { return std::make_shared(std::forward(i)); } + Sptr moveToPtr(I&& i); + + template + decltype(auto) xpr(const Sptr& i); } #endif diff --git a/src/include/ranges/index_traits.h b/src/include/ranges/index_traits.h new file mode 100644 index 0000000..5f1f944 --- /dev/null +++ b/src/include/ranges/index_traits.h @@ -0,0 +1,92 @@ +// -*- C++ -*- +/** + + @file include/ranges/index_traits.h + @brief index traits + + Copyright (c) 2022 Christian Zimmermann. All rights reserved. + Mail: chizeta@f3l.de + +**/ + +#ifndef __cxz_index_traits__ +#define __cxz_index_traits__ + +namespace CNORXZ +{ + /** ***** + check if given type is index-type + + @tparam I type to be checked + **/ + template + struct is_index + { static constexpr bool value = std::is_base_of,I>::value; }; + + /** ***** + check if given type is index-type with a static range size + + @tparam I type to be checked + **/ + template + struct index_has_const_size + { static constexpr bool value = false; }; + + /** ***** + return static range size of an index (0 if there is no static size) + + @tparam I type to be checked + **/ + template + struct index_const_size + { static constexpr SizeT value = 0; }; + + /** ***** + return the static dimension of an index (1 if index is not statically multi-dimensional) + + @tparam I type to be checked + **/ + template + struct index_dim + { static constexpr SizeT value = 1; }; + + /** ***** + check if given type is index-type with sub-indices + + @tparam I type to be checked + **/ + template + struct has_sub + { static constexpr bool value = false; }; + + /** ***** + check if given type is index-type with static sub-indices + + @tparam I type to be checked + **/ + template + struct has_static_sub + { static constexpr bool value = false; }; + + /** ***** + check if given type is a statically multi-index-type + + @tparam I type to be checked + **/ + template + struct index_is_multi + { static constexpr bool value = false; }; + + /** ***** + check if given type is index-type that can be used in an expression, + i.e. has a member function xpr() + + @tparam I type to be checked + **/ + template + struct index_expression_exists + { static constexpr bool value = false; }; + +} + +#endif diff --git a/src/include/ranges/lindex.h b/src/include/ranges/lindex.h index cf550b7..283d347 100644 --- a/src/include/ranges/lindex.h +++ b/src/include/ranges/lindex.h @@ -49,6 +49,16 @@ namespace CNORXZ static constexpr bool value = is_index::value; }; + /** *** + LIndex can be used as expression if this is case for its base type + @see index_expression_exists + */ + template + struct index_expression_exists> + { + static constexpr bool value = index_expression_exists::value; + }; + template decltype(auto) operator*(const Sptr>& a, const Sptr& b); diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index aa30d7c..5faf4f1 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -234,6 +234,16 @@ namespace CNORXZ { static Sptr> func(const RangePtr& r); }; + + /** *** + MIndex can be used as expression if all its sub-indices can be used as expression + @see index_expression_exists + */ + template + struct index_expression_exists> + { + static constexpr bool value = (index_expression_exists::value and ...); + }; } #endif diff --git a/src/include/ranges/ranges.h b/src/include/ranges/ranges.h index 36f51bc..5cff132 100644 --- a/src/include/ranges/ranges.h +++ b/src/include/ranges/ranges.h @@ -12,6 +12,7 @@ #include "range_base.h" #include "index_base.h" +#include "index_traits.h" #include "mrange.h" #include "xindex.h" #include "yrange.h" diff --git a/src/include/ranges/srange.h b/src/include/ranges/srange.h index 0ece1f7..6325126 100644 --- a/src/include/ranges/srange.h +++ b/src/include/ranges/srange.h @@ -134,6 +134,15 @@ namespace CNORXZ static Sptr> func(const RangePtr& r); }; + /** *** + SIndex can be used as expression + @see index_expression_exists + */ + template + struct index_expression_exists> + { + static constexpr bool value = true; + }; } #endif diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index 7d5dade..aec0577 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -144,6 +144,16 @@ namespace CNORXZ template RangePtr urange(const Vector& space); + + /** *** + UIndex can be used as expression + @see index_expression_exists + */ + template + struct index_expression_exists> + { + static constexpr bool value = true; + }; } #endif diff --git a/src/include/ranges/xindex.cc.h b/src/include/ranges/xindex.cc.h index ec1575f..ca866e4 100644 --- a/src/include/ranges/xindex.cc.h +++ b/src/include/ranges/xindex.cc.h @@ -177,15 +177,6 @@ namespace CNORXZ return *this; } - template - DXpr XIndex::xpr(const XIndexPtr& _this) const - { - CXZ_ERROR("IMPLEMENT!!!"); - auto xthis = std::dynamic_pointer_cast(_this); - //mI->xpr(xthis) - return DXpr( ); - } - template DXpr XIndex::ifor(const DXpr& xpr, NoF&& f) const { diff --git a/src/include/ranges/xindex.h b/src/include/ranges/xindex.h index f16d67f..d7bed28 100644 --- a/src/include/ranges/xindex.h +++ b/src/include/ranges/xindex.h @@ -56,7 +56,6 @@ namespace CNORXZ virtual String stringMeta() const = 0; virtual DType meta() const = 0; virtual XIndexBase& at(const DType& meta) = 0; - virtual DXpr xpr(const XIndexPtr& _this) const = 0; virtual DXpr ifor(const DXpr& xpr, NoF&& f) const = 0; @@ -109,7 +108,6 @@ namespace CNORXZ virtual String stringMeta() const override final; virtual DType meta() const override final; virtual XIndexBase& at(const DType& meta) override final; - virtual DXpr xpr(const XIndexPtr& _this) const override final; virtual DXpr ifor(const DXpr& xpr, NoF&& f) const override final; diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index ee2987f..49dd8b6 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -67,7 +67,6 @@ namespace CNORXZ String stringMeta() const; Vector meta() const; YIndex& at(const Vector& meta); - DXpr xpr(const Sptr& _this) const; DXpr ifor(const DXpr& xpr, NoF&& f) const; diff --git a/src/lib/ranges/dindex.cc b/src/lib/ranges/dindex.cc index 534b2ec..5e16be6 100644 --- a/src/lib/ranges/dindex.cc +++ b/src/lib/ranges/dindex.cc @@ -167,11 +167,6 @@ namespace CNORXZ return *this; } - DXpr DIndex::xpr(const Sptr& _this) const - { - return mI->xpr(_this->xptr()); - } - RangePtr DIndex::prange(const DIndex& end) const { return mI->prange( end.xptr() ); diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index 147857f..16864f2 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -343,12 +343,6 @@ namespace CNORXZ return *this; } - DXpr YIndex::xpr(const Sptr& _this) const - { - CXZ_ERROR("IMPLEMENT!!!" << _this->lmax().val()); - return DXpr(); - } - DXpr YIndex::ifor(const DXpr& xpr, NoF&& f) const { return mkIFor(0, xpr, std::forward(f));