start testing operation routines: some open issues...

This commit is contained in:
Christian Zimmermann 2017-02-21 21:47:40 +01:00
parent 876dc58918
commit 07febad83a
9 changed files with 109 additions and 23 deletions

View file

@ -9,7 +9,7 @@
__class_name__(__class_name__&& in) = default; \
__class_name__& operator=(__class_name__&& in) = default
//#define DEBUG_MODE_X
#define DEBUG_MODE_X
#ifdef DEBUG_MODE_X

View file

@ -17,6 +17,8 @@ namespace MultiArrayTools
class IndefinitIndexBase
{
public:
virtual ~IndefinitIndexBase() {}
virtual IndefinitIndexBase& operator=(size_t pos) = 0;
virtual IndefinitIndexBase& operator++() = 0;

View file

@ -8,7 +8,7 @@ namespace MultiArrayTools
*******************/
template <typename T, class Range>
MultiArray<T,Range>::MultiArray(const Range& range) : mRange(new Range(range)), mCont(mRange.size())
MultiArray<T,Range>::MultiArray(const Range& range) : mRange(new Range(range)), mCont(mRange->size())
{
mInit = true;
}
@ -48,9 +48,7 @@ namespace MultiArrayTools
template <class... NameTypes>
MultiArrayOperationBase<T,Range> MultiArray<T,Range>::operator()(const NameTypes&... str)
{
auto index = mRange->begin();
index.name(Name("master", str...));
return MultiArrayOperationBase<T,Range>(*this, index);
return MultiArrayOperationBase<T,Range>(*this, Name("master", str...));
}
template <typename T, class Range>

View file

@ -6,17 +6,47 @@ namespace MultiArrayTools
/*********************************
* MultiArrayOperationBase *
*********************************/
template <typename T, class Range>
MultiArrayOperationBase<T,Range>::
MultiArrayOperationBase(MultiArray<T,Range>& ma,
const Name& nm) : mArrayRef(ma),
mIibPtr(new IndexType(mArrayRef.begin()))
{
mIibPtr->name(nm);
}
template <typename T, class Range>
MultiArrayOperationBase<T,Range>::~MultiArrayOperationBase()
{
delete mIibPtr;
}
template <typename T, class Range>
MultiArrayOperationBase<T,Range>&
MultiArrayOperationBase<T,Range>::operator=(const MultiArrayOperationBase<T,Range>& in)
{
CHECK;
in.linkIndicesTo(mIibPtr);
for(*mIibPtr = mArrayRef.begin(); *mIibPtr != mArrayRef.end(); ++(*mIibPtr)){
// build in vectorization later
get() = in.get();
}
return *this;
}
template <typename T, class Range>
template <class Range2>
MultiArrayOperationBase<T,Range>&
MultiArrayOperationBase<T,Range>::operator=(const MultiArrayOperationBase<T, Range2>& in)
{
CHECK;
in.linkIndicesTo(mIibPtr);
for(*mIibPtr = mArrayRef.begin(); *mIibPtr != mArrayRef.end(); ++(*mIibPtr)){
// build in vectorization later
get() = in.get();
}
return *this;
}
template <typename T, class Range>
@ -48,13 +78,13 @@ namespace MultiArrayTools
template <typename T, class Range>
T& MultiArrayOperationBase<T,Range>::get()
{
return mArrayRef[*mIibPtr];
return mArrayRef[*dynamic_cast<IndexType*>(mIibPtr)];
}
template <typename T, class Range>
const T& MultiArrayOperationBase<T,Range>::get() const
{
return mArrayRef[*mIibPtr];
return mArrayRef[*dynamic_cast<IndexType*>(mIibPtr)];
}
/*****************************

View file

@ -16,8 +16,13 @@ namespace MultiArrayTools
class MultiArrayOperationBase
{
public:
typedef decltype(MultiArray<T,Range>().begin()) IndexType;
MultiArrayOperationBase(MultiArray<T,Range>& ma, const IndefinitIndexBase& iib);
MultiArrayOperationBase(MultiArray<T,Range>& ma, const Name& nm);
MultiArrayOperationBase& operator=(const MultiArrayOperationBase& in);
virtual ~MultiArrayOperationBase();
// execute AnyOperation
// exception if range types are inconsitent with names
@ -52,8 +57,10 @@ namespace MultiArrayTools
protected:
// HERE !!!!!!
MultiArray<T,Range>& mArrayRef;
IndefinitIndexBase* mIibPtr;
IndefinitIndexBase* mIibPtr = nullptr;
};

View file

@ -179,14 +179,14 @@ namespace MultiArrayTools
template <class... Indices>
void MultiIndex<Indices...>::name(const Name& nm)
{
name(nm.own());
IIB::mName = nm.own();
if(nm.size() >= sizeof...(Indices)){
TupleNamer<sizeof...(Indices)-1>::nameTuple(mIPack, nm);
}
else {
Name nm2 = nm;
nm2.autoName(sizeof...(Indices));
TupleNamer<sizeof...(Indices)-1>::nameTuple(mIPack, nm);
TupleNamer<sizeof...(Indices)-1>::nameTuple(mIPack, nm2);
}
}

View file

@ -6,7 +6,17 @@ namespace MultiArrayTools
namespace
{
/*
void giveNames(std::vector<Name>& nvec)
{
nvec.clear();
}
void giveNames(std::vector<Name>& nvec, const Name& name)
{
nvec.push_back(name);
}
template <class... NameTypes>
void giveNames(std::vector<Name>& nvec, const Name& name1, const Name& name2, const NameTypes&... names)
{
@ -14,15 +24,6 @@ namespace MultiArrayTools
giveNames(nvec, name2, names...);
}
void giveNames(std::vector<Name>& nvec, const Name& name)
{
nvec.push_back(name);
}*/
void giveNames(std::vector<Name>& nvec)
{
nvec.clear();
}
}
@ -33,6 +34,13 @@ namespace MultiArrayTools
giveNames(mSub, names...);
}
template <class... NameTypes>
Name::Name(char const* mainName, const NameTypes&... names) : mMain(mainName)
{
mSub.reserve(sizeof...(NameTypes));
giveNames(mSub, names...);
}
const std::string& Name::own() const
{
return mMain;
@ -49,7 +57,7 @@ namespace MultiArrayTools
mSub.resize(newSize);
if(oldSize < newSize){
for(size_t i = oldSize; i != newSize; ++i){
mSub[i] = mMain + std::to_string( i );
mSub[i] = Name( mMain + std::to_string( i ) );
}
}
}

