index reformat: apply format change to same instance (no copy)
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Christian Zimmermann 2023-12-25 22:07:39 +01:00
parent 62b75c01a0
commit bf83c26a12
19 changed files with 75 additions and 82 deletions

View file

@ -111,7 +111,7 @@ namespace CNORXZ
SizeT deepMax() const; SizeT deepMax() const;
/** @copydoc IndexInterface::reformat() */ /** @copydoc IndexInterface::reformat() */
CIndex reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const; CIndex& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s);
/** @copydoc IndexInterface::ifor() */ /** @copydoc IndexInterface::ifor() */
template <class Xpr, class F = NoF> template <class Xpr, class F = NoF>

View file

@ -67,7 +67,7 @@ namespace CNORXZ
Vector<SizeT> deepMax() const; Vector<SizeT> deepMax() const;
/** @copydoc IndexInterface::reformat() */ /** @copydoc IndexInterface::reformat() */
DIndex reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const; DIndex& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s);
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

@ -157,17 +157,6 @@ namespace CNORXZ
return i->xpr(i); return i->xpr(i);
} }
template <class I>
decltype(auto) reformat(const Sptr<I>& i, const Vector<SizeT>& f, const Vector<SizeT>& s)
{
if constexpr(has_sub<I>::value){
return moveToPtr( i->reformat(f,s) );
}
else {
i->reformat(f,s); // checks
return i;
}
}
} }
#endif #endif

View file

