start with map range (not finished)

This commit is contained in:
Christian Zimmermann 2018-08-07 21:49:21 +02:00
parent 8ff366d40f
commit f5cf35ae69
3 changed files with 678 additions and 1 deletions

View file

@ -62,6 +62,10 @@ namespace MultiArrayTools
virtual ConstOperationRoot<T,SRanges...>
operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const;
template <class... MappedRanges>
ConstOperationRoot<T,SRanges...>
operator()(const std::shared_ptr<typename MappedRanges::IndexType>&... inds) const;
virtual bool isInit() const;
template <size_t N>
@ -103,7 +107,13 @@ namespace MultiArrayTools
virtual ConstOperationRoot<T,SRanges...>
operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const override;
virtual OperationRoot<T,SRanges...> operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds);
template <class... MappedRanges>
ConstOperationRoot<T,SRanges...>
operator()(const std::shared_ptr<typename MappedRanges::IndexType>&... inds);
};
@ -198,6 +208,15 @@ namespace MultiArrayTools
return ConstOperationRoot<T,SRanges...>(*this, inds...);
}
template <class... MappedRanges>
ConstOperationRoot<T,SRanges...>
MultiArrayBase<T,SRanges...>::operator()(const std::shared_ptr<typename MappedRanges::IndexType>&... inds) const
{
static_assert(sizeof...(SRanges) == sizeof...(MappedRanges),
"number of mapped ranges must be equal to number of original ranges");
return (*this)(MapResult(inds)...);
}
template <typename T, class... SRanges>
bool MultiArrayBase<T,SRanges...>::isInit() const
{
@ -259,6 +278,15 @@ namespace MultiArrayTools
return ConstOperationRoot<T,SRanges...>(*this, inds...);
}
template <class... MappedRanges>
ConstOperationRoot<T,SRanges...>
MultiArrayBase<T,SRanges...>::operator()(const std::shared_ptr<typename MappedRanges::IndexType>&... inds)
{
static_assert(sizeof...(SRanges) == sizeof...(MappedRanges),
"number of mapped ranges must be equal to number of original ranges");
return (*this)(MapResult(inds)...);
}
} // end namespace MultiArrayTools
#endif

View file

