finished first draft of index mapping (TO BE TESTED AND REFACTORED)

This commit is contained in:
Christian Zimmermann 2017-03-27 19:29:51 +02:00
parent 6f6de5be87
commit 83a712a772
11 changed files with 176 additions and 88 deletions

View file

@ -108,6 +108,10 @@ namespace MultiArrayTools
// multi_array_operation.h // multi_array_operation.h
template <typename T, class Range> template <typename T, class Range>
class ConstMultiArrayOperationRoot; class ConstMultiArrayOperationRoot;
// multi_array_operation.h
template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
class MultiArrayOperationMap;
// multi_array_operation.h // multi_array_operation.h
template <typename T, class Operation, class... MAOps> template <typename T, class Operation, class... MAOps>
@ -132,6 +136,13 @@ namespace MultiArrayTools
// manipulator.h // manipulator.h
template <typename T> template <typename T>
class BinReader; class BinReader;
// ma_functional.h
template <class InRange, class OutRange>
class IndexMapFunction;
// ma_functional.h
class vec3d2Function;
} }
#endif #endif

View file

@ -4,49 +4,48 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
namespace
template <class InRange, class OutRange>
IndexMapFunction<InRange,OutRange>::
IndexMapFunction(const MultiArrayBase<typename OutRange::IndexType,InRange>& ma,
const OutRange& outRange,
const Name& inName, const Name& outName) : mMap(ma, inName),
mOutRange(new OutRange( outRange )),
mOIndex(mOutRange->begin())
{ {
mOIndex.name(outName);
template <size_t N> }
struct MapEvaluation
{
template <class OutIndex, class MapTuple>
static void eval(OutIndex& oi, const MapTuple& mt)
{
oi.template getIndex<N>() = std::get<N>(mt);
MapEvaluation<N-1>::eval(oi, mt);
}
};
template <>
struct MapEvaluation<0>
{
template <class OutIndex, class MapTuple>
static void eval(OutIndex& oi, const MapTuple& mt)
{
oi.template getIndex<0>() = std::get<0>(mt);
}
};
} // anonymous namespace
template <class OutIndex, class... Maps> template <class InRange, class OutRange>
void IndexMapFunction<OutIndex,Maps...>::linkIndicesTo(IndefinitIndexBase* target) void IndexMapFunction<InRange,OutRange>::linkIndicesTo(IndefinitIndexBase* target)
{ {
/*!!!!*/ mMap.linkIndicesTo(target);
} }
template <class OutIndex, class... Maps> template <class InRange, class OutRange>
void IndexMapFunction<OutIndex,Maps...>::eval() void IndexMapFunction<InRange,OutRange>::eval() const
{ {
MapEvaluation<sizeof...(Maps)-1>::eval(mOIndex, mMap); mOIndex.copyPos( mMap.get() );
} }
template <class OutIndex, class... Maps> template <class InRange, class OutRange>
IndefinitIndexBase& IndexMapFunction<OutIndex,Maps...>::index() IndefinitIndexBase& IndexMapFunction<InRange,OutRange>::index() const
{ {
return mOIndex; return mOIndex;
} }
/*
vec3d2Function::vec3d2Function(std::shared_ptr<OutRange>& outRange) : mOutRange(outRange),
out(mOutRange->begin()) {}
*/
vec3d2Function::OutIndex vec3d2Function::operator()(const InIndex& i) const
{
OutSubIndex& osi = out.template getIndex<0>();
osi.atMeta( i.template getIndex<0>().getMetaPos() * i.template getIndex<0>().getMetaPos() +
i.template getIndex<1>().getMetaPos() * i.template getIndex<1>().getMetaPos() +
i.template getIndex<2>().getMetaPos() * i.template getIndex<2>().getMetaPos() );
return out;
}
} }

View file

@ -12,25 +12,52 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
// Maps... are ConstMultiArrayOperationRoots where the corresponding MultiArray defines the map // Map is a ConstMultiArrayOperationRoot where the corresponding MultiArray defines the map
template <class OutIndex, class... Maps> template <class InRange, class OutRange>
class IndexMapFunction class IndexMapFunction
{ {
public: public:
typedef std::tuple<Maps...> MapType; typedef OutRange OR;
typedef typename OutRange::IndexType OutIndex;
IndexMapFunction(const MultiArrayBase<typename OutRange::IndexType,InRange>& ma,
const OutRange& outRange,
const Name& inName, const Name& outName);
void linkIndicesTo(IndefinitIndexBase* target); void linkIndicesTo(IndefinitIndexBase* target);
void eval(); void eval() const;
IndefinitIndexBase& index(); IndefinitIndexBase& index() const;
private: private:
MapType mMap; ConstMultiArrayOperationRoot<typename OutRange::IndexType,InRange> mMap;
OutIndex mOIndex; std::shared_ptr<OutRange> mOutRange;
mutable typename OutRange::IndexType mOIndex;
}; };
class vec3d2Function
{
public:
typedef SingleIndex<int,RangeType::SPACE> CoordIndex;
typedef MultiIndex<CoordIndex,CoordIndex,CoordIndex> InIndex;
typedef SingleIndex<size_t,RangeType::DISTANCE> OutSubIndex;
typedef MultiIndex<OutSubIndex> OutIndex;
vec3d2Function() = default;
//vec3d2Function(std::shared_ptr<OutRange>& outRange);
vec3d2Function(const vec3d2Function& in) = default;
vec3d2Function& operator=(const vec3d2Function& in) = default;
OutIndex operator()(const InIndex& i) const;
private:
//std::shared_ptr<OutRange> mOutRange;
mutable OutIndex out;
};
} }
#include "ma_functional.cc" #include "ma_functional.cc"

View file

@ -563,12 +563,12 @@ 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), mFunc() {} MultiArrayBase<T,Range>(range), mFunc() {}
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,
const Function& func) : const Function& func) :
MultiArrayBase<T>(range), mFunc(func) {} MultiArrayBase<T,Range>(range), mFunc(func) {}
template <typename T, class Range, class Function> template <typename T, class Range, class Function>
const T& FunctionalMultiArray<T,Range,Function>::operator[](const typename Range::IndexType& i) const const T& FunctionalMultiArray<T,Range,Function>::operator[](const typename Range::IndexType& i) const

View file

@ -22,6 +22,8 @@ namespace MultiArrayTools
{ {
public: public:
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>
{ {
public: public:
@ -234,7 +236,7 @@ namespace MultiArrayTools
}; };
template <typename T, class Range, class Function> template <typename T, class Range, class Function>
class FunctionalMultiArray : public MultiArrayBase<T> class FunctionalMultiArray : public MultiArrayBase<T,Range>
{ {
public: public:
typedef MultiArrayBase<T,Range> MAB; typedef MultiArrayBase<T,Range> MAB;
@ -252,7 +254,6 @@ namespace MultiArrayTools
protected: protected:
mutable T mVal; mutable T mVal;
Function mFunc; Function mFunc;
// FUNCTION !!!!
}; };
} }

View file

@ -14,6 +14,7 @@
#include "slice.h" #include "slice.h"
#include "manipulator.h" #include "manipulator.h"
#include "range_transformer.h" #include "range_transformer.h"
#include "ma_functional.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -38,6 +39,7 @@ namespace MultiArrayTools
typedef SingleRange<double,RangeType::ANY> GenericFR; typedef SingleRange<double,RangeType::ANY> GenericFR;
typedef SingleRange<size_t,RangeType::LORENTZ> LorentzR; typedef SingleRange<size_t,RangeType::LORENTZ> LorentzR;
typedef SingleRange<int,RangeType::SPACE> Space1dNR; typedef SingleRange<int,RangeType::SPACE> Space1dNR;
typedef SingleRange<size_t,RangeType::DISTANCE> DistanceNR;
typedef SingleRange<int,RangeType::MOMENTUM> Mom1dNR; typedef SingleRange<int,RangeType::MOMENTUM> Mom1dNR;
typedef SingleRange<size_t, RangeType::ENSEMBLE> EnsR; typedef SingleRange<size_t, RangeType::ENSEMBLE> EnsR;
typedef MultiRange<Space1dNR,Space1dNR,Space1dNR> Space3dNR; typedef MultiRange<Space1dNR,Space1dNR,Space1dNR> Space3dNR;

