diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index a35593b..916da1e 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -7,7 +7,7 @@ #include #include -#include "base_def.h" +#include "rbase_def.h" namespace MultiArrayTools { diff --git a/src/include/ranges/rbase_def.h b/src/include/ranges/rbase_def.h index 1c0e4a2..8212957 100644 --- a/src/include/ranges/rbase_def.h +++ b/src/include/ranges/rbase_def.h @@ -72,6 +72,15 @@ namespace MultiArrayTools // anonymous_range.h //class AnonymousRange; + // value_range.h + template + class ValueRange; + + template + class ValueRangeFactory; + + template + class ValueIndex; } #endif diff --git a/src/include/ranges/rheader.h b/src/include/ranges/rheader.h index 3892b95..60a38e4 100644 --- a/src/include/ranges/rheader.h +++ b/src/include/ranges/rheader.h @@ -1,3 +1,9 @@ +//#ifndef __rheader_h__ +//#define __rheader_h__ + #include "rpheader.h" #include "anonymous_range.h" +#include "value_range.h" + +//#endif diff --git a/src/include/ranges/rpheader.h b/src/include/ranges/rpheader.h index 1ae1f61..c3c4142 100644 --- a/src/include/ranges/rpheader.h +++ b/src/include/ranges/rpheader.h @@ -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 diff --git a/src/include/ranges/single_range.h b/src/include/ranges/single_range.h index e3d18a0..ac6a90e 100644 --- a/src/include/ranges/single_range.h +++ b/src/include/ranges/single_range.h @@ -15,13 +15,19 @@ #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 class SingleIndex : public IndexInterface,U> { diff --git a/src/include/ranges/value_range.h b/src/include/ranges/value_range.h new file mode 100644 index 0000000..af156b2 --- /dev/null +++ b/src/include/ranges/value_range.h @@ -0,0 +1,393 @@ + +#ifndef __value_range_h__ +#define __value_range_h__ + +#include +#include +#include +#include + +#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 + class ValueIndex : public IndexInterface,U> + { + public: + + typedef IndexInterface,U> IB; + typedef U MetaType; + typedef ValueRange 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 range(); + + template + void getPtr(); + + size_t getStepSize(size_t n); + + std::string id() const; + void print(size_t offset); + + template + auto ifor(Expr ex) const + -> For,Expr>; + + template + auto iforh(Expr ex) const + -> For,Expr,ForType::HIDDEN>; + + private: + std::shared_ptr mExplicitRangePtr; + U mMeta; + }; + + template + class ValueRangeFactory : public RangeFactoryBase + { + public: + + typedef ValueRange oType; + + ValueRangeFactory(); + std::shared_ptr create(); + }; + + template + class ValueRange : public RangeInterface > + { + public: + typedef RangeBase RB; + typedef ValueIndex 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 data() const final; + + virtual IndexType begin() const final; + virtual IndexType end() const final; + + friend ValueRangeFactory; + + 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 + std::shared_ptr mkDefaultValueRange() + { + ValueRangeFactory vrf; + return vrf->create(); + } + } + + template + ValueIndex::ValueIndex(const U& val) : IndexInterface,U>(mkDefaultValueRange(), 0), + mExplicitRangePtr(std::dynamic_pointer_cast(IB::mRangePtr)), + mMeta(val) {} + + template + ValueIndex::ValueIndex(U&& val) : IndexInterface,U>(mkDefaultValueRange(), 0), + mExplicitRangePtr(std::dynamic_pointer_cast(IB::mRangePtr)), + mMeta(val) {} + + template + IndexType ValueIndex::type() const + { + return IndexType::SINGLE; + } + + template + ValueIndex& ValueIndex::operator=(size_t pos) + { + IB::mPos = pos; + return *this; + } + + template + ValueIndex& ValueIndex::operator++() + { + ++IB::mPos; + return *this; + } + + template + ValueIndex& ValueIndex::operator--() + { + --IB::mPos; + return *this; + } + + template + int ValueIndex::pp(std::intptr_t idxPtrNum) + { + ++(*this); + return 1; + } + + template + int ValueIndex::mm(std::intptr_t idxPtrNum) + { + --(*this); + return 1; + } + + template + std::string ValueIndex::stringMeta() const + { + return xToString(mMeta); + } + + template + const U& ValueIndex::meta() const + { + return mMeta; + } + + template + const U* ValueIndex::metaPtr() const + { + return &mMeta; + } + + template + ValueIndex& ValueIndex::at(const U& metaPos) + { + mMeta = metaPos; + return *this; + } + + template + size_t ValueIndex::posAt(const U& metaPos) const + { + return 0; + } + + template + size_t ValueIndex::dim() // = 1 + { + return 1; + } + + template + bool ValueIndex::last() + { + return IB::mPos == IB::mMax - 1; + } + + template + bool ValueIndex::first() + { + return IB::mPos == 0; + } + + template + std::shared_ptr::RangeType> ValueIndex::range() + { + return std::dynamic_pointer_cast( IB::mRangePtr ); + } + + template + template + void ValueIndex::getPtr() {} + + template + size_t ValueIndex::getStepSize(size_t n) + { + return 1; + } + + template + std::string ValueIndex::id() const + { + return std::string("val") + std::to_string(IB::mId); + } + + template + void ValueIndex::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(this) + << "](" << IB::mRangePtr << "): " << meta() << std::endl; + } + + template + template + auto ValueIndex::ifor(Expr ex) const + -> For,Expr> + { + //static const size_t LAYER = typename Expr::LAYER; + return For,Expr>(this, ex); + } + + template + template + auto ValueIndex::iforh(Expr ex) const + -> For,Expr,ForType::HIDDEN> + { + //static const size_t LAYER = typename Expr::LAYER; + return For,Expr,ForType::HIDDEN>(this, ex); + } + + + /************************** + * ValueRangeFactory * + **************************/ + + + template + ValueRangeFactory::ValueRangeFactory() + { + mProd = std::shared_ptr( new ValueRange() ); + } + + template + std::shared_ptr ValueRangeFactory::create() + { + setSelf(); + return mProd; + } + + + /******************* + * ValueRange * + *******************/ + + template + size_t ValueRange::size() const + { + return 1; + } + + template + size_t ValueRange::dim() const + { + return 1; + } + + template + SpaceType ValueRange::spaceType() const + { + return SpaceType::NONE; + } + + template + std::string ValueRange::stringMeta(size_t pos) const + { + return ""; + } + + template + std::vector ValueRange::data() const + { + assert(0); + DataHeader h; + h.spaceType = static_cast( SpaceType::NONE ); + h.metaSize = 0; + h.metaType = NumTypeMap::num; + h.multiple = 0; + std::vector out; + out.reserve(h.metaSize + sizeof(DataHeader)); + char* hcp = reinterpret_cast(&h); + out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); + + //const char* scp = reinterpret_cast(mSpace.data()); + //out.insert(out.end(), scp, scp + h.metaSize); + return out; + } + + template + typename ValueRange::IndexType ValueRange::begin() const + { + ValueIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = 0; + return i; + } + + template + typename ValueRange::IndexType ValueRange::end() const + { + ValueIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = size(); + return i; + } + + +} // namespace MultiArrayTools + +#endif diff --git a/src/include/xfor/for_type.h b/src/include/xfor/for_type.h index 0d2deda..d6866a5 100644 --- a/src/include/xfor/for_type.h +++ b/src/include/xfor/for_type.h @@ -4,13 +4,15 @@ namespace MultiArrayHelper { - enum class ForType { DEFAULT = 0, HIDDEN = 1 }; + template + class For; + } // end namespace MultiArrayHelper diff --git a/src/include/xfor/for_utils.h b/src/include/xfor/for_utils.h index b706c97..20679b0 100644 --- a/src/include/xfor/for_utils.h +++ b/src/include/xfor/for_utils.h @@ -2,7 +2,7 @@ #ifndef __for_utils_h__ #define __for_utils_h__ -#include "ranges/rheader.h" +//#include "ranges/rheader.h" #include #include diff --git a/src/include/xfor/xfor.h b/src/include/xfor/xfor.h index cb64be5..14f7ebd 100644 --- a/src/include/xfor/xfor.h +++ b/src/include/xfor/xfor.h @@ -5,8 +5,8 @@ #include #include #include -#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 + template class For { private: