add manipulator

This commit is contained in:
Christian Zimmermann 2017-03-07 23:14:57 +01:00
parent 6164b58cd0
commit 5f4931757d
6 changed files with 160 additions and 6 deletions

56
src/manipulator.cc Normal file
View file

@ -0,0 +1,56 @@
// -*- C++ -*-
#include "manipulator.h"
namespace MultiArrayTools
{
template <typename T>
void ManipulatorBase<T>::setup(std::vector<T>& targetRef, size_t begin, size_t end)
{
mBegin = begin;
mEnd = end;
mTargetPtr = &targetRef;
}
template <typename T>
void ManipulatorBase<T>::reset()
{
mLastPos = 0;
}
template <typename T>
BinReader<T>::BinReader(const std::string& fname, size_t readBegin):
fs(fname, std::fstream::in | std::fstream::binary),
mReadBegin(readBegin) {}
template <typename T>
BinReader<T>::~BinReader()
{
mFs.close();
}
template <typename T>
void BinReader<T>::execute()
{
const size_t readSize = MB::mEnd - MB::mBegin;
const size_t blockSize = sizeof(T) * readSize;
char* buffer = new char[blockSize];
mFs.seekg(mReadBegin + MB::mLastPos, fs.beg);
mFs.read(buffer, blockSize);
MB::mLastPos += blockSize;
T* content = reinterpret_cast<T*>( buffer );
if(mTargetPtr->size() < MB::mEnd){
assert(0);
// throw
}
std::copy(&content[0], &content[readSize], mTargetPtr->begin() + MB::mBegin);
delete[] buffer;
}
}

56
src/manipulator.h Normal file
View file

@ -0,0 +1,56 @@
// -*- C++ -*-
#ifndef __manipulator_h__
#define __manipulator_h__
#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
#include "base_def.h"
namespace MultiArrayTools
{
// some kind of input stream or sth similar...
template <typename T>
class ManipulatorBase
{
public:
virtual ~ManipulatorBase() {}
virtual void setup(std::vector<T>& targetRef, size_t begin, size_t end);
virtual void execute(size_t pos) = 0;
virtual void reset();
protected:
size_t mLastPos = 0;
size_t mBegin = 0;
size_t mEnd = 0;
std::vector<T>* mTargetPtr = nullptr;
};
template <typename T>
class BinReader : public ManipulatorBase
{
public:
typedef ManipulatorBase<T> MB;
BinReader(const std::string& fname, size_t readBegin = 0);
virtual ~BinReader();
virtual void execute() override;
private:
size_t mReadBegin;
std::fstream mFs;
};
}
#include "manipulator.cc"
#endif

View file

@ -70,7 +70,7 @@ namespace MultiArrayTools
mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end());
} }
} }
template <typename T, class Range> template <typename T, class Range>
T& MultiArray<T,Range>::operator[](const typename Range::IndexType& i) T& MultiArray<T,Range>::operator[](const typename Range::IndexType& i)
{ {
@ -88,5 +88,13 @@ namespace MultiArrayTools
{ {
return false; return false;
} }
template <typename T, class Range>
void MultiArray<T,Range>::manipulate(ManipulatorBase<T>& mb, size_t manBegin, size_t manEnd)
{
//mb.reset();
mb.setup(mCont, manBegin, manEnd);
mb.execute();
}
} }

View file

@ -9,6 +9,7 @@
#include "base_def.h" #include "base_def.h"
#include "multi_array_operation.h" #include "multi_array_operation.h"
#include "manipulator.h"
#include "name.h" #include "name.h"
namespace MultiArrayTools namespace MultiArrayTools
@ -35,6 +36,8 @@ namespace MultiArrayTools
template <class... NameTypes> template <class... NameTypes>
MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str); MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str);
virtual void manipulate(ManipulatorBase<T>& mb, size_t manBegin, size_t manEnd) = 0;
protected: protected:
std::shared_ptr<Range> mRange; std::shared_ptr<Range> mRange;
@ -57,6 +60,11 @@ namespace MultiArrayTools
const T& operator[](const typename Range::IndexType& i) const override; const T& operator[](const typename Range::IndexType& i) const override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
virtual void manipulate(ManipulatorBase<T>& mb, size_t manBegin, size_t manEnd) override;
template <typename U, class RangeX>
friend class MultiArray;
private: private:
std::vector<T> mCont; std::vector<T> mCont;

View file

@ -46,7 +46,7 @@ namespace MultiArrayTools
T& Slice<T,Range,MARange>::operator[](const typename Range::IndexType& i) T& Slice<T,Range,MARange>::operator[](const typename Range::IndexType& i)
{ {
mOwnIdx.copyPos(i); mOwnIdx.copyPos(i);
mOwnIdx = i.pos(); //mOwnIdx = i.pos();
return (*mMultiArrayPtr)[ mMAIdx ]; return (*mMultiArrayPtr)[ mMAIdx ];
} }
@ -54,7 +54,30 @@ namespace MultiArrayTools
const T& Slice<T,Range,MARange>::operator[](const typename Range::IndexType& i) const const T& Slice<T,Range,MARange>::operator[](const typename Range::IndexType& i) const
{ {
mOwnIdx.copyPos(i); mOwnIdx.copyPos(i);
mOwnIdx = i.pos(); //mOwnIdx = i.pos();
return (*mMultiArrayPtr)[ mMAIdx ]; return (*mMultiArrayPtr)[ mMAIdx ];
} }
template <typename T, class Range, class MARange>
void Slice<T,Range,MARange>::manipulate(ManipulatorBase<T>& mb, size_t manBegin, size_t manEnd)
{
mOwnIdx = manBegin;
const size_t blockSize = getBlockSize();
const size_t totalManSize = manEnd - manBegin;
size_t curBegin = mMAIdx.pos();
size_t curBlockNum = (mMAIdx.pos() - mMAIdx.pos() % blockSize) / blockSize;
size_t tempEnd = blockSize - mMAIdx.pos() % blockSize + blockSize * curBlockNum;
size_t manipulated = 0;
for(; manipulated < totalManSize; ){
mMultiArrayPtr->manipulate(mb, curBegin, tempEnd);
// update variables... !!!!
}
}
template <typename T, class Range, class MARange>
size_t Slice<T,Range,MARange>::getBlockSize() const
{
// implement !!!
return 1;
}
} }

View file

@ -38,15 +38,18 @@ namespace MultiArrayTools
const Name& ownNm, const Name& ownNm,
const typename MARange::IndexType& MAIdx, const typename MARange::IndexType& MAIdx,
const Name& MANm); const Name& MANm);
virtual void manipulate(ManipulatorBase<T>& mb, size_t manBegin, size_t manEnd) override;
// size of coherent data blocks (vectorizable) managed by the slice
// = 1 if the smallest index of original array is NOT managed by the slice
size_t getBlockSize() const;
private: private:
MultiArrayBase<T,MARange>* mMultiArrayPtr = nullptr; MultiArrayBase<T,MARange>* mMultiArrayPtr = nullptr;
mutable typename Range::IndexType mOwnIdx; mutable typename Range::IndexType mOwnIdx;
mutable typename MARange::IndexType mMAIdx; mutable typename MARange::IndexType mMAIdx;
//std::shared_ptr<IndefinitIndexBase> mMAPtr; // idx ptr for original MA Range
//std::shared_ptr<IndefinitIndexBase> mOwnPtr; // idx ptr for own Range
//Index mSlicePos;
}; };