View file

@ -175,6 +175,13 @@ namespace MultiArrayTools
MAOps...>(cop, *ind, begin, end, *this, mao...); MAOps...>(cop, *ind, begin, end, *this, mao...);
} }
template <typename T, class Range>
template<class TotalInRange, class InRange, class OutRange>
MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,Range>
MultiArrayOperationRoot<T,Range>::map(const IndexMapFunction<InRange,OutRange>& imf)
{
return MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,Range>(*this, imf);
}
template <typename T, class Range> template <typename T, class Range>
template <class MAOp> template <class MAOp>
@ -517,10 +524,11 @@ namespace MultiArrayTools
* MultiArrayOperationMap * * MultiArrayOperationMap *
********************************/ ********************************/
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
MultiArrayOperationMap<T,MapFunction,InRange,OutRange>:: MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>::
MultiArrayOperationMap(MultiArrayOperationRoot<T,OutRange>& root, const MapFunction mf) : MultiArrayOperationMap(MultiArrayOperationRoot<T,TotalRange>& root,
MultiArrayOperationBase<T>(), const IndexMapFunction<InRange,OutRange>& mf) :
MutableMultiArrayOperationBase<T>(),
mMF(mf), mMF(mf),
mRoot(root) mRoot(root)
//mIndex(mArrayRef.beginIndex()), //mIndex(mArrayRef.beginIndex()),
@ -531,11 +539,41 @@ namespace MultiArrayTools
//mIndex.name(nm); //mIndex.name(nm);
} }
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
MultiArrayOperationMap& MultiArrayOperationMap<T,MapFunction,InRange,OutRange>:: MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>&
operator=(const ConstMultiArrayOperationRoot<T,InRange>& in) MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>::
operator=(const MultiArrayOperationRoot<T,TotalInRange>& in)
{ {
mIndex = dynamic_cast<typename InRange::IndexType&>( in.getIndex() ); mIndex = dynamic_cast<typename TotalInRange::IndexType const&>( in.index() );
MAOB::mIibPtr = &mIndex;
mNm = in.name();
mIndex.name(mNm); // to be sure...
mIndex.setPos( mIndex.max() );
typename TotalInRange::IndexType endIndex = mIndex;
// Implement Map Functions !!!!
mRoot.linkIndicesTo( &mMF.index() );
mMF.linkIndicesTo( &mIndex );
MultiArray<size_t,TotalRange> cnt(mRoot->range());
auto cnto = cnt(mRoot.name(), true);
cnto.linkIndicesTo( &mMF.index() );
for(mIndex.setPos(0), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){
get() += in.get();
++cnto.get();
}
// CHECK whether T / size_t mixture works!!
mRoot /= cnto;
return *this;
}
template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>&
MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>::
operator=(const ConstMultiArrayOperationRoot<T,TotalInRange>& in)
{
mIndex = dynamic_cast<typename InRange::IndexType&>( in.index() );
MAOB::mIibPtr = &mIndex; MAOB::mIibPtr = &mIndex;
mNm = in.name(); mNm = in.name();
mIndex.name(mNm); // to be sure... mIndex.name(mNm); // to be sure...
@ -556,39 +594,42 @@ namespace MultiArrayTools
} }
// CHECK whether T / size_t mixture works!! // CHECK whether T / size_t mixture works!!
mRoot /= cnto; mRoot /= cnto;
return *this;
} }
template <typename T, class MapFunction, class InRange, class OutRange>
size_t MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::argNum() const template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
size_t MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>::argNum() const
{ {
return 1; return 1;
} }
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
IndefinitIndexBase* MultiArrayOperationMap<T,MapFunction,InRange,OutRange>:: IndefinitIndexBase* MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>::
getLinked(const std::string& name) const getLinked(const std::string& name) const
{ {
return mRoot.getLinked(name); return mRoot.getLinked(name);
} }
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
void MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::linkIndicesTo(IndefinitIndexBase* target) const void MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>::
linkIndicesTo(IndefinitIndexBase* target) const
{ {
mRoot.linkIndicesTo(target); mRoot.linkIndicesTo(target);
} }
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
void MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::setInternalLinks() const void MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>::setInternalLinks() const
{ } { }
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
const T& MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::get() const const T& MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>::get() const
{ {
return mRoot.get(); return mRoot.get();
} }
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
T& MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::get() T& MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,TotalRange>::get()
{ {
return mRoot.get(); return mRoot.get();
} }

