introduce iterators; ATTENTION: MA::begin -> MA::beginIndex and same for MA::end

This commit is contained in:
Christian Zimmermann 2017-03-20 18:53:30 +01:00
parent cd7c5c6c7b
commit 7f6762f159
8 changed files with 530 additions and 140 deletions

View file

@ -81,6 +81,12 @@ namespace MultiArrayTools
return mLinked != nullptr; return mLinked != nullptr;
} }
bool IndefinitIndexBase::linkedTo(IndefinitIndexBase* link) const
{
//assert(not virt());
return mLinked == link or mSoftLinked == link;
}
void IndefinitIndexBase::setPos(size_t pos) void IndefinitIndexBase::setPos(size_t pos)
{ {
//CHECK; //CHECK;

View file

@ -42,6 +42,7 @@ namespace MultiArrayTools
virtual bool link(IndefinitIndexBase* toLink); virtual bool link(IndefinitIndexBase* toLink);
virtual void freeLinked(); virtual void freeLinked();
virtual bool linked() const; virtual bool linked() const;
virtual bool linkedTo(IndefinitIndexBase* link) const;
virtual void linkTo(IndefinitIndexBase* target) = 0; virtual void linkTo(IndefinitIndexBase* target) = 0;
virtual IndefinitIndexBase* getLinked(const std::string& name) = 0; virtual IndefinitIndexBase* getLinked(const std::string& name) = 0;

View file

@ -4,6 +4,153 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
/**************************************
* MultiArrayBase::const_iterator *
**************************************/
template <typename T, class Range>
MultiArrayBase<T,Range>::const_iterator::const_iterator(const MultiArrayBase<T,Range>& ma):
mMAPtr(&ma), mIndex(mMAPtr->beginIndex())
{ }
template <typename T, class Range>
MultiArrayBase<T,Range>::const_iterator::const_iterator(const MultiArrayBase<T,Range>& ma,
const typename Range::IndexType& index):
mMAPtr(&ma), mIndex(index)
{ }
template <typename T, class Range>
bool MultiArrayBase<T,Range>::const_iterator::operator==(const const_iterator& it) const
{
return mMAPtr == it.mMAPtr and mIndex.pos() == it.mIndex.pos();
}
template <typename T, class Range>
bool MultiArrayBase<T,Range>::const_iterator::operator!=(const const_iterator& it) const
{
return mMAPtr != it.mMAPtr or mIndex.pos() != it.mIndex.pos();
}
template <typename T, class Range>
const T& MultiArrayBase<T,Range>::const_iterator::operator*() const
{
return (*mMAPtr)[mIndex];
}
template <typename T, class Range>
T const* MultiArrayBase<T,Range>::const_iterator::operator->() const
{
return &(*mMAPtr)[mIndex];
}
template <typename T, class Range>
typename MultiArrayBase<T,Range>::const_iterator& MultiArrayBase<T,Range>::const_iterator::operator++()
{
++mIndex;
return *this;
}
template <typename T, class Range>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::const_iterator::operator++(int)
{
const_iterator tmp(*this);
++mIndex;
return tmp;
}
template <typename T, class Range>
typename MultiArrayBase<T,Range>::const_iterator& MultiArrayBase<T,Range>::const_iterator::operator--()
{
--mIndex;
return *this;
}
template <typename T, class Range>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::const_iterator::operator--(int)
{
const_iterator tmp(*this);
--mIndex;
return tmp;
}
template <typename T, class Range>
typename MultiArrayBase<T,Range>::const_iterator& MultiArrayBase<T,Range>::const_iterator::operator+=(int diff)
{
mIndex += diff;
return *this;
}
template <typename T, class Range>
typename MultiArrayBase<T,Range>::const_iterator& MultiArrayBase<T,Range>::const_iterator::operator-=(int diff)
{
mIndex -= diff;
return *this;
}
template <typename T, class Range>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::const_iterator::operator+(int num) const
{
const_iterator tmp(*this);
tmp += num;
return tmp;
}
template <typename T, class Range>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::const_iterator::operator-(int num) const
{
const_iterator tmp(*this);
tmp -= num;
}
template <typename T, class Range>
int MultiArrayBase<T,Range>::const_iterator::operator-(const const_iterator& it) const
{
return mIndex.pos() - it.mIndex.pos();
}
template <typename T, class Range>
const T& MultiArrayBase<T,Range>::const_iterator::operator[](int num) const
{
return *(operator+(num));
}
template <typename T, class Range>
bool MultiArrayBase<T,Range>::const_iterator::operator<(const const_iterator& it) const
{
return mIndex.pos() < it.mIndex.pos();
}
template <typename T, class Range>
bool MultiArrayBase<T,Range>::const_iterator::operator>(const const_iterator& it) const
{
return mIndex.pos() > it.mIndex.pos();
}
template <typename T, class Range>
bool MultiArrayBase<T,Range>::const_iterator::operator<=(const const_iterator& it) const
{
return mIndex.pos() <= it.mIndex.pos();
}
template <typename T, class Range>
bool MultiArrayBase<T,Range>::const_iterator::operator>=(const const_iterator& it) const
{
return mIndex.pos() >= it.mIndex.pos();
}
template <typename T, class Range>
const typename Range::IndexType& MultiArrayBase<T,Range>::const_iterator::index() const
{
return mIndex;
}
template <typename T, class Range>
typename Range::IndexType& MultiArrayBase<T,Range>::const_iterator::index()
{
return mIndex;
}
/********************** /**********************
* MultiArrayBase * * MultiArrayBase *
**********************/ **********************/
@ -11,6 +158,10 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
MultiArrayBase<T,Range>::MultiArrayBase(const Range& range) : mRange(new Range(range)) {} MultiArrayBase<T,Range>::MultiArrayBase(const Range& range) : mRange(new Range(range)) {}
template <typename T, class Range>
void MultiArrayBase<T,Range>::link(IndefinitIndexBase* iibPtr) const
{ }
template <typename T, class Range> template <typename T, class Range>
size_t MultiArrayBase<T,Range>::size() const size_t MultiArrayBase<T,Range>::size() const
{ {
@ -18,16 +169,25 @@ namespace MultiArrayTools
} }
template <typename T, class Range> template <typename T, class Range>
auto MultiArrayBase<T,Range>::begin() const -> decltype(Range().begin()) typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::begin() const
{ {
//VCHECK(not mRange); return const_iterator(*this, beginIndex());
auto i = mRange->begin();
//CHECK;
return i;
} }
template <typename T, class Range> template <typename T, class Range>
auto MultiArrayBase<T,Range>::end() const -> decltype(Range().end()) typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::end() const
{
return const_iterator(*this, endIndex());
}
template <typename T, class Range>
auto MultiArrayBase<T,Range>::beginIndex() const -> decltype(Range().begin())
{
return mRange->begin();
}
template <typename T, class Range>
auto MultiArrayBase<T,Range>::endIndex() const -> decltype(Range().end())
{ {
return mRange->end(); return mRange->end();
} }
@ -70,6 +230,170 @@ namespace MultiArrayTools
return mInit; return mInit;
} }
/****************************************
* MutableMultiArrayBase::iterator *
****************************************/
template <typename T, class Range>
MutableMultiArrayBase<T,Range>::iterator::iterator(MutableMultiArrayBase<T,Range>& ma):
mMAPtr(&ma), mIndex(mMAPtr->beginIndex())
{ }
template <typename T, class Range>
MutableMultiArrayBase<T,Range>::iterator::iterator(MutableMultiArrayBase<T,Range>& ma,
const typename Range::IndexType& index):
mMAPtr(&ma), mIndex(index)
{ }
template <typename T, class Range>
bool MutableMultiArrayBase<T,Range>::iterator::operator==(const iterator& it) const
{
return mMAPtr == it.mMAPtr and mIndex.pos() == it.mIndex.pos();
}
template <typename T, class Range>
bool MutableMultiArrayBase<T,Range>::iterator::operator!=(const iterator& it) const
{
return mMAPtr != it.mMAPtr or mIndex.pos() != it.mIndex.pos();
}
template <typename T, class Range>
const T& MutableMultiArrayBase<T,Range>::iterator::operator*() const
{
return (*mMAPtr)[mIndex];
}
template <typename T, class Range>
T const* MutableMultiArrayBase<T,Range>::iterator::operator->() const
{
return &(*mMAPtr)[mIndex];
}
template <typename T, class Range>
T& MutableMultiArrayBase<T,Range>::iterator::operator*()
{
return (*mMAPtr)[mIndex];
}
template <typename T, class Range>
T* MutableMultiArrayBase<T,Range>::iterator::operator->()
{
return &(*mMAPtr)[mIndex];
}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator& MutableMultiArrayBase<T,Range>::iterator::operator++()
{
++mIndex;
return *this;
}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::iterator::operator++(int)
{
iterator tmp(*this);
++mIndex;
return tmp;
}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator& MutableMultiArrayBase<T,Range>::iterator::operator--()
{
--mIndex;
return *this;
}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::iterator::operator--(int)
{
iterator tmp(*this);
--mIndex;
return tmp;
}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator& MutableMultiArrayBase<T,Range>::iterator::operator+=(int diff)
{
mIndex += diff;
return *this;
}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator& MutableMultiArrayBase<T,Range>::iterator::operator-=(int diff)
{
mIndex -= diff;
return *this;
}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::iterator::operator+(int num) const
{
iterator tmp(*this);
tmp += num;
return tmp;
}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::iterator::operator-(int num) const
{
iterator tmp(*this);
tmp -= num;
}
template <typename T, class Range>
int MutableMultiArrayBase<T,Range>::iterator::operator-(const iterator& it) const
{
return mIndex.pos() - it.mIndex.pos();
}
template <typename T, class Range>
const T& MutableMultiArrayBase<T,Range>::iterator::operator[](int num) const
{
return *(operator+(num));
}
template <typename T, class Range>
T& MutableMultiArrayBase<T,Range>::iterator::operator[](int num)
{
return *(operator+(num));
}
template <typename T, class Range>
bool MutableMultiArrayBase<T,Range>::iterator::operator<(const iterator& it) const
{
return mIndex.pos() < it.mIndex.pos();
}
template <typename T, class Range>
bool MutableMultiArrayBase<T,Range>::iterator::operator>(const iterator& it) const
{
return mIndex.pos() > it.mIndex.pos();
}
template <typename T, class Range>
bool MutableMultiArrayBase<T,Range>::iterator::operator<=(const iterator& it) const
{
return mIndex.pos() <= it.mIndex.pos();
}
template <typename T, class Range>
bool MutableMultiArrayBase<T,Range>::iterator::operator>=(const iterator& it) const
{
return mIndex.pos() >= it.mIndex.pos();
}
template <typename T, class Range>
const typename Range::IndexType& MutableMultiArrayBase<T,Range>::iterator::index() const
{
return mIndex;
}
template <typename T, class Range>
typename Range::IndexType& MutableMultiArrayBase<T,Range>::iterator::index()
{
return mIndex;
}
/****************************** /******************************
* MutableMultiArrayBase * * MutableMultiArrayBase *
******************************/ ******************************/
@ -77,6 +401,18 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
MutableMultiArrayBase<T,Range>::MutableMultiArrayBase(const Range& range) : MultiArrayBase<T,Range>(range) {} MutableMultiArrayBase<T,Range>::MutableMultiArrayBase(const Range& range) : MultiArrayBase<T,Range>(range) {}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::begin()
{
return iterator(*this, MAB::beginIndex());
}
template <typename T, class Range>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::end()
{
return iterator(*this, MAB::endIndex());
}
template <typename T, class Range> template <typename T, class Range>
bool MutableMultiArrayBase<T,Range>::isConst() const bool MutableMultiArrayBase<T,Range>::isConst() const
{ {
@ -141,12 +477,12 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
template <class Range2, class Range3> template <class Range2, class Range3>
MultiArray<T,Range>::MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in) : MultiArray<T,Range>::MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in) :
MutableMultiArrayBase<T,Range>(merge(in.range(), in[ in.begin() ].range())) MutableMultiArrayBase<T,Range>(merge(in.range(), in[ in.beginIndex() ].range()))
// assert that Range2 has always same extension // assert that Range2 has always same extension
{ {
MAB::mInit = true; MAB::mInit = true;
mCont.clear(); mCont.clear();
for(auto i = in.begin(); i != in.end(); ++i){ for(auto i = in.beginIndex(); i != in.endIndex(); ++i){
mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end()); mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end());
} }
assert(mCont.size() == MAB::mRange->size()); assert(mCont.size() == MAB::mRange->size());
@ -156,10 +492,10 @@ namespace MultiArrayTools
template <class Range2, class Range3> template <class Range2, class Range3>
MultiArray<T,Range>& MultiArray<T,Range>::operator=(const MultiArray<MultiArray<T,Range2>,Range3> in) 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()))); MAB::mRange.reset(new Range(merge(in.range(), in[ in.beginIndex() ].range())));
// assert that Range2 has always same extension // assert that Range2 has always same extension
mCont.clear(); mCont.clear();
for(auto i = in.begin(); i != in.end(); ++i){ for(auto i = in.beginIndex(); i != in.endIndex(); ++i){
mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end()); mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end());
} }
assert(mCont.size() == MAB::mRange->size()); assert(mCont.size() == MAB::mRange->size());

