diff --git a/src/manipulator.cc b/src/manipulator.cc new file mode 100644 index 0000000..7ae929d --- /dev/null +++ b/src/manipulator.cc @@ -0,0 +1,56 @@ +// -*- C++ -*- + +#include "manipulator.h" + +namespace MultiArrayTools +{ + + template + void ManipulatorBase::setup(std::vector& targetRef, size_t begin, size_t end) + { + mBegin = begin; + mEnd = end; + mTargetPtr = &targetRef; + } + + template + void ManipulatorBase::reset() + { + mLastPos = 0; + } + + template + BinReader::BinReader(const std::string& fname, size_t readBegin): + fs(fname, std::fstream::in | std::fstream::binary), + mReadBegin(readBegin) {} + + template + BinReader::~BinReader() + { + mFs.close(); + } + + template + void BinReader::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( buffer ); + + if(mTargetPtr->size() < MB::mEnd){ + assert(0); + // throw + } + + std::copy(&content[0], &content[readSize], mTargetPtr->begin() + MB::mBegin); + + delete[] buffer; + } + +} diff --git a/src/manipulator.h b/src/manipulator.h new file mode 100644 index 0000000..1702998 --- /dev/null +++ b/src/manipulator.h @@ -0,0 +1,56 @@ +// -*- C++ -*- + +#ifndef __manipulator_h__ +#define __manipulator_h__ + +#include +#include +#include +#include + +#include "base_def.h" + +namespace MultiArrayTools +{ + + // some kind of input stream or sth similar... + template + class ManipulatorBase + { + public: + virtual ~ManipulatorBase() {} + + virtual void setup(std::vector& 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* mTargetPtr = nullptr; + }; + + template + class BinReader : public ManipulatorBase + { + public: + typedef ManipulatorBase 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 diff --git a/src/multi_array.cc b/src/multi_array.cc index cfcbac4..a42835d 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -70,7 +70,7 @@ namespace MultiArrayTools mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); } } - + template T& MultiArray::operator[](const typename Range::IndexType& i) { @@ -88,5 +88,13 @@ namespace MultiArrayTools { return false; } + + template + void MultiArray::manipulate(ManipulatorBase& mb, size_t manBegin, size_t manEnd) + { + //mb.reset(); + mb.setup(mCont, manBegin, manEnd); + mb.execute(); + } } diff --git a/src/multi_array.h b/src/multi_array.h index f630bd5..bd869ba 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -9,6 +9,7 @@ #include "base_def.h" #include "multi_array_operation.h" +#include "manipulator.h" #include "name.h" namespace MultiArrayTools @@ -35,6 +36,8 @@ namespace MultiArrayTools template MultiArrayOperationRoot operator()(const NameTypes&... str); + + virtual void manipulate(ManipulatorBase& mb, size_t manBegin, size_t manEnd) = 0; protected: std::shared_ptr mRange; @@ -57,6 +60,11 @@ namespace MultiArrayTools const T& operator[](const typename Range::IndexType& i) const override; virtual bool isSlice() const override; + + virtual void manipulate(ManipulatorBase& mb, size_t manBegin, size_t manEnd) override; + + template + friend class MultiArray; private: std::vector mCont; diff --git a/src/slice.cc b/src/slice.cc index da31e0e..cd30cbf 100644 --- a/src/slice.cc +++ b/src/slice.cc @@ -46,7 +46,7 @@ namespace MultiArrayTools T& Slice::operator[](const typename Range::IndexType& i) { mOwnIdx.copyPos(i); - mOwnIdx = i.pos(); + //mOwnIdx = i.pos(); return (*mMultiArrayPtr)[ mMAIdx ]; } @@ -54,7 +54,30 @@ namespace MultiArrayTools const T& Slice::operator[](const typename Range::IndexType& i) const { mOwnIdx.copyPos(i); - mOwnIdx = i.pos(); + //mOwnIdx = i.pos(); return (*mMultiArrayPtr)[ mMAIdx ]; } + + template + void Slice::manipulate(ManipulatorBase& 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 + size_t Slice::getBlockSize() const + { + // implement !!! + return 1; + } } diff --git a/src/slice.h b/src/slice.h index 58ced47..c7c290f 100644 --- a/src/slice.h +++ b/src/slice.h @@ -38,15 +38,18 @@ namespace MultiArrayTools const Name& ownNm, const typename MARange::IndexType& MAIdx, const Name& MANm); + + virtual void manipulate(ManipulatorBase& 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: MultiArrayBase* mMultiArrayPtr = nullptr; mutable typename Range::IndexType mOwnIdx; mutable typename MARange::IndexType mMAIdx; - //std::shared_ptr mMAPtr; // idx ptr for original MA Range - //std::shared_ptr mOwnPtr; // idx ptr for own Range - //Index mSlicePos; };