2017-02-16 11:20:40 +01:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
|
|
|
#ifndef __multi_array_operation_h__
|
|
|
|
#define __multi_array_operation_h__
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <tuple>
|
2017-03-22 21:51:54 +01:00
|
|
|
#include <cmath>
|
2017-02-16 11:20:40 +01:00
|
|
|
|
|
|
|
#include "base_def.h"
|
|
|
|
|
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
/*
|
|
|
|
* OperationBase
|
|
|
|
* MutableOperationBase
|
|
|
|
*
|
|
|
|
* OperationMaster : MutableOperationBase
|
|
|
|
*
|
|
|
|
* OperationTemplate<...>
|
|
|
|
* ConstOperationRoot : OperationBase, OperationTemplate<...>
|
|
|
|
* OperationRoot : MutableOperationBase,
|
|
|
|
* OperationTemplate<...>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-02-24 15:08:42 +01:00
|
|
|
template <typename T>
|
2017-08-10 15:12:26 +02:00
|
|
|
class OperationBase
|
2017-02-16 11:20:40 +01:00
|
|
|
{
|
|
|
|
public:
|
2017-02-24 20:50:58 +01:00
|
|
|
|
2017-03-27 19:29:51 +02:00
|
|
|
typedef T value_type;
|
2017-03-27 11:33:40 +02:00
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
OperationBase() = default;
|
2017-08-11 15:26:10 +02:00
|
|
|
virtual ~OperationBase() = default;
|
2017-02-24 15:08:42 +01:00
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
//virtual size_t argNum() const = 0;
|
2017-02-24 15:08:42 +01:00
|
|
|
virtual const T& get() const = 0;
|
|
|
|
};
|
2017-05-24 19:01:02 +02:00
|
|
|
|
2017-03-16 19:30:43 +01:00
|
|
|
template <typename T>
|
2017-08-10 15:12:26 +02:00
|
|
|
class MutableOperationBase : public OperationBase<T>
|
2017-03-16 19:30:43 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
MutableOperationBase() = default;
|
2017-03-16 19:30:43 +01:00
|
|
|
virtual T& get() = 0;
|
|
|
|
};
|
2017-08-11 11:30:27 +02:00
|
|
|
|
|
|
|
template <class OperationClass>
|
|
|
|
class OperationTemplate
|
|
|
|
{
|
|
|
|
public:
|
2017-08-11 15:26:10 +02:00
|
|
|
|
|
|
|
OperationTemplate(OperationClass* oc);
|
2017-08-11 11:30:27 +02:00
|
|
|
|
|
|
|
template <class Second>
|
2017-08-26 17:18:42 +02:00
|
|
|
auto operator+(const Second& in) const
|
|
|
|
-> Operation<double,std::plus<double>,OperationClass,Second>;
|
|
|
|
|
|
|
|
template <class Second>
|
|
|
|
auto operator-(const Second& in) const
|
|
|
|
-> Operation<double,std::minus<double>,OperationClass,Second>;
|
|
|
|
|
|
|
|
template <class Second>
|
|
|
|
auto operator*(const Second& in) const
|
|
|
|
-> Operation<double,std::multiplies<double>,OperationClass,Second>;
|
|
|
|
|
|
|
|
template <class Second>
|
|
|
|
auto operator/(const Second& in) const
|
|
|
|
-> Operation<double,std::divides<double>,OperationClass,Second>;
|
|
|
|
|
2017-08-11 15:26:10 +02:00
|
|
|
private:
|
|
|
|
OperationClass* mOc;
|
2017-08-11 11:30:27 +02:00
|
|
|
};
|
2017-03-16 19:30:43 +01:00
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
template <typename T, class... Ranges>
|
|
|
|
class OperationMaster : public MutableOperationBase<T>
|
2017-02-24 15:08:42 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef OperationBase<T> OB;
|
2017-08-11 15:26:10 +02:00
|
|
|
typedef ContainerRange<Ranges...> CRange;
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef typename MultiRange<Ranges...>::IndexType IndexType;
|
2017-03-08 22:53:18 +01:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
OperationMaster(MutableMultiArrayBase<T,Ranges...>& ma, const OperationBase<T>& second,
|
2017-08-11 15:26:10 +02:00
|
|
|
std::shared_ptr<typename CRange::IndexType>& index);
|
2017-08-11 11:30:27 +02:00
|
|
|
|
2017-02-24 15:08:42 +01:00
|
|
|
virtual T& get() override;
|
|
|
|
virtual const T& get() const override;
|
2017-02-27 17:00:51 +01:00
|
|
|
|
2017-02-16 11:20:40 +01:00
|
|
|
protected:
|
2017-02-27 17:00:51 +01:00
|
|
|
|
2017-08-11 11:30:27 +02:00
|
|
|
//void performAssignment(const OperationBase<T>& in);
|
|
|
|
OperationBase<T> const& mSecond;
|
2017-08-25 17:02:23 +02:00
|
|
|
MutableMultiArrayBase<T,Ranges...>& mArrayRef;
|
2017-08-11 11:30:27 +02:00
|
|
|
std::shared_ptr<IndexType> mIndex;
|
2017-02-16 11:20:40 +01:00
|
|
|
};
|
2017-08-10 15:12:26 +02:00
|
|
|
|
2017-03-16 19:30:43 +01:00
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
template <typename T, class... Ranges>
|
|
|
|
class ConstOperationRoot : public OperationBase<T>,
|
2017-08-11 15:26:10 +02:00
|
|
|
public OperationTemplate<ConstOperationRoot<T,Ranges...> >
|
2017-03-16 19:30:43 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef OperationBase<T> OB;
|
2017-08-11 15:26:10 +02:00
|
|
|
typedef OperationTemplate<ConstOperationRoot<T,Ranges...> > OT;
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef ContainerRange<Ranges...> CRange;
|
|
|
|
typedef typename CRange::IndexType IndexType;
|
2017-03-16 19:30:43 +01:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
ConstOperationRoot(const MultiArrayBase<T,Ranges...>& ma,
|
2017-08-10 15:12:26 +02:00
|
|
|
const std::shared_ptr<typename Ranges::IndexType>&... indices);
|
2017-03-16 19:30:43 +01:00
|
|
|
|
|
|
|
virtual const T& get() const override;
|
|
|
|
|
|
|
|
protected:
|
2017-08-10 15:12:26 +02:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
MultiArrayBase<T,Ranges...> const& mArrayRef;
|
2017-08-10 15:12:26 +02:00
|
|
|
std::shared_ptr<IndexType> mIndex;
|
2017-03-16 19:30:43 +01:00
|
|
|
};
|
2017-02-16 11:20:40 +01:00
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
template <typename T, class... Ranges>
|
|
|
|
class OperationRoot : public MutableOperationBase<T>,
|
2017-08-11 15:26:10 +02:00
|
|
|
public OperationTemplate<OperationRoot<T,Ranges...> >
|
2017-03-26 16:55:52 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef OperationBase<T> OB;
|
2017-08-11 15:26:10 +02:00
|
|
|
typedef OperationTemplate<OperationRoot<T,Ranges...> > OT;
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef ContainerRange<Ranges...> CRange;
|
|
|
|
typedef typename CRange::IndexType IndexType;
|
2017-03-26 16:55:52 +02:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
OperationRoot(MutableMultiArrayBase<T,Ranges...>& ma,
|
2017-08-10 15:12:26 +02:00
|
|
|
const std::shared_ptr<typename Ranges::IndexType>&... indices);
|
2017-08-11 11:30:27 +02:00
|
|
|
|
|
|
|
OperationMaster<T,Ranges...> operator=(const OperationBase<T>& in);
|
|
|
|
|
2017-03-26 16:55:52 +02:00
|
|
|
virtual const T& get() const override;
|
|
|
|
virtual T& get() override;
|
2017-05-24 19:01:02 +02:00
|
|
|
|
2017-03-26 16:55:52 +02:00
|
|
|
protected:
|
2017-02-24 15:08:42 +01:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
MutableMultiArrayBase<T,Ranges...>& mArrayRef;
|
2017-08-10 15:12:26 +02:00
|
|
|
std::shared_ptr<IndexType> mIndex;
|
2017-02-16 11:20:40 +01:00
|
|
|
};
|
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
template <typename T, class OpFunction, class... Ops>
|
|
|
|
class Operation : public OperationBase<T>,
|
|
|
|
public OperationTemplate<Operation<T,OpFunction,Ops...> >
|
2017-03-22 11:44:33 +01:00
|
|
|
{
|
|
|
|
public:
|
2017-05-22 18:21:14 +02:00
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef OperationBase<T> OB;
|
|
|
|
typedef OperationTemplate<Operation<T,OpFunction,Ops...> > OT;
|
|
|
|
typedef OpFunction F;
|
2017-03-22 11:44:33 +01:00
|
|
|
|
2017-08-11 15:26:10 +02:00
|
|
|
Operation(const Ops&... ops);
|
2017-03-22 11:44:33 +01:00
|
|
|
|
|
|
|
virtual const T& get() const override;
|
2017-08-10 15:12:26 +02:00
|
|
|
|
2017-03-22 11:44:33 +01:00
|
|
|
protected:
|
2017-08-10 15:12:26 +02:00
|
|
|
std::tuple<Ops...> mOps;
|
2017-08-11 15:26:10 +02:00
|
|
|
mutable T mRes;
|
2017-03-22 11:44:33 +01:00
|
|
|
};
|
2017-08-10 15:12:26 +02:00
|
|
|
|
2017-08-11 15:26:10 +02:00
|
|
|
}
|
2017-02-16 11:20:40 +01:00
|
|
|
|
2017-02-17 18:10:03 +01:00
|
|
|
#include "multi_array_operation.cc"
|
2017-02-16 11:20:40 +01:00
|
|
|
|
|
|
|
#endif
|