index format + slice

This commit is contained in:
Christian Zimmermann 2022-12-28 21:21:11 +01:00
parent 16f062b069
commit b86ea957ba
13 changed files with 185 additions and 20 deletions

View file

@ -118,7 +118,7 @@ namespace CNORXZ
if(npack.size() == 0){ if(npack.size() == 0){
return nullptr; return nullptr;
} }
return mkXIndex(yindexPtr(npack)); return xindexPtr(yindexPtr(npack));
} }
/****************** /******************

View file

@ -23,6 +23,33 @@ namespace CNORXZ
{ {
return iteri(g, f, Isqr<B,E>{}); return iteri(g, f, Isqr<B,E>{});
} }
template <SizeT E, SizeT I, class G, class F, class C, typename... Args>
constexpr decltype(auto) iterIfi(const G& g, const F& f, const C& c, const Args&... args)
{
if constexpr(I >= E){
if constexpr(std::is_same<F,NoF>::value){
return;
}
else {
return f(args...);
}
}
else {
if constexpr(c(std::integral_constant<SizeT,I>{})){
return iterIfi<E,I+1>(g, f, c, args..., g(std::integral_constant<SizeT,I>{}));
}
else {
return iterIfi<E,I+1>(g, f, c, args...);
}
}
}
template <SizeT B, SizeT E, class G, class F, class C>
constexpr decltype(auto) iterIf(const G& g, const F& f, const C& c)
{
return iterIfi<E,B>(g, f, c);
}
} }
#endif #endif

View file

@ -12,6 +12,12 @@ namespace CNORXZ
template <SizeT B, SizeT E, class G, class F> template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) iter(const G& g, const F& f); constexpr decltype(auto) iter(const G& g, const F& f);
template <SizeT E, SizeT I, class G, class F, class C, typename... Args>
constexpr decltype(auto) iterIfi(const G& g, const F& f, const C& c, const Args&... args);
template <SizeT B, SizeT E, class G, class F, class C>
constexpr decltype(auto) iterIf(const G& g, const F& f, const C& c);
} }
#endif #endif

View file

@ -54,6 +54,9 @@ namespace CNORXZ
DType meta() const; DType meta() const;
DIndex& at(const DType& meta); DIndex& at(const DType& meta);
Sptr<DIndex> format(const Sptr<DIndex>& ind) const;
Sptr<DIndex> slice(const Sptr<DIndex>& ind) const;
DXpr<SizeT> ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const; DXpr<SizeT> ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const;
const XIndexPtr& xptr() const; const XIndexPtr& xptr() const;
@ -62,6 +65,9 @@ namespace CNORXZ
XIndexPtr mI; XIndexPtr mI;
}; };
template <>
struct has_sub<DIndex>
{ static constexpr bool value = true; };
} }
#endif #endif

View file

@ -414,29 +414,24 @@ namespace CNORXZ
static_assert(has_sub<Index>::value, "try to format single index"); static_assert(has_sub<Index>::value, "try to format single index");
if constexpr(has_static_sub<Index>::value){ if constexpr(has_static_sub<Index>::value){
static_assert(index_dim<Index>::value == NI, "got index with conflicting static dimension"); static_assert(index_dim<Index>::value == NI, "got index with conflicting static dimension");
typedef std::remove_reference<decltype(ind->blockSizes())>::type BT;
return iter<0,NI> return iter<0,NI>
( [&](auto i) { ( [&](auto i) {
std::get<i>(mIPack)->format(std::get<i>(ind->pack())); return std::get<i>(mIPack)->format(std::get<i>(ind->pack()));
}, },
[](const auto&... e) { [&](const auto&... e) {
return std::make_shared<GMIndex<BT,Indices...>> return gmindexPtr(mBlockSizes, e... );
(mBlockSizes, e... );
} ); } );
} }
else { else {
typedef Arr<UPos,NI> BT;
auto pack = ind->pack(); auto pack = ind->pack();
CXZ_ASSERT(pack.size() == NI, "attempt to format index of dimension " << NI CXZ_ASSERT(pack.size() == NI, "attempt to format index of dimension " << NI
<< " using index of dimension " << bs.size()); << " using index of dimension " << pack.size());
auto bs = ind->blockSizes();
return iter<0,NI> return iter<0,NI>
( [&](auto i) { ( [&](auto i) {
return std::get<i>(mIPack)->format(pack[i]); return std::get<i>(mIPack)->format(pack[i]);
}, },
[](const auto&... e) { [&](const auto&... e) {
return std::make_shared<GMIndex<BlockType,Indices...>> return gmindexPtr(mBlockSizes, e... );
(mBlockSizes, e... );
} ); } );
} }
} }
@ -446,6 +441,46 @@ namespace CNORXZ
decltype(auto) GMIndex<BlockType,Indices...>::slice(const Sptr<Index>& ind) const decltype(auto) GMIndex<BlockType,Indices...>::slice(const Sptr<Index>& ind) const
{ {
static_assert(is_index<Index>::value, "got non-index type"); static_assert(is_index<Index>::value, "got non-index type");
static_assert(has_sub<Index>::value, "try to slice single index");
if constexpr(has_static_sub<Index>::value and
((has_static_sub<Indices>::value and ...) or
(not has_sub<Indices>::value and ...)) ){
static_assert(index_dim<Index>::value == NI, "got index with conflicting static dimension");
const auto bs = iterIf<0,NI>
( [&](auto i) { return std::get<i>(mBlockSizes); },
[](const auto&... e) { std::make_tuple(e...); },
[](auto i) {
return std::is_same<typename TupleElem<i>::type,NIndex>::value;
});
return iterIf<0,NI>
( [&](auto i) { return std::get<i>(mIPack)->slice(std::get<i>(ind->pack())); },
[&](const auto&... e) { return gmindex(bs, e... ); },
[](auto i) {
return std::is_same<typename TupleElem<i>::type,NIndex>::value;
} );
}
else {
Vector<SizeT> bs;
Vector<XIndexPtr> ivec;
bs.reserve(NI);
ivec.reserve(NI);
auto pack = ind->pack();
CXZ_ASSERT(pack.size() == NI, "attempt to slice index of dimension " << NI
<< " using index of dimension " << pack.size());
iter<0,NI>
( [&](auto i) {
if(std::get<i>(mIPack)->dim() == 0){
bs.push_back(std::get<i>(mBlockSizes).val());
if constexpr(has_static_sub<Index>::value){
ivec.push_back( xindexPtr( std::get<i>(ind->pack()) ) );
}
else {
ivec.push_back( xindexPtr( ind->pack()[i] ) );
}
}
}, NoF {});
return yindexPtr(bs, ivec);
}
} }
template <class BlockType, class... Indices> template <class BlockType, class... Indices>
@ -523,6 +558,11 @@ namespace CNORXZ
return MIndex<Indices...>(is...); return MIndex<Indices...>(is...);
} }
template <class BlockType, class... Indices>
constexpr decltype(auto) gmindexPtr(const BlockType& bs, const Sptr<Indices>&... is)
{
return std::make_shared<GMIndex<BlockType,Indices...>>(bs, is...);
}
/********************* /*********************
* MRangeFactory * * MRangeFactory *

View file

@ -72,7 +72,7 @@ namespace CNORXZ
template <class Index> template <class Index>
decltype(auto) slice(const Sptr<Index>& ind) const; decltype(auto) slice(const Sptr<Index>& ind) const;
// -> IndexInterface; // -> IndexInterface;
// drop index instance or drop blockSize if that if ind is Null // drop index instance or drop blockSize of ind if that of *this is not Null
// return result as new instance // return result as new instance
template <class Xpr, class F> template <class Xpr, class F>
@ -152,6 +152,8 @@ namespace CNORXZ
template <class... Indices> template <class... Indices>
constexpr decltype(auto) mindex(const Sptr<Indices>&... is); constexpr decltype(auto) mindex(const Sptr<Indices>&... is);
template <class BlockType, class... Indices>
constexpr decltype(auto) gmindexPtr(const BlockType& bs, const Sptr<Indices>&... is);
template <class... Ranges> template <class... Ranges>
class MRangeFactory : public RangeFactoryBase class MRangeFactory : public RangeFactoryBase

View file

@ -137,6 +137,25 @@ namespace CNORXZ
return UPos(id == this->id() ? 1 : 0); return UPos(id == this->id() ? 1 : 0);
} }
template <typename MetaType>
template <class Index>
decltype(auto) UIndex<MetaType>::format(const Sptr<Index>& ind) const
{
return ind;
}
template <typename MetaType>
template <class Index>
decltype(auto) UIndex<MetaType>::slice(const Sptr<Index>& ind) const
{
if(ind != nullptr){
if(ind->dim() != 0) {
return Sptr<CIndex>();
}
}
return std::make_shared<CIndex>(*this);
}
template <typename MetaType> template <typename MetaType>
template <class Xpr, class F> template <class Xpr, class F>
decltype(auto) UIndex<MetaType>::ifor(const Xpr& xpr, F&& f) const decltype(auto) UIndex<MetaType>::ifor(const Xpr& xpr, F&& f) const

View file

@ -47,6 +47,12 @@ namespace CNORXZ
const MetaT& meta() const; const MetaT& meta() const;
UIndex& at(const MetaT& metaPos); UIndex& at(const MetaT& metaPos);
template <class Index>
decltype(auto) format(const Sptr<Index>& ind) const;
template <class Index>
decltype(auto) slice(const Sptr<Index>& ind) const;
template <class Xpr, class F> template <class Xpr, class F>
decltype(auto) ifor(const Xpr& xpr, F&& f) const; decltype(auto) ifor(const Xpr& xpr, F&& f) const;

View file

@ -133,7 +133,7 @@ namespace CNORXZ
if constexpr(has_static_sub<Index>::value){ if constexpr(has_static_sub<Index>::value){
constexpr SizeT D = index_dim<Index>::value; constexpr SizeT D = index_dim<Index>::value;
return iter<0,D> return iter<0,D>
( [&](auto i) { return mkXIndex(std::get<i>(mI->THIS().pack())); }, ( [&](auto i) { return xindexPtr(std::get<i>(mI->THIS().pack())); },
[](const auto&... e) { return Vector<XIndexPtr>({ e ... }); } ); [](const auto&... e) { return Vector<XIndexPtr>({ e ... }); } );
} }
else if constexpr(has_sub<Index>::value){ else if constexpr(has_sub<Index>::value){
@ -177,7 +177,7 @@ namespace CNORXZ
return nullptr; return nullptr;
} }
else { else {
return mkXIndex(replaceBlockSizes(arr, std::static_pointer_cast<Index>(mI))); return xindexPtr(replaceBlockSizes(arr, std::static_pointer_cast<Index>(mI)));
} }
} }
else if constexpr(has_sub<Index>::value) { else if constexpr(has_sub<Index>::value) {
@ -209,6 +209,22 @@ namespace CNORXZ
return *this; return *this;
} }
template <class Index, typename Meta>
Sptr<DIndex> XIndex<Index,Meta>::format(const Sptr<DIndex>& ind) const
{
CXZ_ERROR("IMPLEMENT!!!");
return nullptr;
//return std::make_shared<DIndex>(xindexPtr(mI->format(ind)));
}
template <class Index, typename Meta>
Sptr<DIndex> XIndex<Index,Meta>::slice(const Sptr<DIndex>& ind) const
{
CXZ_ERROR("IMPLEMENT!!!");
return nullptr;
//return std::make_shared<DIndex>(xindexPtr(mI->slice(ind)));
}
template <class Index, typename Meta> template <class Index, typename Meta>
DXpr<SizeT> XIndex<Index,Meta>::ifor(const DXpr<SizeT>& xpr, DXpr<SizeT> XIndex<Index,Meta>::ifor(const DXpr<SizeT>& xpr,
std::function<SizeT(SizeT,SizeT)>&& f) const std::function<SizeT(SizeT,SizeT)>&& f) const
@ -217,12 +233,18 @@ namespace CNORXZ
} }
template <class Index> template <class Index>
XIndexPtr mkXIndex(const Sptr<Index>& i) inline XIndexPtr xindexPtr(const Sptr<Index>& i)
{ {
typedef typename Index::MetaType Meta; typedef typename Index::MetaType Meta;
return std::make_shared<XIndex<Index,Meta>> return std::make_shared<XIndex<Index,Meta>>
(std::dynamic_pointer_cast<IndexInterface<Index,Meta>>(i)); (std::dynamic_pointer_cast<IndexInterface<Index,Meta>>(i));
} }
template <>
inline XIndexPtr xindexPtr<XIndexBase>(const Sptr<XIndexBase>& i)
{
return i;
}
} }
#endif #endif

View file

@ -9,10 +9,12 @@
namespace CNORXZ namespace CNORXZ
{ {
// Future IndexWrapper
class XIndexBase class XIndexBase
{ {
public: public:
//typedef DType MetaType;
DEFAULT_MEMBERS(XIndexBase); DEFAULT_MEMBERS(XIndexBase);
virtual ~XIndexBase() = default; virtual ~XIndexBase() = default;
virtual XIndexPtr copy() const = 0; virtual XIndexPtr copy() const = 0;
@ -44,6 +46,9 @@ namespace CNORXZ
virtual DType meta() const = 0; virtual DType meta() const = 0;
virtual XIndexBase& at(const DType& meta) = 0; virtual XIndexBase& at(const DType& meta) = 0;
virtual Sptr<DIndex> format(const Sptr<DIndex>& ind) const = 0;
virtual Sptr<DIndex> slice(const Sptr<DIndex>& ind) const = 0;
virtual DXpr<SizeT> ifor(const DXpr<SizeT>& xpr, virtual DXpr<SizeT> ifor(const DXpr<SizeT>& xpr,
std::function<SizeT(SizeT,SizeT)>&& f) const = 0; std::function<SizeT(SizeT,SizeT)>&& f) const = 0;
@ -96,6 +101,9 @@ namespace CNORXZ
virtual DType meta() const override final; virtual DType meta() const override final;
virtual XIndexBase& at(const DType& meta) override final; virtual XIndexBase& at(const DType& meta) override final;
virtual Sptr<DIndex> format(const Sptr<DIndex>& ind) const override final;
virtual Sptr<DIndex> slice(const Sptr<DIndex>& ind) const override final;
virtual DXpr<SizeT> ifor(const DXpr<SizeT>& xpr, virtual DXpr<SizeT> ifor(const DXpr<SizeT>& xpr,
std::function<SizeT(SizeT,SizeT)>&& f) const override final; std::function<SizeT(SizeT,SizeT)>&& f) const override final;
@ -109,7 +117,10 @@ namespace CNORXZ
{ static constexpr bool value = true; }; { static constexpr bool value = true; };
template <class Index> template <class Index>
XIndexPtr mkXIndex(const Sptr<Index>& i); inline XIndexPtr xindexPtr(const Sptr<Index>& i);
template <>
inline XIndexPtr xindexPtr<XIndexBase>(const Sptr<XIndexBase>& i);
} }

View file

@ -52,6 +52,9 @@ namespace CNORXZ
DType meta() const; DType meta() const;
YIndex& at(const DType& meta); YIndex& at(const DType& meta);
Sptr<DIndex> format(const Sptr<DIndex>& ind) const;
Sptr<DIndex> slice(const Sptr<DIndex>& ind) const;
DXpr<SizeT> ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const; DXpr<SizeT> ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const;
YIndex& operator()(const Sptr<YIndex>& i); YIndex& operator()(const Sptr<YIndex>& i);
@ -87,6 +90,7 @@ namespace CNORXZ
YIndex yindex(const Vector<XIndexPtr>& is); YIndex yindex(const Vector<XIndexPtr>& is);
Sptr<YIndex> yindexPtr(const Vector<XIndexPtr>& is); Sptr<YIndex> yindexPtr(const Vector<XIndexPtr>& is);
Sptr<YIndex> yindexPtr(const Vector<SizeT>& bs, const Vector<XIndexPtr>& is);
class YRangeFactory : public RangeFactoryBase class YRangeFactory : public RangeFactoryBase
{ {

View file

@ -162,6 +162,16 @@ namespace CNORXZ
return *this; return *this;
} }
Sptr<DIndex> DIndex::format(const Sptr<DIndex>& ind) const
{
return mI->format(ind);
}
Sptr<DIndex> DIndex::slice(const Sptr<DIndex>& ind) const
{
return mI->slice(ind);
}
DXpr<SizeT> DIndex::ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const DXpr<SizeT> DIndex::ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const
{ {
return DXpr<SizeT>(mI->ifor(xpr, std::forward<std::function<SizeT(SizeT,SizeT)>>(f)) ); return DXpr<SizeT>(mI->ifor(xpr, std::forward<std::function<SizeT(SizeT,SizeT)>>(f)) );

View file

@ -339,6 +339,18 @@ namespace CNORXZ
return *this; return *this;
} }
Sptr<DIndex> YIndex::format(const Sptr<DIndex>& ind) const
{
CXZ_ERROR("IMPLEMENT!!!");
return nullptr;
}
Sptr<DIndex> YIndex::slice(const Sptr<DIndex>& ind) const
{
CXZ_ERROR("IMPLEMENT!!!");
return nullptr;
}
DXpr<SizeT> YIndex::ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const DXpr<SizeT> YIndex::ifor(const DXpr<SizeT>& xpr, std::function<SizeT(SizeT,SizeT)>&& f) const
{ {
return mkIFor(0, xpr, std::forward<std::function<SizeT(SizeT,SizeT)>>(f)); return mkIFor(0, xpr, std::forward<std::function<SizeT(SizeT,SizeT)>>(f));