@ -179,9 +179,8 @@ namespace CNORXZ
/** reformat index, create new index instance /** reformat index, create new index instance
@param f new format @param f new format
@param s new sub-index sizes @param s new sub-index sizes
@return new reformatted index instance
*/ */
decltype(auto) reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const { return THIS().reformat(f,s); } I& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) { return THIS().reformat(f,s); }
/** create a for-loop expression /** create a for-loop expression
@ -234,9 +233,6 @@ namespace CNORXZ
template <class I> template <class I>
decltype(auto) xpr(const Sptr<I>& i); decltype(auto) xpr(const Sptr<I>& i);
// do not copy single-indices:
template <class I>
decltype(auto) reformat(const Sptr<I>& i, const Vector<SizeT>& f, const Vector<SizeT>& s);
} }
#endif #endif

View file

@ -24,6 +24,8 @@ namespace CNORXZ
class MFormat class MFormat
{ {
public: public:
typedef Arr<UPos,N> InputType;
SP_DEFAULT_MEMBERS(constexpr,MFormat); SP_DEFAULT_MEMBERS(constexpr,MFormat);
explicit constexpr MFormat(const Arr<UPos,N>& b); explicit constexpr MFormat(const Arr<UPos,N>& b);
@ -50,6 +52,8 @@ namespace CNORXZ
class GMFormat class GMFormat
{ {
public: public:
typedef Tuple<PosT...> InputType;
SP_DEFAULT_MEMBERS(constexpr,GMFormat); SP_DEFAULT_MEMBERS(constexpr,GMFormat);
explicit constexpr GMFormat(const Tuple<PosT...>& b); explicit constexpr GMFormat(const Tuple<PosT...>& b);
explicit constexpr GMFormat(Tuple<PosT...>&& b); explicit constexpr GMFormat(Tuple<PosT...>&& b);
@ -78,6 +82,8 @@ namespace CNORXZ
class YFormat class YFormat
{ {
public: public:
typedef Vector<UPos> InputType;
DEFAULT_MEMBERS(YFormat); DEFAULT_MEMBERS(YFormat);
explicit YFormat(const Vector<UPos>& b); explicit YFormat(const Vector<UPos>& b);

View file

@ -539,41 +539,45 @@ namespace CNORXZ
} }
template <class FormatT, class... Indices> template <class FormatT, class... Indices>
decltype(auto) GMIndex<FormatT,Indices...>&
GMIndex<FormatT,Indices...>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const GMIndex<FormatT,Indices...>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s)
{ {
if constexpr(std::is_same<FormatT,None>::value){
CXZ_ASSERT(CNORXZ::formatIsTrivial(f,s),
"cannot reformat MIndex with format type = None");
return *this;
}
else {
CXZ_ASSERT(f.size() == s.size(), "input error: f.size() != s.size()"); CXZ_ASSERT(f.size() == s.size(), "input error: f.size() != s.size()");
// f: input format // f: input format
// s: input sizes // s: input sizes
SizeT j = 0; SizeT j = 0;
SizeT j0 = 0; SizeT j0 = 0;
Arr<UPos,NI> nformat; typename FormatT::InputType nformat;
auto npack = iter<0,NI>( [&](auto i) { auto npack = iter<0,NI>( [&](auto i) {
SizeT si = 1; SizeT si = 1;
if(mIPack[i]->lmax().val() == 1){ if(mIPack[i]->lmax().val() != 1){
//std::get<i>(npack) = mIPack[i];
return mIPack[i];
}
// CHECK!!! // CHECK!!!
for(; si < mIPack[i]->lmax().val(); ++j){ for(; si < mIPack[i]->lmax().val(); ++j){
si *= s[i]; si *= s[i];
CXZ_ASSERT(j < f.size(), "incompatible index formats"); CXZ_ASSERT(j < f.size(), "incompatible index formats");
} }
CXZ_ASSERT(si == mIPack[i]->lmax().val(), CXZ_ASSERT(si == mIPack[i]->lmax().val(),
"incompatible index formats: " << toString(s) << " vs " //!!! "incompatible index formats: " << toString(s) << " vs " /*!!!*/);
);
Vector<SizeT> nf(j-j0); Vector<SizeT> nf(j-j0);
Vector<SizeT> ns(j-j0); Vector<SizeT> ns(j-j0);
std::copy(f.begin()+j0,f.begin()+j,nf.begin()); std::copy(f.begin()+j0,f.begin()+j,nf.begin());
nformat[i] = *std::min_element(nf.begin(), nf.end()); std::get<i>(nformat) = *std::min_element(nf.begin(), nf.end());
std::for_each(nf.begin(), nf.end(), [&](SizeT& x) std::for_each(nf.begin(), nf.end(), [&](SizeT& x)
{ CXZ_ASSERT(x % nformat[i].val() == 0, "incompatible"); x /= nformat[i].val(); } ); { CXZ_ASSERT(x % std::get<i>(nformat).val() == 0, "incompatible");
x /= std::get<i>(nformat).val(); } );
std::copy(s.begin()+j0,s.begin()+j,ns.begin()); std::copy(s.begin()+j0,s.begin()+j,ns.begin());
return CNORXZ::reformat(mIPack[i],nf,ns); mIPack[i]->reformat(nf,ns);
}, [](const auto&... e) { return std::make_tuple( e... ); } ); }
GMIndex<MFormat<NI>,Indices...> oi { MFormat<NI>(nformat),SPack<Indices...>(npack) }; }, NoF {});
oi = lex(); mFormat = FormatT(nformat);
return oi; return *this;
}
} }
template <class FormatT, class... Indices> template <class FormatT, class... Indices>

View file

@ -94,7 +94,7 @@ namespace CNORXZ
auto deepMax() const; auto deepMax() const;
/** @copydoc IndexInterface::reformat() */ /** @copydoc IndexInterface::reformat() */
decltype(auto) reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const; GMIndex& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s);
GMIndex& setFormat(const FormatT& bs); GMIndex& setFormat(const FormatT& bs);

View file

@ -174,13 +174,12 @@ namespace CNORXZ
} }
template <class IndexT> template <class IndexT>
PIndex<IndexT> PIndex<IndexT>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const PIndex<IndexT>& PIndex<IndexT>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s)
{ {
CXZ_ASSERT(f.size() == 1, "expected format of dimension 1, got " << toString(f)); CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0]
CXZ_ASSERT(s.size() == 1, "expected sizes of dimension 1, got " << toString(s)); << " vs " << lmax().val());
CXZ_ASSERT(f[0] == 1, "trivial format ([1]), got " << toString(f)); CXZ_ASSERT(CNORXZ::formatIsTrivial(f,s), "format is not trivial: f = " << toString(f)
CXZ_ASSERT(s[0] == lmax().val(), "expected size to be equal to index size (" << << ", s = " << toString(s));
lmax().val() << "), got " << s[0]);
return *this; return *this;
} }

