re-implement DIndex ifor (-> template)
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Christian Zimmermann 2023-11-12 23:16:56 +01:00
parent 0f0a01793c
commit 3b7040447c
6 changed files with 156 additions and 5 deletions

View file

@ -170,6 +170,9 @@ namespace CNORXZ
template <class Op> template <class Op>
constexpr OpCont<T,IndexT>& OpCont<T,IndexT>::operator=(const Op& o) constexpr OpCont<T,IndexT>& OpCont<T,IndexT>::operator=(const Op& o)
{ {
// TODO: build and execute assign expression forwarding outer index
// if a1 and a2 are non-expression types (like it is the case now),
// just do what is currently implemented
OI::a(mIndex, [](auto& a1, const auto& a2) { a1 = a2; }, o); OI::a(mIndex, [](auto& a1, const auto& a2) { a1 = a2; }, o);
return *this; return *this;
} }
@ -375,7 +378,39 @@ namespace CNORXZ
return Operation<F,Ops...>(std::forward<F>(f), ops...); return Operation<F,Ops...>(std::forward<F>(f), ops...);
} }
template <class Tar, class Src>
constexpr decltype(auto) assignxpr(const Tar& tar, const Src& src)
{
static_assert(is_xpr<Tar>::value, "expected expression");
if constexpr(is_xpr<Src>::value){
return operation([](auto& a, const auto& b) { a = b; }, tar, src);
}
else {
return operation([&](auto& a) { a = src; }, tar);
}
}
template <class Tar, class Src>
constexpr decltype(auto) assignxpr(Tar& tar, const Src& src)
{
if constexpr(is_xpr<Tar>::value){
if constexpr(is_xpr<Src>::value){
return operation([](auto& a, const auto& b) { a = b; }, tar, src);
}
else {
return operation([&](auto& a) { a = src; }, tar);
}
}
else {
if constexpr(is_xpr<Src>::value){
return operation([&](const auto& b) { tar = b; }, src);
}
else {
return operation([&]() { tar = src; });
}
}
}
/********************* /*********************
* Contraction * * Contraction *
*********************/ *********************/

View file

@ -212,6 +212,12 @@ namespace CNORXZ
template <class F, class... Ops> template <class F, class... Ops>
constexpr decltype(auto) operation(F&& f, const Ops&... ops); constexpr decltype(auto) operation(F&& f, const Ops&... ops);
template <class Tar, class Src>
constexpr decltype(auto) assignxpr(const Tar& tar, const Src& src);
template <class Tar, class Src>
constexpr decltype(auto) assignxpr(Tar& tar, const Src& src);
template <class F, class... Ops> template <class F, class... Ops>
struct op_size<Operation<F,Ops...>> struct op_size<Operation<F,Ops...>>
{ static constexpr SizeT value = ( op_size<Ops>::value + ... ); }; { static constexpr SizeT value = ( op_size<Ops>::value + ... ); };

View file

@ -32,13 +32,11 @@ namespace CNORXZ
return mI->ifor( DXpr<None>(xpr), std::forward<F>(f) ); return mI->ifor( DXpr<None>(xpr), std::forward<F>(f) );
} }
else { else {
CXZ_ERROR("IMPLEMENT!!!"); return DXpr<R>( bufxpr([&](auto x){ return mI->ifor( DXpr<None>( assignxpr(x, xpr) ), NoF {} ); }) );
return DXpr<R>();
} }
} }
else { else {
CXZ_ERROR("IMPLEMENT!!!"); return DXpr<R>( bufxpr([&](auto x){ return mI->ifor( DXpr<None>( assignxpr(x, xpr) ), NoF {} ); }) );
return DXpr<R>();
} }
} }
} }

View file

@ -0,0 +1,71 @@
#include "buf_xpr.h"
namespace CNORXZ
{
template <typename T, class IXprF>
BufXpr<T,IXprF>::BufXpr(const BufXpr& in) :
mBuf(in.mBuf),
mIXprF(in.mIXprF),
mIXpr(mIXprF(mBuf))
{}
template <typename T, class IXprF>
BufXpr<T,IXprF>::BufXpr(BufXpr&& in) :
mBuf(std::move(in.mBuf)),
mIXprF(std::move(in.mIXprF)),
mIXpr(mIXprF(mBuf))
{}
template <typename T, class IXprF>
BufXpr& BufXpr<T,IXprF>::operator=(const BufXpr& in)
{
mBuf = in.mBuf;
mIXprF = in.mIXprF;
mIXpr = mIXprF(mBuf);
return *this;
}
template <typename T, class IXprF>
BufXpr& BufXpr<T,IXprF>::operator=(BufXpr&& in)
{
mBuf = std::move(in.mBuf);
mIXprF = std::move(in.mIXprF);
mIXpr = mIXprF(mBuf);
return *this;
}
template <typename T, class IXprF>
constexpr BufXpr<T,IXprF>::BufXpr(IXprF&& ixprf) :
mIXprF(std::forward<IXprF>(ixprf)),
mIXpr(mIXprF(mBuf))
{}
template <typename T, class IXprF>
template <class PosT>
inline decltype(auto) BufXpr<T,IXprF>::operator()(const PosT& last) const
{
mIXpr(last);
return mBuf;
}
template <typename T, class IXprF>
inline decltype(auto) BufXpr<T,IXprF>::operator()() const
{
mIXpr();
return mBuf;
}
template <typename T, class IXprF>
template <SizeT I>
inline decltype(auto) BufXpr<T,IXprF>::rootSteps(const IndexId<I>& id) const
{
return mIXpr.rootSteps(id);
}
template <typename T, class IXprF>
constexpr decltype(auto) bufxpr(IXprF&& ixprf)
{
return BufXpr<T,IXprF>( std::forward<IXprF>(ixprf) );
}
}

35
src/include/xpr/buf_xpr.h Normal file
View file

@ -0,0 +1,35 @@
#include "xpr_base.h"
namespace CNORXZ
{
template <typename T, class IXprF>
class BufXpr : public XprInterface<BufXpr>
{
public:
BufXpr() = default;
BufXpr(const BufXpr& in);
BufXpr(BufXpr&& in);
BufXpr& operator=(const BufXpr& in);
BufXpr& operator=(BufXpr&& in);
constexpr BufXpr(IXprF&& ixprf);
template <class PosT>
inline decltype(auto) operator()(const PosT& last) const;
inline decltype(auto) operator()() const;
template <SizeT I>
inline decltype(auto) rootSteps(const IndexId<I>& id) const;
private:
T mBuf;
IXprF mIXprF; // function to create set-buffer-xpr (reference to mBuf as arg)
typedef decltype(mIXprF(mBuf)) IXpr;
IXpr mIXpr;
};
template <typename T, class IXprF>
constexpr decltype(auto) bufxpr(IXprF&& ixprf);
}

View file

@ -75,6 +75,12 @@ namespace CNORXZ
template <SizeT I> template <SizeT I>
inline DPos rootSteps(const IndexId<I>& id) const; inline DPos rootSteps(const IndexId<I>& id) const;
}; };
template <class X>
struct is_xpr
{
static constexpr bool value = std::is_base_of<XprInterface<X>,X>::value;
};
} }
#endif #endif