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 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 * * 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; 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 * * SingleIndexBase *
********************/ ********************/
template <typename U, IndexType TYPE> template <typename U, IndexType TYPE>
SingleIndexBase& SingleIndexBase<U,TYPE>::operator=(size_t pos) const U& SingleIndexBase<U,TYPE>::getMetaPos() const
{ {
mPos = pos; return dynamic_cast<SingleRange*>( mRange )->get(mPos);
mActPos = (*mPoss)[pos];
} }
template <typename U, IndexType TYPE> 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; return in.mPos;
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;
} }
@ -60,75 +128,25 @@ namespace ME
namespace namespace
{ {
template <size_t N, class MultiIndex>
template <class Tuple, size_t N> size_t evaluate_x(const MultiIndex& index)
size_t size(const Tuple& tp, size_t curSize = 1)
{ {
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> template <class MultiIndex>
size_t size<Tuple,0>(const Tuple& tp, size_t curSize = 1) size_t evaluate_x<0>(const MultiIndex& index)
{ {
return std::get<0>(tp).size() * curSize; const auto& subIndex = index.getIndex<0>();
} return subIndex.pos();
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...);
} }
} }
template <class... Is> template <class... Indices>
size_t MultiIndexBase<Is...>::size() const size_t MultiIndex<Indices...>::evaluate(const MultiIndex<Indices...>& in) const
{ {
return size<ContType, indNum>(mCont); return evaluate_x<sizeof...(Indices)-1>(in);
}
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));
} }
/******************* /*******************

117
src/me.h
View file

@ -70,13 +70,15 @@ namespace ME
class RangeBase class RangeBase
{ {
public: public:
static Index indexType; typedef Index IndexType;
virtual size_t size() const = 0; virtual size_t size() const = 0;
virtual Index begin() = 0; virtual Index begin() = 0;
virtual Index end() = 0; virtual Index end() = 0;
virtual RangeBase* base() = 0; virtual RangeBase* base() = 0;
virtual bool isSubRange() const; virtual bool isSubRange() const;
protected:
mutable std::string mName;
}; };
template <class Index> template <class Index>
@ -98,23 +100,21 @@ namespace ME
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
class SingleRange : public RangeBase<SingleIndex<U,TYPE> > class SingleRange : public RangeBase<SingleIndex<U,TYPE> >
{ {
public:
const U& get(size_t pos) const;
size_t get(const U& metaPos) const;
protected: protected:
std::vector<U> mSpace; std::vector<U> mSpace;
}; };
template <class... Ranges> template <class... Ranges>
class MultiRange : public RangeBase<MultiIndex<decltype(typename Ranges::indexType)...> > class MultiRange : public RangeBase<MultiIndex<typename Ranges::indexType...> >
{ {
public: public:
static size_t dim = sizeof...(Ranges); 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: protected:
std::tuple<Ranges...> mSpace; std::tuple<Ranges...> mSpace;
}; };
@ -123,63 +123,65 @@ namespace ME
* Index * * Index *
******************/ ******************/
template <Index>
class IndexBase class IndexBase
{ {
public: public:
virtual size_t pos() const; virtual Index& operator=(const Index& in);
virtual IndexBase& operator=(size_t pos);
virtual IndexBase& operator++(); virtual Index& operator=(size_t pos);
//virtual IndexBase operator++(int); virtual Index& operator++();
virtual IndexBase& operator--(); virtual Index& operator--();
//virtual IndexBase operator--(int); virtual Index& operator+=(int n);
virtual IndexBase& operator+=(int n); virtual Index& operator-=(int n);
//virtual IndexBase operator+(int n);
virtual IndexBase& operator-=(int n);
//virtual IndexBase operator-(int n);
virtual bool operator==(const IndexBase& i); virtual bool operator==(const IndexBase& i);
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: protected:
// translate index into position
virtual size_t evaluate(const Index& in) const = 0;
size_t mPos; size_t mPos;
RangeBase* mRange; RangeBase<Index>* mRange;
}; };
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
class SingleIndex : public IndexBase class SingleIndex : public IndexBase<SingleIndex<U,TYPE> >
{ {
public: public:
virtual size_t size() const override; virtual size_t size() const override;
virtual SingleIndexBase& operator=(size_t pos) override; virtual SingleIndexBase& operator=(size_t pos) override;
virtual SingleIndexBase& operator=(const U& upos); 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: protected:
virtual size_t evaluate(const Index& in) const override;
}; };
template <class... Is> template <class... Indices>template <class... Indices>
class MultiIndex : public IndexBase class MultiIndex : public IndexBase<MultiIndex<Indices...> >
{ {
public: 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> template <size_t N>
auto together() -> decltype(std::tuple_cat(make_left<ContType,N>(mCont), auto getIndex() -> decltype(std::get<N>(mIPack));
make_right<ContType,N>(mCont)));
// Inserts an index in 'mCont' at position 'N' // dimension of MultiRange
template <class I, size_t N> virtual size_t dim() const override; // implement !!!
auto add(const I& in) -> decltype(std::tuple_cat(make_left<ContType,N>(mCont), in,
make_right<ContType,N+1>(mCont)));
protected: protected:
ContType mCont; virtual size_t evaluate(const MultiIndex& in) const override;
IndexPack mIPack;
}; };
/****************** /******************
@ -199,26 +201,45 @@ namespace ME
MultiArray(const Range& range); // !!!! MultiArray(const Range& range); // !!!!
template <class Range2, class Range3> template <typename... Strings>
MultiArray<T,Range3> operation(std::array<std::string,typename Range::dim>& d1, MultiArrayOperation<T,Range>& operator()(const Strings&... str) const;
std::array<std::string,typename Range2::dim>& d2,
const MultiArray<T,Range2>& in) const;
T& operator()(const typename Range::indexType& i); T& operator()(const typename Range::indexType& i); // implement
const T& operator()(const typename Range::indexType& i) const; const T& operator()(const typename Range::indexType& i) const; // implement
private: private:
bool mInit = false; bool mInit = false;
std::vector<T> mCont; std::vector<T> mCont;
std::shared_ptr<Range> mRange;
}; };
template <typename T, class Range>
enum class NameTag 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 } // end namespace ME