cnorxz/install/include/ranges/container_range.h

498 lines
13 KiB
C
Raw Normal View History

// -*- C++ -*-
#ifndef __container_range_h__
#define __container_range_h__
#include <cstdlib>
#include <tuple>
#include <memory>
2017-12-17 17:40:55 +01:00
//#include "base_def.h"
#include "ranges/range_base.h"
#include "ranges/index_base.h"
2017-12-17 17:40:55 +01:00
#include "rpack_num.h"
namespace MultiArrayTools
{
2017-08-04 16:19:50 +02:00
template <class... Indices>
class ContainerIndex : public IndexInterface<ContainerIndex<Indices...>,
std::tuple<typename Indices::MetaType...> >
2017-08-04 16:19:50 +02:00
{
public:
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
2018-01-05 13:56:16 +01:00
static IndexType sType() { return IndexType::CONT; }
static size_t sDim() { return sizeof...(Indices); }
2018-01-09 22:38:46 +01:00
static size_t totalDim() { return mkTotalDim<Indices...>(); }
2018-01-05 13:56:16 +01:00
private:
2017-08-04 16:19:50 +02:00
bool mExternControl = false;
IndexPack mIPack;
std::array<size_t,sizeof...(Indices)+1> mBlockSizes;
2017-08-04 16:19:50 +02:00
public:
2017-12-17 14:16:37 +01:00
2017-08-11 15:26:10 +02:00
ContainerIndex() = delete;
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
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 ) )&;
2018-01-05 13:56:16 +01:00
const IndexPack& pack() const { return mIPack; }
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
// ==== >>>>> STATIC POLYMORPHISM <<<<< ====
IndexType type() const;
2017-12-17 14:16:37 +01:00
ContainerIndex& operator++();
ContainerIndex& operator--();
2017-08-30 17:56:38 +02:00
2017-12-17 14:16:37 +01:00
ContainerIndex& operator=(size_t pos);
2017-12-17 14:16:37 +01:00
int pp(std::intptr_t idxPtrNum);
int mm(std::intptr_t idxPtrNum);
2017-08-04 16:19:50 +02:00
2017-12-17 14:16:37 +01:00
MetaType meta();
ContainerIndex& at(const MetaType& metaPos);
2017-12-17 14:16:37 +01:00
size_t dim();
bool first();
bool last();
2017-12-12 11:15:39 +01:00
2017-12-17 14:16:37 +01:00
std::shared_ptr<RangeType> range();
2017-08-04 16:19:50 +02:00
template <size_t N>
2017-12-17 14:16:37 +01:00
auto getPtr() -> decltype( std::get<N>( mIPack ) )&;
2017-12-17 14:16:37 +01:00
std::shared_ptr<VIWB> getVPtr(size_t n);
size_t getStepSize(size_t n);
2017-12-24 18:14:07 +01:00
std::vector<IndexInfo> infoVec() const;
2017-08-11 15:26:10 +02:00
std::string id() const;
2017-12-17 14:16:37 +01:00
void print(size_t offset);
template <class Exprs>
auto ifor(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, exs));
template <class Exprs>
auto iforh(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs));
2017-08-04 16:19:50 +02:00
};
template <class... Ranges>
class ContainerRangeFactory : public RangeFactoryBase
{
public:
2017-07-28 11:33:31 +02:00
typedef ContainerRange<Ranges...> oType;
ContainerRangeFactory();
ContainerRangeFactory(const std::shared_ptr<Ranges>&... rs);
2017-08-04 11:27:47 +02:00
ContainerRangeFactory(const typename ContainerRange<Ranges...>::SpaceType& space);
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;
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;
protected:
ContainerRange() = default;
ContainerRange(const ContainerRange& in) = delete;
2017-08-04 16:19:50 +02:00
ContainerRange& operator=(const ContainerRange& in) = delete;
ContainerRange(const std::shared_ptr<Ranges>&... rs);
ContainerRange(const SpaceType& space);
SpaceType mSpace;
2017-08-04 16:19:50 +02:00
public:
static const size_t sdim = sizeof...(Ranges);
2017-08-04 16:19:50 +02:00
virtual size_t dim() const override;
virtual size_t size() const override;
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-04 16:19:50 +02:00
virtual IndexType begin() const override;
virtual IndexType end() const override;
virtual std::shared_ptr<VIWB> index() const override;
2017-08-04 16:19:50 +02:00
friend ContainerRangeFactory<Ranges...>;
2018-02-13 16:54:13 +01:00
static constexpr bool defaultable = false;
static constexpr size_t ISSTATIC = SubProp<Ranges...>::ISSTATIC;
static constexpr size_t SIZE = SubProp<Ranges...>::SIZE;
};
2017-08-04 16:19:50 +02:00
} // end namespace MultiArrayTools
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
/**********************
* ContainerIndex *
**********************/
template <class... Indices>
template <class MRange>
ContainerIndex<Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range) :
IndexInterface<ContainerIndex<Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0)
{
2017-12-17 17:40:55 +01:00
RPackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
std::get<sizeof...(Indices)>(mBlockSizes) = 1;
2017-12-17 17:40:55 +01:00
RPackNum<sizeof...(Indices)-1>::initBlockSizes(mBlockSizes, mIPack);
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::sync()
{
if(mExternControl){
2017-12-17 17:40:55 +01:00
IB::mPos = RPackNum<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 );
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()(const std::shared_ptr<Indices>&... inds)
{
2017-12-17 17:40:55 +01:00
RPackNum<sizeof...(Indices)-1>::swapIndices(mIPack, inds...);
mExternControl = true;
return sync();
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()()
{
return sync();
}
2017-12-17 14:16:37 +01:00
template <class... Indices>
IndexType ContainerIndex<Indices...>::type() const { return IndexType::CONT; }
2017-12-17 14:16:37 +01:00
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator++()
{
if(mExternControl){
2017-12-17 17:40:55 +01:00
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
2017-12-17 14:16:37 +01:00
}
2017-12-17 17:40:55 +01:00
RPackNum<sizeof...(Indices)-1>::pp( mIPack );
2017-12-17 14:16:37 +01:00
++IB::mPos;
return *this;
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator--()
{
if(mExternControl){
2017-12-17 17:40:55 +01:00
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
2017-12-17 14:16:37 +01:00
}
2017-12-17 17:40:55 +01:00
RPackNum<sizeof...(Indices)-1>::mm( mIPack );
2017-12-17 14:16:37 +01:00
--IB::mPos;
return *this;
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator=(size_t pos)
{
IB::mPos = pos;
2017-12-17 17:40:55 +01:00
RPackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, pos);
2017-12-17 14:16:37 +01:00
return *this;
}
template <class... Indices>
int ContainerIndex<Indices...>::pp(std::intptr_t idxPtrNum)
{
2017-12-17 17:40:55 +01:00
int tmp = RPackNum<sizeof...(Indices)-1>::pp(mIPack, mBlockSizes, idxPtrNum);
2017-12-17 14:16:37 +01:00
IB::mPos += tmp;
return tmp;
}
template <class... Indices>
int ContainerIndex<Indices...>::mm(std::intptr_t idxPtrNum)
{
2017-12-17 17:40:55 +01:00
int tmp = RPackNum<sizeof...(Indices)-1>::mm(mIPack, mBlockSizes, idxPtrNum);
2017-12-17 14:16:37 +01:00
IB::mPos -= tmp;
return tmp;
}
template <class... Indices>
typename ContainerIndex<Indices...>::MetaType ContainerIndex<Indices...>::meta()
{
MetaType metaTuple;
2017-12-17 17:40:55 +01:00
RPackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack);
2017-12-17 14:16:37 +01:00
return metaTuple;
}
template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::at(const MetaType& metaPos)
{
2017-12-17 17:40:55 +01:00
RPackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
2017-12-17 14:16:37 +01:00
return *this;
}
template <class... Indices>
size_t ContainerIndex<Indices...>::dim()
{
return sizeof...(Indices);
}
template <class... Indices>
bool ContainerIndex<Indices...>::first()
{
return IB::pos() == 0;
}
template <class... Indices>
bool ContainerIndex<Indices...>::last()
{
return IB::pos() == IB::mMax - 1;
}
template <class... Indices>
std::shared_ptr<typename ContainerIndex<Indices...>::RangeType>
ContainerIndex<Indices...>::range()
{
return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr );
}
template <class... Indices>
template <size_t N>
auto ContainerIndex<Indices...>::getPtr() -> decltype( std::get<N>( mIPack ) )&
{
return std::get<N>( mIPack );
}
template <class... Indices>
std::shared_ptr<VIWB> ContainerIndex<Indices...>::getVPtr(size_t n)
{
if(n >= sizeof...(Indices)){
assert(0);
// throw !!
}
ContainerIndex<Indices...> const* t = this;
2017-12-17 17:40:55 +01:00
return RPackNum<sizeof...(Indices)-1>::getIndexPtr(*t, n);
2017-12-17 14:16:37 +01:00
}
template <class... Indices>
size_t ContainerIndex<Indices...>::getStepSize(size_t n)
{
if(n >= sizeof...(Indices)){
assert(0);
// throw !!
}
return mBlockSizes[n+1];
}
2017-12-24 18:14:07 +01:00
template <class... Indices>
std::vector<IndexInfo> ContainerIndex<Indices...>::infoVec() const
{
2017-12-25 13:44:55 +01:00
std::vector<IndexInfo> out;
out.reserve(sizeof...(Indices));
RPackNum<sizeof...(Indices)-1>::buildInfoVec(out, mIPack, mBlockSizes);
2017-12-24 18:14:07 +01:00
return std::move( out );
}
2017-12-17 14:16:37 +01:00
template <class... Indices>
std::string ContainerIndex<Indices...>::id() const
2017-12-17 14:16:37 +01:00
{
return std::string("con") + std::to_string(IB::mId);
}
template <class... Indices>
void ContainerIndex<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;
2017-12-17 17:40:55 +01:00
RPackNum<sizeof...(Indices)-1>::printIndex(mIPack, offset+1);
2017-12-17 14:16:37 +01:00
}
template <class... Indices>
template <class Exprs>
auto ContainerIndex<Indices...>::ifor(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, exs))
{
return RPackNum<sizeof...(Indices)-1>::mkFor(mIPack, exs);
}
template <class... Indices>
template <class Exprs>
auto ContainerIndex<Indices...>::iforh(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs))
{
return RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs);
}
2017-12-17 14:16:37 +01:00
/*****************************
* 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
{
2017-12-17 17:40:55 +01:00
return RPackNum<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>
std::shared_ptr<VIWB> ContainerRange<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<ContainerRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) ) );
}
} // end namespace MultiArrayTools
#endif