simple 1dim multi array call works again (without operation stuff)

This commit is contained in:
Christian Zimmermann 2017-08-08 18:07:58 +02:00
parent 16e8e588cb
commit 1652e122ea
7 changed files with 379 additions and 309 deletions

View file

@ -23,6 +23,11 @@ add_executable(iutest src/index_unit_test.cc)
target_link_libraries(iutest ${GTEST_BOTH_LIBRARIES}) target_link_libraries(iutest ${GTEST_BOTH_LIBRARIES})
add_test(NAME iutest COMMAND iutest) add_test(NAME iutest COMMAND iutest)
add_executable(mautest src/ma_unit_test.cc)
target_link_libraries(mautest ${GTEST_BOTH_LIBRARIES})
add_test(NAME mautest COMMAND mautest)
#install(TARGETS testm DESTINATION install) #install(TARGETS testm DESTINATION install)
set(CMAKE_INSTALL_PREFIX ..) set(CMAKE_INSTALL_PREFIX ..)

View file

@ -101,7 +101,6 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
class ContainerIndex; class ContainerIndex;
/*
// multi_array.h // multi_array.h
template <typename T, class Range> template <typename T, class Range>
class MultiArrayBase; class MultiArrayBase;
@ -113,7 +112,7 @@ namespace MultiArrayTools
// multi_array.h // multi_array.h
template <typename T, class Range> template <typename T, class Range>
class MultiArray; class MultiArray;
/*
// multi_array_operation.h // multi_array_operation.h
template <typename T> template <typename T>
class MultiArrayOperationBase; class MultiArrayOperationBase;

View file

@ -84,6 +84,7 @@ namespace MultiArrayTools
ContainerIndex<Indices...>& ContainerIndex<Indices...>::at(const MetaType& metaPos) ContainerIndex<Indices...>& ContainerIndex<Indices...>::at(const MetaType& metaPos)
{ {
PackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos); PackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this; return *this;
} }

79
src/ma_unit_test.cc Normal file
View file

@ -0,0 +1,79 @@
// -*- C++ -*-
#include <cstdlib>
#include "gtest/gtest.h"
#include <iostream>
#include "multi_array_header.h"
namespace MAT = MultiArrayTools;
namespace {
using namespace MAT;
template <class Factory, typename T>
void swapFactory(std::shared_ptr<RangeFactoryBase>& fptr, std::initializer_list<T> ilist)
{
std::vector<T> tmp = ilist;
auto nptr = std::make_shared<Factory>( tmp );
fptr = nptr;
}
template <class Factory, class... Rs>
void swapMFactory(std::shared_ptr<RangeFactoryBase>& fptr, const Rs&... rs)
{
auto nptr = std::make_shared<Factory>( rs... );
fptr = nptr;
}
template <typename... Ts>
auto mkt(Ts&&... ts) -> decltype(std::make_tuple(ts...))
{
return std::make_tuple(ts...);
}
class MATest_1Dim : public ::testing::Test
{
protected:
typedef SingleRangeFactory<char,RangeType::ANY> SRF;
typedef SRF::oType SRange;
typedef ContainerRangeFactory<SRange> CRF;
typedef CRF::oType CRange;
MATest_1Dim()
{
swapFactory<SRF>(rfbptr, {'x', 'y', 'l', 'f', 'g'} );
srptr = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
swapMFactory<CRF>(rfbptr, srptr);
crptr = std::dynamic_pointer_cast<CRange>( rfbptr->create() );
}
std::shared_ptr<RangeFactoryBase> rfbptr;
std::shared_ptr<SRange> srptr;
std::shared_ptr<CRange> crptr;
std::vector<double> vv = { 3.141, 2.718, 1.618, 0.693, 0.577 };
};
TEST_F(MATest_1Dim, SimpleCall)
{
MultiArray<double,MATest_1Dim::CRange> ma(crptr, vv);
auto i = ma.beginIndex();
EXPECT_EQ( ma[ i.at('x') ], 3.141);
EXPECT_EQ( ma[ i.at('y') ], 2.718);
EXPECT_EQ( ma[ i.at('l') ], 1.618);
EXPECT_EQ( ma[ i.at('f') ], 0.693);
EXPECT_EQ( ma[ i.at('g') ], 0.577);
}
} // end namespace
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View file

@ -8,225 +8,212 @@ namespace MultiArrayTools
* MultiArrayBase::const_iterator * * MultiArrayBase::const_iterator *
**************************************/ **************************************/
template <typename T, class Range> template <typename T, class CRange>
MultiArrayBase<T,Range>::const_iterator::const_iterator(const MultiArrayBase<T,Range>& ma): MultiArrayBase<T,CRange>::const_iterator::const_iterator(const MultiArrayBase<T,CRange>& ma):
mMAPtr(&ma), mIndex(mMAPtr->beginIndex()) mMAPtr(&ma), mPos(0) { }
{ }
template <typename T, class Range> template <typename T, class CRange>
MultiArrayBase<T,Range>::const_iterator::const_iterator(const MultiArrayBase<T,Range>& ma, MultiArrayBase<T,CRange>::const_iterator::const_iterator(const MultiArrayBase<T,CRange>& ma,
const typename Range::IndexType& index): const typename CRange::IndexType& index):
mMAPtr(&ma), mIndex(index) mMAPtr(&ma), mPos(index.pos()) { }
{ }
template <typename T, class Range> template <typename T, class CRange>
bool MultiArrayBase<T,Range>::const_iterator::operator==(const const_iterator& it) const bool MultiArrayBase<T,CRange>::const_iterator::operator==(const const_iterator& it) const
{ {
return mMAPtr == it.mMAPtr and mIndex.pos() == it.mIndex.pos(); return mMAPtr == it.mMAPtr and mPos == it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MultiArrayBase<T,Range>::const_iterator::operator!=(const const_iterator& it) const bool MultiArrayBase<T,CRange>::const_iterator::operator!=(const const_iterator& it) const
{ {
return mMAPtr != it.mMAPtr or mIndex.pos() != it.mIndex.pos(); return mMAPtr != it.mMAPtr or mPos != it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
const T& MultiArrayBase<T,Range>::const_iterator::operator*() const const T& MultiArrayBase<T,CRange>::const_iterator::operator*() const
{ {
return (*mMAPtr)[mIndex]; return mMAPtr->data()[mPos];
} }
template <typename T, class Range> template <typename T, class CRange>
T const* MultiArrayBase<T,Range>::const_iterator::operator->() const T const* MultiArrayBase<T,CRange>::const_iterator::operator->() const
{ {
return &(*mMAPtr)[mIndex]; return &mMAPtr->data()[mPos];
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator& MultiArrayBase<T,Range>::const_iterator::operator++() typename MultiArrayBase<T,CRange>::const_iterator& MultiArrayBase<T,CRange>::const_iterator::operator++()
{ {
++mIndex; ++mPos;
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::const_iterator::operator++(int) typename MultiArrayBase<T,CRange>::const_iterator MultiArrayBase<T,CRange>::const_iterator::operator++(int)
{ {
const_iterator tmp(*this); const_iterator tmp(*this);
++mIndex; ++mPos;
return tmp; return tmp;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator& MultiArrayBase<T,Range>::const_iterator::operator--() typename MultiArrayBase<T,CRange>::const_iterator& MultiArrayBase<T,CRange>::const_iterator::operator--()
{ {
--mIndex; --mPos;
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::const_iterator::operator--(int) typename MultiArrayBase<T,CRange>::const_iterator MultiArrayBase<T,CRange>::const_iterator::operator--(int)
{ {
const_iterator tmp(*this); const_iterator tmp(*this);
--mIndex; --mPos;
return tmp; return tmp;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator& MultiArrayBase<T,Range>::const_iterator::operator+=(int diff) typename MultiArrayBase<T,CRange>::const_iterator& MultiArrayBase<T,CRange>::const_iterator::operator+=(int diff)
{ {
mIndex += diff; mPos += diff;
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator& MultiArrayBase<T,Range>::const_iterator::operator-=(int diff) typename MultiArrayBase<T,CRange>::const_iterator& MultiArrayBase<T,CRange>::const_iterator::operator-=(int diff)
{ {
mIndex -= diff; mPos -= diff;
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::const_iterator::operator+(int num) const typename MultiArrayBase<T,CRange>::const_iterator MultiArrayBase<T,CRange>::const_iterator::operator+(int num) const
{ {
const_iterator tmp(*this); const_iterator tmp(*this);
tmp += num; tmp += num;
return tmp; return tmp;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::const_iterator::operator-(int num) const typename MultiArrayBase<T,CRange>::const_iterator MultiArrayBase<T,CRange>::const_iterator::operator-(int num) const
{ {
const_iterator tmp(*this); const_iterator tmp(*this);
tmp -= num; tmp -= num;
} }
template <typename T, class Range> template <typename T, class CRange>
int MultiArrayBase<T,Range>::const_iterator::operator-(const const_iterator& it) const int MultiArrayBase<T,CRange>::const_iterator::operator-(const const_iterator& it) const
{ {
return mIndex.pos() - it.mIndex.pos(); return mPos - it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
const T& MultiArrayBase<T,Range>::const_iterator::operator[](int num) const const T& MultiArrayBase<T,CRange>::const_iterator::operator[](int num) const
{ {
return *(operator+(num)); return *(operator+(num));
} }
template <typename T, class Range> template <typename T, class CRange>
bool MultiArrayBase<T,Range>::const_iterator::operator<(const const_iterator& it) const bool MultiArrayBase<T,CRange>::const_iterator::operator<(const const_iterator& it) const
{ {
return mIndex.pos() < it.mIndex.pos(); return mPos < it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MultiArrayBase<T,Range>::const_iterator::operator>(const const_iterator& it) const bool MultiArrayBase<T,CRange>::const_iterator::operator>(const const_iterator& it) const
{ {
return mIndex.pos() > it.mIndex.pos(); return mPos > it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MultiArrayBase<T,Range>::const_iterator::operator<=(const const_iterator& it) const bool MultiArrayBase<T,CRange>::const_iterator::operator<=(const const_iterator& it) const
{ {
return mIndex.pos() <= it.mIndex.pos(); return mPos <= it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MultiArrayBase<T,Range>::const_iterator::operator>=(const const_iterator& it) const bool MultiArrayBase<T,CRange>::const_iterator::operator>=(const const_iterator& it) const
{ {
return mIndex.pos() >= it.mIndex.pos(); return mPos >= it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
const typename Range::IndexType& MultiArrayBase<T,Range>::const_iterator::index() const typename CRange::IndexType MultiArrayBase<T,CRange>::const_iterator::index() const
{ {
return mIndex; auto i = mMAPtr->beginIndex();
i = mPos;
return i;
} }
template <typename T, class Range>
typename Range::IndexType& MultiArrayBase<T,Range>::const_iterator::index()
{
return mIndex;
}
/********************** /**********************
* MultiArrayBase * * MultiArrayBase *
**********************/ **********************/
template <typename T, class Range> template <typename T, class CRange>
MultiArrayBase<T,Range>::MultiArrayBase(const Range& range) : mRange(new Range(range)) {} MultiArrayBase<T,CRange>::MultiArrayBase(const std::shared_ptr<CRange>& range) : mRange(range) {}
template <typename T, class Range> template <typename T, class CRange>
void MultiArrayBase<T,Range>::link(IndefinitIndexBase* iibPtr) const size_t MultiArrayBase<T,CRange>::size() const
{ }
template <typename T, class Range>
size_t MultiArrayBase<T,Range>::size() const
{ {
return mRange->size(); return mRange->size();
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::begin() const typename MultiArrayBase<T,CRange>::const_iterator MultiArrayBase<T,CRange>::begin() const
{ {
return const_iterator(*this, beginIndex()); return const_iterator(*this, beginIndex());
} }
template <typename T, class Range> template <typename T, class CRange>
typename MultiArrayBase<T,Range>::const_iterator MultiArrayBase<T,Range>::end() const typename MultiArrayBase<T,CRange>::const_iterator MultiArrayBase<T,CRange>::end() const
{ {
return const_iterator(*this, endIndex()); return const_iterator(*this, endIndex());
} }
template <typename T, class Range> template <typename T, class CRange>
auto MultiArrayBase<T,Range>::beginIndex() const -> decltype(Range().begin()) typename CRange::IndexType MultiArrayBase<T,CRange>::beginIndex() const
{ {
//CHECK;
return mRange->begin(); return mRange->begin();
} }
template <typename T, class Range> template <typename T, class CRange>
auto MultiArrayBase<T,Range>::endIndex() const -> decltype(Range().end()) typename CRange::IndexType MultiArrayBase<T,CRange>::endIndex() const
{ {
return mRange->end(); return mRange->end();
} }
template <typename T, class Range> template <typename T, class CRange>
const Range& MultiArrayBase<T,Range>::range() const const CRange& MultiArrayBase<T,CRange>::range() const
{ {
return *mRange; return *mRange;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MultiArrayBase<T,Range>::isConst() const bool MultiArrayBase<T,CRange>::isConst() const
{ {
return true; return true;
} }
/*
template <typename T, class Range> template <typename T, class CRange>
template <class... NameTypes> template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> MultiArrayBase<T,Range>::operator()(const NameTypes&... str) const ConstMultiArrayOperationRoot<T,CRange> MultiArrayBase<T,CRange>::operator()(const NameTypes&... str) const
{ {
return ConstMultiArrayOperationRoot<T,Range>(*this, Name("master", str...)); return ConstMultiArrayOperationRoot<T,CRange>(*this, Name("master", str...));
} }
template <typename T, class Range> template <typename T, class CRange>
template <class NameType> template <class NameType>
ConstMultiArrayOperationRoot<T,Range> MultiArrayBase<T,Range>::operator()(const NameType& name, bool master) const ConstMultiArrayOperationRoot<T,CRange> MultiArrayBase<T,CRange>::operator()(const NameType& name, bool master) const
{ {
//CHECK;
if(master){ if(master){
return ConstMultiArrayOperationRoot<T,Range>(*this, name); return ConstMultiArrayOperationRoot<T,CRange>(*this, name);
} }
else { else {
return operator()(name); return operator()(name);
} }
} }
*/
template <typename T, class Range> template <typename T, class CRange>
bool MultiArrayBase<T,Range>::isInit() const bool MultiArrayBase<T,CRange>::isInit() const
{ {
return mInit; return mInit;
} }
@ -235,246 +222,243 @@ namespace MultiArrayTools
* MutableMultiArrayBase::iterator * * MutableMultiArrayBase::iterator *
****************************************/ ****************************************/
template <typename T, class Range> template <typename T, class CRange>
MutableMultiArrayBase<T,Range>::iterator::iterator(MutableMultiArrayBase<T,Range>& ma): MutableMultiArrayBase<T,CRange>::iterator::iterator(MutableMultiArrayBase<T,CRange>& ma):
mMAPtr(&ma), mIndex(mMAPtr->beginIndex()) mMAPtr(&ma), mPos(0)
{ } { }
template <typename T, class Range> template <typename T, class CRange>
MutableMultiArrayBase<T,Range>::iterator::iterator(MutableMultiArrayBase<T,Range>& ma, MutableMultiArrayBase<T,CRange>::iterator::iterator(MutableMultiArrayBase<T,CRange>& ma,
const typename Range::IndexType& index): const typename CRange::IndexType& index):
mMAPtr(&ma), mIndex(index) mMAPtr(&ma), mPos(index.pos())
{ } { }
template <typename T, class Range> template <typename T, class CRange>
bool MutableMultiArrayBase<T,Range>::iterator::operator==(const iterator& it) const bool MutableMultiArrayBase<T,CRange>::iterator::operator==(const iterator& it) const
{ {
return mMAPtr == it.mMAPtr and mIndex.pos() == it.mIndex.pos(); return mMAPtr == it.mMAPtr and mPos == it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MutableMultiArrayBase<T,Range>::iterator::operator!=(const iterator& it) const bool MutableMultiArrayBase<T,CRange>::iterator::operator!=(const iterator& it) const
{ {
return mMAPtr != it.mMAPtr or mIndex.pos() != it.mIndex.pos(); return mMAPtr != it.mMAPtr or mPos != it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
const T& MutableMultiArrayBase<T,Range>::iterator::operator*() const const T& MutableMultiArrayBase<T,CRange>::iterator::operator*() const
{ {
return (*mMAPtr)[mIndex]; return mMAPtr->data()[mPos];
} }
template <typename T, class Range> template <typename T, class CRange>
T const* MutableMultiArrayBase<T,Range>::iterator::operator->() const T const* MutableMultiArrayBase<T,CRange>::iterator::operator->() const
{ {
return &(*mMAPtr)[mIndex]; return &mMAPtr->data()[mPos];
} }
template <typename T, class Range> template <typename T, class CRange>
T& MutableMultiArrayBase<T,Range>::iterator::operator*() T& MutableMultiArrayBase<T,CRange>::iterator::operator*()
{ {
return (*mMAPtr)[mIndex]; return mMAPtr->data()[mPos];
} }
template <typename T, class Range> template <typename T, class CRange>
T* MutableMultiArrayBase<T,Range>::iterator::operator->() T* MutableMultiArrayBase<T,CRange>::iterator::operator->()
{ {
return &(*mMAPtr)[mIndex]; return &mMAPtr->data()[mPos];
} }
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator& MutableMultiArrayBase<T,Range>::iterator::operator++() typename MutableMultiArrayBase<T,CRange>::iterator& MutableMultiArrayBase<T,CRange>::iterator::operator++()
{ {
++mIndex; ++mPos;
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::iterator::operator++(int) typename MutableMultiArrayBase<T,CRange>::iterator MutableMultiArrayBase<T,CRange>::iterator::operator++(int)
{ {
iterator tmp(*this); iterator tmp(*this);
++mIndex; ++mPos;
return tmp; return tmp;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator& MutableMultiArrayBase<T,Range>::iterator::operator--() typename MutableMultiArrayBase<T,CRange>::iterator& MutableMultiArrayBase<T,CRange>::iterator::operator--()
{ {
--mIndex; --mPos;
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::iterator::operator--(int) typename MutableMultiArrayBase<T,CRange>::iterator MutableMultiArrayBase<T,CRange>::iterator::operator--(int)
{ {
iterator tmp(*this); iterator tmp(*this);
--mIndex; --mPos;
return tmp; return tmp;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator& MutableMultiArrayBase<T,Range>::iterator::operator+=(int diff) typename MutableMultiArrayBase<T,CRange>::iterator& MutableMultiArrayBase<T,CRange>::iterator::operator+=(int diff)
{ {
mIndex += diff; mPos += diff;
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator& MutableMultiArrayBase<T,Range>::iterator::operator-=(int diff) typename MutableMultiArrayBase<T,CRange>::iterator& MutableMultiArrayBase<T,CRange>::iterator::operator-=(int diff)
{ {
mIndex -= diff; mPos -= diff;
return *this; return *this;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::iterator::operator+(int num) const typename MutableMultiArrayBase<T,CRange>::iterator MutableMultiArrayBase<T,CRange>::iterator::operator+(int num) const
{ {
iterator tmp(*this); iterator tmp(*this);
tmp += num; tmp += num;
return tmp; return tmp;
} }
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::iterator::operator-(int num) const typename MutableMultiArrayBase<T,CRange>::iterator MutableMultiArrayBase<T,CRange>::iterator::operator-(int num) const
{ {
iterator tmp(*this); iterator tmp(*this);
tmp -= num; tmp -= num;
} }
template <typename T, class Range> template <typename T, class CRange>
int MutableMultiArrayBase<T,Range>::iterator::operator-(const iterator& it) const int MutableMultiArrayBase<T,CRange>::iterator::operator-(const iterator& it) const
{ {
return mIndex.pos() - it.mIndex.pos(); return mPos - it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
const T& MutableMultiArrayBase<T,Range>::iterator::operator[](int num) const const T& MutableMultiArrayBase<T,CRange>::iterator::operator[](int num) const
{ {
return *(operator+(num)); return *(operator+(num));
} }
template <typename T, class Range> template <typename T, class CRange>
T& MutableMultiArrayBase<T,Range>::iterator::operator[](int num) T& MutableMultiArrayBase<T,CRange>::iterator::operator[](int num)
{ {
return *(operator+(num)); return *(operator+(num));
} }
template <typename T, class Range> template <typename T, class CRange>
bool MutableMultiArrayBase<T,Range>::iterator::operator<(const iterator& it) const bool MutableMultiArrayBase<T,CRange>::iterator::operator<(const iterator& it) const
{ {
return mIndex.pos() < it.mIndex.pos(); return mPos < it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MutableMultiArrayBase<T,Range>::iterator::operator>(const iterator& it) const bool MutableMultiArrayBase<T,CRange>::iterator::operator>(const iterator& it) const
{ {
return mIndex.pos() > it.mIndex.pos(); return mPos > it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MutableMultiArrayBase<T,Range>::iterator::operator<=(const iterator& it) const bool MutableMultiArrayBase<T,CRange>::iterator::operator<=(const iterator& it) const
{ {
return mIndex.pos() <= it.mIndex.pos(); return mPos <= it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MutableMultiArrayBase<T,Range>::iterator::operator>=(const iterator& it) const bool MutableMultiArrayBase<T,CRange>::iterator::operator>=(const iterator& it) const
{ {
return mIndex.pos() >= it.mIndex.pos(); return mPos >= it.mPos;
} }
template <typename T, class Range> template <typename T, class CRange>
const typename Range::IndexType& MutableMultiArrayBase<T,Range>::iterator::index() const typename CRange::IndexType MutableMultiArrayBase<T,CRange>::iterator::index() const
{ {
return mIndex; auto i = mMAPtr->beginIndex();
} i = mPos;
return i;
template <typename T, class Range>
typename Range::IndexType& MutableMultiArrayBase<T,Range>::iterator::index()
{
return mIndex;
} }
/****************************** /******************************
* MutableMultiArrayBase * * MutableMultiArrayBase *
******************************/ ******************************/
template <typename T, class Range> template <typename T, class CRange>
MutableMultiArrayBase<T,Range>::MutableMultiArrayBase(const Range& range) : MultiArrayBase<T,Range>(range) {} MutableMultiArrayBase<T,CRange>::MutableMultiArrayBase(const std::shared_ptr<CRange>& range) :
MultiArrayBase<T,CRange>(range) {}
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::begin() typename MutableMultiArrayBase<T,CRange>::iterator MutableMultiArrayBase<T,CRange>::begin()
{ {
return iterator(*this, MAB::beginIndex()); return iterator(*this, MAB::beginIndex());
} }
template <typename T, class Range> template <typename T, class CRange>
typename MutableMultiArrayBase<T,Range>::iterator MutableMultiArrayBase<T,Range>::end() typename MutableMultiArrayBase<T,CRange>::iterator MutableMultiArrayBase<T,CRange>::end()
{ {
return iterator(*this, MAB::endIndex()); return iterator(*this, MAB::endIndex());
} }
template <typename T, class Range> template <typename T, class CRange>
bool MutableMultiArrayBase<T,Range>::isConst() const bool MutableMultiArrayBase<T,CRange>::isConst() const
{ {
return false; return false;
} }
/*
template <typename T, class Range> template <typename T, class CRange>
template <class... NameTypes> template <class... NameTypes>
MultiArrayOperationRoot<T,Range> MutableMultiArrayBase<T,Range>::operator()(const NameTypes&... str) MultiArrayOperationRoot<T,CRange> MutableMultiArrayBase<T,CRange>::operator()(const NameTypes&... str)
{ {
return MultiArrayOperationRoot<T,Range>(*this, Name("master", str...)); return MultiArrayOperationRoot<T,CRange>(*this, Name("master", str...));
} }
template <typename T, class Range> template <typename T, class CRange>
template <class NameType> template <class NameType>
MultiArrayOperationRoot<T,Range> MutableMultiArrayBase<T,Range>::operator()(const NameType& name, bool master) MultiArrayOperationRoot<T,CRange> MutableMultiArrayBase<T,CRange>::operator()(const NameType& name, bool master)
{ {
//CHECK; //CHECK;
if(master){ if(master){
return MultiArrayOperationRoot<T,Range>(*this, name); return MultiArrayOperationRoot<T,CRange>(*this, name);
} }
else { else {
return operator()(name); return operator()(name);
} }
} }
template <typename T, class Range> template <typename T, class CRange>
template <class... NameTypes> template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> MutableMultiArrayBase<T,Range>::operator()(const NameTypes&... str) const ConstMultiArrayOperationRoot<T,CRange> MutableMultiArrayBase<T,CRange>::operator()(const NameTypes&... str) const
{ {
return ConstMultiArrayOperationRoot<T,Range>(*this, Name("master", str...)); return ConstMultiArrayOperationRoot<T,CRange>(*this, Name("master", str...));
} }
template <typename T, class Range> template <typename T, class CRange>
template <class NameType> template <class NameType>
ConstMultiArrayOperationRoot<T,Range> MutableMultiArrayBase<T,Range>::operator()(const NameType& name, bool master) const ConstMultiArrayOperationRoot<T,CRange> MutableMultiArrayBase<T,CRange>::operator()(const NameType& name, bool master) const
{ {
//CHECK; //CHECK;
if(master){ if(master){
return ConstMultiArrayOperationRoot<T,Range>(*this, name); return ConstMultiArrayOperationRoot<T,CRange>(*this, name);
} }
else { else {
return operator()(name); return operator()(name);
} }
} }
*/
/******************* /*******************
* MultiArray * * MultiArray *
*******************/ *******************/
template <typename T, class Range> template <typename T, class CRange>
MultiArray<T,Range>::MultiArray(const Range& range) : MultiArray<T,CRange>::MultiArray(const std::shared_ptr<CRange>& range) :
MutableMultiArrayBase<T,Range>(range), MutableMultiArrayBase<T,CRange>(range),
mCont(MAB::mRange->size()) mCont(MAB::mRange->size())
{ {
MAB::mInit = true; MAB::mInit = true;
} }
template <typename T, class Range> template <typename T, class CRange>
MultiArray<T,Range>::MultiArray(const Range& range, const std::vector<T>& vec) : MultiArray<T,CRange>::MultiArray(const std::shared_ptr<CRange>& range, const std::vector<T>& vec) :
MutableMultiArrayBase<T,Range>(range), MutableMultiArrayBase<T,CRange>(range),
mCont(vec) mCont(vec)
{ {
MAB::mInit = true; MAB::mInit = true;
@ -483,9 +467,9 @@ namespace MultiArrayTools
} }
} }
template <typename T, class Range> template <typename T, class CRange>
MultiArray<T,Range>::MultiArray(const Range& range, std::vector<T>&& vec) : MultiArray<T,CRange>::MultiArray(const std::shared_ptr<CRange>& range, std::vector<T>&& vec) :
MutableMultiArrayBase<T,Range>(range), MutableMultiArrayBase<T,CRange>(range),
mCont(vec) mCont(vec)
{ {
MAB::mInit = true; MAB::mInit = true;
@ -494,10 +478,10 @@ namespace MultiArrayTools
} }
} }
template <typename T, class Range> template <typename T, class CRange>
template <class Range2, class Range3> template <class Range2, class Range3>
MultiArray<T,Range>::MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in) : MultiArray<T,CRange>::MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in) :
MutableMultiArrayBase<T,Range>(merge(in.range(), in[ in.beginIndex() ].range())) MutableMultiArrayBase<T,CRange>(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;
@ -508,9 +492,10 @@ namespace MultiArrayTools
assert(mCont.size() == MAB::mRange->size()); assert(mCont.size() == MAB::mRange->size());
} }
template <typename T, class Range> /*
template <typename T, class CRange>
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,CRange>& MultiArray<T,CRange>::operator=(const MultiArray<MultiArray<T,Range2>,Range3> in)
{ {
MAB::mRange.reset(new Range(merge(in.range(), in[ in.beginIndex() ].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
@ -520,41 +505,47 @@ namespace MultiArrayTools
} }
assert(mCont.size() == MAB::mRange->size()); assert(mCont.size() == MAB::mRange->size());
return *this; return *this;
} } */
template <typename T, class Range> template <typename T, class CRange>
T& MultiArray<T,Range>::operator[](const typename Range::IndexType& i) T& MultiArray<T,CRange>::operator[](const typename CRange::IndexType& i)
{ {
return mCont[ i.pos() ]; return mCont[ i.pos() ];
} }
template <typename T, class Range> template <typename T, class CRange>
const T& MultiArray<T,Range>::operator[](const typename Range::IndexType& i) const const T& MultiArray<T,CRange>::operator[](const typename CRange::IndexType& i) const
{ {
return mCont[ i.pos() ]; return mCont[ i.pos() ];
} }
template <typename T, class Range> template <typename T, class CRange>
bool MultiArray<T,Range>::isConst() const bool MultiArray<T,CRange>::isConst() const
{ {
return false; return false;
} }
template <typename T, class Range> template <typename T, class CRange>
bool MultiArray<T,Range>::isSlice() const bool MultiArray<T,CRange>::isSlice() const
{ {
return false; return false;
} }
template <typename T, class Range> template <typename T, class CRange>
const T* MultiArray<T,Range>::data() const const T* MultiArray<T,CRange>::data() const
{
return mCont.data();
}
template <typename T, class CRange>
T* MultiArray<T,CRange>::data()
{ {
return mCont.data(); return mCont.data();
} }
/* /*
template <typename T, class Range> template <typename T, class CRange>
void MultiArray<T,Range>::manipulate(ManipulatorBase<T>& mb, void MultiArray<T,CRange>::manipulate(ManipulatorBase<T>& mb,
const typename Range::IndexType& manBegin, const typename Range::IndexType& manBegin,
const typename Range::IndexType& manEnd) const typename Range::IndexType& manEnd)
{ {
@ -571,28 +562,28 @@ namespace MultiArrayTools
/* /*
template <typename T, class Range, class Function> template <typename T, class Range, class Function>
FunctionalMultiArray<T,Range,Function>::FunctionalMultiArray(const Range& range) : FunctionalMultiArray<T,Range,Function>::FunctionalMultiArray(const Range& range) :
MultiArrayBase<T,Range>(range), mFunc() {} MultiArrayBase<T,CRange>(range), mFunc() {}
*/ */
template <typename T, class Range, class Function> template <typename T, class CRange, class Function>
FunctionalMultiArray<T,Range,Function>::FunctionalMultiArray(const Range& range, FunctionalMultiArray<T,CRange,Function>::FunctionalMultiArray(const std::shared_ptr<CRange>& range,
const Function& func) : const Function& func) :
MultiArrayBase<T,Range>(range), mFunc(func) { /*CHECK;*/ } MultiArrayBase<T,CRange>(range), mFunc(func) { /*CHECK;*/ }
template <typename T, class Range, class Function> template <typename T, class CRange, class Function>
const T& FunctionalMultiArray<T,Range,Function>::operator[](const typename Range::IndexType& i) const const T& FunctionalMultiArray<T,CRange,Function>::operator[](const typename CRange::IndexType& i) const
{ {
mVal = mFunc(i); mVal = mFunc(i);
return mVal; return mVal;
} }
template <typename T, class Range, class Function> template <typename T, class CRange, class Function>
bool FunctionalMultiArray<T,Range,Function>::isConst() const bool FunctionalMultiArray<T,CRange,Function>::isConst() const
{ {
return true; return true;
} }
template <typename T, class Range, class Function> template <typename T, class CRange, class Function>
bool FunctionalMultiArray<T,Range,Function>::isSlice() const bool FunctionalMultiArray<T,CRange,Function>::isSlice() const
{ {
return false; return false;
} }

View file

@ -11,22 +11,18 @@
#include "base_def.h" #include "base_def.h"
#include "multi_range.h" #include "multi_range.h"
#include "multi_array_operation.h" //#include "multi_array_operation.h"
#include "manipulator.h" //#include "manipulator.h"
#include "name.h" //#include "name.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
template <class... Ranges> template <typename T, class CRange>
using TopRange = MultiRange<Ranges...>;
template <typename T, class... Ranges>
class MultiArrayBase class MultiArrayBase
{ {
public: public:
typedef TopRange<Ranges...> TopRangeType
typedef T value_type; typedef T value_type;
class const_iterator : public std::iterator<std::random_access_iterator_tag,T> class const_iterator : public std::iterator<std::random_access_iterator_tag,T>
@ -36,7 +32,7 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(const_iterator); DEFAULT_MEMBERS(const_iterator);
const_iterator(const MultiArrayBase& ma); const_iterator(const MultiArrayBase& ma);
const_iterator(const MultiArrayBase& ma, const typename TopRangeType::IndexType& index); const_iterator(const MultiArrayBase& ma, const typename CRange::IndexType& index);
virtual ~const_iterator() = default; virtual ~const_iterator() = default;
// Requirements: // Requirements:
@ -66,24 +62,21 @@ namespace MultiArrayTools
bool operator>=(const const_iterator& it) const; bool operator>=(const const_iterator& it) const;
// Multi Array specific: // Multi Array specific:
typename CRange::IndexType index() const;
const typename TopRangeType::IndexType& index() const;
typename TopRangeType::IndexType& index();
protected: protected:
MultiArrayBase const* mMAPtr = nullptr; MultiArrayBase const* mMAPtr = nullptr;
typename TopRangeType::IndexType mIndex; size_t mPos;
}; };
DEFAULT_MEMBERS(MultiArrayBase); DEFAULT_MEMBERS(MultiArrayBase);
MultiArrayBase(const TopRangeType& range); MultiArrayBase(const std::shared_ptr<CRange>& range);
virtual ~MultiArrayBase() = default; virtual ~MultiArrayBase() = default;
// only relevant for slices... has no effect for usual multiarrays virtual const T& operator[](const typename CRange::IndexType& i) const = 0;
virtual void link(IndefinitIndexBase* iibPtr) const;
virtual const T& operator[](const typename TopRangeType::IndexType& i) const = 0; virtual const T* data() const = 0;
virtual size_t size() const; virtual size_t size() const;
virtual bool isSlice() const = 0; virtual bool isSlice() const = 0;
@ -91,36 +84,35 @@ namespace MultiArrayTools
virtual const_iterator begin() const; virtual const_iterator begin() const;
virtual const_iterator end() const; virtual const_iterator end() const;
virtual auto beginIndex() const -> decltype(Range().begin()); virtual typename CRange::IndexType beginIndex() const;
virtual auto endIndex() const -> decltype(Range().end()); virtual typename CRange::IndexType endIndex() const;
virtual const TopRangeType& range() const; virtual const CRange& range() const;
virtual bool isConst() const; virtual bool isConst() const;
/*
template <class... NameTypes> template <class... NameTypes>
ConstMultiArrayOperationRoot<T,TopRangeType> operator()(const NameTypes&... str) const; ConstMultiArrayOperationRoot<T,CRange> operator()(const NameTypes&... str) const;
template <class NameType> template <class NameType>
ConstMultiArrayOperationRoot<T,TopRangeType> operator()(const NameType& name, bool master) const; ConstMultiArrayOperationRoot<T,CRange> operator()(const NameType& name, bool master) const;
*/
virtual bool isInit() const; virtual bool isInit() const;
protected: protected:
bool mInit = false; bool mInit = false;
std::shared_ptr<TopRangeType> mRange; std::shared_ptr<CRange> mRange;
}; };
template <typename T, class... Ranges> template <typename T, class CRange>
class MutableMultiArrayBase : public MultiArrayBase<T,Ranges...> class MutableMultiArrayBase : public MultiArrayBase<T,CRange>
{ {
public: public:
typedef typename MultiArrayBase<T,Ranges...>::const_iterator const_iterator; typedef typename MultiArrayBase<T,CRange>::const_iterator const_iterator;
typedef MultiArrayBase<T,Ranges...> MAB; typedef MultiArrayBase<T,CRange> MAB;
typedef typename MAB::TopRangeType TopRangeType; typedef typename CRange::IndexType IndexType;
typedef typename TopRangeType::IndexType IndexType;
class iterator : public std::iterator<std::random_access_iterator_tag,T>, class iterator : public std::iterator<std::random_access_iterator_tag,T>,
public std::iterator<std::output_iterator_tag,T> public std::iterator<std::output_iterator_tag,T>
@ -163,69 +155,68 @@ namespace MultiArrayTools
bool operator>=(const iterator& it) const; bool operator>=(const iterator& it) const;
// Multi Array specific: // Multi Array specific:
typename CRange::IndexType index() const;
const IndexType& index() const;
IndexType& index();
protected: protected:
MutableMultiArrayBase* mMAPtr = nullptr; MutableMultiArrayBase* mMAPtr = nullptr;
IndexType mIndex; size_t mPos;
}; };
DEFAULT_MEMBERS(MutableMultiArrayBase); DEFAULT_MEMBERS(MutableMultiArrayBase);
MutableMultiArrayBase(const TopRangeType& range); MutableMultiArrayBase(const std::shared_ptr<CRange>& range);
virtual T& operator[](const IndexType& i) = 0; virtual T& operator[](const IndexType& i) = 0;
virtual T* data() = 0;
virtual iterator begin(); virtual iterator begin();
virtual iterator end(); virtual iterator end();
virtual bool isConst() const override; virtual bool isConst() const override;
/*
template <class... NameTypes> template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Ranges...> operator()(bool x, const NameTypes&... str) const ConstMultiArrayOperationRoot<T,CRange> operator()(bool x, const NameTypes&... str) const
{ {
return MAB::operator()(str...); return MAB::operator()(str...);
} }
template <class... NameTypes> template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Ranges...> operator()(const NameTypes&... str) const; ConstMultiArrayOperationRoot<T,CRange> operator()(const NameTypes&... str) const;
template <class NameType> template <class NameType>
ConstMultiArrayOperationRoot<T,Ranges...> operator()(const NameType& name, bool master) const; ConstMultiArrayOperationRoot<T,CRange> operator()(const NameType& name, bool master) const;
template <class... NameTypes> template <class... NameTypes>
MultiArrayOperationRoot<T,Ranges...> operator()(const NameTypes&... str); MultiArrayOperationRoot<T,CRange> operator()(const NameTypes&... str);
template <class NameType> template <class NameType>
MultiArrayOperationRoot<T,Ranges...> operator()(const NameType& name, bool master); MultiArrayOperationRoot<T,CRange> operator()(const NameType& name, bool master);
*/
}; };
template <typename T, class Ranges...> template <typename T, class CRange>
class MultiArray : public MutableMultiArrayBase<T,Ranges...> class MultiArray : public MutableMultiArrayBase<T,CRange>
{ {
public: public:
typedef MultiArrayBase<T,Ranges...> MAB; typedef MultiArrayBase<T,CRange> MAB;
typedef typename MultiArrayBase<T,Ranges...>::const_iterator const_iterator; typedef typename MultiArrayBase<T,CRange>::const_iterator const_iterator;
typedef typename MutableMultiArrayBase<T,Ranges...>::iterator iterator; typedef typename MutableMultiArrayBase<T,CRange>::iterator iterator;
typedef typename MAB::TopRangeType TopRangeType; typedef typename CRange::IndexType IndexType;
typedef typename TopRangeType::IndexType IndexType;
DEFAULT_MEMBERS(MultiArray); DEFAULT_MEMBERS(MultiArray);
MultiArray(const TopRangeType& range); MultiArray(const std::shared_ptr<CRange>& range);
MultiArray(const TopRangeType& range, const std::vector<T>& vec); MultiArray(const std::shared_ptr<CRange>& range, const std::vector<T>& vec);
MultiArray(const TopRangeType& range, std::vector<T>&& vec); MultiArray(const std::shared_ptr<CRange>& range, std::vector<T>&& vec);
template <class Range2, class Range3> template <class Range2, class Range3>
MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in); MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in);
// implement contstructor using FunctionalMultiArray as Input !!! // implement contstructor using FunctionalMultiArray as Input !!!
template <class Range2, class Range3> //template <class Range2, class Range3>
MultiArray& operator=(const MultiArray<MultiArray<T,Range2>,Range3> in); //MultiArray& operator=(const MultiArray<MultiArray<T,Range2>,Range3> in);
T& operator[](const IndexType& i) override; T& operator[](const IndexType& i) override;
const T& operator[](const IndexType& i) const override; const T& operator[](const IndexType& i) const override;
@ -233,11 +224,16 @@ namespace MultiArrayTools
virtual bool isConst() const override; virtual bool isConst() const override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
const T* data() const; template <class Range2>
MultiArray& operator()(const std::shared_ptr<Range2>& nr); // reformat array using 'nr' which in
// total must have the same size as mRange
const T* data() const override;
T* data() override;
// virtual void manipulate(ManipulatorBase<T>& mb, // virtual void manipulate(ManipulatorBase<T>& mb,
// const typename Ranges...::IndexType& manBegin, // const typename CRange::IndexType& manBegin,
// const typename Ranges...::IndexType& manEnd); // const typename CRange::IndexType& manEnd);
template <typename U, class RangeX> template <typename U, class RangeX>
friend class MultiArray; friend class MultiArray;
@ -246,18 +242,17 @@ namespace MultiArrayTools
std::vector<T> mCont; std::vector<T> mCont;
}; };
template <typename T, class Ranges..., class Function> template <typename T, class CRange, class Function>
class FunctionalMultiArray : public MultiArrayBase<T,Ranges...> class FunctionalMultiArray : public MultiArrayBase<T,CRange>
{ {
public: public:
typedef MultiArrayBase<T,Ranges...> MAB; typedef MultiArrayBase<T,CRange> MAB;
typedef typename MultiArrayBase<T,Ranges...>::const_iterator const_iterator; typedef typename MultiArrayBase<T,CRange>::const_iterator const_iterator;
typedef typename MAB::TopRangeType TopRangeType; typedef typename CRange::IndexType IndexType;
typedef typename TopRangeType::IndexType IndexType;
DEFAULT_MEMBERS(FunctionalMultiArray); DEFAULT_MEMBERS(FunctionalMultiArray);
//FunctionalMultiArray(const Ranges...& range); //FunctionalMultiArray(const CRange& range);
FunctionalMultiArray(const TopRangeType& range, const Function& func); FunctionalMultiArray(const std::shared_ptr<CRange>& range, const Function& func);
virtual const T& operator[](const IndexType& i) const override; virtual const T& operator[](const IndexType& i) const override;

View file

@ -11,7 +11,7 @@
#include "multi_range.h" #include "multi_range.h"
#include "container_range.h" #include "container_range.h"
//#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 "manipulator.h"
//#include "range_transformer.h" //#include "range_transformer.h"