@ -97,6 +97,12 @@ namespace MultiArrayTools
std::intptr_t mPtrNum;
};
template <class Index>
std::shared_ptr<Index> mapResult(const std::shared_ptr<Index>& iptr)
{
return iptr;
}
}
/* ========================= *

View file

@ -0,0 +1,643 @@
// -*- C++ -*-
#ifndef __map_range_h__
#define __map_range_h__
#include <cstdlib>
#include <tuple>
#include <memory>
#include <map>
//#include "base_def.h"
#include "ranges/range_base.h"
#include "ranges/index_base.h"
#include "ranges/rpack_num.h"
#include "ranges/multi_range_factory_product_map.h"
#include "ranges/x_to_string.h"
#include "ranges/type_map.h"
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
template <class MapF, class... Indices>
class MapIndex : public IndexInterface<MapIndex<MapF,Indices...>,
std::tuple<typename Indices::MetaType...> >
{
public:
typedef IndexInterface<MapIndex<MapF,Indices...>,
std::tuple<typename Indices::MetaType...> > IB;
typedef std::tuple<std::shared_ptr<Indices>...> IndexPack;
typedef std::tuple<typename Indices::MetaType...> MetaType;
typedef MapRange<typename Indices::RangeType...> RangeType;
typedef MapIndex IType;
typedef SingleIndex<MapF::value_type,SpaceType::ANY> OIType;
static constexpr IndexType sType() { return IndexType::MULTI; }
static constexpr size_t sDim() { return sizeof...(Indices); }
static constexpr size_t totalDim() { return mkTotalDim<MapF,Indices...>(); }
static constexpr SpaceType STYPE = SpaceType::ANY;
private:
IndexPack mIPack;
std::array<size_t,sizeof...(Indices)+1> mBlockSizes;
std::shared_ptr<OIType> mOutIndex;
public:
const IndexPack& pack() const { return mIPack; }
MapIndex() = delete;
// NO DEFAULT HERE !!!
// ( have to assign sub-indices (ptr!) correctly )
//MapIndex(const MapIndex& in);
//MapIndex& operator=(const MapIndex& in);
template <class MRange>
MapIndex(const std::shared_ptr<MRange>& range);
template <size_t DIR>
MapIndex& up();
template <size_t DIR>
MapIndex& down();
template <size_t N>
auto get() const -> decltype( *std::get<N>( mIPack ) )&;
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mIPack ) )&;
std::shared_ptr<OIType> outIndex() const;
// raplace instances (in contrast to its analogon in ContainerIndex
// MultiIndices CANNOT be influences be its subindices, so there is
// NO foreign/external controll)
// Do NOT share index instances between two or more MapIndex instances
MapIndex& operator()(std::shared_ptr<Indices>&... indices);
// ==== >>>>> STATIC POLYMORPHISM <<<<< ====
IndexType type() const;
MapIndex& operator=(size_t pos);
MapIndex& operator++();
MapIndex& operator--();
int pp(std::intptr_t idxPtrNum);
int mm(std::intptr_t idxPtrNum);
std::string stringMeta() const;
MetaType meta() const;
MapIndex& at(const MetaType& metaPos);
size_t dim();
bool first();
bool last();
std::shared_ptr<RangeType> range();
template <size_t N>
auto getPtr() -> decltype( std::get<N>( mIPack ) )&;
size_t getStepSize(size_t n);
std::string id() const;
void print(size_t offset);
template <class Exprs>
auto ifor(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkFors(mIPack, mOutIndex, exs));
/*
template <class Exprs>
auto iforh(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs));
*/
};
template <class MapF, class... Indices>
auto mapResult<MapIndex<MapF,Indices...> >(const std::shared_ptr<MapIndex<MapF,Indices...> >& ind)
-> decltype(ind->outIndex())
{
return ind->outIndex();
}
/*************************
* MapRangeFactory *
*************************/
// NOT THREAD SAVE
template <class MapF, class... Ranges>
class MapRangeFactory : public RangeFactoryBase
{
public:
typedef MapRange<MapF,Ranges...> oType;
MapRangeFactory() = delete;
MapRangeFactory(const std::shared_ptr<Ranges>&... rs);
MapRangeFactory(const typename MapRange<MapF,Ranges...>::Space& space);
template <typename T>
MapRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr);
virtual std::shared_ptr<RangeBase> create() override;
private:
std::shared_ptr<RangeBase> checkIfCreated(const std::tuple<std::shared_ptr<Ranges>...>& ptp);
};
/******************
* MapRange *
******************/
template <class MapF, class... Ranges>
class MapRange : public RangeInterface<MapIndex<typename Ranges::IndexType...> >
{
public:
typedef RangeBase RB;
typedef std::tuple<std::shared_ptr<Ranges>...> Space;
typedef MapIndex<typename Ranges::IndexType...> IndexType;
typedef MapRange RangeType;
typedef SingleRange<MapF::value_type,SpaceType::ANY> ORType;
typedef SingleRangeFactory<MapF::value_type,SpaceType::ANY> ORFType;
//typedef typename RangeInterface<MapIndex<typename Ranges::IndexType...> >::IndexType IndexType;
protected:
MapRange() = delete;
MapRange(const MapRange& in) = delete;
MapRange& operator=(const MapRange& in) = delete;
MapRange(const MapF& mapf, const std::shared_ptr<Ranges>&... rs);
MapRange(const MapF& mapf, const Space& space);
Space mSpace;
shred_ptr<ORType> mOutRange;
public:
static const size_t sdim = sizeof...(Ranges);
template <size_t N>
auto get() const -> decltype( *std::get<N>( mSpace ) )&;
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mSpace ) )&;
shred_ptr<ORType> outRange() const;
virtual size_t dim() const final;
virtual size_t size() const final;
virtual SpaceType spaceType() const final;
virtual std::string stringMeta(size_t pos) const final;
virtual std::vector<char> data() const final;
const Space& space() const;
virtual IndexType begin() const final;
virtual IndexType end() const final;
template <class... ERanges>
auto cat(const std::shared_ptr<MapRange<ERanges...> >& erange)
-> std::shared_ptr<MapRange<Ranges...,ERanges...> >;
friend MapRangeFactory<MapF,Ranges...>;
static constexpr bool HASMETACONT = false;
static constexpr bool defaultable = false;
static constexpr size_t ISSTATIC = SubProp<MapF,Ranges...>::ISSTATIC;
static constexpr size_t SIZE = SubProp<MapF,Ranges...>::SIZE;
};
}
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
// -> define in range_base.cc
std::shared_ptr<RangeFactoryBase> mkMULTI(const char** dp);
/******************
* MapIndex *
******************/
/*
template <class MapF, class... Indices>
MapIndex<MapF,Indices...>::MapIndex(const MapIndex<MapF,Indices...>& in) :
IndexInterface<std::tuple<typename Indices::MetaType...> >(in)
{
RPackNum<sizeof...(Indices)-1>::copy(mIPack, in);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
}
template <class MapF, class... Indices>
MapIndex<MapF,Indices...>& MapIndex<MapF,Indices...>::operator=(const MapIndex<MapF,Indices...>& in)
{
IndexI::operator=(in);
RPackNum<sizeof...(Indices)-1>::copy(mIPack, in);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this;
}
*/
template <class MapF, class... Indices>
template <class MRange>
MapIndex<MapF,Indices...>::MapIndex(const std::shared_ptr<MRange>& range) :
IndexInterface<MapIndex<MapF,Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0)
{
RPackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
std::get<sizeof...(Indices)>(mBlockSizes) = 1;
RPackNum<sizeof...(Indices)-1>::initBlockSizes(mBlockSizes, mIPack); // has one more element!
mOutIndex = std::dynamic_pointer_cast<ORType>( IB::mRangePtr )->outRange()->begin();
}
template <class MapF, class... Indices>
template <size_t DIR>
MapIndex<MapF,Indices...>& MapIndex<MapF,Indices...>::up()
{
static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices");
IB::mPos += RPackNum<sizeof...(Indices)-DIR-1>::blockSize( mIPack );
RPackNum<DIR>::pp( mIPack );
return *this;
}
template <class MapF, class... Indices>
template <size_t DIR>
MapIndex<MapF,Indices...>& MapIndex<MapF,Indices...>::down()
{
static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices");
IB::mPos -= RPackNum<sizeof...(Indices)-DIR-1>::blockSize( mIPack );
RPackNum<DIR>::mm( mIPack );
return *this;
}
template <class MapF, class... Indices>
template <size_t N>
auto MapIndex<MapF,Indices...>::get() const -> decltype( *std::get<N>( mIPack ) )&
{
return *std::get<N>(mIPack);
}
template <class MapF, class... Indices>
template <size_t N>
auto MapIndex<MapF,Indices...>::getPtr() const -> decltype( std::get<N>( mIPack ) )&
{
return std::get<N>(mIPack);
}
template <class MapF, class... Indices>
std::shared_ptr<OIType> MapIndex<MapF,Indices...>::outIndex() const
{
return mOutIndex;
}
template <class MapF, class... Indices>
MapIndex<MapF,Indices...>& MapIndex<MapF,Indices...>::operator()(std::shared_ptr<Indices>&... indices)
{
RPackNum<sizeof...(Indices)-1>::swapIndices(mIPack, indices...);
RPackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, IB::mPos);
return *this;
}
template <class MapF, class... Indices>
IndexType MapIndex<MapF,Indices...>::type() const
{
return IndexType::MULTI;
}
template <class MapF, class... Indices>
MapIndex<MapF,Indices...>& MapIndex<MapF,Indices...>::operator=(size_t pos)
{
IB::mPos = pos;
RPackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, pos);
return *this;
}
template <class MapF, class... Indices>
MapIndex<MapF,Indices...>& MapIndex<MapF,Indices...>::operator++()
{
RPackNum<sizeof...(Indices)-1>::pp( mIPack );
++IB::mPos;
return *this;
}
template <class MapF, class... Indices>
MapIndex<MapF,Indices...>& MapIndex<MapF,Indices...>::operator--()
{
RPackNum<sizeof...(Indices)-1>::mm( mIPack );
--IB::mPos;
return *this;
}
template <class MapF, class... Indices>
int MapIndex<MapF,Indices...>::pp(std::intptr_t idxPtrNum)
{
int tmp = RPackNum<sizeof...(Indices)-1>::pp(mIPack, mBlockSizes, idxPtrNum);
IB::mPos += tmp;
return tmp;
}
template <class MapF, class... Indices>
int MapIndex<MapF,Indices...>::mm(std::intptr_t idxPtrNum)
{
int tmp = RPackNum<sizeof...(Indices)-1>::mm(mIPack, mBlockSizes, idxPtrNum);
IB::mPos -= tmp;
return tmp;
}
template <class MapF, class... Indices>
std::string MapIndex<MapF,Indices...>::stringMeta() const
{
return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr )->stringMeta(IB::mPos);
}
template <class MapF, class... Indices>
typename MapIndex<MapF,Indices...>::MetaType MapIndex<MapF,Indices...>::meta() const
{
MetaType metaTuple;
RPackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack);
return metaTuple;
}
template <class MapF, class... Indices>
MapIndex<MapF,Indices...>& MapIndex<MapF,Indices...>::at(const MetaType& metaPos)
{
RPackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this;
}
template <class MapF, class... Indices>
size_t MapIndex<MapF,Indices...>::dim()
{
return sizeof...(Indices);
}
template <class MapF, class... Indices>
bool MapIndex<MapF,Indices...>::first()
{
return IB::mPos == 0;
}
template <class MapF, class... Indices>
bool MapIndex<MapF,Indices...>::last()
{
return IB::mPos == IB::mMax - 1;
}
template <class MapF, class... Indices>
std::shared_ptr<typename MapIndex<MapF,Indices...>::RangeType>
MapIndex<MapF,Indices...>::range()
{
return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr );
}
template <class MapF, class... Indices>
template <size_t N>
auto MapIndex<MapF,Indices...>::getPtr() -> decltype( std::get<N>( mIPack ) )&
{
return std::get<N>(mIPack);
}
template <class MapF, class... Indices>
size_t MapIndex<MapF,Indices...>::getStepSize(size_t n)
{
if(n >= sizeof...(Indices)){
assert(0);
// throw !!
}
return mBlockSizes[n+1];
}
template <class MapF, class... Indices>
std::string MapIndex<MapF,Indices...>::id() const
{
return std::string("mul") + std::to_string(IB::mId);
}
template <class MapF, class... Indices>
void MapIndex<MapF,Indices...>::print(size_t offset)
{
if(offset == 0){
std::cout << " === " << std::endl;
}
for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; }
std::cout << id() << "[" << reinterpret_cast<std::intptr_t>(this)
<< "]" << "(" << IB::mRangePtr << "): " << meta() << std::endl;
RPackNum<sizeof...(Indices)-1>::printIndex(mIPack, offset+1);
}
template <class MapF, class... Indices>
template <class Exprs>
auto MapIndex<MapF,Indices...>::ifor(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, exs))
{
return RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, exs);
}
template <class MapF, class... Indices>
template <class Exprs>
auto MapIndex<MapF,Indices...>::iforh(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs))
{
return RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs);
}
/*************************
* MapRangeFactory *
*************************/
template <class MapF, class... Ranges>
MapRangeFactory<MapF,Ranges...>::MapRangeFactory(const std::shared_ptr<Ranges>&... rs)
{
mProd = std::shared_ptr< MapRange<MapF,Ranges...> >( new MapRange<MapF,Ranges...>( rs... ) );
}
template <class MapF, class... Ranges>
MapRangeFactory<MapF,Ranges...>::MapRangeFactory(const typename MapRange<MapF,Ranges...>::Space& st)
{
mProd = std::shared_ptr< MapRange<MapF,Ranges...> >( new MapRange<MapF,Ranges...>( st ) );
}
template <class MapF, class... Ranges>
template <typename T>
MapRangeFactory<MapF,Ranges...>::MapRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr)
{
mProd = std::shared_ptr< MapRange<MapF,Ranges...> >( new MapRange<MapF,Ranges...>( cr->space() ) );
}
template <class MapF, class... Ranges>
std::shared_ptr<RangeBase> MapRangeFactory<MapF,Ranges...>::create()
{
mProd = checkIfCreated( std::dynamic_pointer_cast<oType>( mProd )->mSpace );
setSelf();
return mProd;
}
template <class MapF, class... Ranges>
std::shared_ptr<RangeBase> MapRangeFactory<MapF,Ranges...>::checkIfCreated(const std::tuple<std::shared_ptr<Ranges>...>& ptp)
{
std::shared_ptr<RangeBase> out;
bool check = false;
for(auto& x: MapRangeFactoryProductMap::mAleadyCreated){
if(x.second.size() == sizeof...(Ranges)){
check = RPackNum<sizeof...(Ranges)-1>::checkIfCreated(ptp, x.second);
if(check){
out = x.first;
break;
}
}
}
if(not check){
std::vector<std::intptr_t> pv(sizeof...(Ranges));
RPackNum<sizeof...(Ranges)-1>::RangesToVec(ptp, pv);
MapRangeFactoryProductMap::mAleadyCreated[mProd] = pv;
out = mProd;
}
return out;
}
/******************
* MapRange *
******************/
template <class MapF, class... Ranges>
MapRange<MapF,Ranges...>::MapRange(const std::shared_ptr<Ranges>&... rs) : mSpace(std::make_tuple(rs...))
{
std::vector<MapF::value_type> outmeta;
// set !!!!
ORFType orf(outmeta);
mOutRange = std::dynamic_pointer_cast<ORType>( orf.create() );
}
template <class MapF, class... Ranges>
MapRange<MapF,Ranges...>::MapRange(const Space& space) : mSpace( space )
{
std::vector<MapF::value_type> outmeta;
// set !!!!
ORFType orf(outmeta);
mOutRange = std::dynamic_pointer_cast<ORType>( orf.create() );
}
template <class MapF, class... Ranges>
template <size_t N>
auto MapRange<MapF,Ranges...>::get() const -> decltype( *std::get<N>( mSpace ) )&
{
return *std::get<N>(mSpace);
}
template <class MapF, class... Ranges>
template <size_t N>
auto MapRange<MapF,Ranges...>::getPtr() const -> decltype( std::get<N>( mSpace ) )&
{
return std::get<N>(mSpace);
}
template <class MapF, class... Ranges>
shred_ptr<ORType> MapRange<MapF,Ranges...>::outRange() const
{
return mOutRange;
}
template <class MapF, class... Ranges>
size_t MapRange<MapF,Ranges...>::dim() const
{
return sdim;
}
template <class MapF, class... Ranges>
size_t MapRange<MapF,Ranges...>::size() const
{
return RPackNum<sizeof...(Ranges)-1>::getSize(mSpace);
}
template <class MapF, class... Ranges>
SpaceType MapRange<MapF,Ranges...>::spaceType() const
{
return SpaceType::ANY;
}
template <class MapF, class... Ranges>
const typename MapRange<MapF,Ranges...>::Space& MapRange<MapF,Ranges...>::space() const
{
return mSpace;
}
template <class MapF, class... Ranges>
std::string MapRange<MapF,Ranges...>::stringMeta(size_t pos) const
{
auto i = begin();
i = pos;
return "[ " + RPackNum<sizeof...(Ranges)-1>::metaTupleToString(i.meta()) + " ]";
}
template <class MapF, class... Ranges>
std::vector<char> MapRange<MapF,Ranges...>::data() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::ANY );
h.metaSize = sizeof...(Ranges);
h.multiple = 1;
std::vector<char> out;
//out.reserve(h.metaSize + sizeof(DataHeader));
char* hcp = reinterpret_cast<char*>(&h);
out.insert(out.end(), hcp, hcp + sizeof(DataHeader));
RPackNum<sizeof...(Ranges)-1>::fillRangeDataVec(out, mSpace);
return out;
}
template <class MapF, class... Ranges>
typename MapRange<MapF,Ranges...>::IndexType MapRange<MapF,Ranges...>::begin() const
{
MapIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<MapRange<MapF,Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = 0;
return i;
}
template <class MapF, class... Ranges>
typename MapRange<MapF,Ranges...>::IndexType MapRange<MapF,Ranges...>::end() const
{
MapIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<MapRange<MapF,Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis )) );
i = size();
return i;
}
template <class MapF, class... Ranges>
template <class... ERanges>
auto MapRange<MapF,Ranges...>::cat(const std::shared_ptr<MapRange<ERanges...> >& erange)
-> std::shared_ptr<MapRange<Ranges...,ERanges...> >
{
auto crange = std::tuple_cat(mSpace, erange->space());
MapRangeFactory<Ranges...,ERanges...> rf(crange);
return std::dynamic_pointer_cast<MapRange<Ranges...,ERanges...> >(rf.create());
}
}
#endif