cnorxz/src/multi_array_operation.h

229 lines
6.3 KiB
C
Raw Normal View History

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
{
namespace
{
using namespace MultiArrayHelper;
}
2017-08-10 15:12:26 +02:00
/*
* OperationBase
* MutableOperationBase
*
* OperationMaster : MutableOperationBase
*
* OperationTemplate<...>
* ConstOperationRoot : OperationBase, OperationTemplate<...>
* OperationRoot : MutableOperationBase,
* OperationTemplate<...>
*
*/
void seekIndexInst(std::shared_ptr<IndexBase> i, std::vector<std::shared_ptr<IndexBase> >& ivec);
2017-08-30 17:56:38 +02:00
// <block type, step size within actual instance>
typedef std::pair<BlockType,size_t> BTSS;
BTSS getBlockType(std::shared_ptr<IndexBase> i,
std::shared_ptr<IndexBase> j, bool first);
2017-08-30 17:56:38 +02:00
template <typename T>
std::shared_ptr<BlockBase<T> > makeBlock(const std::vector<T>& vec, size_t stepSize, size_t blockSize);
template <typename T>
std::shared_ptr<MutableBlockBase<T> > makeBlock(std::vector<T>& vec, size_t stepSize, size_t blockSize);
size_t getBTNum(const std::vector<BTSS>& mp, BlockType bt);
2017-08-30 17:56:38 +02:00
void minimizeAppearanceOfType(std::map<std::shared_ptr<IndexBase>, std::vector<BTSS> > mp,
BlockType bt);
2017-08-30 17:56:38 +02:00
template <typename T>
std::shared_ptr<IndexBase> seekBlockIndex(std::shared_ptr<IndexBase> ownIdx,
const OperationBase<T>& second);
2017-08-10 15:12:26 +02:00
template <typename T>
2017-08-10 15:12:26 +02:00
class OperationBase
2017-02-16 11:20:40 +01:00
{
public:
typedef T value_type;
2017-08-10 15:12:26 +02:00
OperationBase() = default;
2017-08-11 15:26:10 +02:00
virtual ~OperationBase() = default;
// init block, return resulting type (BLOCK, VALUE, SPLIT)
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-10 15:12:26 +02:00
//virtual size_t argNum() const = 0;
2017-08-27 17:52:50 +02:00
virtual const BlockBase<T>& get() const = 0;
};
2017-05-24 19:01:02 +02:00
template <typename T>
2017-08-10 15:12:26 +02:00
class MutableOperationBase : public OperationBase<T>
{
public:
2017-08-27 17:52:50 +02:00
typedef T value_type;
2017-08-10 15:12:26 +02:00
MutableOperationBase() = default;
virtual MutableBlockBase<T>& get() = 0;
};
2017-08-11 11:30:27 +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
-> Operation<T,std::plus<T>,OperationClass,Second>;
2017-08-26 17:18:42 +02:00
template <class Second>
auto operator-(const Second& in) const
-> Operation<T,std::minus<T>,OperationClass,Second>;
2017-08-26 17:18:42 +02:00
template <class Second>
auto operator*(const Second& in) const
-> Operation<T,std::multiplies<T>,OperationClass,Second>;
2017-08-26 17:18:42 +02:00
template <class Second>
auto operator/(const Second& in) const
-> Operation<T,std::divides<T>,OperationClass,Second>;
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-08-10 15:12:26 +02:00
template <typename T, class... Ranges>
class OperationMaster : public MutableOperationBase<T>
{
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
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-08-30 17:56:38 +02:00
virtual MutableBlockBase<T>& get() override;
2017-08-27 17:52:50 +02:00
virtual const BlockBase<T>& get() const override;
2017-02-27 17:00:51 +01:00
virtual std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const override;
2017-09-09 19:59:09 +02:00
virtual const OperationMaster& block() const override;
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;
MutableMultiArrayBase<T,Ranges...>& mArrayRef;
2017-08-11 11:30:27 +02:00
std::shared_ptr<IndexType> mIndex;
mutable std::shared_ptr<MutableBlockBase<T> > mBlockPtr;
2017-02-16 11:20:40 +01:00
};
2017-08-10 15:12:26 +02:00
2017-08-10 15:12:26 +02:00
template <typename T, class... Ranges>
class ConstOperationRoot : public OperationBase<T>,
public OperationTemplate<T,ConstOperationRoot<T,Ranges...> >
{
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;
typedef OperationTemplate<T,ConstOperationRoot<T,Ranges...> > OT;
2017-08-10 15:12:26 +02:00
typedef ContainerRange<Ranges...> CRange;
typedef typename CRange::IndexType IndexType;
ConstOperationRoot(const MultiArrayBase<T,Ranges...>& ma,
2017-08-10 15:12:26 +02:00
const std::shared_ptr<typename Ranges::IndexType>&... indices);
2017-08-27 17:52:50 +02:00
virtual const BlockBase<T>& get() const override;
virtual std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const override;
2017-09-09 19:59:09 +02:00
virtual const ConstOperationRoot& block() const override;
protected:
2017-08-10 15:12:26 +02:00
MultiArrayBase<T,Ranges...> const& mArrayRef;
2017-08-10 15:12:26 +02:00
std::shared_ptr<IndexType> mIndex;
mutable std::shared_ptr<BlockBase<T> > mBlockPtr;
};
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>,
public OperationTemplate<T,OperationRoot<T,Ranges...> >
{
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;
typedef OperationTemplate<T,OperationRoot<T,Ranges...> > OT;
2017-08-10 15:12:26 +02:00
typedef ContainerRange<Ranges...> CRange;
typedef typename CRange::IndexType IndexType;
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-08-27 17:52:50 +02:00
virtual const BlockBase<T>& get() const override;
2017-08-30 17:56:38 +02:00
virtual MutableBlockBase<T>& get() override;
virtual std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const override;
2017-09-09 19:59:09 +02:00
virtual const OperationRoot& block() const override;
2017-05-24 19:01:02 +02:00
protected:
MutableMultiArrayBase<T,Ranges...>& mArrayRef;
2017-08-10 15:12:26 +02:00
std::shared_ptr<IndexType> mIndex;
mutable std::shared_ptr<MutableBlockBase<T> > mBlockPtr;
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<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;
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-08-27 17:52:50 +02:00
virtual const BlockBase<T>& get() const override;
virtual std::vector<BTSS> block(const std::shared_ptr<IndexBase> blockIndex) const override;
2017-09-09 19:59:09 +02:00
virtual const Operation& block() const override;
2017-03-22 11:44:33 +01:00
protected:
2017-08-10 15:12:26 +02:00
std::tuple<Ops...> mOps;
mutable BlockResult<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
#include "multi_array_operation.cc"
2017-02-16 11:20:40 +01:00
#endif