2017-08-26 22:53:02 +02:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
2017-08-30 19:41:49 +02:00
|
|
|
#ifndef __ma_block_h__
|
|
|
|
#define __ma_block_h__
|
2017-08-26 22:53:02 +02:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base_def.h"
|
|
|
|
|
|
|
|
namespace MultiArrayHelper
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class BlockType {
|
|
|
|
INDEF = 0,
|
|
|
|
BLOCK = 1,
|
|
|
|
VALUE = 2,
|
|
|
|
SPLIT = 3,
|
2017-08-30 19:41:49 +02:00
|
|
|
RESULT = 4
|
2017-08-26 22:53:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// manage vectorization in the future !!
|
2017-09-09 19:59:09 +02:00
|
|
|
|
|
|
|
template <typename T, class OpFunc>
|
|
|
|
class BlockBinaryOp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BlockBinaryOp() = default;
|
|
|
|
BlockResult<T> operator()(const BlockBase<T>& arg1, const BlockBase<T>& arg2);
|
|
|
|
};
|
2017-08-26 22:53:02 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class BlockBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BlockBase() = default;
|
|
|
|
BlockBase(size_t size);
|
|
|
|
|
2017-08-30 19:41:49 +02:00
|
|
|
BlockBase(BlockBase&& res) = default;
|
|
|
|
BlockBase& operator=(BlockBase&& res) = default;
|
|
|
|
|
2017-08-26 22:53:02 +02:00
|
|
|
virtual BlockType type() const = 0;
|
|
|
|
|
|
|
|
virtual size_t size() const;
|
|
|
|
virtual const T& operator[](size_t pos) const = 0;
|
|
|
|
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual BlockBase& set(size_t npos) = 0;
|
2017-08-27 17:52:50 +02:00
|
|
|
|
2017-08-26 22:53:02 +02:00
|
|
|
template <class OpFunction>
|
|
|
|
BlockResult<T> operate(const BlockBase& in);
|
2017-08-30 19:41:49 +02:00
|
|
|
|
2017-08-27 17:52:50 +02:00
|
|
|
BlockResult<T> operator+(const BlockBase& in);
|
|
|
|
BlockResult<T> operator-(const BlockBase& in);
|
|
|
|
BlockResult<T> operator*(const BlockBase& in);
|
|
|
|
BlockResult<T> operator/(const BlockBase& in);
|
2017-08-26 22:53:02 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
size_t mSize;
|
|
|
|
};
|
|
|
|
|
2017-08-30 17:56:38 +02:00
|
|
|
template <typename T>
|
|
|
|
class MutableBlockBase : public BlockBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
MutableBlockBase() = default;
|
|
|
|
MutableBlockBase(size_t size);
|
2017-08-30 19:41:49 +02:00
|
|
|
|
|
|
|
MutableBlockBase& operator=(const BlockBase<T>& in);
|
2017-08-30 17:56:38 +02:00
|
|
|
|
2017-08-30 19:41:49 +02:00
|
|
|
MutableBlockBase(MutableBlockBase&& res) = default;
|
|
|
|
MutableBlockBase& operator=(MutableBlockBase&& res) = default;
|
2017-09-09 19:59:09 +02:00
|
|
|
|
2017-08-30 19:41:49 +02:00
|
|
|
virtual T& operator[](size_t pos) = 0;
|
2017-08-30 17:56:38 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-08-26 22:53:02 +02:00
|
|
|
template <typename T>
|
|
|
|
class Block : public BlockBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Block() = default;
|
|
|
|
Block(const std::vector<T>& data, size_t begPos, size_t size);
|
|
|
|
|
|
|
|
virtual BlockType type() const override;
|
|
|
|
virtual const T& operator[](size_t pos) const override;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual Block& set(size_t npos) override;
|
2017-08-27 17:52:50 +02:00
|
|
|
|
2017-08-26 22:53:02 +02:00
|
|
|
protected:
|
2017-09-09 19:59:09 +02:00
|
|
|
const std::vector<T>* mData;
|
2017-08-27 17:52:50 +02:00
|
|
|
const T* mBegPtr;
|
2017-08-26 22:53:02 +02:00
|
|
|
};
|
|
|
|
|
2017-08-30 17:56:38 +02:00
|
|
|
template <typename T>
|
|
|
|
class MBlock : public MutableBlockBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MBlock() = default;
|
|
|
|
MBlock(std::vector<T>& data, size_t begPos, size_t size);
|
|
|
|
|
|
|
|
virtual BlockType type() const override;
|
|
|
|
virtual const T& operator[](size_t pos) const override;
|
|
|
|
virtual T& operator[](size_t pos) override;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual MBlock& set(size_t npos) override;
|
2017-08-30 17:56:38 +02:00
|
|
|
|
|
|
|
protected:
|
2017-09-09 19:59:09 +02:00
|
|
|
std::vector<T>* mData;
|
2017-08-30 17:56:38 +02:00
|
|
|
T* mBegPtr;
|
|
|
|
};
|
|
|
|
|
2017-08-26 22:53:02 +02:00
|
|
|
template <typename T>
|
|
|
|
class BlockValue : public BlockBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BlockValue() = default;
|
2017-09-09 19:59:09 +02:00
|
|
|
BlockValue(const std::vector<T>& data,
|
|
|
|
size_t pos, size_t size);
|
2017-08-26 22:53:02 +02:00
|
|
|
|
|
|
|
virtual BlockType type() const override;
|
|
|
|
virtual const T& operator[](size_t pos) const override;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual BlockValue& set(size_t npos) override;
|
2017-08-26 22:53:02 +02:00
|
|
|
|
|
|
|
protected:
|
2017-09-09 19:59:09 +02:00
|
|
|
const std::vector<T>* mData;
|
|
|
|
T& mVal;
|
2017-08-26 22:53:02 +02:00
|
|
|
};
|
|
|
|
|
2017-08-30 17:56:38 +02:00
|
|
|
template <typename T>
|
|
|
|
class MBlockValue : public MutableBlockBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MBlockValue() = default;
|
2017-09-09 19:59:09 +02:00
|
|
|
MBlockValue(std::vector<T>& data,
|
|
|
|
size_t pos, size_t size);
|
2017-08-30 17:56:38 +02:00
|
|
|
|
|
|
|
virtual BlockType type() const override;
|
|
|
|
virtual const T& operator[](size_t pos) const override;
|
|
|
|
virtual T& operator[](size_t pos) override;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual MBlockValue& set(size_t npos) override;
|
2017-08-30 17:56:38 +02:00
|
|
|
|
|
|
|
protected:
|
2017-09-09 19:59:09 +02:00
|
|
|
std::vector<T>* mData;
|
2017-08-30 17:56:38 +02:00
|
|
|
T& mVal;
|
|
|
|
};
|
|
|
|
|
2017-08-26 22:53:02 +02:00
|
|
|
template <typename T>
|
|
|
|
class SplitBlock : public BlockBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SplitBlock() = default;
|
2017-08-27 17:52:50 +02:00
|
|
|
SplitBlock(const std::vector<T>& data, size_t begPos,
|
|
|
|
size_t stepSize, size_t size);
|
2017-08-26 22:53:02 +02:00
|
|
|
|
|
|
|
virtual BlockType type() const override;
|
|
|
|
virtual const T& operator[](size_t pos) const override;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual SplitBlock& set(size_t npos) override;
|
2017-08-26 22:53:02 +02:00
|
|
|
|
|
|
|
protected:
|
2017-09-09 19:59:09 +02:00
|
|
|
const std::vector<T>* mData;
|
2017-08-27 17:52:50 +02:00
|
|
|
size_t mStepSize;
|
|
|
|
const T* mBegPtr;
|
2017-08-26 22:53:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2017-08-30 17:56:38 +02:00
|
|
|
class MSplitBlock : public MutableBlockBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
MSplitBlock() = default;
|
|
|
|
MSplitBlock(std::vector<T>& data, size_t begPos,
|
|
|
|
size_t stepSize, size_t size);
|
|
|
|
|
|
|
|
virtual BlockType type() const override;
|
|
|
|
virtual const T& operator[](size_t pos) const override;
|
|
|
|
virtual T& operator[](size_t pos) override;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual MSplitBlock& set(size_t npos) override;
|
2017-08-30 17:56:38 +02:00
|
|
|
|
|
|
|
protected:
|
2017-09-09 19:59:09 +02:00
|
|
|
std::vector<T>* mData;
|
2017-08-30 17:56:38 +02:00
|
|
|
size_t mStepSize;
|
|
|
|
T* mBegPtr;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class BlockResult : public MutableBlockBase<T>
|
2017-08-26 22:53:02 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
BlockResult() = default;
|
|
|
|
BlockResult(size_t size);
|
|
|
|
|
2017-08-30 19:41:49 +02:00
|
|
|
BlockResult(BlockResult&& res) = default;
|
|
|
|
BlockResult& operator=(BlockResult&& res) = default;
|
|
|
|
|
2017-08-26 22:53:02 +02:00
|
|
|
virtual BlockType type() const override;
|
|
|
|
virtual const T& operator[](size_t pos) const override;
|
2017-08-30 17:56:38 +02:00
|
|
|
virtual T& operator[](size_t i) override;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual BlockResult& set(size_t npos) override;
|
2017-08-26 22:53:02 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
std::vector<T> mRes;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace MultiArrayHelper
|
|
|
|
|
|
|
|
#include "block.cc"
|
|
|
|
|
|
|
|
#endif
|