start implementing class Slice

This commit is contained in:
Christian Zimmermann 2017-02-23 19:33:46 +01:00
parent db4ad28f02
commit 6eb53111bb
7 changed files with 122 additions and 20 deletions

View file

@ -80,6 +80,11 @@ namespace MultiArrayTools
// multi_array_operation.h // multi_array_operation.h
template <typename T, class Range, class Operation, class... Ranges> template <typename T, class Range, class Operation, class... Ranges>
class MultiArrayOperation; class MultiArrayOperation;
// slice.h
template <typename T, class Range, class MARange, class Index>
class Slice;
} }
#endif #endif

View file

@ -69,5 +69,10 @@ namespace MultiArrayTools
return mRange->end(); return mRange->end();
} }
template <typename T, class Range>
bool MultiArray<T,Range>::isSlice() const
{
return false;
}
} }

View file

@ -33,12 +33,17 @@ namespace MultiArrayTools
size_t size() const; size_t size() const;
virtual bool isSlice() const;
auto begin() -> decltype(Range().begin()); auto begin() -> decltype(Range().begin());
auto end() -> decltype(Range().end()); auto end() -> decltype(Range().end());
protected:
std::shared_ptr<Range> mRange;
private: private:
bool mInit = false; bool mInit = false;
std::shared_ptr<Range> mRange;
std::vector<T> mCont; std::vector<T> mCont;
}; };

View file

