intermediate commit

This commit is contained in:
Christian Zimmermann 2017-02-03 14:01:15 +01:00
parent aa4868afd4
commit 2d5d7770b0
2 changed files with 180 additions and 141 deletions

188
src/me.cc
View file

@ -5,52 +5,120 @@
namespace ME
{
/******************
* RangeBase *
******************/
template <class Index>
bool RangeBase<Index>::isSubRange() const
{
return false;
}
/*********************
* SubRangeBase *
*********************/
template <class Index>
bool SubRangeBase<Index>::isSubRange() const
{
return true;
}
/********************
* SingleRange *
********************/
template <typename U, RangeType TYPE>
const U& SingleRange<U,TYPE>::get(size_t pos) const
{
return mSpace[pos];
}
template <typename U, RangeType TYPE>
size_t SingleRange<U,TYPE>::get(const U& metaPos) const
{
size_t cnt = 0;
for(auto& x: mSpace){
if(x == metaPos){
return cnt;
}
++cnt;
}
return cnt;
}
/**************
* IndexBase *
**************/
size_t IndexBase::size() const
template <class Index>
Index& IndexBase<Index>::operator=(const Index& in)
{
return mPoss->size();
mPos = evaluate(in);
}
IndexBase& IndexBase::operator=(size_t pos)
template <class Index>
Index& IndexBase<Index>::operator=(size_t pos)
{
mPos = pos;
return *this;
}
template <class Index>
Index& IndexBase<Index>::operator++()
{
++mPos;
return *this;
}
template <class Index>
Index& IndexBase<Index>::operator--()
{
--mPos;
return *this;
}
template <class Index>
Index& IndexBase<Index>::operator+=(int n)
{
mPos += n;
return *this;
}
template <class Index>
Index& IndexBase<Index>::operator-=(int n)
{
mPos -= n;
return *this;
}
template <class Index>
bool IndexBase<Index>::operator==(const Index& i)
{
return mRange == i.mRange and mPos == i.mPos;
}
template <class Index>
bool IndexBase<Index>::operator!=(const Index& i)
{
return mRange != i.mRange or mPos != i.mPos;
}
/********************
* SingleIndexBase *
********************/
template <typename U, IndexType TYPE>
SingleIndexBase& SingleIndexBase<U,TYPE>::operator=(size_t pos)
const U& SingleIndexBase<U,TYPE>::getMetaPos() const
{
mPos = pos;
mActPos = (*mPoss)[pos];
return dynamic_cast<SingleRange*>( mRange )->get(mPos);
}
template <typename U, IndexType TYPE>
SingleIndexBase& SingleIndexBase<U,TYPE>::operator=(const U& upos)
size_t SingleIndexBase<U,TYPE>::evaluate(const Index& in)
{
size_t cnt = 0;
for(auto& x: *mPoss){
if(x == upos){
mPos = cnt;
return *this;
}
++cnt;
}
// THROW !!
return *this;
}
template <typename U, IndexType TYPE>
const U& SingleIndexBase<U,TYPE>::getActPos() const
{
return mActPos;
return in.mPos;
}
@ -60,75 +128,25 @@ namespace ME
namespace
{
template <class Tuple, size_t N>
size_t size(const Tuple& tp, size_t curSize = 1)
template <size_t N, class MultiIndex>
size_t evaluate_x(const MultiIndex& index)
{
return size<Tuple,N-1>( tp, std::get<N>(tp).size() * curSize );
const auto& subIndex = index.getIndex<N>();
return evaluate_x<N-1>(index) * subIndex.size() + subIndex.pos();
}
template <class Tuple>
size_t size<Tuple,0>(const Tuple& tp, size_t curSize = 1)
template <class MultiIndex>
size_t evaluate_x<0>(const MultiIndex& index)
{
return std::get<0>(tp).size() * curSize;
}
template <class Tuple, size_t N>
size_t pos(const Tuple& tp)
{
const size_t M = std::tuple_size(tp) - N;
return pos<Tuple,M-1>(tp) * std::get<M-1>(tp).size() + std::get<M>(tp).pos()
}
template <class Tuple>
size_t pos<Tuple,0>(const Tuple& tp)
{
const size_t M = std::tuple_size(tp);
return std::get<M>(tp).pos();
}
size_t varpos(size_t curPos, const I& i)
{
return curPos * i.size() + i.pos();
}
template <class... Is>
size_t varpos(size_t curPos, const I& i, const Is&... is)
{
return varpos(curPos * i.size() + i.pos(), is...);
const auto& subIndex = index.getIndex<0>();
return subIndex.pos();
}
}
template <class... Is>
size_t MultiIndexBase<Is...>::size() const
template <class... Indices>
size_t MultiIndex<Indices...>::evaluate(const MultiIndex<Indices...>& in) const
{
return size<ContType, indNum>(mCont);
}
template <class... Is>
MultiIndexBase& MultiIndexBase<Is...>::operator=(const ContType& upos)
{
mCont = upos;
mPos = pos<ContType, indNum>(mCont);
}
const ContType& MultiIndexBase<Is...>::getActPos() const
{
return mCont;
}
template <size_t N>
auto together() -> decltype(std::tuple_cat(make_left<ContType,N>(mCont),
make_right<ContType,N+1>(mCont) ))
{
return std::tuple_cat(make_left<ContType,N-1>(mCont), make_right<ContType,N+1>(mCont));
}
template <class I, size_t N>
auto add(const I& in) -> decltype(std::tuple_cat(make_left<ContType,N>(mCont), in,
make_right<ContType,N+1>(mCont)))
{
return std::tuple_cat(make_left<ContType,N>(mCont), in, make_right<ContType,N+1>(mCont));
return evaluate_x<sizeof...(Indices)-1>(in);
}
/*******************

117
src/me.h
View file

@ -70,13 +70,15 @@ namespace ME
class RangeBase
{
public:
static Index indexType;
typedef Index IndexType;
virtual size_t size() const = 0;
virtual Index begin() = 0;
virtual Index end() = 0;
virtual RangeBase* base() = 0;
virtual bool isSubRange() const;
protected:
mutable std::string mName;
};
template <class Index>
@ -98,23 +100,21 @@ namespace ME
template <typename U, RangeType TYPE>
class SingleRange : public RangeBase<SingleIndex<U,TYPE> >
{
public:
const U& get(size_t pos) const;
size_t get(const U& metaPos) const;
protected:
std::vector<U> mSpace;
};
template <class... Ranges>
class MultiRange : public RangeBase<MultiIndex<decltype(typename Ranges::indexType)...> >
class MultiRange : public RangeBase<MultiIndex<typename Ranges::indexType...> >
{
public:
static size_t dim = sizeof...(Ranges);
template <class... Ranges2>
auto combine(const MultiRange<Ranges2...>& in) -> MultiRange<Ranges... ,Ranges2...>;
template <size_t N, size_t M>
auto merge() -> /**/;
protected:
std::tuple<Ranges...> mSpace;
};
@ -123,63 +123,65 @@ namespace ME
* Index *
******************/
template <Index>
class IndexBase
{
public:
virtual size_t pos() const;
virtual IndexBase& operator=(size_t pos);
virtual IndexBase& operator++();
//virtual IndexBase operator++(int);
virtual IndexBase& operator--();
//virtual IndexBase operator--(int);
virtual IndexBase& operator+=(int n);
//virtual IndexBase operator+(int n);
virtual IndexBase& operator-=(int n);
//virtual IndexBase operator-(int n);
virtual Index& operator=(const Index& in);
virtual Index& operator=(size_t pos);
virtual Index& operator++();
virtual Index& operator--();
virtual Index& operator+=(int n);
virtual Index& operator-=(int n);
virtual bool operator==(const IndexBase& i);
virtual bool operator!=(const IndexBase& i);
virtual size_t dim() const = 0;
virtual size_t pos() const; // = mPos; implement !!!
protected:
// translate index into position
virtual size_t evaluate(const Index& in) const = 0;
size_t mPos;
RangeBase* mRange;
RangeBase<Index>* mRange;
};
template <typename U, RangeType TYPE>
class SingleIndex : public IndexBase
class SingleIndex : public IndexBase<SingleIndex<U,TYPE> >
{
public:
virtual size_t size() const override;
virtual SingleIndexBase& operator=(size_t pos) override;
virtual SingleIndexBase& operator=(const U& upos);
virtual const U& getActPos() const;
virtual const U& getMetaPos() const;
// = 1
virtual size_t dim() const override; // implement !!!
protected:
virtual size_t evaluate(const Index& in) const override;
};
template <class... Is>
class MultiIndex : public IndexBase
template <class... Indices>template <class... Indices>
class MultiIndex : public IndexBase<MultiIndex<Indices...> >
{
public:
typedef std::tuple<Is...> ContType;
typedef std::tuple<Indices...> IndexPack;
virtual size_t size() const override;
virtual MultiIndex& operator=(const U& upos);
virtual const ContType& getActPos() const;
// treat index N and N+1 as the same index
template <size_t N>
auto together() -> decltype(std::tuple_cat(make_left<ContType,N>(mCont),
make_right<ContType,N>(mCont)));
auto getIndex() -> decltype(std::get<N>(mIPack));
// Inserts an index in 'mCont' at position 'N'
template <class I, size_t N>
auto add(const I& in) -> decltype(std::tuple_cat(make_left<ContType,N>(mCont), in,
make_right<ContType,N+1>(mCont)));
// dimension of MultiRange
virtual size_t dim() const override; // implement !!!
protected:
ContType mCont;
virtual size_t evaluate(const MultiIndex& in) const override;
IndexPack mIPack;
};
/******************
@ -199,26 +201,45 @@ namespace ME
MultiArray(const Range& range); // !!!!
template <class Range2, class Range3>
MultiArray<T,Range3> operation(std::array<std::string,typename Range::dim>& d1,
std::array<std::string,typename Range2::dim>& d2,
const MultiArray<T,Range2>& in) const;
template <typename... Strings>
MultiArrayOperation<T,Range>& operator()(const Strings&... str) const;
T& operator()(const typename Range::indexType& i);
const T& operator()(const typename Range::indexType& i) const;
T& operator()(const typename Range::indexType& i); // implement
const T& operator()(const typename Range::indexType& i) const; // implement
private:
bool mInit = false;
std::vector<T> mCont;
std::shared_ptr<Range> mRange;
};
enum class NameTag
template <typename T, class Range>
class MultiArrayOperation
{
// to enumerate the ranges/indices when merging two MultiArrays, etc...
public:
template <class Range2>
MultiArrayOperation<T,Range2> operation(/*some operation*/) const;
template <class Range2, class Range3>
MultiArrayOperation<T,Range3> operation(const MultiArrayOperation<T,Range2>& in, /*some operation*/) const;
// execute operation
template <typename... Strings>
MultiArray<T,Range> operator()(const Strings&... str);
};
template <class Range1, class Range2, >
// =========
// Code that should finally work:
MultiArray<double,/*Tensor*/,/*Tensor*/,/*3DSpace*/,/*3DSpace*/> ma1;
MultiArray<double,/*Tensor*/,/*Tensor*/,/*3DSpace*/,/*3DSpace*/> ma2;
MultiArray<double,/*Tensor*/,/*Tensor*/,/*Tensor*/,/*3DSpace*/,/*3DSpace*/,/*3DSpace*/> ma3;
ma3 = ( ma1("mu","nu","x","y") * ma2("nu","lambda","y","z") )("mu","nu","lambda","x","y","z");
} // end namespace ME