2017-07-27 14:48:41 +02:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
|
|
|
#ifndef __container_range_h__
|
|
|
|
#define __container_range_h__
|
|
|
|
|
|
|
|
#include <cstdlib>
|
2017-07-28 14:02:44 +02:00
|
|
|
#include <tuple>
|
|
|
|
#include <memory>
|
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
#include "base_def.h"
|
2017-07-28 14:02:44 +02:00
|
|
|
#include "range_base.h"
|
|
|
|
#include "index_base.h"
|
2017-07-27 14:48:41 +02:00
|
|
|
|
2017-11-20 21:35:25 +01:00
|
|
|
#include "pack_num.h"
|
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
2017-08-25 22:03:20 +02:00
|
|
|
|
2017-08-04 16:19:50 +02:00
|
|
|
template <class... Indices>
|
2017-12-11 18:49:43 +01:00
|
|
|
class ContainerIndex : public IndexInterface<ContainerIndex<Indices...>,
|
|
|
|
std::tuple<typename Indices::MetaType...> >
|
2017-08-04 16:19:50 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
typedef IndexInterface<ContainerIndex<Indices...>,
|
|
|
|
std::tuple<typename Indices::MetaType...> > IB;
|
2017-08-11 15:26:10 +02:00
|
|
|
typedef std::tuple<typename Indices::MetaType...> MetaType;
|
2017-08-04 16:19:50 +02:00
|
|
|
typedef std::tuple<std::shared_ptr<Indices>...> IndexPack;
|
2017-08-11 15:26:10 +02:00
|
|
|
typedef ContainerRange<typename Indices::RangeType...> RangeType;
|
2017-08-04 16:19:50 +02:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
friend IB;
|
|
|
|
|
2017-08-04 16:19:50 +02:00
|
|
|
bool mExternControl = false;
|
|
|
|
IndexPack mIPack;
|
2017-08-30 19:41:49 +02:00
|
|
|
std::array<size_t,sizeof...(Indices)+1> mBlockSizes;
|
|
|
|
|
2017-08-04 16:19:50 +02:00
|
|
|
public:
|
2017-08-11 15:26:10 +02:00
|
|
|
ContainerIndex() = delete;
|
2017-12-11 18:49:43 +01:00
|
|
|
|
2017-08-04 16:19:50 +02:00
|
|
|
template <class MRange>
|
|
|
|
ContainerIndex(const std::shared_ptr<MRange>& range);
|
|
|
|
|
2017-12-12 11:15:39 +01:00
|
|
|
ContainerIndex& operator=(size_t pos) { IB::operator=(pos) ; return *this; }
|
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
template <size_t N>
|
|
|
|
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 ) )&;
|
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
ContainerIndex& sync(); // recalculate 'IB::mPos' when externalControl == true
|
|
|
|
ContainerIndex& operator()(const std::shared_ptr<Indices>&... inds); // control via external indices
|
|
|
|
ContainerIndex& operator()(); // -> sync; just to shorten the code
|
2017-08-28 18:28:43 +02:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
private:
|
2017-08-04 16:19:50 +02:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
//ContainerIndex(const ContainerIndex& in);
|
|
|
|
//ContainerIndex& operator=(const ContainerIndex& in);
|
|
|
|
|
|
|
|
// ==== >>>>> STATIC POLYMORPHISM <<<<< ====
|
|
|
|
|
2017-12-12 11:15:39 +01:00
|
|
|
static IndexType S_type(ContainerIndex const* i) { return IndexType::CONT; }
|
2017-12-11 18:49:43 +01:00
|
|
|
|
|
|
|
static ContainerIndex& S_pp_op(ContainerIndex* i)
|
|
|
|
{
|
|
|
|
if(i->mExternControl){
|
|
|
|
i->mPos = PackNum<sizeof...(Indices)-1>::makePos(i->mIPack);
|
|
|
|
}
|
|
|
|
PackNum<sizeof...(Indices)-1>::pp( i->mIPack );
|
|
|
|
++i->mPos;
|
|
|
|
return *i;
|
|
|
|
}
|
2017-08-30 17:56:38 +02:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
static ContainerIndex& S_mm_op(ContainerIndex* i)
|
|
|
|
{
|
|
|
|
if(i->mExternControl){
|
|
|
|
i->mPos = PackNum<sizeof...(Indices)-1>::makePos(i->mIPack);
|
|
|
|
}
|
|
|
|
PackNum<sizeof...(Indices)-1>::mm( i->mIPack );
|
|
|
|
--i->mPos;
|
|
|
|
return *i;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static ContainerIndex& S_ass_op(ContainerIndex* i, size_t pos)
|
|
|
|
{
|
|
|
|
i->mPos = pos;
|
|
|
|
PackNum<sizeof...(Indices)-1>::setIndexPack(i->mIPack, pos);
|
|
|
|
return *i;
|
|
|
|
}
|
2017-08-04 16:19:50 +02:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
static int S_pp(ContainerIndex* i, intptr_t idxPtrNum)
|
|
|
|
{
|
|
|
|
int tmp = PackNum<sizeof...(Indices)-1>::pp(i->mIPack, i->mBlockSizes, idxPtrNum);
|
|
|
|
i->mPos += tmp;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int S_mm(ContainerIndex* i, intptr_t idxPtrNum)
|
|
|
|
{
|
|
|
|
int tmp = PackNum<sizeof...(Indices)-1>::mm(i->mIPack, i->mBlockSizes, idxPtrNum);
|
|
|
|
i->mPos -= tmp;
|
|
|
|
return tmp;
|
|
|
|
}
|
2017-08-04 16:19:50 +02:00
|
|
|
|
2017-12-12 11:15:39 +01:00
|
|
|
static MetaType S_meta(ContainerIndex const* i)
|
2017-12-11 18:49:43 +01:00
|
|
|
{
|
|
|
|
MetaType metaTuple;
|
|
|
|
PackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, i->mIPack);
|
|
|
|
return metaTuple;
|
|
|
|
}
|
2017-08-07 17:11:03 +02:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
static ContainerIndex& S_at(ContainerIndex* 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(ContainerIndex const* i)
|
2017-12-11 18:49:43 +01:00
|
|
|
{
|
|
|
|
return sizeof...(Indices);
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:15:39 +01:00
|
|
|
static bool S_first(ContainerIndex const* i)
|
2017-12-11 18:49:43 +01:00
|
|
|
{
|
|
|
|
return i->pos() == 0;
|
|
|
|
}
|
|
|
|
|
2017-12-12 11:15:39 +01:00
|
|
|
static bool S_last(ContainerIndex const* i)
|
2017-12-11 18:49:43 +01:00
|
|
|
{
|
|
|
|
return i->pos() == i->mMax - 1;
|
|
|
|
}
|
2017-12-12 11:15:39 +01:00
|
|
|
|
|
|
|
static std::shared_ptr<RangeType> S_range(ContainerIndex const* i)
|
|
|
|
{
|
|
|
|
return std::dynamic_pointer_cast<RangeType>( i->mRangePtr );
|
|
|
|
}
|
2017-08-04 16:19:50 +02:00
|
|
|
|
2017-08-07 17:11:03 +02:00
|
|
|
template <size_t N>
|
2017-12-12 11:15:39 +01:00
|
|
|
static auto S_getPtr(ContainerIndex const* i) -> decltype( std::get<N>( mIPack ) )&
|
2017-12-11 18:49:43 +01:00
|
|
|
{
|
|
|
|
return std::get<N>( i->mIPack );
|
|
|
|
}
|
2017-08-04 16:19:50 +02:00
|
|
|
|
2017-12-12 11:15:39 +01:00
|
|
|
static std::shared_ptr<VIWB> S_getVPtr(ContainerIndex const* i, size_t n)
|
2017-12-11 18:49:43 +01:00
|
|
|
{
|
|
|
|
if(n >= sizeof...(Indices)){
|
|
|
|
assert(0);
|
|
|
|
// throw !!
|
|
|
|
}
|
|
|
|
ContainerIndex<Indices...> const* t = i;
|
|
|
|
return PackNum<sizeof...(Indices)-1>::getIndexPtr(*t, n);
|
|
|
|
}
|
2017-08-28 18:28:43 +02:00
|
|
|
|
2017-12-12 11:15:39 +01:00
|
|
|
static size_t S_getStepSize(ContainerIndex const* i, size_t n)
|
2017-12-11 18:49:43 +01:00
|
|
|
{
|
|
|
|
if(n >= sizeof...(Indices)){
|
|
|
|
assert(0);
|
|
|
|
// throw !!
|
|
|
|
}
|
|
|
|
return i->mBlockSizes[n+1];
|
|
|
|
}
|
2017-08-11 15:26:10 +02:00
|
|
|
|
2017-12-12 11:15:39 +01:00
|
|
|
static std::string S_id(ContainerIndex const* i) { return std::string("con") + std::to_string(i->mId); }
|
2017-08-04 16:19:50 +02:00
|
|
|
};
|
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
class ContainerRangeFactory : public RangeFactoryBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-07-28 11:33:31 +02:00
|
|
|
typedef ContainerRange<Ranges...> oType;
|
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
ContainerRangeFactory() = delete;
|
2017-07-27 20:34:14 +02:00
|
|
|
ContainerRangeFactory(const std::shared_ptr<Ranges>&... rs);
|
2017-08-04 11:27:47 +02:00
|
|
|
ContainerRangeFactory(const typename ContainerRange<Ranges...>::SpaceType& space);
|
2017-07-27 20:34:14 +02:00
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
virtual std::shared_ptr<RangeBase> create() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
class ContainerRange : public RangeInterface<ContainerIndex<typename Ranges::IndexType...> >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-08-04 11:27:47 +02:00
|
|
|
typedef RangeBase RB;
|
2017-07-27 14:48:41 +02:00
|
|
|
typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType;
|
2017-12-12 11:15:39 +01:00
|
|
|
typedef ContainerIndex<typename Ranges::IndexType...> IndexType;
|
|
|
|
//typedef typename RangeInterface<ContainerIndex<typename Ranges::IndexType...> >::IndexType IndexType;
|
2017-07-27 14:48:41 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
ContainerRange() = default;
|
|
|
|
ContainerRange(const ContainerRange& in) = delete;
|
2017-08-04 16:19:50 +02:00
|
|
|
ContainerRange& operator=(const ContainerRange& in) = delete;
|
|
|
|
|
2017-07-27 20:34:14 +02:00
|
|
|
ContainerRange(const std::shared_ptr<Ranges>&... rs);
|
2017-07-27 14:48:41 +02:00
|
|
|
ContainerRange(const SpaceType& space);
|
|
|
|
|
|
|
|
SpaceType mSpace;
|
|
|
|
|
2017-08-04 16:19:50 +02:00
|
|
|
public:
|
|
|
|
static const size_t sdim = sizeof...(Ranges);
|
2017-07-27 14:48:41 +02:00
|
|
|
|
2017-08-04 16:19:50 +02:00
|
|
|
virtual size_t dim() const override;
|
|
|
|
virtual size_t size() const override;
|
2017-07-27 14:48:41 +02:00
|
|
|
|
2017-08-07 17:11:03 +02:00
|
|
|
template <size_t N>
|
|
|
|
auto get() const -> decltype( *std::get<N>( mSpace ) )&;
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
auto getPtr() const -> decltype( std::get<N>( mSpace ) )&;
|
2017-08-11 15:26:10 +02:00
|
|
|
|
|
|
|
const SpaceType& space() const;
|
2017-08-07 17:11:03 +02:00
|
|
|
|
2017-08-04 16:19:50 +02:00
|
|
|
virtual IndexType begin() const override;
|
|
|
|
virtual IndexType end() const override;
|
2017-08-07 17:11:03 +02:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
virtual std::shared_ptr<VIWB> index() const override;
|
2017-07-27 14:48:41 +02:00
|
|
|
|
2017-08-04 16:19:50 +02:00
|
|
|
friend ContainerRangeFactory<Ranges...>;
|
2017-07-27 20:34:14 +02:00
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
};
|
2017-08-04 16:19:50 +02:00
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
} // end namespace MultiArrayTools
|
|
|
|
|
2017-11-20 21:35:25 +01:00
|
|
|
/* ========================= *
|
|
|
|
* --- TEMPLATE CODE --- *
|
|
|
|
* ========================= */
|
|
|
|
|
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using namespace MultiArrayHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* ContainerIndex *
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
template <class... Indices>
|
|
|
|
template <class MRange>
|
|
|
|
ContainerIndex<Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range) :
|
2017-12-11 18:49:43 +01:00
|
|
|
IndexInterface<ContainerIndex<Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0)
|
2017-11-20 21:35:25 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Indices>
|
|
|
|
ContainerIndex<Indices...>& ContainerIndex<Indices...>::sync()
|
|
|
|
{
|
|
|
|
if(mExternControl){
|
|
|
|
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
|
|
|
|
//VCHECK(id());
|
|
|
|
//VCHECK(sizeof...(Indices));
|
|
|
|
//assert(IB::mPos < IB::max());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Indices>
|
|
|
|
template <size_t N>
|
|
|
|
auto ContainerIndex<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 ContainerIndex<Indices...>::getPtr() const -> decltype( std::get<N>( mIPack ) )&
|
|
|
|
{
|
|
|
|
return std::get<N>( mIPack );
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:35:25 +01:00
|
|
|
template <class... Indices>
|
|
|
|
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()(const std::shared_ptr<Indices>&... inds)
|
|
|
|
{
|
|
|
|
PackNum<sizeof...(Indices)-1>::swapIndices(mIPack, inds...);
|
|
|
|
mExternControl = true;
|
|
|
|
return sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Indices>
|
|
|
|
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()()
|
|
|
|
{
|
|
|
|
return sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************
|
|
|
|
* ContainerRangeFactory *
|
|
|
|
*****************************/
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
ContainerRangeFactory<Ranges...>::ContainerRangeFactory(const std::shared_ptr<Ranges>&... rs)
|
|
|
|
{
|
|
|
|
mProd = std::shared_ptr<ContainerRange<Ranges...> >( new ContainerRange<Ranges...>( rs... ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
ContainerRangeFactory<Ranges...>::
|
|
|
|
ContainerRangeFactory(const typename ContainerRange<Ranges...>::SpaceType& space)
|
|
|
|
{
|
|
|
|
mProd = std::shared_ptr<ContainerRange<Ranges...> >( new ContainerRange<Ranges...>( space ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
std::shared_ptr<RangeBase> ContainerRangeFactory<Ranges...>::create()
|
|
|
|
{
|
|
|
|
setSelf();
|
|
|
|
return mProd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* ContainerRange *
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
ContainerRange<Ranges...>::ContainerRange(const std::shared_ptr<Ranges>&... rs) :
|
|
|
|
mSpace( std::make_tuple( rs... ) ) {}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
ContainerRange<Ranges...>::ContainerRange(const SpaceType& space) : mSpace( space ) {}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
size_t ContainerRange<Ranges...>::dim() const
|
|
|
|
{
|
|
|
|
return sizeof...(Ranges);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
size_t ContainerRange<Ranges...>::size() const
|
|
|
|
{
|
|
|
|
return PackNum<sizeof...(Ranges)-1>::getSize(mSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
template <size_t N>
|
|
|
|
auto ContainerRange<Ranges...>::get() const -> decltype( *std::get<N>( mSpace ) )&
|
|
|
|
{
|
|
|
|
return *std::get<N>( mSpace );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
template <size_t N>
|
|
|
|
auto ContainerRange<Ranges...>::getPtr() const -> decltype( std::get<N>( mSpace ) )&
|
|
|
|
{
|
|
|
|
return std::get<N>( mSpace );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
const typename ContainerRange<Ranges...>::SpaceType& ContainerRange<Ranges...>::space() const
|
|
|
|
{
|
|
|
|
return mSpace;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
typename ContainerRange<Ranges...>::IndexType ContainerRange<Ranges...>::begin() const
|
|
|
|
{
|
|
|
|
ContainerIndex<typename Ranges::IndexType...>
|
|
|
|
i( std::dynamic_pointer_cast<ContainerRange<Ranges...> >
|
|
|
|
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
|
|
|
|
i = 0;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
|
|
|
typename ContainerRange<Ranges...>::IndexType ContainerRange<Ranges...>::end() const
|
|
|
|
{
|
|
|
|
ContainerIndex<typename Ranges::IndexType...>
|
|
|
|
i( std::dynamic_pointer_cast<ContainerRange<Ranges...> >
|
|
|
|
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
|
|
|
|
i = size();
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Ranges>
|
2017-12-11 18:49:43 +01:00
|
|
|
std::shared_ptr<VIWB> ContainerRange<Ranges...>::index() const
|
2017-11-20 21:35:25 +01:00
|
|
|
{
|
2017-12-12 11:15:39 +01:00
|
|
|
typedef IndexWrapper<IndexType> IW;
|
|
|
|
return std::make_shared<IW>
|
|
|
|
( std::make_shared<IndexType>
|
2017-12-11 18:49:43 +01:00
|
|
|
( std::dynamic_pointer_cast<ContainerRange<Ranges...> >
|
|
|
|
( std::shared_ptr<RangeBase>( RB::mThis ) ) ) );
|
2017-11-20 21:35:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace MultiArrayTools
|
|
|
|
|
2017-07-27 14:48:41 +02:00
|
|
|
|
|
|
|
#endif
|