@ -11,10 +11,12 @@ namespace MultiArrayTools
MultiArrayOperationBase<T,Range>:: MultiArrayOperationBase<T,Range>::
MultiArrayOperationBase(MultiArray<T,Range>& ma, MultiArrayOperationBase(MultiArray<T,Range>& ma,
const Name& nm) : mArrayRef(ma), const Name& nm) : mArrayRef(ma),
//mIndex(mArrayRef.begin()),
mIibPtr(new IndexType(mArrayRef.begin())), mIibPtr(new IndexType(mArrayRef.begin())),
mNm(nm) mNm(nm)
{ {
mIibPtr->name(nm); mIibPtr->name(nm);
//mIndex.name(nm);
} }
template <typename T, class Range> template <typename T, class Range>
@ -43,7 +45,12 @@ namespace MultiArrayTools
MultiArrayOperationBase<T,Range>::operator=(const MultiArrayOperationBase<T, Range2>& in) MultiArrayOperationBase<T,Range>::operator=(const MultiArrayOperationBase<T, Range2>& in)
{ {
in.linkIndicesTo(mIibPtr); in.linkIndicesTo(mIibPtr);
for(*mIibPtr = mArrayRef.begin(); *mIibPtr != mArrayRef.end(); ++(*mIibPtr)){ IndexType& iref = dynamic_cast<IndexType&>(*mIibPtr);
if(mArrayRef.isSlice()){
linkSlice(&in.index(), mIibPtr);
return *this;
}
for(iref = mArrayRef.begin().pos(); iref != mArrayRef.end(); ++iref){
// build in vectorization later // build in vectorization later
get() = in.get(); get() = in.get();
} }
@ -121,26 +128,16 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
T& MultiArrayOperationBase<T,Range>::get() T& MultiArrayOperationBase<T,Range>::get()
{ {
//return mArrayRef[mIndex];
return mArrayRef[*dynamic_cast<IndexType*>(mIibPtr)]; return mArrayRef[*dynamic_cast<IndexType*>(mIibPtr)];
} }
template <typename T, class Range> template <typename T, class Range>
const T& MultiArrayOperationBase<T,Range>::get() const const T& MultiArrayOperationBase<T,Range>::get() const
{ {
//return mArrayRef[mIndex];
return mArrayRef[*dynamic_cast<IndexType*>(mIibPtr)]; return mArrayRef[*dynamic_cast<IndexType*>(mIibPtr)];
} }
template <typename T, class Range>
T& MultiArrayOperationBase<T,Range>::get(IndefinitIndexBase* iibPtr)
{
return mArrayRef[*dynamic_cast<IndexType*>(iibPtr)];
}
template <typename T, class Range>
const T& MultiArrayOperationBase<T,Range>::get(IndefinitIndexBase* iibPtr) const
{
return mArrayRef[*dynamic_cast<IndexType*>(iibPtr)];
}
/***************************** /*****************************
* MultiArrayOperation * * MultiArrayOperation *
@ -197,7 +194,7 @@ namespace MultiArrayTools
template <typename T, class Operation, class Tuple, class... MBases> template <typename T, class Operation, class Tuple, class... MBases>
static auto callOperation(const Operation& op, const Tuple& tp, const T& first, const MBases&... secs) static auto callOperation(const Operation& op, const Tuple& tp, const T& first, const MBases&... secs)
-> decltype(op(first.get(), std::get<0>(tp).get(), secs.get()...)) -> decltype(op(first, std::get<0>(tp).get(), secs.get()...))
{ {
return op(first, std::get<0>(tp).get(), secs.get()...); return op(first, std::get<0>(tp).get(), secs.get()...);
} }
@ -239,6 +236,7 @@ namespace MultiArrayTools
{ {
mVal = OperationCall<sizeof...(Ranges)-1>:: mVal = OperationCall<sizeof...(Ranges)-1>::
template callOperation(mOp, mSecs, template callOperation(mOp, mSecs,
//OB::mArrayRef[OB::mIndex]);
OB::mArrayRef[*dynamic_cast<typename OB::IndexType*>(OB::mIibPtr)]); OB::mArrayRef[*dynamic_cast<typename OB::IndexType*>(OB::mIibPtr)]);
return mVal; return mVal;
} }
@ -248,6 +246,7 @@ namespace MultiArrayTools
{ {
mVal = OperationCall<sizeof...(Ranges)-1>:: mVal = OperationCall<sizeof...(Ranges)-1>::
template callOperation(mOp, mSecs, template callOperation(mOp, mSecs,
//OB::mArrayRef[OB::mIndex]);
OB::mArrayRef[*dynamic_cast<typename OB::IndexType*>(OB::mIibPtr)]); OB::mArrayRef[*dynamic_cast<typename OB::IndexType*>(OB::mIibPtr)]);
return mVal; return mVal;
} }

View file

@ -53,6 +53,7 @@ namespace MultiArrayTools
virtual size_t argNum() const; virtual size_t argNum() const;
//IndexType& index() ;
IndefinitIndexBase* index(); IndefinitIndexBase* index();
virtual void linkIndicesTo(IndefinitIndexBase* target) const; virtual void linkIndicesTo(IndefinitIndexBase* target) const;
@ -60,13 +61,11 @@ namespace MultiArrayTools
virtual T& get(); virtual T& get();
virtual const T& get() const; virtual const T& get() const;
virtual T& get(IndefinitIndexBase* iibPtr);
virtual const T& get(IndefinitIndexBase* iibPtr) const;
protected: protected:
MultiArray<T,Range>& mArrayRef; MultiArray<T,Range>& mArrayRef;
IndefinitIndexBase mutable* mIibPtr = nullptr; //mutable IndexType mIndex;
IndefinitIndexBase* mIibPtr = nullptr;
Name mNm; Name mNm;
}; };

35
src/slice.cc Normal file
View file

@ -0,0 +1,35 @@
// -*- C++ -*-
#include "slice.h"
namespace MultiArrayTools
{
template <typename T, class Range, class MARange, class Index>
Slice<T,Range,MARange,Index>::
Slice(const Range& range, const Index& slicePos) :
MA::mRange(new Range(range)),
mSlicePos(slicePos) {}
template <typename T, class Range, class MARange, class Index>
Slice<T,Range,MARange,Index>& Slice<T,Range,MARange,Index>::setSlicePos(const Index& slicePos)
{
mSlicePos = slicePos;
mMAPtr->linkTo(&mSlicePos);
return *this;
}
template <typename T, class Range, class MARange, class Index>
bool Slice<T,Range,MARange,Index>::isSlice() const
{
return true;
}
template <typename T, class Range, class MARange, class Index>
void Slice<T,Range,MARange,Index>::setPtr(IndefinitIndexBase* MAPtr,
IndefinitIndexBase* ownPtr)
{
mMAPtr.reset(new typename MARange::IndexType(MAPtr));
mOwnPtr.reset(new typename Range::IndexType(ownPtr));
}
}

54
src/slice.h Normal file
View file

@ -0,0 +1,54 @@
// -*- C++ -*-
#ifndef __slice_h__
#define __slice_h__
#include <cstdlib>
#include "base_def.h"
#include "multi_array.h"
namespace MultiArrayTools
{
// T = data type
// Range = range of slice
// MARange = original range of multi array of which this is the slice
// Index = index which determines the slice position (remnant of MARange w.o. Range)
template <typename T, class Range, class MARange, class Index>
class Slice : public MultiArray<T,Range> // yes, Range is correct !!!
{
//MA::mCont has to be empty; only make use of the provided functions
public:
typedef MultiArray<T,MARange> MA;
Slice(const Range& range, const Index& slicePos);
T& operator[](const typename Range::IndexType& i) override;
const T& operator[](const typename Range::IndexType& i) const override;
Slice& setSlicePos(const Index& slicePos);
// link given Index to mMAPtr which is index of total array
auto begin() override -> decltype(Range().begin());
auto end() override -> decltype(Range().end());
virtual bool isSlice() const override;
void setPtr(IndefinitIndexBase* MAPtr,
IndefinitIndexBase* ownPtr);
private:
MultiArray<T,MARange>& multiArrayRef;
IndefinitIndexBase* mMAPtr; // idx ptr for original MA Range
IndefinitIndexBase* mOwnPtr; // idx ptr for own Range
Index mSlicePos;
};
}
#include "slice.cc"
#endif