cnorxz/src/include/ranges/single_range.h

424 lines
10 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"
2018-07-16 00:12:05 +02:00
//#include "ranges/rpack_num.h"
2017-12-17 17:40:55 +01:00
#include "ranges/index_base.h"
#include "ranges/range_base.h"
#include "ranges/x_to_string.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;
2018-03-05 00:04:50 +01:00
typedef SingleIndex IType;
2018-02-13 16:54:13 +01:00
//DEFAULT_MEMBERS_X(SingleIndex);
2017-08-04 11:27:47 +02:00
SingleIndex(const std::shared_ptr<SingleRange<U,TYPE> >& range);
static constexpr IndexType sType() { return IndexType::SINGLE; }
static constexpr size_t totalDim() { return 1; }
static constexpr size_t sDim() { return 1; }
// ==== >>>>> 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);
2018-07-22 16:16:24 +02:00
std::string stringMeta() const;
U meta() const;
const U* metaPtr() const;
2017-12-17 14:23:23 +01:00
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
size_t getStepSize(size_t n);
std::string id() const;
2017-12-17 14:23:23 +01:00
void print(size_t offset);
template <class Expr>
auto ifor(Expr ex) const
-> For<SingleIndex<U,TYPE>,Expr>;
template <class Expr>
auto iforh(Expr ex) const
-> For<SingleIndex<U,TYPE>,Expr,ForType::HIDDEN>;
2018-03-19 18:38:53 +01:00
private:
const U* mMetaPtr;
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;
2018-03-05 00:04:50 +01:00
typedef SingleRange RangeType;
typedef U MetaType;
2017-12-12 11:15:39 +01:00
//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;
virtual std::string stringMeta(size_t pos) const override;
virtual std::vector<char> data() const override;
2017-08-04 11:27:47 +02:00
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;
2018-02-15 16:24:54 +01:00
friend SingleRangeFactory<U,TYPE>;
static constexpr bool defaultable = false;
2018-02-13 16:54:13 +01:00
static constexpr size_t ISSTATIC = 0;
static constexpr size_t SIZE = -1;
2018-03-19 18:38:53 +01:00
static constexpr bool HASMETACONT = true;
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 *
******************/
template <bool HASMETACONT>
struct MetaPtrHandle
{
template <class Range>
static const typename Range::MetaType* set(Range* r)
{
return &r->get(0);
}
template <typename U, class Range>
static inline U getMeta(U* metaPtr, size_t pos, std::shared_ptr<Range> r)
{
return metaPtr[pos];
}
};
template <>
struct MetaPtrHandle<false>
{
template <class Range>
static const typename Range::MetaType* set(Range* r)
{
return nullptr;
}
template <typename U, class Range>
static inline U getMeta(U* metaPtr, size_t pos, std::shared_ptr<Range> r)
{
return r->get(pos);
}
};
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) :
2018-03-19 18:38:53 +01:00
IndexInterface<SingleIndex<U,TYPE>,U>(range, 0),
mMetaPtr(MetaPtrHandle<SingleIndex<U,TYPE>::RangeType::HASMETACONT>::set
( dynamic_cast<RangeType*>(IB::mRangePtr.get() ) ) ) {}
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>
2018-07-22 16:16:24 +02:00
std::string SingleIndex<U,TYPE>::stringMeta() const
{
return std::dynamic_pointer_cast<SingleRange<U,TYPE> const>( IB::mRangePtr )->stringMeta(IB::mPos);
}
template <typename U, SpaceType TYPE>
U SingleIndex<U,TYPE>::meta() const
2017-12-17 14:23:23 +01:00
{
return MetaPtrHandle<SingleIndex<U,TYPE>::RangeType::HASMETACONT>::getMeta
( mMetaPtr, IB::mPos,
std::dynamic_pointer_cast<SingleRange<U,TYPE> const>( IB::mRangePtr ) );
2017-12-17 14:23:23 +01:00
}
2018-03-19 18:38:53 +01:00
template <typename U, SpaceType TYPE>
const U* SingleIndex<U,TYPE>::metaPtr() const
2018-03-19 18:38:53 +01:00
{
return mMetaPtr;
}
2017-12-17 14:23:23 +01:00
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() {}
2017-12-24 18:14:07 +01:00
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() const
2017-12-17 14:23:23 +01:00
{
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>
auto SingleIndex<U,TYPE>::ifor(Expr ex) const
-> For<SingleIndex<U,TYPE>,Expr>
{
//static const size_t LAYER = typename Expr::LAYER;
return For<SingleIndex<U,TYPE>,Expr>(this, ex);
}
template <typename U, SpaceType TYPE>
template <class Expr>
auto SingleIndex<U,TYPE>::iforh(Expr ex) const
-> For<SingleIndex<U,TYPE>,Expr,ForType::HIDDEN>
{
//static const size_t LAYER = typename Expr::LAYER;
return For<SingleIndex<U,TYPE>,Expr,ForType::HIDDEN>(this, ex);
}
/********************
* 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
template <typename U, SpaceType TYPE>
std::string SingleRange<U,TYPE>::stringMeta(size_t pos) const
{
return xToString(get(pos));
}
template <typename U, SpaceType TYPE>
std::vector<char> SingleRange<U,TYPE>::data() const
{
DataHeader h;
h.spaceType = static_cast<int>( TYPE );
h.metaSize = size() * sizeof(U);
h.multiple = 0;
std::vector<char> out;
out.reserve(h.metaSize + sizeof(DataHeader));
char* hcp = reinterpret_cast<char*>(&h);
out.insert(out.end(), hcp, hcp + sizeof(DataHeader));
stringCat(out, mSpace);
//const char* scp = reinterpret_cast<const char*>(mSpace.data());
//out.insert(out.end(), scp, scp + h.metaSize);
return out;
}
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-02-16 11:20:40 +01:00
#include "range_types/header.h"
2017-02-16 11:20:40 +01:00
#endif