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 <class IndexType>
auto OperationBase<T,OperationClass>::c(const std::shared_ptr<IndexType>& ind) const
-> Contraction<T,OperationClass,IndexType>
{
return Contraction<T,OperationClass,IndexType>(THIS(), ind);
}
@ -59,7 +58,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass>
template <class... Indices>
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()...);
out.define(inds...) = THIS();
@ -69,7 +67,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass>
template <class... Indices>
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...>>
(inds->range()... , static_cast<T>(0));
@ -80,7 +77,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass>
template <class... Indices>
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()...);
out(inds...) = THIS();
@ -90,7 +86,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass>
template <class... Indices>
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()...,
static_cast<T>(0));
@ -101,7 +96,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass>
template <class... Indices>
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()...);
out(inds...).par() = THIS();
@ -111,7 +105,6 @@ namespace MultiArrayTools
template <typename T, class OperationClass>
template <class... Indices>
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()...,
static_cast<T>(0));
@ -123,14 +116,12 @@ namespace MultiArrayTools
template <typename R, class... Args>
auto OperationBase<T,OperationClass>::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...>
{
return Operation<R,function<R,T,typename Args::value_type...>,OperationClass, Args...>(ll, THIS(), args...);
}
template <typename T, class OperationClass>
auto OperationBase<T,OperationClass>::ptr() const
-> OperationPointer<T,OperationClass>
{
return OperationPointer<T,OperationClass>(THIS());
}
@ -144,7 +135,30 @@ namespace MultiArrayTools
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 *
************************/

View file

@ -50,39 +50,30 @@ namespace MultiArrayTools
auto operator/(const OperationBase<U,Second>& in) const;
template <class IndexType>
auto c(const std::shared_ptr<IndexType>& ind) const
-> Contraction<T,OperationClass,IndexType>;
auto c(const std::shared_ptr<IndexType>& ind) const;
template <class... Indices>
auto sl(const std::shared_ptr<Indices>&... inds) const
-> ConstSlice<T,typename Indices::RangeType...>;
auto sl(const std::shared_ptr<Indices>&... inds) const;
template <class... Indices>
auto p(const std::shared_ptr<Indices>&... inds) const
-> ConstOperationRoot<T,typename Indices::RangeType...>;
auto p(const std::shared_ptr<Indices>&... inds) const;
template <class... Indices>
auto to(const std::shared_ptr<Indices>&... inds) const
-> MultiArray<T,typename Indices::RangeType...>;
auto to(const std::shared_ptr<Indices>&... inds) const;
template <class... Indices>
auto addto(const std::shared_ptr<Indices>&... inds) const
-> MultiArray<T,typename Indices::RangeType...>;
auto addto(const std::shared_ptr<Indices>&... inds) const;
template <class... Indices>
auto pto(const std::shared_ptr<Indices>&... inds) const
-> MultiArray<T,typename Indices::RangeType...>;
auto pto(const std::shared_ptr<Indices>&... inds) const;
template <class... Indices>
auto paddto(const std::shared_ptr<Indices>&... inds) const
-> MultiArray<T,typename Indices::RangeType...>;
auto paddto(const std::shared_ptr<Indices>&... inds) const;
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
-> Operation<R,function<R,T,typename Args::value_type...>,OperationClass, Args...>;
auto a(const std::shared_ptr<function<R,T,typename Args::value_type...>>& ll, const Args&... args) const;
auto ptr() const
-> OperationPointer<T,OperationClass>;
auto ptr() const;
private:
friend OperationClass;
@ -212,7 +203,65 @@ namespace MultiArrayTools
template <typename 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>
class AssignmentExpr : public ExpressionBase
{