cnorxz/orig/include/ranges/value_range.h

416 lines
8.6 KiB
C
Raw Normal View History

#ifndef __cxz_value_range_h__
#define __cxz_value_range_h__
#include <cstdlib>
#include <vector>
#include <memory>
#include <map>
#include "rbase_def.h"
//#include "base_def.h"
#include "ranges/rpheader.h"
#include "ranges/index_base.h"
#include "ranges/range_base.h"
#include "ranges/x_to_string.h"
#include "ranges/type_map.h"
#include "xfor/for_type.h"
2021-07-28 20:29:56 +02:00
namespace CNORXZ
{
namespace
{
2021-07-28 20:29:56 +02:00
using namespace CNORXZInternal;
}
template <typename U>
class ValueIndex : public IndexInterface<ValueIndex<U>,U>
{
public:
typedef IndexInterface<ValueIndex<U>,U> IB;
typedef U MetaType;
typedef ValueRange<U> RangeType;
typedef ValueIndex IType;
2018-09-12 20:56:55 +02:00
ValueIndex(const std::shared_ptr<RangeType>& rptr);
static constexpr IndexType sType() { return IndexType::SINGLE; }
static constexpr size_t totalDim() { return 1; }
static constexpr size_t sDim() { return 1; }
static constexpr SpaceType STYPE = SpaceType::NONE;
IndexType type() const;
ValueIndex& operator=(size_t pos);
ValueIndex& operator++();
ValueIndex& operator--();
int pp(std::intptr_t idxPtrNum);
int mm(std::intptr_t idxPtrNum);
std::string stringMeta() const;
const U& meta() const;
const U* metaPtr() const;
ValueIndex& at(const U& metaPos);
size_t posAt(const U& metaPos) const;
size_t dim(); // = 1
bool last();
bool first();
std::shared_ptr<RangeType> range();
template <size_t N>
void getPtr();
size_t getStepSize(size_t n);
template <class Expr>
auto ifor(size_t step, Expr ex) const
-> For<ValueIndex<U>,Expr>;
template <class Expr>
auto iforh(size_t step, Expr ex) const
-> For<ValueIndex<U>,Expr,ForType::HIDDEN>;
private:
std::shared_ptr<RangeType> mExplicitRangePtr;
U mMeta;
};
template <typename U>
class ValueRangeFactory : public RangeFactoryBase
{
public:
typedef ValueRange<U> oType;
ValueRangeFactory();
std::shared_ptr<RangeBase> create();
};
template <typename U>
class ValueRange : public RangeInterface<ValueIndex<U> >
{
public:
typedef RangeBase RB;
typedef ValueIndex<U> IndexType;
typedef ValueRange RangeType;
typedef U MetaType;
2019-03-14 19:10:06 +01:00
typedef ValueRangeFactory<U> FType;
virtual size_t size() const final;
virtual size_t dim() const final;
virtual SpaceType spaceType() const final;
virtual DataHeader dataHeader() const final;
virtual vector<size_t> typeNum() const final;
2019-03-14 19:10:06 +01:00
virtual size_t cmeta(char* target, size_t pos) const final;
virtual size_t cmetaSize() const final;
virtual std::string stringMeta(size_t pos) const final;
2019-02-13 21:59:13 +01:00
virtual vector<char> data() const final;
2018-11-26 18:23:38 +01:00
U get(size_t pos) const;
virtual IndexType begin() const final;
virtual IndexType end() const final;
friend ValueRangeFactory<U>;
static constexpr bool defaultable = true;
static constexpr size_t ISSTATIC = 1;
static constexpr size_t SIZE = 1;
static constexpr bool HASMETACONT = false;
2018-09-12 20:56:55 +02:00
static std::shared_ptr<ValueRange> Default()
{
ValueRangeFactory<U> vrf;
return std::dynamic_pointer_cast<ValueRange>( vrf.create() );
}
protected:
2018-11-26 18:23:38 +01:00
mutable U const* mMeta;
ValueRange() = default;
};
2021-07-28 20:29:56 +02:00
} // namespace CNORXZ
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
2021-07-28 20:29:56 +02:00
namespace CNORXZ
{
/*****************
* ValueIndex *
*****************/
namespace
{
template<typename U>
std::shared_ptr<RangeBase> mkDefaultValueRange()
{
ValueRangeFactory<U> vrf;
2018-09-12 20:56:55 +02:00
return vrf.create();
}
}
template <typename U>
2018-09-12 20:56:55 +02:00
ValueIndex<U>::ValueIndex(const std::shared_ptr<RangeType>& rptr) :
IndexInterface<ValueIndex<U>,U>(rptr, 0),
mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr))
{}
template <typename U>
IndexType ValueIndex<U>::type() const
{
return IndexType::SINGLE;
}
template <typename U>
ValueIndex<U>& ValueIndex<U>::operator=(size_t pos)
{
IB::mPos = pos;
return *this;
}
template <typename U>
ValueIndex<U>& ValueIndex<U>::operator++()
{
++IB::mPos;
return *this;
}
template <typename U>
ValueIndex<U>& ValueIndex<U>::operator--()
{
--IB::mPos;
return *this;
}
template <typename U>
int ValueIndex<U>::pp(std::intptr_t idxPtrNum)
{
++(*this);
return 1;
}
template <typename U>
int ValueIndex<U>::mm(std::intptr_t idxPtrNum)
{
--(*this);
return 1;
}
template <typename U>
std::string ValueIndex<U>::stringMeta() const
{
return xToString(mMeta);
}
template <typename U>
const U& ValueIndex<U>::meta() const
{
return mMeta;
}
template <typename U>
const U* ValueIndex<U>::metaPtr() const
{
return &mMeta;
}
template <typename U>
ValueIndex<U>& ValueIndex<U>::at(const U& metaPos)
{
mMeta = metaPos;
return *this;
}
template <typename U>
size_t ValueIndex<U>::posAt(const U& metaPos) const
{
return 0;
}
template <typename U>
size_t ValueIndex<U>::dim() // = 1
{
return 1;
}
template <typename U>
bool ValueIndex<U>::last()
{
return IB::mPos == IB::mMax - 1;
}
template <typename U>
bool ValueIndex<U>::first()
{
return IB::mPos == 0;
}
template <typename U>
std::shared_ptr<typename ValueIndex<U>::RangeType> ValueIndex<U>::range()
{
2018-11-26 18:23:38 +01:00
return mExplicitRangePtr;
}
template <typename U>
template <size_t N>
void ValueIndex<U>::getPtr() {}
template <typename U>
size_t ValueIndex<U>::getStepSize(size_t n)
{
return 1;
}
template <typename U>
template <class Expr>
auto ValueIndex<U>::ifor(size_t step, Expr ex) const
-> For<ValueIndex<U>,Expr>
{
//static const size_t LAYER = typename Expr::LAYER;
return For<ValueIndex<U>,Expr>(this, step, ex);
}
template <typename U>
template <class Expr>
auto ValueIndex<U>::iforh(size_t step, Expr ex) const
-> For<ValueIndex<U>,Expr,ForType::HIDDEN>
{
//static const size_t LAYER = typename Expr::LAYER;
return For<ValueIndex<U>,Expr,ForType::HIDDEN>(this, step, ex);
}
/**************************
* ValueRangeFactory *
**************************/
template <typename U>
ValueRangeFactory<U>::ValueRangeFactory()
{
mProd = std::shared_ptr<oType>( new ValueRange<U>() );
}
template <typename U>
std::shared_ptr<RangeBase> ValueRangeFactory<U>::create()
{
setSelf();
return mProd;
}
/*******************
* ValueRange *
*******************/
template <typename U>
size_t ValueRange<U>::size() const
{
return 1;
}
template <typename U>
size_t ValueRange<U>::dim() const
{
return 1;
}
template <typename U>
SpaceType ValueRange<U>::spaceType() const
{
return SpaceType::NONE;
}
2019-03-14 14:20:38 +01:00
template <typename U>
vector<size_t> ValueRange<U>::typeNum() const
2019-03-14 14:20:38 +01:00
{
2020-07-08 23:34:17 +02:00
return {NumTypeMap<U>::num()};
2019-03-14 14:20:38 +01:00
}
template <typename U>
size_t ValueRange<U>::cmeta(char* target, size_t pos) const
{
*reinterpret_cast<U*>(target) = *mMeta;
return sizeof(U);
}
template <typename U>
size_t ValueRange<U>::cmetaSize() const
{
2019-03-14 14:20:38 +01:00
return sizeof(U);
}
template <typename U>
std::string ValueRange<U>::stringMeta(size_t pos) const
{
return "";
}
2018-11-26 18:23:38 +01:00
template <typename U>
U ValueRange<U>::get(size_t pos) const
{
return *mMeta;
}
template <typename U>
2019-02-13 21:59:13 +01:00
vector<char> ValueRange<U>::data() const
{
assert(0);
DataHeader h = dataHeader();
2019-02-13 21:59:13 +01:00
vector<char> out;
out.reserve(h.metaSize + sizeof(DataHeader));
char* hcp = reinterpret_cast<char*>(&h);
out.insert(out.end(), hcp, hcp + sizeof(DataHeader));
//const char* scp = reinterpret_cast<const char*>(mSpace.data());
//out.insert(out.end(), scp, scp + h.metaSize);
return out;
}
template <typename U>
DataHeader ValueRange<U>::dataHeader() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::NONE );
h.metaSize = 0;
2020-07-08 23:34:17 +02:00
h.metaType = NumTypeMap<U>::num();
h.multiple = 0;
return h;
}
template <typename U>
typename ValueRange<U>::IndexType ValueRange<U>::begin() const
{
ValueIndex<U> i( std::dynamic_pointer_cast<ValueRange<U> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = 0;
2018-11-26 18:23:38 +01:00
mMeta = &i.meta();
return i;
}
template <typename U>
typename ValueRange<U>::IndexType ValueRange<U>::end() const
{
ValueIndex<U> i( std::dynamic_pointer_cast<ValueRange<U> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = size();
2018-11-26 18:23:38 +01:00
mMeta = &i.meta();
return i;
}
2021-07-28 20:29:56 +02:00
} // namespace CNORXZ
#endif