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-08-30 17:56:38 +02:00
|
|
|
#include <map>
|
|
|
|
#include <utility>
|
2017-02-16 11:20:40 +01:00
|
|
|
|
|
|
|
#include "base_def.h"
|
|
|
|
|
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
|
|
|
|
2017-08-26 22:53:02 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using namespace MultiArrayHelper;
|
|
|
|
}
|
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
/*
|
|
|
|
* OperationBase
|
|
|
|
* MutableOperationBase
|
|
|
|
*
|
|
|
|
* OperationMaster : MutableOperationBase
|
|
|
|
*
|
|
|
|
* OperationTemplate<...>
|
|
|
|
* ConstOperationRoot : OperationBase, OperationTemplate<...>
|
|
|
|
* OperationRoot : MutableOperationBase,
|
|
|
|
* OperationTemplate<...>
|
|
|
|
*
|
|
|
|
*/
|
2017-08-28 18:28:43 +02:00
|
|
|
|
2017-08-30 19:41:49 +02:00
|
|
|
void seekIndexInst(std::shared_ptr<IndexBase> i, std::vector<std::shared_ptr<IndexBase> >& ivec);
|
2017-08-28 18:28:43 +02:00
|
|
|
|
2017-08-30 17:56:38 +02:00
|
|
|
|
|
|
|
// <block type, step size within actual instance>
|
|
|
|
typedef std::pair<BlockType,size_t> BTSS;
|
|
|
|
|
2017-08-30 19:41:49 +02:00
|
|
|
BTSS getBlockType(std::shared_ptr<IndexBase> i,
|
2017-09-12 18:36:05 +02:00
|
|
|
std::shared_ptr<IndexBase> j,
|
|
|
|
bool first, size_t higherStepSize = 1);
|
2017-08-30 17:56:38 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2017-10-31 19:22:34 +01:00
|
|
|
std::shared_ptr<Block<T> > makeBlock(const std::vector<T>& vec, size_t stepSize, size_t blockSize);
|
2017-08-30 17:56:38 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2017-10-31 19:22:34 +01:00
|
|
|
std::shared_ptr<MBlock<T> > makeBlock(std::vector<T>& vec, size_t stepSize, size_t blockSize);
|
2017-08-30 17:56:38 +02:00
|
|
|
|
2017-08-30 19:41:49 +02:00
|
|
|
size_t getBTNum(const std::vector<BTSS>& mp, BlockType bt);
|
2017-08-30 17:56:38 +02:00
|
|
|
|
2017-10-30 17:52:27 +01:00
|
|
|
void minimizeAppearanceOfType(std::map<std::shared_ptr<IndexBase>, std::vector<BTSS> >& mp,
|
2017-08-30 19:41:49 +02:00
|
|
|
BlockType bt);
|
2017-08-30 17:56:38 +02:00
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
template <class OpClass>
|
2017-08-30 19:41:49 +02:00
|
|
|
std::shared_ptr<IndexBase> seekBlockIndex(std::shared_ptr<IndexBase> ownIdx,
|
2017-10-31 19:22:34 +01:00
|
|
|
const OpClass& second);
|
|
|
|
/*
|
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-03-27 19:29:51 +02:00
|
|
|
typedef T value_type;
|
2017-08-30 19:41:49 +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-28 18:28:43 +02:00
|
|
|
// init block, return resulting type (BLOCK, VALUE, SPLIT)
|
2017-08-30 19:41:49 +02:00
|
|
|
virtual std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const = 0;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual const OperationBase& block() const = 0; // update block
|
2017-08-26 22:53:02 +02:00
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
//virtual size_t argNum() const = 0;
|
2017-10-31 19:22:34 +01:00
|
|
|
virtual const Block<T>& get() const = 0;
|
2017-02-24 15:08:42 +01:00
|
|
|
};
|
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-27 17:52:50 +02:00
|
|
|
typedef T value_type;
|
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
MutableOperationBase() = default;
|
2017-08-30 19:41:49 +02:00
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
virtual MBlock<T>& get() = 0;
|
2017-08-30 19:41:49 +02:00
|
|
|
|
2017-03-16 19:30:43 +01:00
|
|
|
};
|
2017-10-31 19:22:34 +01:00
|
|
|
*/
|
2017-08-30 19:41:49 +02:00
|
|
|
template <typename T, class OperationClass>
|
2017-08-11 11:30:27 +02:00
|
|
|
class OperationTemplate
|
|
|
|
{
|
|
|
|
public:
|
2017-08-27 17:52:50 +02:00
|
|
|
|
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
|
2017-08-30 19:41:49 +02:00
|
|
|
-> Operation<T,std::plus<T>,OperationClass,Second>;
|
2017-08-26 17:18:42 +02:00
|
|
|
|
|
|
|
template <class Second>
|
|
|
|
auto operator-(const Second& in) const
|
2017-08-30 19:41:49 +02:00
|
|
|
-> Operation<T,std::minus<T>,OperationClass,Second>;
|
2017-08-26 17:18:42 +02:00
|
|
|
|
|
|
|
template <class Second>
|
|
|
|
auto operator*(const Second& in) const
|
2017-08-30 19:41:49 +02:00
|
|
|
-> Operation<T,std::multiplies<T>,OperationClass,Second>;
|
2017-08-26 17:18:42 +02:00
|
|
|
|
|
|
|
template <class Second>
|
|
|
|
auto operator/(const Second& in) const
|
2017-08-30 19:41:49 +02:00
|
|
|
-> Operation<T,std::divides<T>,OperationClass,Second>;
|
2017-11-02 21:20:31 +01:00
|
|
|
|
|
|
|
template <class IndexType>
|
|
|
|
auto c(std::shared_ptr<IndexType>& ind) const
|
|
|
|
-> Contraction<T,OperationClass,IndexType>;
|
2017-08-26 17:18:42 +02:00
|
|
|
|
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-10-31 19:22:34 +01:00
|
|
|
template <typename T, class OpClass, class... Ranges>
|
|
|
|
class OperationMaster/* : public MutableOperationBase<T>*/
|
2017-02-24 15:08:42 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-08-27 17:52:50 +02:00
|
|
|
typedef T value_type;
|
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-10-31 19:22:34 +01:00
|
|
|
OperationMaster(MutableMultiArrayBase<T,Ranges...>& ma, const OpClass& second,
|
2017-08-11 15:26:10 +02:00
|
|
|
std::shared_ptr<typename CRange::IndexType>& index);
|
2017-10-31 19:22:34 +01:00
|
|
|
|
|
|
|
MBlock<T>& get();
|
|
|
|
const Block<T>& get() const;
|
2017-02-27 17:00:51 +01:00
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const;
|
|
|
|
const OperationMaster& block() const;
|
2017-08-28 18:28:43 +02: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);
|
2017-10-31 19:22:34 +01:00
|
|
|
OpClass 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-10-31 19:22:34 +01:00
|
|
|
mutable std::shared_ptr<MBlock<T> > mBlockPtr;
|
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>
|
2017-10-31 19:22:34 +01:00
|
|
|
class ConstOperationRoot : /*public OperationBase<T>,*/
|
2017-08-30 19:41:49 +02:00
|
|
|
public OperationTemplate<T,ConstOperationRoot<T,Ranges...> >
|
2017-03-16 19:30:43 +01:00
|
|
|
{
|
|
|
|
public:
|
2017-08-27 17:52:50 +02:00
|
|
|
|
|
|
|
typedef T value_type;
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef OperationBase<T> OB;
|
2017-08-30 19:41:49 +02:00
|
|
|
typedef OperationTemplate<T,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
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
const Block<T>& get() const;
|
2017-08-28 18:28:43 +02:00
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const;
|
|
|
|
const ConstOperationRoot& block() const;
|
2017-03-16 19:30:43 +01:00
|
|
|
|
|
|
|
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-10-31 19:22:34 +01:00
|
|
|
mutable std::shared_ptr<Block<T> > mBlockPtr;
|
2017-03-16 19:30:43 +01:00
|
|
|
};
|
2017-09-11 12:54:24 +02:00
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
template <typename T, class... Ranges>
|
2017-10-31 19:22:34 +01:00
|
|
|
class OperationRoot : /*public MutableOperationBase<T>,*/
|
2017-08-30 19:41:49 +02:00
|
|
|
public OperationTemplate<T,OperationRoot<T,Ranges...> >
|
2017-03-26 16:55:52 +02:00
|
|
|
{
|
|
|
|
public:
|
2017-08-27 17:52:50 +02:00
|
|
|
|
|
|
|
typedef T value_type;
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef OperationBase<T> OB;
|
2017-08-30 19:41:49 +02:00
|
|
|
typedef OperationTemplate<T,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
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
template <class OpClass>
|
|
|
|
OperationMaster<T,OpClass,Ranges...> operator=(const OpClass& in);
|
2017-08-11 11:30:27 +02:00
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
const MBlock<T>& get() const;
|
|
|
|
MBlock<T>& get();
|
2017-08-28 18:28:43 +02:00
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const;
|
|
|
|
const OperationRoot& block() const;
|
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-10-31 19:22:34 +01:00
|
|
|
mutable std::shared_ptr<MBlock<T> > mBlockPtr;
|
2017-02-16 11:20:40 +01:00
|
|
|
};
|
2017-09-11 12:54:24 +02:00
|
|
|
|
2017-08-10 15:12:26 +02:00
|
|
|
template <typename T, class OpFunction, class... Ops>
|
2017-10-31 19:22:34 +01:00
|
|
|
class Operation : /*public OperationBase<T>,*/
|
2017-08-30 19:41:49 +02:00
|
|
|
public OperationTemplate<T,Operation<T,OpFunction,Ops...> >
|
2017-03-22 11:44:33 +01:00
|
|
|
{
|
|
|
|
public:
|
2017-08-27 17:52:50 +02:00
|
|
|
|
|
|
|
typedef T value_type;
|
2017-08-10 15:12:26 +02:00
|
|
|
typedef OperationBase<T> OB;
|
2017-08-30 19:41:49 +02:00
|
|
|
typedef OperationTemplate<T,Operation<T,OpFunction,Ops...> > OT;
|
2017-08-10 15:12:26 +02:00
|
|
|
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
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
const BlockResult<T>& get() const;
|
2017-08-28 18:28:43 +02:00
|
|
|
|
2017-10-31 19:22:34 +01:00
|
|
|
std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const;
|
|
|
|
const Operation& block() const;
|
2017-08-28 18:28:43 +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-26 22:53:02 +02:00
|
|
|
mutable BlockResult<T> mRes;
|
2017-03-22 11:44:33 +01:00
|
|
|
};
|
2017-08-10 15:12:26 +02:00
|
|
|
|
2017-11-02 21:20:31 +01:00
|
|
|
template <typename T, class Op, class IndexType>
|
2017-11-01 21:26:45 +01:00
|
|
|
class Contraction : public OperationTemplate<T,Contraction<T,Op> >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef T value_type;
|
2017-11-02 21:20:31 +01:00
|
|
|
typedef OperationTemplate<T,Contraction<T,Op,IndexType> > OT;
|
|
|
|
|
|
|
|
Contraction(const Op& op, std::shared_ptr<IndexType> ind);
|
2017-11-01 21:26:45 +01:00
|
|
|
|
|
|
|
const BlockResult<T>& get() const;
|
|
|
|
|
|
|
|
std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const;
|
|
|
|
const Contraction& block() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
const Op& mOp;
|
2017-11-02 21:20:31 +01:00
|
|
|
std::shared_ptr<IndexType> mInd;
|
2017-11-01 21:26:45 +01:00
|
|
|
mutable BlockResult<T> mRes;
|
|
|
|
};
|
|
|
|
|
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
|