View file

@ -58,7 +58,7 @@ namespace CNORXZ
decltype(auto) deepMax() const; decltype(auto) deepMax() const;
/** @copydoc IndexInterface::reformat() */ /** @copydoc IndexInterface::reformat() */
PIndex reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const; PIndex& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s);
String stringMeta() const; String stringMeta() const;
decltype(auto) meta() const; decltype(auto) meta() const;

View file

@ -184,7 +184,7 @@ namespace CNORXZ
} }
template <typename MetaT, SizeT S> template <typename MetaT, SizeT S>
SIndex<MetaT,S> SIndex<MetaT,S>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const SIndex<MetaT,S>& SIndex<MetaT,S>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s)
{ {
CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0] CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0]
<< " vs " << lmax().val()); << " vs " << lmax().val());

View file

@ -64,7 +64,7 @@ namespace CNORXZ
SizeT deepMax() const; SizeT deepMax() const;
/** @copydoc IndexInterface::reformat() */ /** @copydoc IndexInterface::reformat() */
SIndex reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const; SIndex& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s);
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

@ -170,7 +170,7 @@ namespace CNORXZ
} }
template <typename MetaT> template <typename MetaT>
UIndex<MetaT> UIndex<MetaT>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const UIndex<MetaT>& UIndex<MetaT>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s)
{ {
CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0] CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0]
<< " vs " << lmax().val()); << " vs " << lmax().val());

View file

@ -70,7 +70,7 @@ namespace CNORXZ
SizeT deepMax() const; SizeT deepMax() const;
/** @copydoc IndexInterface::reformat() */ /** @copydoc IndexInterface::reformat() */
UIndex reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const; UIndex& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s);
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

@ -165,9 +165,10 @@ namespace CNORXZ
} }
template <class Index, typename Meta> template <class Index, typename Meta>
XIndexPtr XIndex<Index,Meta>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const XIndex<Index,Meta>& XIndex<Index,Meta>::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s)
{ {
return xindexPtr( moveToPtr( mI->reformat(f,s) ) ); mI->reformat(f,s);
return *this;
} }
template <class Index, typename Meta> template <class Index, typename Meta>

View file

@ -53,7 +53,7 @@ namespace CNORXZ
virtual RangePtr prange(const XIndexPtr& last) const = 0; virtual RangePtr prange(const XIndexPtr& last) const = 0;
virtual Vector<SizeT> deepFormat() const = 0; virtual Vector<SizeT> deepFormat() const = 0;
virtual Vector<SizeT> deepMax() const = 0; virtual Vector<SizeT> deepMax() const = 0;
virtual XIndexPtr reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const = 0; virtual XIndexBase& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) = 0;
virtual String stringMeta() const = 0; virtual String stringMeta() const = 0;
virtual DType meta() const = 0; virtual DType meta() const = 0;
@ -107,7 +107,7 @@ namespace CNORXZ
virtual RangePtr prange(const XIndexPtr& last) const override final; virtual RangePtr prange(const XIndexPtr& last) const override final;
virtual Vector<SizeT> deepFormat() const override final; virtual Vector<SizeT> deepFormat() const override final;
virtual Vector<SizeT> deepMax() const override final; virtual Vector<SizeT> deepMax() const override final;
virtual XIndexPtr reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const override final; virtual XIndex& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) override final;
virtual String stringMeta() const override final; virtual String stringMeta() const override final;
virtual DType meta() const override final; virtual DType meta() const override final;

View file