View file

@ -18,6 +18,9 @@ namespace MultiArrayTools
template <class... NameTypes>
Name(const std::string& mainName, const NameTypes&... names);
template <class... NameTypes>
Name(char const* mainName, const NameTypes&... names);
const std::string& own() const;
const Name& get(size_t n) const;

View file

@ -47,7 +47,27 @@ namespace {
MultiArray2dAny ma;
};
class ReorderTest : public ::testing::Test
{
protected:
typedef MAT::SingleRange<char,MAT::RangeType::ANY> Range1dAny;
typedef MAT::MultiRange<Range1dAny,Range1dAny> Range2dAny;
typedef MAT::MultiArray<int,Range2dAny> MultiArray2dAny;
ReorderTest() : r1({'a','b','c'}), r2({'a','b'}),
ra(r1,r2), rb(r2,r1),
ma(ra, {-5,6,2,1,9,54}) {}
Range1dAny r1;
Range1dAny r2;
Range2dAny ra;
Range2dAny rb;
MultiArray2dAny ma;
};
TEST_F(OneDimTest, CorrectExtensions)
{
EXPECT_EQ(ma.size(), 5);
@ -88,6 +108,24 @@ namespace {
EXPECT_EQ(ma[i(i1 = 2, i2 = 2)], 90);
EXPECT_EQ(ma[i(i1 = 2, i2 = 3)], -67);
}
TEST_F(ReorderTest, ReorderingWorks)
{
MultiArray2dAny ma2(rb);
auto i = ma2.begin();
auto i1 = i.template getIndex<0>();
auto i2 = i.template getIndex<1>();
//ma2("alpha","beta");
//ma("beta","alpha");
CHECK;
ma2("alpha","beta") = ma("beta","alpha");
EXPECT_EQ(ma2[i(i1 = 0,i2 = 0)],-5);
EXPECT_EQ(ma2[i(i1 = 1,i2 = 0)],6);
EXPECT_EQ(ma2[i(i1 = 0,i2 = 1)],2);
EXPECT_EQ(ma2[i(i1 = 1,i2 = 1)],1);
EXPECT_EQ(ma2[i(i1 = 0,i2 = 2)],9);
EXPECT_EQ(ma2[i(i1 = 1,i2 = 2)],54);
}
} // end namespace