value range + fix in header/include structure

This commit is contained in:
Christian Zimmermann 2018-09-12 17:05:27 +02:00
parent de6b80a90b
commit 6896cc3ca7
9 changed files with 429 additions and 8 deletions

View file

@ -7,7 +7,7 @@
#include <vector>
#include <memory>
#include "base_def.h"
#include "rbase_def.h"
namespace MultiArrayTools
{

View file

@ -72,6 +72,15 @@ namespace MultiArrayTools
// anonymous_range.h
//class AnonymousRange;
// value_range.h
template <typename U>
class ValueRange;
template <typename U>
class ValueRangeFactory;
template <typename U>
class ValueIndex;
}
#endif

View file

@ -1,3 +1,9 @@
//#ifndef __rheader_h__
//#define __rheader_h__
#include "rpheader.h"
#include "anonymous_range.h"
#include "value_range.h"
//#endif

View file

@ -1,5 +1,10 @@
//#ifndef __rpheader_h__
//#define __rpheader_h__
#include "single_range.h"
#include "multi_range.h"
#include "container_range.h"
#include "anonymous_range.h"
//#include "anonymous_range.h"
//#endif

View file

@ -15,12 +15,18 @@
#include "ranges/x_to_string.h"
#include "ranges/type_map.h"
#include "xfor/xfor.h"
#include "xfor/for_type.h"
//#include "xfor/xfor.h"
using MultiArrayHelper::For;
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
template <typename U, SpaceType TYPE>
class SingleIndex : public IndexInterface<SingleIndex<U,TYPE>,U>

View file

@ -0,0 +1,393 @@
#ifndef __value_range_h__
#define __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"
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
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;
ValueIndex(const U& val); // default range
ValueIndex(U&& val); // default range
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);
std::string id() const;
void print(size_t offset);
template <class Expr>
auto ifor(Expr ex) const
-> For<ValueIndex<U>,Expr>;
template <class Expr>
auto iforh(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;
virtual size_t size() const final;
virtual size_t dim() const final;
virtual SpaceType spaceType() const final;
virtual std::string stringMeta(size_t pos) const final;
virtual std::vector<char> data() const final;
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;
protected:
ValueRange() = default;
};
} // namespace MultiArrayTools
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
namespace MultiArrayTools
{
/*****************
* ValueIndex *
*****************/
namespace
{
template<typename U>
std::shared_ptr<RangeBase> mkDefaultValueRange()
{
ValueRangeFactory<U> vrf;
return vrf->create();
}
}
template <typename U>
ValueIndex<U>::ValueIndex(const U& val) : IndexInterface<ValueIndex<U>,U>(mkDefaultValueRange<U>(), 0),
mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr)),
mMeta(val) {}
template <typename U>
ValueIndex<U>::ValueIndex(U&& val) : IndexInterface<ValueIndex<U>,U>(mkDefaultValueRange<U>(), 0),
mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr)),
mMeta(val) {}
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()
{
return std::dynamic_pointer_cast<RangeType>( IB::mRangePtr );
}
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>
std::string ValueIndex<U>::id() const
{
return std::string("val") + std::to_string(IB::mId);
}
template <typename U>
void ValueIndex<U>::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>
template <class Expr>
auto ValueIndex<U>::ifor(Expr ex) const
-> For<ValueIndex<U>,Expr>
{
//static const size_t LAYER = typename Expr::LAYER;
return For<ValueIndex<U>,Expr>(this, ex);
}
template <typename U>
template <class Expr>
auto ValueIndex<U>::iforh(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, 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;
}
template <typename U>
std::string ValueRange<U>::stringMeta(size_t pos) const
{
return "";
}
template <typename U>
std::vector<char> ValueRange<U>::data() const
{
assert(0);
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::NONE );
h.metaSize = 0;
h.metaType = NumTypeMap<U>::num;
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));
//const char* scp = reinterpret_cast<const char*>(mSpace.data());
//out.insert(out.end(), scp, scp + h.metaSize);
return out;
}
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;
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();
return i;
}
} // namespace MultiArrayTools
#endif

View file

@ -5,12 +5,14 @@
namespace MultiArrayHelper
{
enum class ForType {
DEFAULT = 0,
HIDDEN = 1
};
template <class IndexClass, class Expr, ForType FT = ForType::DEFAULT>
class For;
} // end namespace MultiArrayHelper

View file

@ -2,7 +2,7 @@
#ifndef __for_utils_h__
#define __for_utils_h__
#include "ranges/rheader.h"
//#include "ranges/rheader.h"
#include <array>
#include <tuple>

View file

@ -5,8 +5,8 @@
#include <cstdlib>
#include <memory>
#include <tuple>
#include "xfor/for_utils.h"
#include "xfor/for_type.h"
#include "xfor/for_utils.h"
#include "xfor/exttype.h"
namespace MultiArrayHelper
@ -91,7 +91,7 @@ namespace MultiArrayHelper
};
template <class IndexClass, class Expr, ForType FT = ForType::DEFAULT>
template <class IndexClass, class Expr, ForType FT>
class For
{
private: