cnorxz/src/ranges/single_range.h

350 lines
8.3 KiB
C
Raw Normal View History

2017-02-16 11:20:40 +01:00
// -*- C++ -*-
#ifndef __single_range_h__
#define __single_range_h__
#include <cstdlib>
#include <vector>
2017-06-01 13:19:27 +02:00
#include <memory>
2017-02-16 11:20:40 +01:00
2017-12-17 17:40:55 +01:00
//#include "base_def.h"
#include "ranges/index_base.h"
#include "ranges/range_base.h"
2017-02-16 11:20:40 +01:00
#include "xfor/xfor.h"
using MultiArrayHelper::For;
2017-02-16 11:20:40 +01:00
namespace MultiArrayTools
{
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
class SingleIndex : public IndexInterface<SingleIndex<U,TYPE>,U>
2017-02-16 11:20:40 +01:00
{
public:
typedef IndexInterface<SingleIndex<U,TYPE>,U> IB;
2017-08-04 14:57:19 +02:00
typedef U MetaType;
2017-08-11 15:26:10 +02:00
typedef SingleRange<U,TYPE> RangeType;
2017-08-04 11:27:47 +02:00
//DEFAULT_MEMBERS_X(SingleIndex);
2017-08-04 11:27:47 +02:00
SingleIndex(const std::shared_ptr<SingleRange<U,TYPE> >& range);
2018-01-05 13:56:16 +01:00
static IndexType sType() { return IndexType::SINGLE; }
// ==== >>>>> STATIC POLYMORPHISM <<<<< ====
IndexType type() const;
2017-12-17 14:23:23 +01:00
SingleIndex& operator=(size_t pos);
SingleIndex& operator++();
SingleIndex& operator--();
2017-12-17 14:23:23 +01:00
int pp(std::intptr_t idxPtrNum);
int mm(std::intptr_t idxPtrNum);
2017-08-30 17:56:38 +02:00
2017-12-17 14:23:23 +01:00
U meta();
SingleIndex& at(const U& metaPos);
2017-12-17 14:23:23 +01:00
size_t dim(); // = 1
bool last();
bool first();
2017-12-17 14:23:23 +01:00
std::shared_ptr<RangeType> range();
2017-12-12 11:15:39 +01:00
template <size_t N>
2017-12-17 14:23:23 +01:00
void getPtr();
2017-12-17 14:23:23 +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-12-17 14:23:23 +01:00
std::string id();
void print(size_t offset);
template <class Expr, size_t DIM, class... Ops>
auto ifor(const std::array<std::tuple<to_size_t<Ops>...>,DIM>& ee, Expr&& ex) const
2018-01-09 11:37:26 +01:00
-> For<SingleIndex<U,TYPE>,Expr,Ops...>;
2017-02-16 11:20:40 +01:00
};
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
2017-07-28 11:33:31 +02:00
class SingleRangeFactory : public RangeFactoryBase
{
public:
typedef SingleRange<U,TYPE> oType;
SingleRangeFactory() = delete;
SingleRangeFactory(const std::vector<U>& space);
std::shared_ptr<RangeBase> create();
};
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
2017-08-04 11:27:47 +02:00
class SingleRange : public RangeInterface<SingleIndex<U,TYPE> >
2017-02-16 11:20:40 +01:00
{
public:
2017-08-04 11:27:47 +02:00
typedef RangeBase RB;
2017-12-12 11:15:39 +01:00
typedef SingleIndex<U,TYPE> IndexType;
//typedef typename RangeInterface<SingleIndex<U,TYPE> >::IndexType IndexType;
2017-02-16 16:06:23 +01:00
virtual size_t size() const override;
2017-08-04 11:27:47 +02:00
virtual size_t dim() const override;
2017-02-16 11:20:40 +01:00
const U& get(size_t pos) const;
size_t getMeta(const U& metaPos) const;
2017-08-04 14:57:19 +02:00
virtual IndexType begin() const override;
virtual IndexType end() const override;
virtual std::shared_ptr<VIWB> index() const override;
friend SingleRangeFactory<U,TYPE>;
static const bool defaultable = false;
2017-02-16 11:20:40 +01:00
protected:
SingleRange() = delete;
SingleRange(const SingleRange& in) = delete;
2017-06-01 13:19:27 +02:00
SingleRange(const std::vector<U>& space);
2017-02-16 11:20:40 +01:00
std::vector<U> mSpace;
};
2017-03-14 23:00:41 +01:00
}
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
2017-03-14 23:00:41 +01:00
namespace MultiArrayTools
{
/******************
* SingleIndex *
******************/
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
SingleIndex<U,TYPE>::SingleIndex(const std::shared_ptr<SingleRange<U,TYPE> >& range) :
IndexInterface<SingleIndex<U,TYPE>,U>(range, 0) {}
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
IndexType SingleIndex<U,TYPE>::type() const
2017-12-17 14:23:23 +01:00
{
return IndexType::SINGLE;
}
template <typename U, SpaceType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator=(size_t pos)
{
IB::mPos = pos;
return *this;
}
template <typename U, SpaceType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator++()
{
++IB::mPos;
return *this;
}
template <typename U, SpaceType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::operator--()
{
--IB::mPos;
return *this;
}
template <typename U, SpaceType TYPE>
int SingleIndex<U,TYPE>::pp(std::intptr_t idxPtrNum)
{
++(*this);
return 1;
}
template <typename U, SpaceType TYPE>
int SingleIndex<U,TYPE>::mm(std::intptr_t idxPtrNum)
{
--(*this);
return 1;
}
template <typename U, SpaceType TYPE>
U SingleIndex<U,TYPE>::meta()
{
return std::dynamic_pointer_cast<SingleRange<U,TYPE> const>( IB::mRangePtr )->get( IB::pos() );
}
template <typename U, SpaceType TYPE>
SingleIndex<U,TYPE>& SingleIndex<U,TYPE>::at(const U& metaPos)
{
(*this) = std::dynamic_pointer_cast<SingleRange<U,TYPE> const>( IB::mRangePtr )->getMeta( metaPos );
return *this;
}
template <typename U, SpaceType TYPE>
size_t SingleIndex<U,TYPE>::dim() // = 1
{
return 1;
}
template <typename U, SpaceType TYPE>
bool SingleIndex<U,TYPE>::last()
{
return IB::mPos == IB::mMax - 1;
}
template <typename U, SpaceType TYPE>
bool SingleIndex<U,TYPE>::first()
{
return IB::mPos == 0;
}
template <typename U, SpaceType TYPE>
std::shared_ptr<typename SingleIndex<U,TYPE>::RangeType> SingleIndex<U,TYPE>::range()
{
return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr );
}
template <typename U, SpaceType TYPE>
template <size_t N>
void SingleIndex<U,TYPE>::getPtr() {}
template <typename U, SpaceType TYPE>
std::shared_ptr<VIWB> SingleIndex<U,TYPE>::getVPtr(size_t n)
{
return std::shared_ptr<VIWB>();
}
2017-12-24 18:14:07 +01:00
template <typename U, SpaceType TYPE>
std::vector<IndexInfo> SingleIndex<U,TYPE>::infoVec() const
{
return std::move( std::vector<IndexInfo>() );
}
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
size_t SingleIndex<U,TYPE>::getStepSize(size_t n)
{
return 1;
}
template <typename U, SpaceType TYPE>
std::string SingleIndex<U,TYPE>::id()
{
return std::string("sin") + std::to_string(IB::mId);
}
template <typename U, SpaceType TYPE>
void SingleIndex<U,TYPE>::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;
}
template <typename U, SpaceType TYPE>
template <class Expr, size_t DIM, class... Ops>
auto SingleIndex<U,TYPE>::ifor(const std::array<std::tuple<to_size_t<Ops>...>,DIM>& ee, Expr&& ex) const
2018-01-09 11:37:26 +01:00
-> For<SingleIndex<U,TYPE>,Expr,Ops...>
{
static const size_t layer = typename Expr::layer();
return For<SingleIndex<U,TYPE>,Expr,Ops...>( this, ex, std::get<layer>( ee ) );
}
/********************
* SingleRange *
********************/
2017-03-14 23:00:41 +01:00
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
SingleRangeFactory<U,TYPE>::SingleRangeFactory(const std::vector<U>& space)
2017-03-14 23:00:41 +01:00
{
mProd = std::shared_ptr<oType>( new SingleRange<U,TYPE>( space ) );
}
2017-03-14 23:00:41 +01:00
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
std::shared_ptr<RangeBase> SingleRangeFactory<U,TYPE>::create()
{
setSelf();
return mProd;
}
/********************
* SingleRange *
********************/
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
SingleRange<U,TYPE>::SingleRange(const std::vector<U>& space) : RangeInterface<SingleIndex<U,TYPE> >(),
mSpace(space) {}
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
const U& SingleRange<U,TYPE>::get(size_t pos) const
2017-03-14 23:00:41 +01:00
{
return mSpace[pos];
}
2017-03-14 23:00:41 +01:00
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
size_t SingleRange<U,TYPE>::getMeta(const U& metaPos) const
{
size_t cnt = 0;
for(auto& x: mSpace){
if(x == metaPos){
return cnt;
}
++cnt;
}
return cnt;
}
2017-03-14 23:00:41 +01:00
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
size_t SingleRange<U,TYPE>::size() const
{
return mSpace.size();
}
2017-03-14 23:00:41 +01:00
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
size_t SingleRange<U,TYPE>::dim() const
2017-03-14 23:00:41 +01:00
{
return 1;
}
2017-03-14 23:00:41 +01:00
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::begin() const
{
SingleIndex<U,TYPE> i( std::dynamic_pointer_cast<SingleRange<U,TYPE> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = 0;
return i;
}
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::end() const
{
SingleIndex<U,TYPE> i( std::dynamic_pointer_cast<SingleRange<U,TYPE> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = size();
return i;
}
2017-03-14 23:00:41 +01:00
// put this in the interface class !!!
2017-12-17 14:23:23 +01:00
template <typename U, SpaceType TYPE>
std::shared_ptr<VIWB> SingleRange<U,TYPE>::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<SingleRange<U,TYPE> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) ) );
}
2017-02-16 11:20:40 +01:00
}
2017-02-16 11:20:40 +01:00
#include "range_types/header.h"
2017-02-16 11:20:40 +01:00
#endif