View file

@ -18,7 +18,7 @@ namespace MultiArrayTools
{ {
public: public:
typedef T ValType; typedef T value_type;
MultiArrayOperationBase() /*{ CHECK; }*/ = default; MultiArrayOperationBase() /*{ CHECK; }*/ = default;
virtual ~MultiArrayOperationBase(); virtual ~MultiArrayOperationBase();
@ -97,6 +97,9 @@ namespace MultiArrayTools
size_t end, size_t end,
const MAOps&... mao) const; const MAOps&... mao) const;
template<class TotalInRange, class InRange, class OutRange>
MultiArrayOperationMap<T,InRange,TotalInRange,OutRange,Range>
map(const IndexMapFunction<InRange,OutRange>& imf);
template <class MAOp> template <class MAOp>
auto operator+(const MAOp& sec) -> decltype(operator()(std::plus<T>(), sec)); auto operator+(const MAOp& sec) -> decltype(operator()(std::plus<T>(), sec));
@ -238,15 +241,18 @@ namespace MultiArrayTools
Name mNm; Name mNm;
}; };
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
class MultiArrayOperationMap : public MutableMultiArrayOperationBase<T> class MultiArrayOperationMap : public MutableMultiArrayOperationBase<T>
{ {
public: public:
typedef MultiArrayOperationBase<T> MAOB; typedef MultiArrayOperationBase<T> MAOB;
MultiArrayOperationMap(MultiArrayOperationRoot<T,OutRange>& root, const MapFunction mf); MultiArrayOperationMap(MultiArrayOperationRoot<T,TotalRange>& root,
MultiArrayOperationMap& operator=(const ConstMultiArrayOperationRoot<T,InRange>& in); const IndexMapFunction<InRange,OutRange>& mf);
MultiArrayOperationMap& operator=(const MultiArrayOperationRoot<T,TotalInRange>& in);
MultiArrayOperationMap& operator=(const ConstMultiArrayOperationRoot<T,TotalInRange>& in);
virtual size_t argNum() const override; virtual size_t argNum() const override;
virtual IndefinitIndexBase* getLinked(const std::string& name) const override; virtual IndefinitIndexBase* getLinked(const std::string& name) const override;
@ -258,9 +264,9 @@ namespace MultiArrayTools
virtual T& get() override; virtual T& get() override;
// !!!! // !!!!
protected: protected:
MapFunction mMF; IndexMapFunction<InRange,OutRange> mMF;
MultiArrayOperationRoot<T,OutRange>& mRoot; MultiArrayOperationRoot<T,TotalRange>& mRoot;
mutable IndexType mIndex; // Index of incoming range mutable typename TotalInRange::IndexType mIndex; // Index of incoming range
Name mNm; // Name of incoming range Name mNm; // Name of incoming range
}; };