@ -80,7 +80,7 @@ namespace CNORXZ
const YFormat& format() const; const YFormat& format() const;
const YFormat& lexFormat() const; const YFormat& lexFormat() const;
YIndex& setFormat(const YFormat& bs); YIndex& setFormat(const YFormat& bs);
YIndex reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const; YIndex& reformat(const Vector<SizeT>& f, const Vector<SizeT>& s);
/** @copydoc IndexInterface::formatIsTrivial() */ /** @copydoc IndexInterface::formatIsTrivial() */
bool formatIsTrivial() const; bool formatIsTrivial() const;

View file

@ -144,7 +144,7 @@ namespace CNORXZ
return lmax().val(); return lmax().val();
} }
CIndex CIndex::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const CIndex& CIndex::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s)
{ {
CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0] CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0]
<< " vs " << lmax().val()); << " vs " << lmax().val());

View file

@ -182,9 +182,10 @@ namespace CNORXZ
return mI->deepMax(); return mI->deepMax();
} }
DIndex DIndex::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const DIndex& DIndex::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s)
{ {
return DIndex(mI->reformat(f,s)); mI->reformat(f,s);
return *this;
} }
bool DIndex::formatIsTrivial() const bool DIndex::formatIsTrivial() const

View file

@ -413,27 +413,25 @@ namespace CNORXZ
return o; return o;
} }
YIndex YIndex::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s) const YIndex& YIndex::reformat(const Vector<SizeT>& f, const Vector<SizeT>& s)
{ {
CXZ_ASSERT(f.size() == s.size(), "input error: f.size() != s.size()"); CXZ_ASSERT(f.size() == s.size(), "input error: f.size() != s.size()");
// f: input format // f: input format
// s: input sizes // s: input sizes
SizeT j = 0; SizeT j = 0;
SizeT j0 = 0; SizeT j0 = 0;
Vector<UPos> nformat(dim()); Vector<UPos> nformat;
Vector<XIndexPtr> npack(dim());
for(SizeT i = 0; i != dim(); ++i){ for(SizeT i = 0; i != dim(); ++i){
SizeT si = 1; SizeT si = 1;
if(mIs[i]->lmax().val() == 1){ if(mIs[i]->lmax().val() == 1){
npack[i] = mIs[i]; continue;
} }
for(; si < mIs[i]->lmax().val(); ++j){ for(; si < mIs[i]->lmax().val(); ++j){
si *= s[i]; si *= s[i];
CXZ_ASSERT(j < f.size(), "incompatible index formats"); CXZ_ASSERT(j < f.size(), "incompatible index formats");
} }
CXZ_ASSERT(si == mIs[i]->lmax().val(), CXZ_ASSERT(si == mIs[i]->lmax().val(),
"incompatible index formats: " << toString(s) << " vs " //!!! "incompatible index formats: " << toString(s) << " vs " /*!!!*/);
);
Vector<SizeT> nf(j-j0); Vector<SizeT> nf(j-j0);
Vector<SizeT> ns(j-j0); Vector<SizeT> ns(j-j0);
std::copy(f.begin()+j0,f.begin()+j,nf.begin()); std::copy(f.begin()+j0,f.begin()+j,nf.begin());
@ -441,11 +439,10 @@ namespace CNORXZ
std::for_each(nf.begin(), nf.end(), [&](SizeT& x) std::for_each(nf.begin(), nf.end(), [&](SizeT& x)
{ CXZ_ASSERT(x % nformat[i].val() == 0, "incompatible"); x /= nformat[i].val(); } ); { CXZ_ASSERT(x % nformat[i].val() == 0, "incompatible"); x /= nformat[i].val(); } );
std::copy(s.begin()+j0,s.begin()+j,ns.begin()); std::copy(s.begin()+j0,s.begin()+j,ns.begin());
npack[i] = mIs[i]->reformat(nf,ns); mIs[i]->reformat(nf,ns);
} }
YIndex oi ( YFormat(nformat), npack ); mFormat = YFormat(nformat);
oi = lex(); return *this;
return oi;
} }
bool YIndex::formatIsTrivial() const bool YIndex::formatIsTrivial() const