add some range transformation routines

This commit is contained in:
Christian Zimmermann 2017-03-08 20:10:11 +01:00
parent 5f4931757d
commit a6c860a352
14 changed files with 102 additions and 52 deletions

View file

@ -93,6 +93,13 @@ namespace MultiArrayTools
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
class Slice; class Slice;
// manipulator.h
template <typename T>
class ManipulatorBase;
// manipulator.h
template <typename T>
class BinReader;
} }
#endif #endif

View file

@ -91,12 +91,6 @@ namespace MultiArrayTools
} }
} }
size_t IndefinitIndexBase::max() const
{
//assert(not virt());
return static_cast<size_t>( -1 );
}
size_t IndefinitIndexBase::outOfRange() const size_t IndefinitIndexBase::outOfRange() const
{ {
//assert(not virt()); //assert(not virt());

View file

@ -44,9 +44,11 @@ namespace MultiArrayTools
virtual bool linked() const; virtual bool linked() const;
virtual void linkTo(IndefinitIndexBase* target) = 0; virtual void linkTo(IndefinitIndexBase* target) = 0;
virtual IndefinitIndexBase* getLinked(const std::string& name) = 0;
virtual void setPos(size_t pos); virtual void setPos(size_t pos);
virtual size_t max() const; virtual size_t max() const = 0;
virtual size_t outOfRange() const; virtual size_t outOfRange() const;
virtual bool toNull() const; virtual bool toNull() const;

View file

@ -21,7 +21,7 @@ namespace MultiArrayTools
template <typename T> template <typename T>
BinReader<T>::BinReader(const std::string& fname, size_t readBegin): BinReader<T>::BinReader(const std::string& fname, size_t readBegin):
fs(fname, std::fstream::in | std::fstream::binary), mFs(fname, std::fstream::in | std::fstream::binary),
mReadBegin(readBegin) {} mReadBegin(readBegin) {}
template <typename T> template <typename T>
@ -37,18 +37,18 @@ namespace MultiArrayTools
const size_t blockSize = sizeof(T) * readSize; const size_t blockSize = sizeof(T) * readSize;
char* buffer = new char[blockSize]; char* buffer = new char[blockSize];
mFs.seekg(mReadBegin + MB::mLastPos, fs.beg); mFs.seekg(mReadBegin + MB::mLastPos, mFs.beg);
mFs.read(buffer, blockSize); mFs.read(buffer, blockSize);
MB::mLastPos += blockSize; MB::mLastPos += blockSize;
T* content = reinterpret_cast<T*>( buffer ); T* content = reinterpret_cast<T*>( buffer );
if(mTargetPtr->size() < MB::mEnd){ if(MB::mTargetPtr->size() < MB::mEnd){
assert(0); assert(0);
// throw // throw
} }
std::copy(&content[0], &content[readSize], mTargetPtr->begin() + MB::mBegin); std::copy(&content[0], &content[readSize], MB::mTargetPtr->begin() + MB::mBegin);
delete[] buffer; delete[] buffer;
} }

View file

@ -33,7 +33,7 @@ namespace MultiArrayTools
}; };
template <typename T> template <typename T>
class BinReader : public ManipulatorBase class BinReader : public ManipulatorBase<T>
{ {
public: public:
typedef ManipulatorBase<T> MB; typedef ManipulatorBase<T> MB;

View file

@ -71,6 +71,32 @@ namespace MultiArrayTools
} }
} }
template <typename T, class Range>
template <class Range2, class Range3>
MultiArray<T,Range>::MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in) :
MultiArrayBase<T,Range>(merge(in.range(), in[ in.begin() ].range()))
// assert that Range2 has always same extension
{
mCont.clear();
for(auto i = in.begin(); i != in.end(); ++i){
mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end());
}
assert(mCont.size() == MAB::mRange->size());
}
template <typename T, class Range>
template <class Range2, class Range3>
MultiArray<T,Range>& MultiArray<T,Range>::operator=(const MultiArray<MultiArray<T,Range2>,Range3> in)
{
MAB::mRange.reset(new Range(merge(in.range(), in[ in.begin() ].range())));
// assert that Range2 has always same extension
mCont.clear();
for(auto i = in.begin(); i != in.end(); ++i){
mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end());
}
assert(mCont.size() == MAB::mRange->size());
}
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)
{ {
@ -89,12 +115,14 @@ namespace MultiArrayTools
return false; return false;
} }
/*
template <typename T, class Range> template <typename T, class Range>
void MultiArray<T,Range>::manipulate(ManipulatorBase<T>& mb, size_t manBegin, size_t manEnd) void MultiArray<T,Range>::manipulate(ManipulatorBase<T>& mb,
const typename Range::IndexType& manBegin,
const typename Range::IndexType& manEnd)
{ {
//mb.reset(); mb.setup(mCont, manBegin.pos(), manEnd.pos());
mb.setup(mCont, manBegin, manEnd);
mb.execute(); mb.execute();
} }
*/
} }

