add pspace range + some dev reg anonymous range (scast implementation missing)

This commit is contained in:
Christian Zimmermann 2018-05-20 20:03:44 +02:00
parent 3367ab684a
commit 689ad3801f
11 changed files with 308 additions and 21 deletions

View file

@ -7,6 +7,20 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
template <typename T>
struct ArrayCatter;
template <typename T>
struct ArrayCatter
{
template <class... Ranges>
static auto cat(const MultiArray<T,Ranges...>& ma)
-> MultiArray<T,Ranges...>
{
return ma;
}
};
template <typename T, class... SRanges> template <typename T, class... SRanges>
@ -23,6 +37,7 @@ namespace MultiArrayTools
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::SpaceType& space);
MultiArray(const typename CRange::SpaceType& space, std::vector<T>&& vec);
// Only if ALL ranges have default extensions: // Only if ALL ranges have default extensions:
//MultiArray(const std::vector<T>& vec); //MultiArray(const std::vector<T>& vec);
@ -51,6 +66,10 @@ namespace MultiArrayTools
virtual const T* data() const override; virtual const T* data() const override;
virtual T* data() override; virtual T* data() override;
virtual std::vector<T>& vdata() { return mCont; } virtual std::vector<T>& vdata() { return mCont; }
virtual const std::vector<T>& vdata() const { return mCont; }
auto cat() const
-> decltype(ArrayCatter<T>::cat(*this));
operator T() const; operator T() const;
@ -58,12 +77,36 @@ namespace MultiArrayTools
friend class MultiArray; friend class MultiArray;
private: private:
std::vector<T> mCont; std::vector<T> mCont;
}; };
template <typename T> template <typename T>
using Scalar = MultiArray<T>; using Scalar = MultiArray<T>;
template <typename T, class... ERanges>
struct ArrayCatter<MultiArray<T,ERanges...> >
{
template <class... Ranges>
static auto cat(const MultiArray<MultiArray<T,ERanges...>,Ranges...>& ma)
-> MultiArray<T,Ranges...,ERanges...>
{
auto sma = *ma.begin();
const size_t smas = sma.size();
const size_t mas = ma.size();
auto cr = ma.range()->cat(sma.range());
std::vector<T> ov;
ov.reserve(mas * smas);
for(auto& x: ma){
assert(x.size() == smas);
ov.insert(ov.end(), x.vdata().begin(), x.vdata().end());
}
return MultiArray<T,Ranges...,ERanges...>(cr->space(), std::move(ov));
}
};
} }
/* ========================= * /* ========================= *
@ -85,6 +128,19 @@ namespace MultiArrayTools
MAB::mInit = true; MAB::mInit = true;
} }
template <typename T, class... SRanges>
MultiArray<T,SRanges...>::MultiArray(const typename CRange::SpaceType& space,
std::vector<T>&& vec) :
MutableMultiArrayBase<T,SRanges...>(space),
mCont(vec)
{
MAB::mInit = true;
if(mCont.size() > MAB::mRange->size()){
mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end());
}
}
template <typename T, class... SRanges> template <typename T, class... SRanges>
MultiArray<T,SRanges...>::MultiArray(const std::shared_ptr<SRanges>&... ranges) : MultiArray<T,SRanges...>::MultiArray(const std::shared_ptr<SRanges>&... ranges) :
MutableMultiArrayBase<T,SRanges...>(ranges...), MutableMultiArrayBase<T,SRanges...>(ranges...),
@ -206,6 +262,13 @@ namespace MultiArrayTools
static_assert( sizeof...(SRanges) == 0, "try to cast non-scalar type into scalar" ); static_assert( sizeof...(SRanges) == 0, "try to cast non-scalar type into scalar" );
return mCont[0]; return mCont[0];
} }
template <typename T, class... SRanges>
auto MultiArray<T,SRanges...>::cat() const
-> decltype(ArrayCatter<T>::cat(*this))
{
return ArrayCatter<T>::cat(*this);
}
} }
#endif #endif

View file

@ -26,6 +26,13 @@ namespace MultiArrayTools
typedef ContainerRange<T,SRanges...> CRange; typedef ContainerRange<T,SRanges...> CRange;
typedef ContainerIndex<T,typename SRanges::IndexType...> IndexType; typedef ContainerIndex<T,typename SRanges::IndexType...> IndexType;
protected:
bool mInit = false;
std::shared_ptr<CRange> mRange;
std::shared_ptr<IndexType> mProtoI;
public:
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::SpaceType& space);
@ -55,10 +62,10 @@ namespace MultiArrayTools
virtual bool isInit() const; virtual bool isInit() const;
protected: template <size_t N>
bool mInit = false; auto getRangePtr() const
std::shared_ptr<CRange> mRange; -> decltype(mRange->template getPtr<N>());
std::shared_ptr<IndexType> mProtoI;
}; };
template <typename T, class... SRanges> template <typename T, class... SRanges>
@ -193,6 +200,14 @@ namespace MultiArrayTools
return mInit; return mInit;
} }
template <typename T, class... SRanges>
template <size_t N>
auto MultiArrayBase<T,SRanges...>::getRangePtr() const
-> decltype(mRange->template getPtr<N>())
{
return mRange->template getPtr<N>();
}
/****************************** /******************************
* MutableMultiArrayBase * * MutableMultiArrayBase *

View file

@ -27,6 +27,9 @@ namespace MultiArrayTools
template <class... RangeTypes> template <class... RangeTypes>
AnonymousRangeFactory(std::shared_ptr<RangeTypes>... origs); AnonymousRangeFactory(std::shared_ptr<RangeTypes>... origs);
template <class Range>
void append(std::shared_ptr<Range> r);
std::shared_ptr<RangeBase> create(); std::shared_ptr<RangeBase> create();
}; };
@ -57,6 +60,9 @@ namespace MultiArrayTools
template <class Range> template <class Range>
std::shared_ptr<Range> fullsub(size_t num) const; std::shared_ptr<Range> fullsub(size_t num) const;
template <class... Ranges>
MultiRange<Ranges...> scast() const; // save cast
friend AnonymousRangeFactory; friend AnonymousRangeFactory;
protected: protected:
@ -100,6 +106,12 @@ namespace MultiArrayTools
mProd = std::shared_ptr<oType>( new AnonymousRange( origs... ) ); mProd = std::shared_ptr<oType>( new AnonymousRange( origs... ) );
} }
template <class Range>
void AnonymousRangeFactory::append(std::shared_ptr<Range> r)
{
std::dynamic_pointer_cast<oType>(mProd)->mOrig.push_back(r);
}
/*********************** /***********************
* AnonymousRange * * AnonymousRange *
***********************/ ***********************/
@ -129,6 +141,12 @@ namespace MultiArrayTools
return std::dynamic_pointer_cast<Range>( mOrig.at(num) ); return std::dynamic_pointer_cast<Range>( mOrig.at(num) );
} }
template <class... Ranges>
MultiRange<Ranges...> AnonymousRange::scast() const
{
// !!!!!!
}
/***************** /*****************
* Functions * * Functions *
*****************/ *****************/

View file

@ -494,6 +494,7 @@ namespace MultiArrayTools
(*this)++; (*this)++;
} }
} }
return *this;
} }
template <typename T, class... Indices> template <typename T, class... Indices>
@ -509,6 +510,7 @@ namespace MultiArrayTools
(*this)--; (*this)--;
} }
} }
return *this;
} }
template <typename T, class... Indices> template <typename T, class... Indices>

View file

@ -176,6 +176,10 @@ namespace MultiArrayTools
virtual IndexType begin() const final; virtual IndexType begin() const final;
virtual IndexType end() const final; virtual IndexType end() const final;
template <class... ERanges>
auto cat(const std::shared_ptr<MultiRange<ERanges...> >& erange)
-> std::shared_ptr<MultiRange<Ranges...,ERanges...> >;
friend MultiRangeFactory<Ranges...>; friend MultiRangeFactory<Ranges...>;
static constexpr bool defaultable = false; static constexpr bool defaultable = false;
@ -510,6 +514,17 @@ namespace MultiArrayTools
i = size(); i = size();
return i; return i;
} }
template <class... Ranges>
template <class... ERanges>
auto MultiRange<Ranges...>::cat(const std::shared_ptr<MultiRange<ERanges...> >& erange)
-> std::shared_ptr<MultiRange<Ranges...,ERanges...> >
{
auto crange = std::tuple_cat(mSpace, erange->space());
MultiRangeFactory<Ranges...,ERanges...> rf(crange);
return std::dynamic_pointer_cast<MultiRange<Ranges...,ERanges...> >(rf.create());
}
} }
#endif #endif

View file

@ -15,6 +15,7 @@
//#define __ranges_header__ //#define __ranges_header__
#include "spin_range.h" #include "spin_range.h"
#include "space_range.h"
#include "classic_range.h" #include "classic_range.h"
#undef __ranges_header__ #undef __ranges_header__

View file

@ -0,0 +1,118 @@
#ifdef include_range_type
include_range_type(PSPACE,3) // Periodic 1dim space
#else
#ifdef __ranges_header__
namespace MultiArrayTools
{
// Periodic 1dim space
typedef SingleIndex<int,SpaceType::PSPACE> XSpaceIndex;
template <>
class SingleRangeFactory<int,SpaceType::PSPACE> : public RangeFactoryBase
{
public:
typedef SingleRange<int,SpaceType::PSPACE> oType;
SingleRangeFactory(size_t size = 0);
std::shared_ptr<RangeBase> create();
};
template <>
class SingleRange<int,SpaceType::PSPACE> : public RangeInterface<XSpaceIndex>
{
public:
typedef RangeBase RB;
typedef typename RangeInterface<SingleIndex<int,SpaceType::PSPACE> >::IndexType IndexType;
typedef SingleRange<int,SpaceType::PSPACE> RangeType;
typedef int MetaType;
virtual size_t size() const override;
virtual size_t dim() const override;
int get(size_t pos) const;
size_t getMeta(int metaPos) const;
virtual IndexType begin() const override;
virtual IndexType end() const override;
//virtual std::shared_ptr<VIWB> index() const override;
friend SingleRangeFactory<int,SpaceType::PSPACE>;
static constexpr bool defaultable = true;
static constexpr size_t ISSTATIC = 0;
static constexpr size_t SIZE = -1;
static constexpr bool HASMETACONT = false;
static SingleRangeFactory<int, SpaceType::PSPACE> factory(size_t size = 0)
{ return SingleRangeFactory<int, SpaceType::PSPACE>(size); }
protected:
size_t mSize = 0;
SingleRange() = default;
SingleRange(const SingleRange& in) = delete;
SingleRange(size_t size);
};
typedef SingleRange<int,SpaceType::PSPACE> PSpaceRange;
typedef SingleRangeFactory<int,SpaceType::PSPACE> PSpaceRF;
template <class SpaceRange>
struct PromoteMSpaceRange
{
template <class... SpaceRanges>
static auto mk(MultiRange<SpaceRanges...>) -> MultiRange<SpaceRange,SpaceRanges...>;
template <class... SpaceRanges>
static auto mkf(MultiRangeFactory<SpaceRanges...>) -> MultiRangeFactory<SpaceRange,SpaceRanges...>;
};
template <size_t N>
struct CreateNDimSpaceRange
{
template <class SpaceRange>
static auto mk()
-> decltype(PromoteMSpaceRange<SpaceRange>::
template mk(CreateNDimSpaceRange<N-1>::
template mk<SpaceRange>()));
template <class SpaceRange>
static auto mkf()
-> decltype(PromoteMSpaceRange<SpaceRange>::
template mkf(CreateNDimSpaceRange<N-1>::
template mkf<SpaceRange>()));
};
template <>
struct CreateNDimSpaceRange<1>
{
template <class SpaceRange>
static auto mk()
-> MultiRange<SpaceRange>;
template <class SpaceRange>
static auto mkf()
-> MultiRangeFactory<SpaceRange>;
};
template <class SpaceRange, size_t N>
using MSpaceRange = decltype(CreateNDimSpaceRange<N>::template mk<SpaceRange>());
template <class SpaceRange, size_t N>
using MSpaceRF = decltype(CreateNDimSpaceRange<N>::template mkf<SpaceRange>());
}
#endif // #ifdef __ranges_header__
#endif // #ifdef include_range_type

View file

@ -203,7 +203,7 @@ namespace MultiArrayHelper
template <class... Ranges> template <class... Ranges>
static void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst, static void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<RangeBase> v) std::vector<std::shared_ptr<RangeBase> >& v)
{ {
v[N] = std::get<N>(rst); v[N] = std::get<N>(rst);
RPackNum<N-1>::RangesToVec(rst, v); RPackNum<N-1>::RangesToVec(rst, v);
@ -387,7 +387,7 @@ namespace MultiArrayHelper
template <class... Ranges> template <class... Ranges>
static void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst, static void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<RangeBase> v) std::vector<std::shared_ptr<RangeBase> >& v)
{ {
v[0] = std::get<0>(rst); v[0] = std::get<0>(rst);
} }

View file

@ -9,7 +9,7 @@ namespace MultiArrayTools
std::shared_ptr<RangeBase> AnonymousRangeFactory::create() std::shared_ptr<RangeBase> AnonymousRangeFactory::create()
{ {
setSelf(); //setSelf();
return mProd; return mProd;
} }
@ -55,6 +55,7 @@ namespace MultiArrayTools
return i; return i;
} }
std::shared_ptr<RangeBase> AnonymousRange::sub(size_t num) const std::shared_ptr<RangeBase> AnonymousRange::sub(size_t num) const
{ {
return mOrig.at(num); return mOrig.at(num);

View file

@ -62,17 +62,5 @@ namespace MultiArrayTools
i = size(); i = size();
return i; return i;
} }
// put this in the interface class !!!
/*
std::shared_ptr<VIWB> SingleRange<size_t,SpaceType::NONE>::index() const
{
typedef IndexWrapper<IndexType> IW;
return std::make_shared<IW>
( std::make_shared<IndexType>
( std::dynamic_pointer_cast<SingleRange<size_t,SpaceType::NONE> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) ) );
}
*/
} }

View file

@ -0,0 +1,66 @@
#include "ranges/rheader.h"
namespace MultiArrayTools
{
/********************
* SingleRange *
********************/
SingleRangeFactory<int,SpaceType::PSPACE>::SingleRangeFactory(size_t size)
{
// Quasi Singleton
if(not mProd){
mProd = std::shared_ptr<oType>( new SingleRange<int,SpaceType::PSPACE>(size) );
setSelf();
}
}
std::shared_ptr<RangeBase> SingleRangeFactory<int,SpaceType::PSPACE>::create()
{
return mProd;
}
/********************
* SingleRange *
********************/
SingleRange<int,SpaceType::PSPACE>::SingleRange(size_t size) : mSize(size) { }
int SingleRange<int,SpaceType::PSPACE>::get(size_t pos) const
{
return pos > mSize / 2 ? pos - mSize : pos;
}
size_t SingleRange<int,SpaceType::PSPACE>::getMeta(int metaPos) const
{
return metaPos < 0 ? metaPos + mSize : metaPos;
}
size_t SingleRange<int,SpaceType::PSPACE>::size() const
{
return mSize;
}
size_t SingleRange<int,SpaceType::PSPACE>::dim() const
{
return 1;
}
typename SingleRange<int,SpaceType::PSPACE>::IndexType SingleRange<int,SpaceType::PSPACE>::begin() const
{
SingleIndex<int,SpaceType::PSPACE> i( std::dynamic_pointer_cast<SingleRange<int,SpaceType::PSPACE> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = 0;
return i;
}
typename SingleRange<int,SpaceType::PSPACE>::IndexType SingleRange<int,SpaceType::PSPACE>::end() const
{
SingleIndex<int,SpaceType::PSPACE> i( std::dynamic_pointer_cast<SingleRange<int,SpaceType::PSPACE> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = size();
return i;
}
}