View file

@ -6,6 +6,8 @@
#include <cstdlib> #include <cstdlib>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <iterator>
#include <algorithm>
#include "base_def.h" #include "base_def.h"
#include "multi_array_operation.h" #include "multi_array_operation.h"
@ -20,20 +22,70 @@ namespace MultiArrayTools
{ {
public: public:
// iterator ( containing idx of Range ) class const_iterator : public std::iterator<std::random_access_iterator_tag,T>
{
public:
DEFAULT_MEMBERS(const_iterator);
const_iterator(const MultiArrayBase& ma);
const_iterator(const MultiArrayBase& ma, const typename Range::IndexType& index);
virtual ~const_iterator() = default;
// Requirements:
bool operator==(const const_iterator& it) const;
bool operator!=(const const_iterator& it) const;
const T& operator*() const;
T const* operator->() const;
const_iterator& operator++();
const_iterator operator++(int);
const_iterator& operator--();
const_iterator operator--(int);
const_iterator& operator+=(int diff);
const_iterator& operator-=(int diff);
const_iterator operator+(int num) const;
const_iterator operator-(int num) const;
int operator-(const const_iterator& it) const;
const T& operator[](int num) const;
bool operator<(const const_iterator& it) const;
bool operator>(const const_iterator& it) const;
bool operator<=(const const_iterator& it) const;
bool operator>=(const const_iterator& it) const;
// Multi Array specific:
const typename Range::IndexType& index() const;
typename Range::IndexType& index();
protected:
MultiArrayBase const* mMAPtr = nullptr;
typename Range::IndexType mIndex;
};
DEFAULT_MEMBERS(MultiArrayBase); DEFAULT_MEMBERS(MultiArrayBase);
MultiArrayBase(const Range& range); MultiArrayBase(const Range& range);
virtual ~MultiArrayBase() = default; virtual ~MultiArrayBase() = default;
// only relevant for slices... has no effect for usual multiarrays
virtual void link(IndefinitIndexBase* iibPtr) const;
virtual const T& operator[](const typename Range::IndexType& i) const = 0; virtual const T& operator[](const typename Range::IndexType& i) const = 0;
virtual size_t size() const; virtual size_t size() const;
virtual bool isSlice() const = 0; virtual bool isSlice() const = 0;
virtual auto begin() const -> decltype(Range().begin()); virtual const_iterator begin() const;
virtual auto end() const -> decltype(Range().end()); virtual const_iterator end() const;
virtual auto beginIndex() const -> decltype(Range().begin());
virtual auto endIndex() const -> decltype(Range().end());
virtual const Range& range() const; virtual const Range& range() const;
@ -58,13 +110,68 @@ namespace MultiArrayTools
{ {
public: public:
// iterator ( containing idx of Range ) typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator;
typedef MultiArrayBase<T,Range> MAB;
class iterator : public std::iterator<std::random_access_iterator_tag,T>,
public std::iterator<std::output_iterator_tag,T>
{
public:
DEFAULT_MEMBERS(iterator);
iterator(MutableMultiArrayBase& ma);
iterator(MutableMultiArrayBase& ma, const typename Range::IndexType& index);
virtual ~iterator() = default;
// Requirements:
bool operator==(const iterator& it) const;
bool operator!=(const iterator& it) const;
const T& operator*() const;
T const* operator->() const;
T& operator*();
T* operator->();
iterator& operator++();
iterator operator++(int);
iterator& operator--();
iterator operator--(int);
iterator& operator+=(int diff);
iterator& operator-=(int diff);
iterator operator+(int num) const;
iterator operator-(int num) const;
int operator-(const iterator& it) const;
const T& operator[](int num) const;
T& operator[](int num);
bool operator<(const iterator& it) const;
bool operator>(const iterator& it) const;
bool operator<=(const iterator& it) const;
bool operator>=(const iterator& it) const;
// Multi Array specific:
const typename Range::IndexType& index() const;
typename Range::IndexType& index();
protected:
MutableMultiArrayBase* mMAPtr = nullptr;
typename Range::IndexType mIndex;
};
DEFAULT_MEMBERS(MutableMultiArrayBase); DEFAULT_MEMBERS(MutableMultiArrayBase);
MutableMultiArrayBase(const Range& range); MutableMultiArrayBase(const Range& range);
virtual T& operator[](const typename Range::IndexType& i) = 0; virtual T& operator[](const typename Range::IndexType& i) = 0;
virtual iterator begin();
virtual iterator end();
virtual bool isConst() const override; virtual bool isConst() const override;
template <class... NameTypes> template <class... NameTypes>
@ -81,6 +188,8 @@ namespace MultiArrayTools
public: public:
typedef MultiArrayBase<T,Range> MAB; typedef MultiArrayBase<T,Range> MAB;
typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator;
typedef typename MutableMultiArrayBase<T,Range>::iterator iterator;
DEFAULT_MEMBERS(MultiArray); DEFAULT_MEMBERS(MultiArray);
MultiArray(const Range& range); MultiArray(const Range& range);

View file

@ -34,14 +34,15 @@ namespace MultiArrayTools
template <typename T, class Range> template <typename T, class Range>
void MultiArrayOperationRoot<T,Range>::performAssignment(const MultiArrayOperationBase<T>& in) void MultiArrayOperationRoot<T,Range>::performAssignment(const MultiArrayOperationBase<T>& in)
{ {
#error "WRITE MAOR INTRINSIC CONTRACT FUNCTION"
//CHECK; //CHECK;
in.linkIndicesTo(MAOB::mIibPtr); in.linkIndicesTo(MAOB::mIibPtr);
//CHECK; //CHECK;
IndexType& iref = dynamic_cast<IndexType&>(*MAOB::mIibPtr); IndexType& iref = dynamic_cast<IndexType&>(*MAOB::mIibPtr);
//CHECK; //CHECK;
const size_t endPos = mArrayRef.end().pos(); const size_t endPos = mArrayRef.endIndex().pos();
std::cout << "assignment: " << endPos << " elements" << std::endl; std::cout << "assignment: " << endPos << " elements" << std::endl;
for(iref = mArrayRef.begin().pos(); iref != mArrayRef.end(); ++iref){ for(iref = mArrayRef.beginIndex().pos(); iref != mArrayRef.endIndex(); ++iref){
std::cout << iref.pos() << '\r' << std::flush; std::cout << iref.pos() << '\r' << std::flush;
get() = in.get(); get() = in.get();
} }
@ -79,7 +80,7 @@ namespace MultiArrayTools
const Name& nm) : const Name& nm) :
MutableMultiArrayOperationBase<T>(), MutableMultiArrayOperationBase<T>(),
mArrayRef(ma), mArrayRef(ma),
mIndex(mArrayRef.begin()), mIndex(mArrayRef.beginIndex()),
mNm(nm) mNm(nm)
{ {
MAOB::mIibPtr = &mIndex; MAOB::mIibPtr = &mIndex;
@ -300,7 +301,7 @@ namespace MultiArrayTools
void MultiArrayOperationRoot<T,Range>::freeIndex() void MultiArrayOperationRoot<T,Range>::freeIndex()
{ {
MAOB::mIibPtr->freeLinked(); MAOB::mIibPtr->freeLinked();
mIndex = mArrayRef.begin(); mIndex = mArrayRef.beginIndex();
MAOB::mIibPtr = &mIndex; MAOB::mIibPtr = &mIndex;
MAOB::mIibPtr->name(mNm); MAOB::mIibPtr->name(mNm);
} }
@ -339,7 +340,7 @@ namespace MultiArrayTools
const Name& nm) : const Name& nm) :
MultiArrayOperationBase<T>(), MultiArrayOperationBase<T>(),
mArrayRef(ma), mArrayRef(ma),
mIndex(mArrayRef.begin()), mIndex(mArrayRef.beginIndex()),
mNm(nm) mNm(nm)
{ {
MAOB::mIibPtr = &mIndex; MAOB::mIibPtr = &mIndex;
@ -352,7 +353,7 @@ namespace MultiArrayTools
ConstMultiArrayOperationRoot(const MultiArrayOperationRoot<T,Range>& in) : ConstMultiArrayOperationRoot(const MultiArrayOperationRoot<T,Range>& in) :
MultiArrayOperationBase<T>(), MultiArrayOperationBase<T>(),
mArrayRef(in.getCont()), mArrayRef(in.getCont()),
mIndex(mArrayRef.begin()), mIndex(mArrayRef.beginIndex()),
mNm(in.name()) mNm(in.name())
{ {
MAOB::mIibPtr = &mIndex; MAOB::mIibPtr = &mIndex;
@ -497,7 +498,7 @@ namespace MultiArrayTools
void ConstMultiArrayOperationRoot<T,Range>::freeIndex() void ConstMultiArrayOperationRoot<T,Range>::freeIndex()
{ {
MAOB::mIibPtr->freeLinked(); MAOB::mIibPtr->freeLinked();
mIndex = mArrayRef.begin(); mIndex = mArrayRef.beginIndex();
MAOB::mIibPtr = &mIndex; MAOB::mIibPtr = &mIndex;
MAOB::mIibPtr->name(mNm); MAOB::mIibPtr->name(mNm);
} }

View file

@ -14,7 +14,7 @@ namespace MultiArrayTools
const Name& MANm) : const Name& MANm) :
MutableMultiArrayBase<T,Range>(range), MutableMultiArrayBase<T,Range>(range),
mOwnName(ownNm), mMAName(MANm), mMultiArrayPtr(&multiArrayRef), mOwnName(ownNm), mMAName(MANm), mMultiArrayPtr(&multiArrayRef),
mMAIdx(MAIdx) mMAIdx(MAIdx), mBeginIndex(range.begin()), mEndIndex(range.end())
{ {
MAB::mInit = true; MAB::mInit = true;
mOwnIdx = MAB::mRange->begin(); mOwnIdx = MAB::mRange->begin();
@ -31,7 +31,7 @@ namespace MultiArrayTools
const typename MARange::IndexType& MAIdx) : const typename MARange::IndexType& MAIdx) :
MutableMultiArrayBase<T,Range>(range), MutableMultiArrayBase<T,Range>(range),
mOwnName(ownNm), mMAName(mor.name()), mMultiArrayPtr(&(*mor)), mOwnName(ownNm), mMAName(mor.name()), mMultiArrayPtr(&(*mor)),
mMAIdx(MAIdx) mMAIdx(MAIdx), mBeginIndex(range.begin()), mEndIndex(range.end())
{ {
MAB::mInit = true; MAB::mInit = true;
mOwnIdx = MAB::mRange->begin(); mOwnIdx = MAB::mRange->begin();
@ -46,69 +46,31 @@ namespace MultiArrayTools
return true; return true;
} }
/*
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
void Slice<T,Range,MARange>::set(MutableMultiArrayBase<T,MARange>& multiArrayRef, auto Slice<T,Range,MARange>::beginIndex() const -> decltype(Range().begin())
const Name& ownNm, // for correct linkage
const typename MARange::IndexType& MAIdx, // for desired slice position
const Name& MANm) // for correct linkage)
{ {
MAB::mInit = true; return mBeginIndex;
mOwnName = ownNm;
mMAName = MANm;
mMultiArrayPtr = &multiArrayRef;
mMAIdx = MAIdx;
mOwnIdx = MAB::mRange->begin();
mMAIdx.name(MANm);
mOwnIdx.name(ownNm);
mMAIdx.linkTo(&mOwnIdx);
}
*/
/*
template <typename T, class Range, class MARange>
void Slice<T,Range,MARange>::setConst(const MultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm, // for correct linkage
const typename MARange::IndexType& MAIdx, // for desired slice position
const Name& MANm) // for correct linkage)
{
MAB::mInit = true;
mOwnName = ownNm;
mMAName = MANm;
mConstMultiArrayPtr = &multiArrayRef;
mMAIdx = MAIdx;
mOwnIdx = MAB::mRange->begin();
mMAIdx.name(MANm);
mOwnIdx.name(ownNm);
mMAIdx.linkTo(&mOwnIdx);
}
*/
template <typename T, class Range, class MARange>
auto Slice<T,Range,MARange>::begin() const -> decltype(Range().begin())
{
return MAB::mRange->begin();
} }
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
auto Slice<T,Range,MARange>::end() const -> decltype(Range().end()) auto Slice<T,Range,MARange>::endIndex() const -> decltype(Range().end())
{ {
return MAB::mRange->end(); return mEndIndex;
} }
// slow!!
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
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();
return (*mMultiArrayPtr)[ mMAIdx ]; return (*mMultiArrayPtr)[ mMAIdx ];
} }
// slow!!
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
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();
return (*mMultiArrayPtr)[ mMAIdx ]; return (*mMultiArrayPtr)[ mMAIdx ];
} }
@ -118,12 +80,12 @@ namespace MultiArrayTools
return false; return false;
} }
/*
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
ConstSlice<T,Range,MARange>:: void Slice<T,Range,MARange>::link(IndefinitIndexBase* iibPtr) const
ConstSlice(const Range& range) : {
MultiArrayBase<T,Range>(range) {} mMAIdx.linkTo(iibPtr);
*/ }
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
ConstSlice<T,Range,MARange>:: ConstSlice<T,Range,MARange>::
ConstSlice(const Range& range, ConstSlice(const Range& range,
@ -133,7 +95,7 @@ namespace MultiArrayTools
const Name& MANm) : const Name& MANm) :
MultiArrayBase<T,Range>(range), MultiArrayBase<T,Range>(range),
mMultiArrayPtr(&multiArrayRef), mMultiArrayPtr(&multiArrayRef),
mMAIdx(MAIdx) mMAIdx(MAIdx), mBeginIndex(range.begin()), mEndIndex(range.end())
{ {
MAB::mInit = true; MAB::mInit = true;
mOwnIdx = MAB::mRange->begin(); mOwnIdx = MAB::mRange->begin();
@ -150,7 +112,7 @@ namespace MultiArrayTools
const typename MARange::IndexType& MAIdx) : const typename MARange::IndexType& MAIdx) :
MultiArrayBase<T,Range>(range), MultiArrayBase<T,Range>(range),
mMultiArrayPtr(&(*mor)), mMultiArrayPtr(&(*mor)),
mMAIdx(MAIdx) mMAIdx(MAIdx), mBeginIndex(range.begin()), mEndIndex(range.end())
{ {
MAB::mInit = true; MAB::mInit = true;
mOwnIdx = MAB::mRange->begin(); mOwnIdx = MAB::mRange->begin();
@ -164,7 +126,7 @@ namespace MultiArrayTools
ConstSlice(const Slice<T,Range,MARange>& slice) : ConstSlice(const Slice<T,Range,MARange>& slice) :
MultiArrayBase<T,Range>(slice.range()), MultiArrayBase<T,Range>(slice.range()),
mOwnIdx(slice.mOwnIdx), mOwnIdx(slice.mOwnIdx),
mMAIdx(slice.mMAIdx) mMAIdx(slice.mMAIdx), mBeginIndex(slice.mBeginIndex), mEndIndex(slice.mEndIndex)
{ {
if(slice.mInit){ if(slice.mInit){
MAB::mInit = true; MAB::mInit = true;
@ -190,34 +152,25 @@ namespace MultiArrayTools
return true; return true;
} }
/*
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
void ConstSlice<T,Range,MARange>::set(const MultiArrayBase<T,MARange>& multiArrayRef, void ConstSlice<T,Range,MARange>::link(IndefinitIndexBase* iibPtr) const
const Name& ownNm, // for correct linkage
const typename MARange::IndexType& MAIdx, // for desired slice position
const Name& MANm) const // for correct linkage)
{ {
MAB::mInit = true; mMAIdx.linkTo(iibPtr);
mMultiArrayPtr = &multiArrayRef;
mMAIdx = MAIdx;
mOwnIdx = MAB::mRange->begin();
mMAIdx.name(MANm);
mOwnIdx.name(ownNm);
mMAIdx.linkTo(&mOwnIdx);
}
*/
template <typename T, class Range, class MARange>
auto ConstSlice<T,Range,MARange>::begin() const -> decltype(Range().begin())
{
return MAB::mRange->begin();
} }
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
auto ConstSlice<T,Range,MARange>::end() const -> decltype(Range().end()) auto ConstSlice<T,Range,MARange>::beginIndex() const -> decltype(Range().begin())
{ {
return MAB::mRange->end(); return mBeginIndex;
} }
template <typename T, class Range, class MARange>
auto ConstSlice<T,Range,MARange>::endIndex() const -> decltype(Range().end())
{
return mEndIndex;
}
// slow!!
template <typename T, class Range, class MARange> template <typename T, class Range, class MARange>
const T& ConstSlice<T,Range,MARange>::operator[](const typename Range::IndexType& i) const const T& ConstSlice<T,Range,MARange>::operator[](const typename Range::IndexType& i) const
{ {

View file

@ -36,25 +36,15 @@ namespace MultiArrayTools
virtual T& operator[](const typename Range::IndexType& i) override; virtual T& operator[](const typename Range::IndexType& i) override;
virtual const T& operator[](const typename Range::IndexType& i) const override; virtual 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 // link given Index to mMAPtr which is index of total array
virtual auto begin() const -> decltype(Range().begin()) override; virtual auto beginIndex() const -> decltype(Range().begin()) override;
virtual auto end() const -> decltype(Range().end()) override; virtual auto endIndex() const -> decltype(Range().end()) override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
virtual bool isConst() const override; virtual bool isConst() const override;
/* virtual void link(IndefinitIndexBase* iibPtr) const override;
void set();
*/
/*
void setConst(const MultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm,
const typename MARange::IndexType& MAIdx,
const Name& MANm);
*/
template <typename U, class RangeX, class MARangeX> template <typename U, class RangeX, class MARangeX>
friend class ConstSlice; friend class ConstSlice;
@ -65,7 +55,8 @@ namespace MultiArrayTools
MutableMultiArrayBase<T,MARange>* mMultiArrayPtr = nullptr; MutableMultiArrayBase<T,MARange>* mMultiArrayPtr = nullptr;
mutable typename Range::IndexType mOwnIdx; mutable typename Range::IndexType mOwnIdx;
mutable typename MARange::IndexType mMAIdx; mutable typename MARange::IndexType mMAIdx;
typename Range::IndexType mBeginIndex;
typename Range::IndexType mEndIndex;
}; };
template <typename T, class Range, class MARange/*, class Index*/> template <typename T, class Range, class MARange/*, class Index*/>
@ -91,29 +82,22 @@ namespace MultiArrayTools
virtual const T& operator[](const typename Range::IndexType& i) const override; virtual 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 // link given Index to mMAPtr which is index of total array
virtual auto begin() const -> decltype(Range().begin()) override; virtual auto beginIndex() const -> decltype(Range().begin()) override;
virtual auto end() const -> decltype(Range().end()) override; virtual auto endIndex() const -> decltype(Range().end()) override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
virtual bool isConst() const override; virtual bool isConst() const override;
/* virtual void link(IndefinitIndexBase* iibPtr) const override;
void set(const MultiArrayBase<T,MARange>& multiArrayRef,
const Name& ownNm,
const typename MARange::IndexType& MAIdx,
const Name& MANm) const;
*/
private:
// !!! private:
T x = static_cast<T>(0);
MultiArrayBase<T,MARange> const* mMultiArrayPtr = nullptr; MultiArrayBase<T,MARange> const* mMultiArrayPtr = nullptr;
mutable typename Range::IndexType mOwnIdx; mutable typename Range::IndexType mOwnIdx;
mutable typename MARange::IndexType mMAIdx; mutable typename MARange::IndexType mMAIdx;
typename Range::IndexType mBeginIndex;
typename Range::IndexType mEndIndex;
}; };

View file

@ -143,7 +143,7 @@ namespace {
TEST_F(OneDimTest, CorrectAssigned) TEST_F(OneDimTest, CorrectAssigned)
{ {
auto i = ma.begin(); auto i = ma.beginIndex();
EXPECT_EQ(ma[i = 0], -5); EXPECT_EQ(ma[i = 0], -5);
EXPECT_EQ(ma[i = 1], 6); EXPECT_EQ(ma[i = 1], 6);
EXPECT_EQ(ma[i = 2], 2); EXPECT_EQ(ma[i = 2], 2);
@ -153,7 +153,7 @@ namespace {
TEST_F(TwoDimTest, CorrectExtensions) TEST_F(TwoDimTest, CorrectExtensions)
{ {
auto i = ma.begin(); auto i = ma.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
EXPECT_EQ(ma.size(), 12); EXPECT_EQ(ma.size(), 12);
@ -165,7 +165,7 @@ namespace {
TEST_F(TwoDimTest, CorrectAssigned) TEST_F(TwoDimTest, CorrectAssigned)
{ {
auto i = ma.begin(); auto i = ma.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
@ -186,7 +186,7 @@ namespace {
TEST_F(ReorderTest, ReorderingWorks2d) TEST_F(ReorderTest, ReorderingWorks2d)
{ {
MultiArray2dAny ma2(rb); MultiArray2dAny ma2(rb);
auto i = ma2.begin(); auto i = ma2.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
ma2("alpha","beta") = ma("beta","alpha"); ma2("alpha","beta") = ma("beta","alpha");
@ -210,7 +210,7 @@ namespace {
TEST_F(ReorderTest, ReorderingWorks3d_Test1) TEST_F(ReorderTest, ReorderingWorks3d_Test1)
{ {
MultiArray3dAny ma3d2(r3db); MultiArray3dAny ma3d2(r3db);
auto i = ma3d2.begin(); auto i = ma3d2.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
@ -255,7 +255,7 @@ namespace {
TEST_F(ReorderTest, ReorderingWorks3d_Test2) TEST_F(ReorderTest, ReorderingWorks3d_Test2)
{ {
MultiArray3dAny ma3d2(r3dc); MultiArray3dAny ma3d2(r3dc);
auto i = ma3d2.begin(); auto i = ma3d2.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
@ -293,7 +293,7 @@ namespace {
TEST_F(OperationTest, CorrectlyAdded) TEST_F(OperationTest, CorrectlyAdded)
{ {
MultiArray3dAny ma3d2(r3d); MultiArray3dAny ma3d2(r3d);
auto i = ma3d2.begin(); auto i = ma3d2.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
@ -331,7 +331,7 @@ namespace {
TEST_F(OperationTest, CorrectlyAdded2) TEST_F(OperationTest, CorrectlyAdded2)
{ {
//MultiArray3dAny ma3d2(r3d); //MultiArray3dAny ma3d2(r3d);
auto i = ma3d.begin(); auto i = ma3d.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
@ -370,7 +370,7 @@ namespace {
{ {
MultiArray3dAny ma3d2(r3d); MultiArray3dAny ma3d2(r3d);
//MultiArray2dAny ma2(ra); //MultiArray2dAny ma2(ra);
auto i = ma3d2.begin(); auto i = ma3d2.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
@ -408,7 +408,7 @@ namespace {
TEST_F(SliceTest, CorrectSize) TEST_F(SliceTest, CorrectSize)
{ {
auto i = ma.begin(); auto i = ma.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
@ -432,7 +432,7 @@ namespace {
TEST_F(SliceTest, CorrectContent) TEST_F(SliceTest, CorrectContent)
{ {
auto i = ma.begin(); auto i = ma.beginIndex();
auto i1 = i.template getIndex<0>(); auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>(); auto i2 = i.template getIndex<1>();
auto i3 = i.template getIndex<2>(); auto i3 = i.template getIndex<2>();
@ -441,7 +441,7 @@ namespace {
MAT::Name("master", "alpha","beta","gamma")); MAT::Name("master", "alpha","beta","gamma"));
//sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)]; //sl("alpha","gamma") = ma("alpha","beta","gamma")[i(i1 = 0, i2 = 2, i3 = 0)];
auto j = sl.begin(); auto j = sl.beginIndex();
auto j1 = j.template getIndex<0>(); auto j1 = j.template getIndex<0>();
auto j2 = j.template getIndex<1>(); auto j2 = j.template getIndex<1>();