cnorxz/src/multi_range.h

456 lines
12 KiB
C
Raw Normal View History

2017-02-16 11:20:40 +01:00
// -*- C++ -*-
#ifndef __multi_range_h__
#define __multi_range_h__
#include <cstdlib>
#include <tuple>
#include <memory>
2017-02-16 11:20:40 +01:00
#include "base_def.h"
#include "range_base.h"
#include "index_base.h"
#include "pack_num.h"
2017-02-16 11:20:40 +01:00
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
2017-02-16 11:20:40 +01:00
template <class... Indices>
class MultiIndex : public IndexInterface<MultiIndex<Indices...>,
std::tuple<typename Indices::MetaType...> >
2017-02-16 11:20:40 +01:00
{
public:
typedef IndexInterface<MultiIndex<Indices...>,
std::tuple<typename Indices::MetaType...> > IB;
typedef std::tuple<std::shared_ptr<Indices>...> IndexPack;
2017-08-11 15:26:10 +02:00
typedef std::tuple<typename Indices::MetaType...> MetaType;
typedef MultiRange<typename Indices::RangeType...> RangeType;
private:
2017-08-04 11:27:47 +02:00
IndexPack mIPack;
2017-08-29 17:52:08 +02:00
std::array<size_t,sizeof...(Indices)+1> mBlockSizes;
2017-02-16 11:20:40 +01:00
public:
2017-08-11 15:26:10 +02:00
MultiIndex() = delete;
2017-12-12 11:15:39 +01:00
MultiIndex& operator=(size_t pos) { IB::operator=(pos) ; return *this; }
2017-02-28 19:29:52 +01:00
// NO DEFAULT HERE !!!
// ( have to assign sub-indices (ptr!) correctly )
//MultiIndex(const MultiIndex& in);
//MultiIndex& operator=(const MultiIndex& in);
2017-08-11 15:26:10 +02:00
MultiIndex& operator=(ContainerIndex<Indices...>& ci);
2017-05-24 19:01:02 +02:00
template <class MRange>
MultiIndex(const std::shared_ptr<MRange>& range);
template <size_t DIR>
MultiIndex& up();
template <size_t DIR>
MultiIndex& down();
2017-02-16 11:20:40 +01:00
template <size_t N>
2017-08-04 11:27:47 +02:00
auto get() const -> decltype( *std::get<N>( mIPack ) )&;
2017-12-12 11:15:39 +01:00
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mIPack ) )&;
// 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 MultiIndex instances
MultiIndex& operator()(std::shared_ptr<Indices>&... indices);
std::shared_ptr<RangeType> range() const { return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr ); }
private:
friend IB;
// ==== >>>>> STATIC POLYMORPHISM <<<<< ====
2017-12-12 11:15:39 +01:00
static IndexType S_type(MultiIndex const* i) { return IndexType::MULTI; }
static MultiIndex& S_ass_op(MultiIndex* i, size_t pos)
{
i->mPos = pos;
PackNum<sizeof...(Indices)-1>::setIndexPack(i->mIPack, pos);
return *i;
}
static MultiIndex& S_pp_op(MultiIndex* i)
{
PackNum<sizeof...(Indices)-1>::pp( i->mIPack );
++i->mPos;
return *i;
}
static MultiIndex& S_mm_op(MultiIndex* i)
{
PackNum<sizeof...(Indices)-1>::mm( i->mIPack );
--i->mPos;
return *i;
}
static int S_pp(MultiIndex* i, std::intptr_t idxPtrNum)
{
int tmp = PackNum<sizeof...(Indices)-1>::pp(i->mIPack, i->mBlockSizes, idxPtrNum);
i->mPos += tmp;
return tmp;
}
static int S_mm(MultiIndex* i, std::intptr_t idxPtrNum)
{
int tmp = PackNum<sizeof...(Indices)-1>::mm(i->mIPack, i->mBlockSizes, idxPtrNum);
i->mPos -= tmp;
return tmp;
}
2017-08-04 14:57:19 +02:00
2017-12-12 11:15:39 +01:00
static MetaType S_meta(MultiIndex const* i)
{
MetaType metaTuple;
PackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, i->mIPack);
return metaTuple;
}
static MultiIndex& S_at(MultiIndex* i, const MetaType& metaPos)
{
PackNum<sizeof...(Indices)-1>::setMeta(i->mIPack, metaPos);
i->mPos = PackNum<sizeof...(Indices)-1>::makePos(i->mIPack);
return *i;
}
2017-12-12 11:15:39 +01:00
static size_t S_dim(MultiIndex const* i)
{
return sizeof...(Indices);
}
2017-12-12 11:15:39 +01:00
static bool S_first(MultiIndex const* i)
{
return i->mPos == 0;
}
2017-08-11 15:26:10 +02:00
2017-12-12 11:15:39 +01:00
static bool S_last(MultiIndex const* i)
{
return i->mPos == i->mMax - 1;
}
2017-12-12 11:15:39 +01:00
static std::shared_ptr<RangeType> S_range(MultiIndex const* i)
{
return std::dynamic_pointer_cast<RangeType>( i->mRangePtr );
}
template <size_t N>
2017-12-12 11:15:39 +01:00
static auto S_getPtr(MultiIndex const* i) -> decltype( std::get<N>( mIPack ) )&
{
return std::get<N>(i->mIPack);
}
2017-08-29 17:52:08 +02:00
//const IndexBase& get(size_t n);
2017-12-12 11:15:39 +01:00
static std::shared_ptr<VIWB> S_getVPtr(MultiIndex const* i, size_t n)
{
if(n >= sizeof...(Indices)){
assert(0);
// throw !!
}
MultiIndex<Indices...> const* t = i;
return PackNum<sizeof...(Indices)-1>::getIndexPtr(*t, n);
}
2017-08-26 16:45:46 +02:00
2017-12-12 11:15:39 +01:00
static size_t S_getStepSize(MultiIndex const* i, size_t n)
{
if(n >= sizeof...(Indices)){
assert(0);
// throw !!
}
return i->mBlockSizes[n+1];
}
2017-12-12 11:15:39 +01:00
static std::string S_id(MultiIndex const* i) { return std::string("mul") + std::to_string(i->mId); }
static void S_print(MultiIndex const* i, size_t offset)
{
if(offset == 0){
std::cout << " === " << std::endl;
}
for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; }
std::cout << S_id(i) << "[" << reinterpret_cast<std::intptr_t>(i)
<< "]" << "(" << i->mRangePtr << "): " << S_meta(i) << std::endl;
PackNum<sizeof...(Indices)-1>::printIndex(i->mIPack, offset+1);
}
2017-02-16 11:20:40 +01:00
};
/*************************
* MultiRangeFactory *
*************************/
template <class... Ranges>
class MultiRangeFactory : public RangeFactoryBase
{
public:
2017-07-28 11:33:31 +02:00
typedef MultiRange<Ranges...> oType;
MultiRangeFactory() = delete;
MultiRangeFactory(const std::shared_ptr<Ranges>&... rs);
2017-08-04 11:27:47 +02:00
MultiRangeFactory(const typename MultiRange<Ranges...>::SpaceType& space);
2017-08-11 15:26:10 +02:00
MultiRangeFactory(const std::shared_ptr<ContainerRange<Ranges...> >& cr);
virtual std::shared_ptr<RangeBase> create() override;
};
2017-02-21 17:41:48 +01:00
/******************
* MultiRange *
******************/
2017-02-16 11:20:40 +01:00
template <class... Ranges>
class MultiRange : public RangeInterface<MultiIndex<typename Ranges::IndexType...> >
2017-02-16 11:20:40 +01:00
{
public:
2017-08-04 11:27:47 +02:00
typedef RangeBase RB;
typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType;
2017-12-12 11:15:39 +01:00
typedef MultiIndex<typename Ranges::IndexType...> IndexType;
//typedef typename RangeInterface<MultiIndex<typename Ranges::IndexType...> >::IndexType IndexType;
2017-08-04 11:27:47 +02:00
protected:
MultiRange() = delete;
MultiRange(const MultiRange& in) = delete;
MultiRange& operator=(const MultiRange& in) = delete;
MultiRange(const std::shared_ptr<Ranges>&... rs);
MultiRange(const SpaceType& space);
SpaceType mSpace;
public:
static const size_t sdim = sizeof...(Ranges);
2017-02-16 11:20:40 +01:00
2017-08-04 11:27:47 +02:00
template <size_t N>
auto get() const -> decltype( *std::get<N>( mSpace ) )&;
2017-08-04 14:57:19 +02:00
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mSpace ) )&;
virtual size_t dim() const override;
virtual size_t size() const override;
2017-02-28 19:29:52 +01:00
const SpaceType& space() const;
2017-08-04 11:27:47 +02:00
virtual IndexType begin() const override;
virtual IndexType end() const override;
2017-05-24 19:01:02 +02:00
virtual std::shared_ptr<VIWB> index() const override;
friend MultiRangeFactory<Ranges...>;
2017-02-16 11:20:40 +01:00
};
}
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
/******************
* MultiIndex *
******************/
/*
template <class... Indices>
MultiIndex<Indices...>::MultiIndex(const MultiIndex<Indices...>& in) :
IndexInterface<std::tuple<typename Indices::MetaType...> >(in)
{
PackNum<sizeof...(Indices)-1>::copy(mIPack, in);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(const MultiIndex<Indices...>& in)
{
IndexI::operator=(in);
PackNum<sizeof...(Indices)-1>::copy(mIPack, in);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this;
}
*/
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(ContainerIndex<Indices...>& ci)
{
PackNum<sizeof...(Indices)-1>::copyInst(mIPack, ci);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this;
}
template <class... Indices>
template <class MRange>
MultiIndex<Indices...>::MultiIndex(const std::shared_ptr<MRange>& range) :
IndexInterface<MultiIndex<Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0)
{
PackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
std::get<sizeof...(Indices)>(mBlockSizes) = 1;
PackNum<sizeof...(Indices)-1>::initBlockSizes(mBlockSizes, mIPack); // has one more element!
}
template <class... Indices>
template <size_t DIR>
MultiIndex<Indices...>& MultiIndex<Indices...>::up()
{
static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices");
IB::mPos += PackNum<sizeof...(Indices)-DIR-1>::blockSize( mIPack );
PackNum<DIR>::pp( mIPack );
return *this;
}
template <class... Indices>
template <size_t DIR>
MultiIndex<Indices...>& MultiIndex<Indices...>::down()
{
static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices");
IB::mPos -= PackNum<sizeof...(Indices)-DIR-1>::blockSize( mIPack );
PackNum<DIR>::mm( mIPack );
return *this;
}
template <class... Indices>
template <size_t N>
auto MultiIndex<Indices...>::get() const -> decltype( *std::get<N>( mIPack ) )&
{
return *std::get<N>(mIPack);
}
2017-12-12 11:15:39 +01:00
template <class... Indices>
template <size_t N>
auto MultiIndex<Indices...>::getPtr() const -> decltype( std::get<N>( mIPack ) )&
{
return std::get<N>(mIPack);
}
template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator()(std::shared_ptr<Indices>&... indices)
{
PackNum<sizeof...(Indices)-1>::swapIndices(mIPack, indices...);
PackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, IB::mPos);
return *this;
}
/*************************
* MultiRangeFactory *
*************************/
template <class... Ranges>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const std::shared_ptr<Ranges>&... rs)
{
mProd = std::shared_ptr< MultiRange<Ranges...> >( new MultiRange<Ranges...>( rs... ) );
}
template <class... Ranges>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const typename MultiRange<Ranges...>::SpaceType& st)
{
mProd = std::shared_ptr< MultiRange<Ranges...> >( new MultiRange<Ranges...>( st ) );
}
template <class... Ranges>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const std::shared_ptr<ContainerRange<Ranges...> >& cr)
{
mProd = std::shared_ptr< MultiRange<Ranges...> >( new MultiRange<Ranges...>( cr->space() ) );
}
template <class... Ranges>
std::shared_ptr<RangeBase> MultiRangeFactory<Ranges...>::create()
{
setSelf();
return mProd;
}
/******************
* MultiRange *
******************/
template <class... Ranges>
MultiRange<Ranges...>::MultiRange(const std::shared_ptr<Ranges>&... rs) : mSpace(std::make_tuple(rs...)) {}
template <class... Ranges>
MultiRange<Ranges...>::MultiRange(const SpaceType& space) : mSpace( space ) {}
template <class... Ranges>
template <size_t N>
auto MultiRange<Ranges...>::get() const -> decltype( *std::get<N>( mSpace ) )&
{
return *std::get<N>(mSpace);
}
template <class... Ranges>
template <size_t N>
auto MultiRange<Ranges...>::getPtr() const -> decltype( std::get<N>( mSpace ) )&
{
return std::get<N>(mSpace);
}
template <class... Ranges>
size_t MultiRange<Ranges...>::dim() const
{
return sdim;
}
template <class... Ranges>
size_t MultiRange<Ranges...>::size() const
{
return PackNum<sizeof...(Ranges)-1>::getSize(mSpace);
}
template <class... Ranges>
const typename MultiRange<Ranges...>::SpaceType& MultiRange<Ranges...>::space() const
{
return mSpace;
}
template <class... Ranges>
typename MultiRange<Ranges...>::IndexType MultiRange<Ranges...>::begin() const
{
MultiIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<MultiRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = 0;
return i;
}
template <class... Ranges>
typename MultiRange<Ranges...>::IndexType MultiRange<Ranges...>::end() const
{
MultiIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<MultiRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis )) );
i = size();
return i;
}
template <class... Ranges>
std::shared_ptr<VIWB> MultiRange<Ranges...>::index() const
{
2017-12-12 11:15:39 +01:00
typedef IndexWrapper<IndexType> IW;
return std::make_shared<IW>
( std::make_shared<IndexType>
( std::dynamic_pointer_cast<MultiRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) ) );
}
}
2017-02-16 11:20:40 +01:00
#endif