operations: AccessTemplate (static base class)

This commit is contained in:
Christian Zimmermann 2021-07-01 17:55:46 +02:00
parent e80c71aa62
commit bf7853c1b5
2 changed files with 92 additions and 29 deletions

View file

@ -51,7 +51,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass> template <typename T, class OperationClass>
template <class IndexType> template <class IndexType>
auto OperationBase<T,OperationClass>::c(const std::shared_ptr<IndexType>& ind) const auto OperationBase<T,OperationClass>::c(const std::shared_ptr<IndexType>& ind) const
-> Contraction<T,OperationClass,IndexType>
{ {
return Contraction<T,OperationClass,IndexType>(THIS(), ind); return Contraction<T,OperationClass,IndexType>(THIS(), ind);
} }
@ -59,7 +58,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass> template <typename T, class OperationClass>
template <class... Indices> template <class... Indices>
auto OperationBase<T,OperationClass>::sl(const std::shared_ptr<Indices>&... inds) const auto OperationBase<T,OperationClass>::sl(const std::shared_ptr<Indices>&... inds) const
-> ConstSlice<T,typename Indices::RangeType...>
{ {
ConstSlice<T,typename Indices::RangeType...> out(inds->range()...); ConstSlice<T,typename Indices::RangeType...> out(inds->range()...);
out.define(inds...) = THIS(); out.define(inds...) = THIS();
@ -69,7 +67,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass> template <typename T, class OperationClass>
template <class... Indices> template <class... Indices>
auto OperationBase<T,OperationClass>::p(const std::shared_ptr<Indices>&... inds) const auto OperationBase<T,OperationClass>::p(const std::shared_ptr<Indices>&... inds) const
-> ConstOperationRoot<T,typename Indices::RangeType...>
{ {
auto ma = std::make_shared<MultiArray<T,typename Indices::RangeType...>> auto ma = std::make_shared<MultiArray<T,typename Indices::RangeType...>>
(inds->range()... , static_cast<T>(0)); (inds->range()... , static_cast<T>(0));
@ -80,7 +77,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass> template <typename T, class OperationClass>
template <class... Indices> template <class... Indices>
auto OperationBase<T,OperationClass>::to(const std::shared_ptr<Indices>&... inds) const auto OperationBase<T,OperationClass>::to(const std::shared_ptr<Indices>&... inds) const
-> MultiArray<T,typename Indices::RangeType...>
{ {
MultiArray<T,typename Indices::RangeType...> out(inds->range()...); MultiArray<T,typename Indices::RangeType...> out(inds->range()...);
out(inds...) = THIS(); out(inds...) = THIS();
@ -90,7 +86,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass> template <typename T, class OperationClass>
template <class... Indices> template <class... Indices>
auto OperationBase<T,OperationClass>::addto(const std::shared_ptr<Indices>&... inds) const auto OperationBase<T,OperationClass>::addto(const std::shared_ptr<Indices>&... inds) const
-> MultiArray<T,typename Indices::RangeType...>
{ {
MultiArray<T,typename Indices::RangeType...> out(inds->range()..., MultiArray<T,typename Indices::RangeType...> out(inds->range()...,
static_cast<T>(0)); static_cast<T>(0));
@ -101,7 +96,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass> template <typename T, class OperationClass>
template <class... Indices> template <class... Indices>
auto OperationBase<T,OperationClass>::pto(const std::shared_ptr<Indices>&... inds) const auto OperationBase<T,OperationClass>::pto(const std::shared_ptr<Indices>&... inds) const
-> MultiArray<T,typename Indices::RangeType...>
{ {
MultiArray<T,typename Indices::RangeType...> out(inds->range()...); MultiArray<T,typename Indices::RangeType...> out(inds->range()...);
out(inds...).par() = THIS(); out(inds...).par() = THIS();
@ -111,7 +105,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass> template <typename T, class OperationClass>
template <class... Indices> template <class... Indices>
auto OperationBase<T,OperationClass>::paddto(const std::shared_ptr<Indices>&... inds) const auto OperationBase<T,OperationClass>::paddto(const std::shared_ptr<Indices>&... inds) const
-> MultiArray<T,typename Indices::RangeType...>
{ {
MultiArray<T,typename Indices::RangeType...> out(inds->range()..., MultiArray<T,typename Indices::RangeType...> out(inds->range()...,
static_cast<T>(0)); static_cast<T>(0));
@ -123,14 +116,12 @@ namespace MultiArrayTools
template <typename R, class... Args> template <typename R, class... Args>
auto OperationBase<T,OperationClass>::a(const std::shared_ptr<function<R,T,typename Args::value_type...>>& ll, auto OperationBase<T,OperationClass>::a(const std::shared_ptr<function<R,T,typename Args::value_type...>>& ll,
const Args&... args) const const Args&... args) const
-> Operation<R,function<R,T,typename Args::value_type...>,OperationClass, Args...>
{ {
return Operation<R,function<R,T,typename Args::value_type...>,OperationClass, Args...>(ll, THIS(), args...); return Operation<R,function<R,T,typename Args::value_type...>,OperationClass, Args...>(ll, THIS(), args...);
} }
template <typename T, class OperationClass> template <typename T, class OperationClass>
auto OperationBase<T,OperationClass>::ptr() const auto OperationBase<T,OperationClass>::ptr() const
-> OperationPointer<T,OperationClass>
{ {
return OperationPointer<T,OperationClass>(THIS()); return OperationPointer<T,OperationClass>(THIS());
} }
@ -144,7 +135,30 @@ namespace MultiArrayTools
return HyperOperation<T,SubOp,Indices...>() return HyperOperation<T,SubOp,Indices...>()
} }
*/ */
/************************
* AccessTemplate *
************************/
template <class AccessClass>
auto AccessTemplate<AccessClass>::get(size_t pos)
{
return THIS().get(pos);
}
template <class AccessClass>
auto AccessTemplate<AccessClass>::get(size_t pos) const
{
return THIS().get(pos);
}
template <class AccessClass>
template <class F, typename Op, class ExtType>
void AccessTemplate<AccessClass>::exec(size_t pos, const Op& op, ExtType e) const
{
return THIS().template exec<F>(pos,op,e);
}
/************************ /************************
* AssignmentExpr * * AssignmentExpr *
************************/ ************************/

View file

@ -50,39 +50,30 @@ namespace MultiArrayTools
auto operator/(const OperationBase<U,Second>& in) const; auto operator/(const OperationBase<U,Second>& in) const;
template <class IndexType> template <class IndexType>
auto c(const std::shared_ptr<IndexType>& ind) const auto c(const std::shared_ptr<IndexType>& ind) const;
-> Contraction<T,OperationClass,IndexType>;
template <class... Indices> template <class... Indices>
auto sl(const std::shared_ptr<Indices>&... inds) const auto sl(const std::shared_ptr<Indices>&... inds) const;
-> ConstSlice<T,typename Indices::RangeType...>;
template <class... Indices> template <class... Indices>
auto p(const std::shared_ptr<Indices>&... inds) const auto p(const std::shared_ptr<Indices>&... inds) const;
-> ConstOperationRoot<T,typename Indices::RangeType...>;
template <class... Indices> template <class... Indices>
auto to(const std::shared_ptr<Indices>&... inds) const auto to(const std::shared_ptr<Indices>&... inds) const;
-> MultiArray<T,typename Indices::RangeType...>;
template <class... Indices> template <class... Indices>
auto addto(const std::shared_ptr<Indices>&... inds) const auto addto(const std::shared_ptr<Indices>&... inds) const;
-> MultiArray<T,typename Indices::RangeType...>;
template <class... Indices> template <class... Indices>
auto pto(const std::shared_ptr<Indices>&... inds) const auto pto(const std::shared_ptr<Indices>&... inds) const;
-> MultiArray<T,typename Indices::RangeType...>;
template <class... Indices> template <class... Indices>
auto paddto(const std::shared_ptr<Indices>&... inds) const auto paddto(const std::shared_ptr<Indices>&... inds) const;
-> MultiArray<T,typename Indices::RangeType...>;
template <typename R, class... Args> // Args = Operation Classes template <typename R, class... Args> // Args = Operation Classes
auto a(const std::shared_ptr<function<R,T,typename Args::value_type...>>& ll, const Args&... args) const auto a(const std::shared_ptr<function<R,T,typename Args::value_type...>>& ll, const Args&... args) const;
-> Operation<R,function<R,T,typename Args::value_type...>,OperationClass, Args...>;
auto ptr() const auto ptr() const;
-> OperationPointer<T,OperationClass>;
private: private:
friend OperationClass; friend OperationClass;
@ -212,7 +203,65 @@ namespace MultiArrayTools
template <typename T> template <typename T>
using IVPlus = IVAccess<T,plus<T>>; using IVPlus = IVAccess<T,plus<T>>;
// static polymorphism
template <class AccessClass>
class AccessTemplate
{
public:
typedef AccessClass AC;
AccessClass& THIS() { return static_cast<AccessClass&>(*this); }
const AccessClass& THIS() const { return static_cast<const AccessClass&>(*this); }
inline auto get(size_t pos);
inline auto get(size_t pos) const;
template <class F, typename Op, class ExtType>
inline void exec(size_t pos, const Op& op, ExtType e) const;
};
template <typename T>
class PointerAccess : public AccessTemplate<PointerAccess<T>>
{
public:
typedef T value_type;
typedef T in_type;
private:
T* mPtr;
public:
PointerAccess(T* ptr) : mPtr(ptr) {}
T* get(size_t pos) { return mPtr; }
T* get(size_t pos) const { return mPtr; }
template <class F, typename Op, class ExtType>
inline void exec(size_t pos, const Op& op, ExtType e) const
{
F::selfApply(*get(pos),op.get(e));
}
};
template <typename T>
class ConstPointerAccess : public AccessTemplate<ConstPointerAccess<T>>
{
public:
typedef T value_type;
typedef T in_type;
private:
const T* mPtr;
public:
ConstPointerAccess(T* ptr) : mPtr(ptr) {}
const T* get(size_t pos) { return mPtr; }
const T* get(size_t pos) const { return mPtr; }
template <class F, typename Op, class ExtType>
inline void exec(size_t pos, const Op& op, ExtType e) const { assert(0); }
};
template <typename T, class IOp, class Target, class OpClass, OpIndexAff OIA=OpIndexAff::EXTERN> template <typename T, class IOp, class Target, class OpClass, OpIndexAff OIA=OpIndexAff::EXTERN>
class AssignmentExpr : public ExpressionBase class AssignmentExpr : public ExpressionBase
{ {