View file

@ -21,7 +21,8 @@ namespace MultiArrayTools
LORENTZ = 4, LORENTZ = 4,
SPIN = 5, SPIN = 5,
ENSEMBLE = 6, ENSEMBLE = 6,
VALUE_ERROR = 7 VALUE_ERROR = 7,
DISTANCE = 8
}; };
class MultiRangeType class MultiRangeType

View file

@ -96,38 +96,38 @@ namespace MultiArrayTools
// //
SingleRange<size_t,RangeType::SPACE>::SingleRange(size_t ext) : SingleRange<size_t,RangeType::DISTANCE>::SingleRange(size_t ext) :
RangeBase<SingleIndex<size_t,RangeType::SPACE> >(), RangeBase<SingleIndex<size_t,RangeType::DISTANCE> >(),
mExt(ext) {} mExt(ext) {}
size_t SingleRange<size_t,RangeType::SPACE>::get(size_t pos) const size_t SingleRange<size_t,RangeType::DISTANCE>::get(size_t pos) const
{ {
return pos; return pos;
} }
size_t SingleRange<size_t,RangeType::SPACE>::getMeta(size_t metaPos) const size_t SingleRange<size_t,RangeType::DISTANCE>::getMeta(size_t metaPos) const
{ {
return metaPos; return metaPos;
} }
size_t SingleRange<size_t,RangeType::SPACE>::size() const size_t SingleRange<size_t,RangeType::DISTANCE>::size() const
{ {
return mExt; return mExt;
} }
MultiRangeType SingleRange<size_t,RangeType::SPACE>::type() const MultiRangeType SingleRange<size_t,RangeType::DISTANCE>::type() const
{ {
return MultiRangeType(RangeType::SPACE); return MultiRangeType(RangeType::DISTANCE);
} }
SingleIndex<size_t,RangeType::SPACE> SingleRange<size_t,RangeType::SPACE>::begin() const SingleIndex<size_t,RangeType::DISTANCE> SingleRange<size_t,RangeType::DISTANCE>::begin() const
{ {
return SingleIndex<size_t,RangeType::SPACE>(this, 0); return SingleIndex<size_t,RangeType::DISTANCE>(this, 0);
} }
SingleIndex<size_t,RangeType::SPACE> SingleRange<size_t,RangeType::SPACE>::end() const SingleIndex<size_t,RangeType::DISTANCE> SingleRange<size_t,RangeType::DISTANCE>::end() const
{ {
return SingleIndex<size_t,RangeType::SPACE>(this, size()); return SingleIndex<size_t,RangeType::DISTANCE>(this, size());
} }
// //

View file

@ -113,10 +113,10 @@ namespace MultiArrayTools
}; };
template <> template <>
class SingleRange<size_t,RangeType::SPACE> : public RangeBase<SingleIndex<size_t,RangeType::SPACE> > class SingleRange<size_t,RangeType::DISTANCE> : public RangeBase<SingleIndex<size_t,RangeType::DISTANCE> >
{ {
public: public:
typedef typename RangeBase<SingleIndex<int,RangeType::SPACE> >::IndexType IndexType; typedef typename RangeBase<SingleIndex<size_t,RangeType::DISTANCE> >::IndexType IndexType;
DEFAULT_MEMBERS(SingleRange); DEFAULT_MEMBERS(SingleRange);
@ -129,8 +129,8 @@ namespace MultiArrayTools
virtual MultiRangeType type() const override; virtual MultiRangeType type() const override;
SingleIndex<size_t,RangeType::SPACE> begin() const override; SingleIndex<size_t,RangeType::DISTANCE> begin() const override;
SingleIndex<size_t,RangeType::SPACE> end() const override; SingleIndex<size_t,RangeType::DISTANCE> end() const override;
protected: protected:
size_t mExt; size_t mExt;