View file

@ -20,6 +20,8 @@ namespace MultiArrayTools
{ {
public: public:
// iterator ( containing idx of Range )
DEFAULT_MEMBERS(MultiArrayBase); DEFAULT_MEMBERS(MultiArrayBase);
MultiArrayBase(const Range& range); MultiArrayBase(const Range& range);
@ -37,8 +39,6 @@ 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;
@ -56,12 +56,20 @@ namespace MultiArrayTools
MultiArray(const Range& range, const std::vector<T>& vec); MultiArray(const Range& range, const std::vector<T>& vec);
MultiArray(const Range& range, std::vector<T>&& vec); MultiArray(const Range& range, std::vector<T>&& vec);
template <class Range2, class Range3>
MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in);
template <class Range2, class Range3>
MultiArray& operator=(const MultiArray<MultiArray<T,Range2>,Range3> in);
T& operator[](const typename Range::IndexType& i) override; T& operator[](const typename Range::IndexType& i) override;
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; // virtual void manipulate(ManipulatorBase<T>& mb,
// const typename Range::IndexType& manBegin,
// const typename Range::IndexType& manEnd);
template <typename U, class RangeX> template <typename U, class RangeX>
friend class MultiArray; friend class MultiArray;

View file

@ -12,6 +12,8 @@
#include "multi_array_operation.h" #include "multi_array_operation.h"
#include "multi_array.h" #include "multi_array.h"
#include "slice.h" #include "slice.h"
#include "manipulator.h"
#include "range_transformer.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {

View file

@ -598,6 +598,9 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
MultiRange<Ranges...>::MultiRange(const Ranges&... rs) : mSpace(std::make_tuple(rs...)) {} MultiRange<Ranges...>::MultiRange(const Ranges&... rs) : mSpace(std::make_tuple(rs...)) {}
template <class... Ranges>
MultiRange<Ranges...>::MultiRange(const SpaceType& space) : mSpace(space) {}
template <class... Ranges> template <class... Ranges>
template <size_t N> template <size_t N>
auto MultiRange<Ranges...>::getRange() -> decltype( std::get<N>(MultiRange<Ranges...>::SpaceType()) ) auto MultiRange<Ranges...>::getRange() -> decltype( std::get<N>(MultiRange<Ranges...>::SpaceType()) )

View file

@ -104,6 +104,7 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(MultiRange); DEFAULT_MEMBERS(MultiRange);
MultiRange(const Ranges&... rs); MultiRange(const Ranges&... rs);
MultiRange(const SpaceType& space);
static const size_t dim = sizeof...(Ranges); static const size_t dim = sizeof...(Ranges);

View file

@ -5,5 +5,30 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
template <class... Ranges>
MultiRange<Ranges...> makeRange(const std::tuple<Ranges...>& rangeTuple)
{
return MultiRange<Ranges...>( rangeTuple );
}
template <class... Ranges>
MultiRange<Ranges...> combine(const Ranges&... ranges)
{
return MultiRange<Ranges...>(ranges...);
}
template <class Range, class... Ranges>
auto extend(const Range& r, const MultiRange<Ranges...>& mr)
-> decltype( MultiRange<Range,Ranges...>(std::tuple_cat( std::make_tuple(r), mr.space() ) ) )
{
return MultiRange<Range,Ranges...>(std::tuple_cat( std::make_tuple(r), mr.space() ) );
}
template <class MultiRange1, class MultiRange2>
auto merge(const MultiRange1& mr1, const MultiRange2& mr2)
-> decltype( makeRange( std::tuple_cat( mr1.space(), mr2.space() ) ) )
{
return makeRange( std::tuple_cat( mr1.space(), mr2.space() ) );
}
} }

View file

@ -9,10 +9,19 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
// init 'MultiRange' with 'new'
template <class... Ranges> template <class... Ranges>
shared_ptr<MultiRange<Ranges...> > combine(const Ranges&... ranges); MultiRange<Ranges...> makeRange(const std::tuple<Ranges...>& rangeTuple);
template <class... Ranges>
MultiRange<Ranges...> combine(const Ranges&... ranges);
template <class Range, class... Ranges>
auto extend(const Range& r, const MultiRange<Ranges...>& mr)
-> decltype( MultiRange<Range,Ranges...>(std::tuple_cat( std::make_tuple(r), mr.space() ) ) );
template <class MultiRange1, class MultiRange2>
auto merge(const MultiRange1& mr1, const MultiRange2& mr2)
-> decltype(makeRange( std::tuple_cat( mr1.space(), mr2.space() ) ) );
} }
#include "range_transformer.cc" #include "range_transformer.cc"

View file

@ -57,27 +57,4 @@ namespace MultiArrayTools
//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

@ -39,12 +39,6 @@ namespace MultiArrayTools
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;