enable string meta data info by virtual function in RangeBase

This commit is contained in:
Christian Zimmermann 2018-07-21 18:05:53 +02:00
parent 341a70ef06
commit fc40f83dd8
18 changed files with 353 additions and 45 deletions

View file

@ -77,11 +77,11 @@ namespace MultiArrayTools
// EVALUTAION CLASS ??!!!! // EVALUTAION CLASS ??!!!!
auto exec(std::shared_ptr<typename SRanges::IndexType>&... inds) const auto exec(const std::shared_ptr<typename SRanges::IndexType>&... inds) const
-> decltype( mkOperation( mFunc, ConstOperationRoot<typename SRanges::IndexType::MetaType,SRanges>( mkMAObject( inds ), inds) ... ) ); -> decltype( mkOperation( mFunc, ConstOperationRoot<typename SRanges::IndexType::MetaType,SRanges>( mkMAObject( inds ), inds) ... ) );
virtual ConstOperationRoot<T,SRanges...> virtual ConstOperationRoot<T,SRanges...>
operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const override; operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const override;
}; };
@ -175,7 +175,7 @@ namespace MultiArrayTools
template <typename T, class Function, class... SRanges> template <typename T, class Function, class... SRanges>
ConstOperationRoot<T,SRanges...> FunctionalMultiArray<T,Function,SRanges...>:: ConstOperationRoot<T,SRanges...> FunctionalMultiArray<T,Function,SRanges...>::
operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const
{ {
if(not mMaPtr){ if(not mMaPtr){
mMaPtr = std::make_shared<MAType>( MAB::mRange->space() ); mMaPtr = std::make_shared<MAType>( MAB::mRange->space() );
@ -187,7 +187,7 @@ namespace MultiArrayTools
template <typename T, class Function, class... SRanges> template <typename T, class Function, class... SRanges>
auto FunctionalMultiArray<T,Function,SRanges...>:: auto FunctionalMultiArray<T,Function,SRanges...>::
exec(std::shared_ptr<typename SRanges::IndexType>&... inds) const exec(const std::shared_ptr<typename SRanges::IndexType>&... inds) const
-> decltype( mkOperation( mFunc, ConstOperationRoot<typename SRanges::IndexType::MetaType,SRanges>( mkMAObject( inds ), inds) ... ) ) -> decltype( mkOperation( mFunc, ConstOperationRoot<typename SRanges::IndexType::MetaType,SRanges>( mkMAObject( inds ), inds) ... ) )
{ {
return mkOperation( mFunc, ConstOperationRoot<typename SRanges::IndexType::MetaType,SRanges>( mkMAObject( inds ), inds ) ... ); return mkOperation( mFunc, ConstOperationRoot<typename SRanges::IndexType::MetaType,SRanges>( mkMAObject( inds ), inds ) ... );

View file

@ -40,8 +40,8 @@ namespace MultiArrayTools
MultiArray(const std::shared_ptr<SRanges>&... ranges, const T& val); MultiArray(const std::shared_ptr<SRanges>&... ranges, const T& val);
MultiArray(const std::shared_ptr<SRanges>&... ranges, const std::vector<T>& vec); MultiArray(const std::shared_ptr<SRanges>&... ranges, const std::vector<T>& vec);
MultiArray(const std::shared_ptr<SRanges>&... ranges, std::vector<T>&& vec); MultiArray(const std::shared_ptr<SRanges>&... ranges, std::vector<T>&& vec);
MultiArray(const typename CRange::SpaceType& space); MultiArray(const typename CRange::Space& space);
MultiArray(const typename CRange::SpaceType& space, std::vector<T>&& vec); MultiArray(const typename CRange::Space& space, std::vector<T>&& vec);
MultiArray(MultiArray<T,AnonymousRange>& ama, SIZET<SRanges>... sizes); MultiArray(MultiArray<T,AnonymousRange>& ama, SIZET<SRanges>... sizes);
// Only if ALL ranges have default extensions: // Only if ALL ranges have default extensions:
@ -143,7 +143,7 @@ namespace MultiArrayTools
*******************/ *******************/
template <typename T, class... SRanges> template <typename T, class... SRanges>
MultiArray<T,SRanges...>::MultiArray(const typename CRange::SpaceType& space) : MultiArray<T,SRanges...>::MultiArray(const typename CRange::Space& space) :
MutableMultiArrayBase<T,SRanges...>(space), MutableMultiArrayBase<T,SRanges...>(space),
mCont(MAB::mRange->size()) mCont(MAB::mRange->size())
{ {
@ -151,7 +151,7 @@ namespace MultiArrayTools
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>
MultiArray<T,SRanges...>::MultiArray(const typename CRange::SpaceType& space, MultiArray<T,SRanges...>::MultiArray(const typename CRange::Space& space,
std::vector<T>&& vec) : std::vector<T>&& vec) :
MutableMultiArrayBase<T,SRanges...>(space), MutableMultiArrayBase<T,SRanges...>(space),
mCont(vec) mCont(vec)

View file

@ -35,7 +35,7 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(MultiArrayBase); DEFAULT_MEMBERS(MultiArrayBase);
MultiArrayBase(const std::shared_ptr<SRanges>&... ranges); MultiArrayBase(const std::shared_ptr<SRanges>&... ranges);
MultiArrayBase(const typename CRange::SpaceType& space); MultiArrayBase(const typename CRange::Space& space);
virtual ~MultiArrayBase() = default; virtual ~MultiArrayBase() = default;
@ -60,7 +60,7 @@ namespace MultiArrayTools
virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymous() const = 0; virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymous() const = 0;
virtual ConstOperationRoot<T,SRanges...> virtual ConstOperationRoot<T,SRanges...>
operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const; operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const;
virtual bool isInit() const; virtual bool isInit() const;
@ -87,7 +87,7 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(MutableMultiArrayBase); DEFAULT_MEMBERS(MutableMultiArrayBase);
MutableMultiArrayBase(const std::shared_ptr<SRanges>&... ranges); MutableMultiArrayBase(const std::shared_ptr<SRanges>&... ranges);
MutableMultiArrayBase(const typename CRange::SpaceType& space); MutableMultiArrayBase(const typename CRange::Space& space);
virtual T& operator[](const IndexType& i) = 0; virtual T& operator[](const IndexType& i) = 0;
virtual T& at(const typename CRange::IndexType::MetaType& meta) = 0; virtual T& at(const typename CRange::IndexType::MetaType& meta) = 0;
@ -102,8 +102,8 @@ namespace MultiArrayTools
virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymousMove() = 0; virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymousMove() = 0;
virtual ConstOperationRoot<T,SRanges...> virtual ConstOperationRoot<T,SRanges...>
operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const override; operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const override;
virtual OperationRoot<T,SRanges...> operator()(std::shared_ptr<typename SRanges::IndexType>&... inds); virtual OperationRoot<T,SRanges...> operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds);
}; };
@ -129,7 +129,7 @@ namespace MultiArrayTools
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>
MultiArrayBase<T,SRanges...>::MultiArrayBase(const typename CRange::SpaceType& space) MultiArrayBase<T,SRanges...>::MultiArrayBase(const typename CRange::Space& space)
{ {
ContainerRangeFactory<T,SRanges...> crf(space); ContainerRangeFactory<T,SRanges...> crf(space);
mRange = std::dynamic_pointer_cast<ContainerRange<T,SRanges...> >( crf.create() ); mRange = std::dynamic_pointer_cast<ContainerRange<T,SRanges...> >( crf.create() );
@ -193,7 +193,7 @@ namespace MultiArrayTools
template <typename T, class... SRanges> template <typename T, class... SRanges>
ConstOperationRoot<T,SRanges...> ConstOperationRoot<T,SRanges...>
MultiArrayBase<T,SRanges...>::operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const MultiArrayBase<T,SRanges...>::operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const
{ {
return ConstOperationRoot<T,SRanges...>(*this, inds...); return ConstOperationRoot<T,SRanges...>(*this, inds...);
} }
@ -222,7 +222,7 @@ namespace MultiArrayTools
MultiArrayBase<T,SRanges...>(ranges...) {} MultiArrayBase<T,SRanges...>(ranges...) {}
template <typename T, class... SRanges> template <typename T, class... SRanges>
MutableMultiArrayBase<T,SRanges...>::MutableMultiArrayBase(const typename CRange::SpaceType& space) : MutableMultiArrayBase<T,SRanges...>::MutableMultiArrayBase(const typename CRange::Space& space) :
MultiArrayBase<T,SRanges...>(space) {} MultiArrayBase<T,SRanges...>(space) {}
/* /*
template <typename T, class... SRanges> template <typename T, class... SRanges>
@ -247,14 +247,14 @@ namespace MultiArrayTools
template <typename T, class... SRanges> template <typename T, class... SRanges>
OperationRoot<T,SRanges...> OperationRoot<T,SRanges...>
MutableMultiArrayBase<T,SRanges...>::operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) MutableMultiArrayBase<T,SRanges...>::operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds)
{ {
return OperationRoot<T,SRanges...>(*this, inds...); return OperationRoot<T,SRanges...>(*this, inds...);
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>
ConstOperationRoot<T,SRanges...> ConstOperationRoot<T,SRanges...>
MutableMultiArrayBase<T,SRanges...>::operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const MutableMultiArrayBase<T,SRanges...>::operator()(const std::shared_ptr<typename SRanges::IndexType>&... inds) const
{ {
return ConstOperationRoot<T,SRanges...>(*this, inds...); return ConstOperationRoot<T,SRanges...>(*this, inds...);
} }

View file

@ -8,6 +8,7 @@
#include "rbase_def.h" #include "rbase_def.h"
#include "ranges/range_base.h" #include "ranges/range_base.h"
#include "ranges/rpheader.h" #include "ranges/rpheader.h"
#include "ranges/x_to_string.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -63,16 +64,19 @@ namespace MultiArrayTools
typedef SingleRange<size_t,SpaceType::ANON> RangeType; typedef SingleRange<size_t,SpaceType::ANON> RangeType;
typedef size_t MetaType; typedef size_t MetaType;
virtual size_t size() const override; virtual size_t size() const final;
virtual size_t dim() const override; virtual size_t dim() const final;
size_t anonymousDim() const; size_t anonymousDim() const;
size_t get(size_t pos) const; size_t get(size_t pos) const;
size_t getMeta(size_t metaPos) const; size_t getMeta(size_t metaPos) const;
virtual IndexType begin() const override; virtual IndexType begin() const final;
virtual IndexType end() const override; virtual IndexType end() const final;
virtual std::string stringMeta(size_t pos) const final;
virtual std::vector<char> data() const final;
std::shared_ptr<RangeBase> sub(size_t num) const; std::shared_ptr<RangeBase> sub(size_t num) const;
template <class Range> template <class Range>

View file

@ -14,6 +14,7 @@
#include "ranges/rpack_num.h" #include "ranges/rpack_num.h"
#include "ranges/multi_range_factory_product_map.h" #include "ranges/multi_range_factory_product_map.h"
#include "ranges/x_to_string.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -129,7 +130,7 @@ namespace MultiArrayTools
MultiRangeFactory() = delete; MultiRangeFactory() = delete;
MultiRangeFactory(const std::shared_ptr<Ranges>&... rs); MultiRangeFactory(const std::shared_ptr<Ranges>&... rs);
MultiRangeFactory(const typename MultiRange<Ranges...>::SpaceType& space); MultiRangeFactory(const typename MultiRange<Ranges...>::Space& space);
template <typename T> template <typename T>
MultiRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr); MultiRangeFactory(const std::shared_ptr<ContainerRange<T,Ranges...> >& cr);
@ -145,13 +146,13 @@ namespace MultiArrayTools
/****************** /******************
* MultiRange * * MultiRange *
******************/ ******************/
template <class... Ranges> template <class... Ranges>
class MultiRange : public RangeInterface<MultiIndex<typename Ranges::IndexType...> > class MultiRange : public RangeInterface<MultiIndex<typename Ranges::IndexType...> >
{ {
public: public:
typedef RangeBase RB; typedef RangeBase RB;
typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType; typedef std::tuple<std::shared_ptr<Ranges>...> Space;
typedef MultiIndex<typename Ranges::IndexType...> IndexType; typedef MultiIndex<typename Ranges::IndexType...> IndexType;
typedef MultiRange RangeType; typedef MultiRange RangeType;
//typedef typename RangeInterface<MultiIndex<typename Ranges::IndexType...> >::IndexType IndexType; //typedef typename RangeInterface<MultiIndex<typename Ranges::IndexType...> >::IndexType IndexType;
@ -162,9 +163,9 @@ namespace MultiArrayTools
MultiRange& operator=(const MultiRange& in) = delete; MultiRange& operator=(const MultiRange& in) = delete;
MultiRange(const std::shared_ptr<Ranges>&... rs); MultiRange(const std::shared_ptr<Ranges>&... rs);
MultiRange(const SpaceType& space); MultiRange(const Space& space);
SpaceType mSpace; Space mSpace;
public: public:
@ -178,8 +179,11 @@ namespace MultiArrayTools
virtual size_t dim() const final; virtual size_t dim() const final;
virtual size_t size() const final; virtual size_t size() const final;
virtual std::string stringMeta(size_t pos) const final;
virtual std::vector<char> data() const final;
const SpaceType& space() const; const Space& space() const;
virtual IndexType begin() const final; virtual IndexType begin() const final;
virtual IndexType end() const final; virtual IndexType end() const final;
@ -442,7 +446,7 @@ namespace MultiArrayTools
} }
template <class... Ranges> template <class... Ranges>
MultiRangeFactory<Ranges...>::MultiRangeFactory(const typename MultiRange<Ranges...>::SpaceType& st) MultiRangeFactory<Ranges...>::MultiRangeFactory(const typename MultiRange<Ranges...>::Space& st)
{ {
mProd = std::shared_ptr< MultiRange<Ranges...> >( new MultiRange<Ranges...>( st ) ); mProd = std::shared_ptr< MultiRange<Ranges...> >( new MultiRange<Ranges...>( st ) );
} }
@ -493,7 +497,7 @@ namespace MultiArrayTools
MultiRange<Ranges...>::MultiRange(const std::shared_ptr<Ranges>&... rs) : mSpace(std::make_tuple(rs...)) {} MultiRange<Ranges...>::MultiRange(const std::shared_ptr<Ranges>&... rs) : mSpace(std::make_tuple(rs...)) {}
template <class... Ranges> template <class... Ranges>
MultiRange<Ranges...>::MultiRange(const SpaceType& space) : mSpace( space ) {} MultiRange<Ranges...>::MultiRange(const Space& space) : mSpace( space ) {}
template <class... Ranges> template <class... Ranges>
template <size_t N> template <size_t N>
@ -522,10 +526,33 @@ namespace MultiArrayTools
} }
template <class... Ranges> template <class... Ranges>
const typename MultiRange<Ranges...>::SpaceType& MultiRange<Ranges...>::space() const const typename MultiRange<Ranges...>::Space& MultiRange<Ranges...>::space() const
{ {
return mSpace; return mSpace;
} }
template <class... Ranges>
std::string MultiRange<Ranges...>::stringMeta(size_t pos) const
{
auto i = begin();
i = pos;
return "[ " + RPackNum<sizeof...(Ranges)-1>::metaTupleToString(i.meta()) + " ]";
}
template <class... Ranges>
std::vector<char> MultiRange<Ranges...>::data() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::ANY );
h.metaSize = sizeof...(Ranges);
h.multiple = 1;
std::vector<char> out;
//out.reserve(h.metaSize + sizeof(DataHeader));
char* hcp = reinterpret_cast<char*>(&h);
out.insert(out.end(), hcp, hcp + sizeof(DataHeader));
RPackNum<sizeof...(Ranges)-1>::fillRangeDataVec(out, mSpace);
return out;
}
template <class... Ranges> template <class... Ranges>
typename MultiRange<Ranges...>::IndexType MultiRange<Ranges...>::begin() const typename MultiRange<Ranges...>::IndexType MultiRange<Ranges...>::begin() const

View file

@ -20,15 +20,27 @@ namespace MultiArrayTools
size_t indexId(); size_t indexId();
enum class SpaceType enum class SpaceType
{ {
NONE = 0, NONE = 0, // meta data is that of a classic range, i.e. 0,1,2,...,N-1
ANY = 1, ANY = 1, // meta data is arbitrary, i.e. explicitly stored
#define include_range_type(x,n) x = n, #define include_range_type(x,n) x = n,
#include "range_types/header.h" #include "range_types/header.h"
#undef include_range_type #undef include_range_type
ANON = -1 ANON = -1 // anonymous content
}; };
struct DataHeader
{
public:
static constexpr size_t VERSION = 1; // fixed by version of this repository !
private:
size_t version = VERSION;
public:
int spaceType = static_cast<int>( SpaceType::NONE );
size_t metaSize = 0; // size of meta data
int multiple = 0; // = 1 if multi range
};
class RangeFactoryBase class RangeFactoryBase
{ {
public: public:
@ -50,17 +62,20 @@ namespace MultiArrayTools
class RangeBase class RangeBase
{ {
public: public:
static constexpr bool ISINDEX = false; static constexpr bool ISINDEX = false;
virtual ~RangeBase() = default; virtual ~RangeBase() = default;
virtual size_t size() const = 0; virtual size_t size() const = 0;
virtual size_t dim() const = 0; virtual size_t dim() const = 0;
bool operator==(const RangeBase& in) const; bool operator==(const RangeBase& in) const;
bool operator!=(const RangeBase& in) const; bool operator!=(const RangeBase& in) const;
virtual std::string stringMeta(size_t pos) const = 0;
virtual std::vector<char> data() const = 0; // usefull when writing to files, etc...
//virtual bool regular() const = 0; // integer distance (e.g. 2,3,4,...) //virtual bool regular() const = 0; // integer distance (e.g. 2,3,4,...)
//virtual bool linear() const = 0; // 1dim valuable (e.g. 2.45, 3.12, 3.56,...) //virtual bool linear() const = 0; // 1dim valuable (e.g. 2.45, 3.12, 3.56,...)
//virtual bool multi() const = 0; // mdim //virtual bool multi() const = 0; // mdim
@ -69,11 +84,11 @@ namespace MultiArrayTools
friend RangeFactoryBase; friend RangeFactoryBase;
protected: protected:
RangeBase() = default; RangeBase() = default;
std::weak_ptr<RangeBase> mThis; std::weak_ptr<RangeBase> mThis;
}; };
template <class Index> template <class Index>
class RangeInterface : public RangeBase class RangeInterface : public RangeBase
{ {

View file

@ -33,6 +33,9 @@ namespace MultiArrayTools
virtual size_t size() const override; virtual size_t size() const override;
virtual size_t dim() const override; virtual size_t dim() const override;
virtual std::string stringMeta(size_t pos) const override;
virtual std::vector<char> data() const override;
size_t get(size_t pos) const; size_t get(size_t pos) const;
size_t getMeta(size_t metaPos) const; size_t getMeta(size_t metaPos) const;

View file

@ -44,6 +44,9 @@ namespace MultiArrayTools
virtual size_t size() const override; virtual size_t size() const override;
virtual size_t dim() const override; virtual size_t dim() const override;
virtual std::string stringMeta(size_t pos) const override;
virtual std::vector<char> data() const override;
size_t get(size_t pos) const; size_t get(size_t pos) const;
size_t getMeta(size_t metapos) const; size_t getMeta(size_t metapos) const;

View file

@ -33,13 +33,15 @@ namespace MultiArrayTools
virtual size_t size() const override; virtual size_t size() const override;
virtual size_t dim() const override; virtual size_t dim() const override;
virtual std::string stringMeta(size_t pos) const override;
virtual std::vector<char> data() const override;
int get(size_t pos) const; int get(size_t pos) const;
size_t getMeta(int metaPos) const; size_t getMeta(int metaPos) const;
virtual IndexType begin() const override; virtual IndexType begin() const override;
virtual IndexType end() const override; virtual IndexType end() const override;
//virtual std::shared_ptr<VIWB> index() const override;
friend SingleRangeFactory<int,SpaceType::PSPACE>; friend SingleRangeFactory<int,SpaceType::PSPACE>;

View file

@ -37,6 +37,9 @@ namespace MultiArrayTools
virtual size_t size() const override; virtual size_t size() const override;
virtual size_t dim() const override; virtual size_t dim() const override;
virtual std::string stringMeta(size_t pos) const override;
virtual std::vector<char> data() const override;
size_t get(size_t pos) const; size_t get(size_t pos) const;
size_t getMeta(size_t metaPos) const; size_t getMeta(size_t metaPos) const;

View file

@ -7,7 +7,7 @@
#include <cassert> #include <cassert>
#include "ranges/rbase_def.h" #include "ranges/rbase_def.h"
#include "ranges/index_base.h" #include "ranges/index_base.h"
#include "ranges/x_to_string.h"
namespace MultiArrayHelper namespace MultiArrayHelper
{ {
@ -59,6 +59,8 @@ namespace MultiArrayHelper
{ {
v.insert(v.begin(), r); v.insert(v.begin(), r);
} }
template <size_t N> template <size_t N>
struct RPackNum struct RPackNum
@ -297,6 +299,21 @@ namespace MultiArrayHelper
return reinterpret_cast<std::intptr_t>( std::get<N>(p).get() ) == a[N] and return reinterpret_cast<std::intptr_t>( std::get<N>(p).get() ) == a[N] and
RPackNum<N-1>::checkIfCreated(p,a); RPackNum<N-1>::checkIfCreated(p,a);
} }
template <class MetaTuple>
static inline std::string metaTupleToString(const MetaTuple& mtp)
{
return RPackNum<N-1>::metaTupleToString(mtp) + " , " + xToString(std::get<N>(mtp));
}
template <class... Ranges>
static inline void fillRangeDataVec(std::vector<char>& out,
const std::tuple<std::shared_ptr<Ranges>...>& tp)
{
std::vector<char> part = std::get<sizeof...(Ranges)-N-1>(tp)->data();
out.insert(out.end(), part.begin(), part.end());
RPackNum<N-1>::fillRangeDataVec(out, tp);
}
}; };
@ -496,6 +513,20 @@ namespace MultiArrayHelper
return reinterpret_cast<std::intptr_t>( std::get<0>(p).get() ) == a[0]; return reinterpret_cast<std::intptr_t>( std::get<0>(p).get() ) == a[0];
} }
template <class MetaTuple>
static inline std::string metaTupleToString(const MetaTuple& mtp)
{
return xToString(std::get<0>(mtp));
}
template <class... Ranges>
static inline void fillRangeDataVec(std::vector<char>& out,
const std::tuple<std::shared_ptr<Ranges>...>& tp)
{
std::vector<char> part = std::get<sizeof...(Ranges)-1>(tp)->data();
out.insert(out.end(), part.begin(), part.end());
}
}; };
template <IndexType IT> template <IndexType IT>

View file

@ -11,6 +11,7 @@
//#include "ranges/rpack_num.h" //#include "ranges/rpack_num.h"
#include "ranges/index_base.h" #include "ranges/index_base.h"
#include "ranges/range_base.h" #include "ranges/range_base.h"
#include "ranges/x_to_string.h"
#include "xfor/xfor.h" #include "xfor/xfor.h"
@ -103,6 +104,9 @@ namespace MultiArrayTools
virtual size_t size() const override; virtual size_t size() const override;
virtual size_t dim() const override; virtual size_t dim() const override;
virtual std::string stringMeta(size_t pos) const override;
virtual std::vector<char> data() const override;
const U& get(size_t pos) const; const U& get(size_t pos) const;
size_t getMeta(const U& metaPos) const; size_t getMeta(const U& metaPos) const;
@ -365,6 +369,28 @@ namespace MultiArrayTools
return 1; return 1;
} }
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));
const char* scp = reinterpret_cast<const char*>(mSpace.data());
out.insert(out.end(), scp, scp + h.metaSize);
return out;
}
template <typename U, SpaceType TYPE> template <typename U, SpaceType TYPE>
typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::begin() const typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::begin() const
{ {

View file

@ -0,0 +1,83 @@
#ifndef __x_to_string_h__
#define __x_to_string_h__
#include <string>
#include <vector>
#include <array>
#include <tuple>
namespace MultiArrayHelper
{
template <typename T>
inline std::string xToString(const T& x)
{
return std::to_string(x);
}
template <size_t N>
struct TupleToString
{
template <typename... Ts>
static inline std::string mk(const std::tuple<Ts...>& tp)
{
return TupleToString<N-1>::mk(tp) + " , " + xToString(std::get<N>(tp));
}
};
template <>
struct TupleToString<0>
{
template <typename... Ts>
static inline std::string mk(const std::tuple<Ts...>& tp)
{
return xToString(std::get<0>(tp));
}
};
template <>
inline std::string xToString<char>(const char& x)
{
std::string out = "";
return out += x;
}
template <>
inline std::string xToString<std::string>(const std::string& x)
{
return x;
}
template <typename T>
inline std::string xToString(const std::vector<T>& x)
{
std::string out = "[ ";
for(auto& y: x){
out += y + " , ";
}
out.pop_back();
out.back() = ']';
return out;
}
template <typename T, size_t N>
inline std::string xToString(const std::array<T,N>& x)
{
std::string out = "[ ";
for(auto& y: x){
out += y + " , ";
}
out.pop_back();
out.back() = ']';
return out;
}
template <typename... Ts>
inline std::string xToString(const std::tuple<Ts...>& tp)
{
return "{ " + TupleToString<sizeof...(Ts)-1>::mk(tp) + " }";
}
}
#endif

View file

@ -77,6 +77,36 @@ namespace MultiArrayTools
return 1; return 1;
} }
std::string AnonymousRange::stringMeta(size_t pos) const
{
std::string out = "[ ";
size_t xpos = pos;
for(size_t i = mOrig.size(); i != 0; --i) {
auto& x = mOrig[i-1];
const size_t redpos = pos % x->size();
out = ( (i == mOrig.size()) ? out : out + " , " ) + x->stringMeta(redpos);
xpos -= redpos;
}
out += " ]";
return out;
}
std::vector<char> AnonymousRange::data() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::ANON );
h.metaSize = mOrig.size();
h.multiple = 1;
std::vector<char> out;
char* hcp = reinterpret_cast<char*>(&h);
out.insert(out.end(), hcp, hcp + sizeof(DataHeader));
for(auto& x: mOrig){
auto part = x->data();
out.insert(out.end(), part.begin(), part.end());
}
return out;
}
size_t AnonymousRange::anonymousDim() const size_t AnonymousRange::anonymousDim() const
{ {
return mOrig.size(); return mOrig.size();

View file

@ -1,5 +1,6 @@
#include "ranges/rheader.h" #include "ranges/rheader.h"
#include "ranges/x_to_string.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -46,6 +47,27 @@ namespace MultiArrayTools
{ {
return 1; return 1;
} }
std::string SingleRange<size_t,SpaceType::NONE>::stringMeta(size_t pos) const
{
return std::to_string(get(pos));
}
std::vector<char> SingleRange<size_t,SpaceType::NONE>::data() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::NONE );
h.metaSize = sizeof(size_t);
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*>(&mSize);
out.insert(out.end(), scp, scp + h.metaSize);
return out;
}
typename SingleRange<size_t,SpaceType::NONE>::IndexType SingleRange<size_t,SpaceType::NONE>::begin() const typename SingleRange<size_t,SpaceType::NONE>::IndexType SingleRange<size_t,SpaceType::NONE>::begin() const
{ {

View file

@ -1,5 +1,6 @@
#include "ranges/rheader.h" #include "ranges/rheader.h"
#include "ranges/x_to_string.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -61,7 +62,25 @@ namespace MultiArrayTools
{ {
return 1; return 1;
} }
std::string SingleRange<size_t,SpaceType::NUL>::stringMeta(size_t pos) const
{
return std::to_string(get(pos));
}
std::vector<char> SingleRange<size_t,SpaceType::NUL>::data() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::NUL );
h.metaSize = 0;
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));
return out;
}
typename SingleRange<size_t,SpaceType::NUL>::IndexType SingleRange<size_t,SpaceType::NUL>::begin() const typename SingleRange<size_t,SpaceType::NUL>::IndexType SingleRange<size_t,SpaceType::NUL>::begin() const
{ {
SingleIndex<size_t,SpaceType::NUL> i( std::dynamic_pointer_cast<SingleRange<size_t,SpaceType::NUL> > SingleIndex<size_t,SpaceType::NUL> i( std::dynamic_pointer_cast<SingleRange<size_t,SpaceType::NUL> >

View file

@ -1,5 +1,6 @@
#include "ranges/rheader.h" #include "ranges/rheader.h"
#include "ranges/x_to_string.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -46,6 +47,26 @@ namespace MultiArrayTools
{ {
return 1; return 1;
} }
std::string SingleRange<int,SpaceType::PSPACE>::stringMeta(size_t pos) const
{
return std::to_string(get(pos));
}
std::vector<char> SingleRange<int,SpaceType::PSPACE>::data() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::PSPACE );
h.metaSize = sizeof(size_t);
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*>(&mSize);
out.insert(out.end(), scp, scp + h.metaSize);
return out;
}
typename SingleRange<int,SpaceType::PSPACE>::IndexType SingleRange<int,SpaceType::PSPACE>::begin() const typename SingleRange<int,SpaceType::PSPACE>::IndexType SingleRange<int,SpaceType::PSPACE>::begin() const
{ {

View file

@ -1,5 +1,6 @@
#include "ranges/rheader.h" #include "ranges/rheader.h"
#include "ranges/x_to_string.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -44,7 +45,25 @@ namespace MultiArrayTools
{ {
return 1; return 1;
} }
std::string SingleRange<size_t,SpaceType::SPIN>::stringMeta(size_t pos) const
{
return std::to_string(get(pos));
}
std::vector<char> SingleRange<size_t,SpaceType::SPIN>::data() const
{
DataHeader h;
h.spaceType = static_cast<int>( SpaceType::SPIN );
h.metaSize = 0;
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));
return out;
}
typename SingleRange<size_t,SpaceType::SPIN>::IndexType SingleRange<size_t,SpaceType::SPIN>::begin() const typename SingleRange<size_t,SpaceType::SPIN>::IndexType SingleRange<size_t,SpaceType::SPIN>::begin() const
{ {
SingleIndex<size_t,SpaceType::SPIN> i( std::dynamic_pointer_cast<SingleRange<size_t,SpaceType::SPIN> > SingleIndex<size_t,SpaceType::SPIN> i( std::dynamic_pointer_cast<SingleRange<size_t,SpaceType::SPIN> >