further development of functional array and index mapping

This commit is contained in:
Christian Zimmermann 2017-03-27 11:33:40 +02:00
parent 6126188ed0
commit 6f6de5be87
6 changed files with 167 additions and 16 deletions

52
src/ma_functional.cc Normal file
View file

@ -0,0 +1,52 @@
// -*- C++ -*-
#include "ma_functional.h"
namespace MultiArrayTools
{
namespace
{
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>
void IndexMapFunction<OutIndex,Maps...>::linkIndicesTo(IndefinitIndexBase* target)
{
/*!!!!*/
}
template <class OutIndex, class... Maps>
void IndexMapFunction<OutIndex,Maps...>::eval()
{
MapEvaluation<sizeof...(Maps)-1>::eval(mOIndex, mMap);
}
template <class OutIndex, class... Maps>
IndefinitIndexBase& IndexMapFunction<OutIndex,Maps...>::index()
{
return mOIndex;
}
}

38
src/ma_functional.h Normal file
View file

@ -0,0 +1,38 @@
// -*- C++ -*-
#ifndef __ma_functional_h__
#define __ma_functional_h__
#include <cstdlib>
#include <tuple>
#include <utility>
#include "base_def.h"
namespace MultiArrayTools
{
// Maps... are ConstMultiArrayOperationRoots where the corresponding MultiArray defines the map
template <class OutIndex, class... Maps>
class IndexMapFunction
{
public:
typedef std::tuple<Maps...> MapType;
void linkIndicesTo(IndefinitIndexBase* target);
void eval();
IndefinitIndexBase& index();
private:
MapType mMap;
OutIndex mOIndex;
};
}
#include "ma_functional.cc"
#endif

View file

@ -555,4 +555,38 @@ namespace MultiArrayTools
mb.execute(); mb.execute();
} }
*/ */
/****************************
* FunctionalMultiArray *
****************************/
template <typename T, class Range, class Function>
FunctionalMultiArray<T,Range,Function>::FunctionalMultiArray(const Range& range) :
MultiArrayBase<T>(range), mFunc() {}
template <typename T, class Range, class Function>
FunctionalMultiArray<T,Range,Function>::FunctionalMultiArray(const Range& range,
const Function& func) :
MultiArrayBase<T>(range), mFunc(func) {}
template <typename T, class Range, class Function>
const T& FunctionalMultiArray<T,Range,Function>::operator[](const typename Range::IndexType& i) const
{
mVal = mFunc(i);
return mVal;
}
template <typename T, class Range, class Function>
bool FunctionalMultiArray<T,Range,Function>::isConst() const
{
return true;
}
template <typename T, class Range, class Function>
bool FunctionalMultiArray<T,Range,Function>::isSlice() const
{
return false;
}
} }

View file

@ -233,7 +233,7 @@ namespace MultiArrayTools
std::vector<T> mCont; std::vector<T> mCont;
}; };
template <typename T, class Range> template <typename T, class Range, class Function>
class FunctionalMultiArray : public MultiArrayBase<T> class FunctionalMultiArray : public MultiArrayBase<T>
{ {
public: public:
@ -241,15 +241,17 @@ namespace MultiArrayTools
typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator; typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator;
DEFAULT_MEMBERS(FunctionalMultiArray); DEFAULT_MEMBERS(FunctionalMultiArray);
FunctionalMultiArray(const Range& range, /*Function*/); FunctionalMultiArray(const Range& range);
FunctionalMultiArray(const Range& range, const Function& func);
T& operator[](const typename Range::IndexType& i) override; virtual const T& operator[](const typename Range::IndexType& i) const override;
const T& operator[](const typename Range::IndexType& i) const override;
virtual bool isConst() const override; virtual bool isConst() const override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
protected: protected:
mutable T mVal;
Function mFunc;
// FUNCTION !!!! // FUNCTION !!!!
}; };

View file

@ -532,11 +532,13 @@ namespace MultiArrayTools
} }
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class MapFunction, class InRange, class OutRange>
MultiArrayOperationMap& operator=(const ConstMultiArrayOperationRoot<T,InRange>& in) MultiArrayOperationMap& MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::
operator=(const ConstMultiArrayOperationRoot<T,InRange>& in)
{ {
mIndex = dynamic_cast<typename InRange::IndexType&>( in.getIndex() ); mIndex = dynamic_cast<typename InRange::IndexType&>( in.getIndex() );
MAOB::mIibPtr = &mIndex; MAOB::mIibPtr = &mIndex;
mNm = in.name(); mNm = in.name();
mIndex.name(mNm); // to be sure...
mIndex.setPos( mIndex.max() ); mIndex.setPos( mIndex.max() );
typename OutRange::IndexType endIndex = mIndex; typename OutRange::IndexType endIndex = mIndex;
@ -546,34 +548,51 @@ namespace MultiArrayTools
MultiArray<size_t,OutRange> cnt(mRoot->range()); MultiArray<size_t,OutRange> cnt(mRoot->range());
auto cnto = cnt(mRoot.name(), true); auto cnto = cnt(mRoot.name(), true);
cnto.linkIndicesTo( &mMF.index() ) cnto.linkIndicesTo( &mMF.index() );
for(mIndex.setPos(0); mIndex != endIndex; ++mIndex){ for(mIndex.setPos(0), mMF.eval(); mIndex != endIndex; ++mIndex, mMF.eval()){
mRoot.get() += in.get(); get() += in.get();
++cnto.get(); ++cnto.get();
} }
// CHECK whether T / size_t mixture works!!
mRoot /= cnto; mRoot /= cnto;
} }
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class MapFunction, class InRange, class OutRange>
virtual size_t argNum() const override; size_t MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::argNum() const
{
return 1;
}
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class MapFunction, class InRange, class OutRange>
virtual IndefinitIndexBase* getLinked(const std::string& name) const override; IndefinitIndexBase* MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::
getLinked(const std::string& name) const
{
return mRoot.getLinked(name);
}
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class MapFunction, class InRange, class OutRange>
virtual void linkIndicesTo(IndefinitIndexBase* target) const override; void MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::linkIndicesTo(IndefinitIndexBase* target) const
{
mRoot.linkIndicesTo(target);
}
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class MapFunction, class InRange, class OutRange>
virtual void setInternalLinks() const override; void MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::setInternalLinks() const
{ }
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class MapFunction, class InRange, class OutRange>
virtual const T& get() const override; const T& MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::get() const
{
return mRoot.get();
}
template <typename T, class MapFunction, class InRange, class OutRange> template <typename T, class MapFunction, class InRange, class OutRange>
virtual T& get() override; T& MultiArrayOperationMap<T,MapFunction,InRange,OutRange>::get()
{
return mRoot.get();
}
/***************************** /*****************************
* MultiArrayOperation * * MultiArrayOperation *
*****************************/ *****************************/
@ -625,6 +644,10 @@ namespace MultiArrayTools
-> decltype(op(std::get<0>(tp).get(), args.get()...)) -> decltype(op(std::get<0>(tp).get(), args.get()...))
{ {
return op(std::get<0>(tp).get(), args.get()...); return op(std::get<0>(tp).get(), args.get()...);
/*
return op(static_cast<typename Operation::result_type>( std::get<0>(tp).get() ),
static_cast<typename Operation::result_type>( args.get() )...);
*/
} }
template <class Operation, class Tuple, class... MAOps> template <class Operation, class Tuple, class... MAOps>

View file

@ -18,6 +18,8 @@ namespace MultiArrayTools
{ {
public: public:
typedef T ValType;
MultiArrayOperationBase() /*{ CHECK; }*/ = default; MultiArrayOperationBase() /*{ CHECK; }*/ = default;
virtual ~MultiArrayOperationBase(); virtual ~MultiArrayOperationBase();