From f41ada859a58df712cfef68fa3f9de0844b37c0a Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Mon, 12 Dec 2022 02:15:42 +0100 Subject: [PATCH] im com --- src/include/array/array_base.cc.h | 59 +++++++++++++++++++++- src/include/array/array_base.h | 37 ++++++++++++++ src/include/base/types.h | 3 ++ src/include/ranges/dindex.h | 1 + src/include/ranges/index_utils.cc.h | 76 +++++++++++++++++++++++++++++ src/include/ranges/index_utils.h | 18 +++++++ src/include/ranges/xindex.cc.h | 25 ++++++++++ src/include/ranges/xindex.h | 7 ++- src/lib/ranges/dindex.cc | 5 ++ 9 files changed, 228 insertions(+), 3 deletions(-) create mode 100644 src/include/ranges/index_utils.cc.h create mode 100644 src/include/ranges/index_utils.h diff --git a/src/include/array/array_base.cc.h b/src/include/array/array_base.cc.h index 48cc44d..719da84 100644 --- a/src/include/array/array_base.cc.h +++ b/src/include/array/array_base.cc.h @@ -28,8 +28,10 @@ namespace CNORXZ const T& CArrayBase::at(const IndexInterface& i) const { CXZ_ASSERT(i.lex() < this->size(), "index out of range"); + auto beg = this->begin(); + //assertCompatible(i,beg); // check further compatibility of index/range format!!! - auto ai = this->begin() + i.lex(); + auto ai = beg + i.lex(); return *ai; } @@ -37,7 +39,13 @@ namespace CNORXZ template Sptr> CArrayBase::sl(const IndexInterface& i) const { - + ////auto r = mkSliceRange(i.range()); + //auto beg = this->begin(); + //assertCompatible(i,beg); + ////auto bs = mkSliceBlockSizes(i, beg); + //auto it = beg + i.lex(); + ////return std::make_shared(r, this, bs, it.pos()); + return std::make_shared(); } template @@ -73,6 +81,42 @@ namespace CNORXZ } */ + /****************************** + * CArrayBase (protected) * + ******************************/ + + template + template + inline Vector CArrayBase::mkSliceBlockSize(const IPack1& ip1, const IPack2& ip2) const + { + const SizeT ip1dim = indexPackDim(ip1); + const SizeT ip2dim = indexPackDim(ip2); + if(ip1dim > ip2dim){ + //const SizeT ip1sdim = indexPackSDim(ip1); + const SizeT ip2sdim = indexPackSDim(ip2); + CXZ_ASSERT(ip1dim == ip2sdim or ip2sdim == ip2dim, + "") + } + else if(ip1dim < ip2dim){ + + } + else { + + } + } + + template + inline RangePtr CArrayBase::mkSliceRange(const RangePtr& r) const + { + + } + + template + inline void CArrayBase::assertCompatible() const + { + + } + /***************** * ArrayBase * *****************/ @@ -100,6 +144,17 @@ namespace CNORXZ return *ai; } + template + template + Sptr> ArrayBase::sl(const IndexInterface& i) + { + auto r = mkSliceRange(i); + auto beg = this->begin(); + auto bs = mkSliceBlockSizes(i, beg); + auto it = beg + i.lex(); + return std::make_shared(r, this, bs, it.pos()); + } + template typename ArrayBase::iterator ArrayBase::begin() { diff --git a/src/include/array/array_base.h b/src/include/array/array_base.h index 7a4a9c2..8fc1d14 100644 --- a/src/include/array/array_base.h +++ b/src/include/array/array_base.h @@ -22,6 +22,13 @@ namespace CNORXZ protected: RangePtr mRange; + template + inline Vector mkSliceBlockSize(const I1& i, const I2& beg) const; + + inline RangePtr mkSliceRange(const RangePtr& r) const; + + inline void assertCompatible() const; + public: CArrayBase(const RangePtr& range); @@ -89,7 +96,37 @@ namespace CNORXZ //template //OperationRoot operator()(const IndexPtr& i); + }; + + // to extra header file !!!: + template + constexpr decltype(auto) flattenIndexPack(const Tuple...>& ipack) + { + constexpr SizeT D = sizeof...(Indices); + + } + + inline Vector flattenIndexPack(const Vector& ipack) + { + + } + + template + inline SizeT indexPackDim(Tuple...> ipack) + { + constexpr SizeT D = sizeof...(Indices); + return iter<0,D>([&](const auto& i) { return std::get(ipack)->dim(); }, + [](const auto&... e) { return (e + ...); }); + } + + inline SizeT indexPackDim(const Vector& ipack) + { + return std::accumulate(ipack.begin(), ipack.end(), ipack[0]->dim(), + [](auto a, auto b) { return a->dim() + b->dim(); }); + } + + } #endif diff --git a/src/include/base/types.h b/src/include/base/types.h index 616a652..2f004dd 100644 --- a/src/include/base/types.h +++ b/src/include/base/types.h @@ -40,6 +40,9 @@ namespace CNORXZ template using Uptr = std::unique_ptr; + template + using Pair = std::pair; + template using Tuple = std::tuple; diff --git a/src/include/ranges/dindex.h b/src/include/ranges/dindex.h index 05a1273..0e26f74 100644 --- a/src/include/ranges/dindex.h +++ b/src/include/ranges/dindex.h @@ -46,6 +46,7 @@ namespace CNORXZ RangePtr range() const; UPos stepSize(const IndexId<0>& id) const; + Vector pack() const; String stringMeta() const; DType meta() const; diff --git a/src/include/ranges/index_utils.cc.h b/src/include/ranges/index_utils.cc.h new file mode 100644 index 0000000..8d1f9c7 --- /dev/null +++ b/src/include/ranges/index_utils.cc.h @@ -0,0 +1,76 @@ + +#ifndef __cxz_index_utils_cc_h__ +#define __cxz_index_utils_cc_h__ + +#include "index_utils.h" + +namespace CNORZX +{ + inline decltype(auto) getPack(const XIndexPtr& i) + { + typedef Vector OutT; + OutT p = i.pack(); + while(p.size() == 1u){ + p = p[0]->pack(); + } + return p; + } + + // -> base + template + Vector mkVector(const T&... t) { return Vector({t...}); } + + template + inline decltype(auto) indexZip(const XIndexPtr& a, const XIndexPtr& b, F&& f) + { + const auto ap = getPack(a); + const auto bp = getPack(b); + if(ap.size() == 1u or bp.size() == 1u){ + return mkVector( f(a, b) ); + } + else { + return indexPackZip(ap, bp, std::forward(f)); + } + } + + template + inline decltype(auto) indexPackZip(const Vector& a, const Vector& b, F&& f) + { + if(a.size() > b.size()) { + Vector bn; + for(const auto& x: b){ + auto p = getPack(x); + if(p.size() == 0){ + bn.push_back(x); + } + else { + bn.insert(bn.end(), p.begin(), p.end()); + } + } + return indexPackZip(a, bn, std::forward(f)); + } + else if(a.size() < b.size()) { + Vector an; + for(const auto& x: a){ + auto p = getPack(x); + if(p.size() == 0){ + an.push_back(x); + } + else { + an.insert(bn.end(), p.begin(), p.end()); + } + } + return indexPackZip(an, b, std::forward(f)); + } + else { + typedef decltype(indexZip(a[0], b[0], std::forward(f))) OutT; + OutT o(a.size()); + std::transform(o.begin(), o.end(), a.begin(), b.begin(), + [](const auto& ax, const auto& bx) { F fc = f; return indexZip(ax, bx, std::move(fc)); }); + return o; + } + } + +} + +#endif diff --git a/src/include/ranges/index_utils.h b/src/include/ranges/index_utils.h new file mode 100644 index 0000000..73ec4e2 --- /dev/null +++ b/src/include/ranges/index_utils.h @@ -0,0 +1,18 @@ + +#ifndef __cxz_index_utils_h__ +#define __cxz_index_utils_h__ + +#include "base/base.h" + +namespace CNORZX +{ + inline decltype(auto) getPack(const XIndexPtr& i); + + template + inline decltype(auto) indexZip(const XIndexPtr& a, const XIndexPtr& b, F&& f); + + template + inline decltype(auto) indexPackZip(const Vector& a, const Vector& b, F&& f); +} + +#endif diff --git a/src/include/ranges/xindex.cc.h b/src/include/ranges/xindex.cc.h index 4b7e748..0302fa0 100644 --- a/src/include/ranges/xindex.cc.h +++ b/src/include/ranges/xindex.cc.h @@ -126,6 +126,24 @@ namespace CNORXZ { return mI->stepSize(id); } + + template + Vector XIndex::pack() const + { + constexpr SizeT D = index_dim::value; + // replace by traits: has_sub, has_static_sub (-> to be implemented!!!) + if constexpr(D > 1){ + return iter<0,D> + ( [&](auto i) { return mkXIndex(std::get(mI->pack())); }, + [](const auto&... e) { return { e ... }; } ); + } + else if constexpr(std::is_same::value){ + return mI->pack(); + } + else { + return {}; + } + } template String XIndex::stringMeta() const @@ -154,6 +172,13 @@ namespace CNORXZ return DXpr(mI->ifor(xpr, std::forward>(f))); } + template + XIndexPtr mkXIndex(const Sptr& i) + { + typedef typename Index::MetaType Meta; + return std::make_shared> + (std::dynamic_pointer_cast>(i)); + } } #endif diff --git a/src/include/ranges/xindex.h b/src/include/ranges/xindex.h index ce1d331..0c32577 100644 --- a/src/include/ranges/xindex.h +++ b/src/include/ranges/xindex.h @@ -36,7 +36,8 @@ namespace CNORXZ virtual SizeT dim() const = 0; virtual RangePtr range() const = 0; virtual UPos stepSize(const IndexId<0>& id) const = 0; - + virtual Vector pack() const = 0; + virtual String stringMeta() const = 0; virtual DType meta() const = 0; virtual XIndexBase& at(const DType& meta) = 0; @@ -84,6 +85,7 @@ namespace CNORXZ virtual SizeT dim() const override final; virtual RangePtr range() const override final; virtual UPos stepSize(const IndexId<0>& id) const override final; + virtual Vector pack() const override final; virtual String stringMeta() const override final; virtual DType meta() const override final; @@ -97,6 +99,9 @@ namespace CNORXZ }; + template + XIndexPtr mkXIndex(const Sptr& i); + } #endif diff --git a/src/lib/ranges/dindex.cc b/src/lib/ranges/dindex.cc index a974454..9e50e93 100644 --- a/src/lib/ranges/dindex.cc +++ b/src/lib/ranges/dindex.cc @@ -129,6 +129,11 @@ namespace CNORXZ return mI->stepSize(id); } + Vector DIndex::pack() const + { + return mI->pack(); + } + String DIndex::stringMeta() const { return mI->stringMeta();