cnorxz/src/include/slice.h

199 lines
5.6 KiB
C
Raw Normal View History

#ifndef __slice_h__
#define __slice_h__
#include "multi_array_base.h"
2018-03-05 18:46:29 +01:00
#include "multi_array_operation.h"
namespace MultiArrayTools
{
template <typename T, class... SRanges>
class ConstSlice : public MultiArrayBase<T,SRanges...>
{
public:
typedef ContainerRange<SRanges...> CRange;
typedef MultiArrayBase<T,SRanges...> MAB;
2021-05-28 17:29:13 +02:00
typedef ConstContainerIndex<T,typename SRanges::IndexType...> IType;
2018-09-16 18:53:28 +02:00
using MultiArrayBase<T,SRanges...>::operator();
using MultiArrayBase<T,SRanges...>::operator[];
DEFAULT_MEMBERS(ConstSlice);
ConstSlice(const std::tuple<std::shared_ptr<SRanges>...>& ranges,
const T* data = nullptr);
ConstSlice(const std::shared_ptr<SRanges>&... ranges, const T* data = nullptr);
2018-08-06 15:20:06 +02:00
ConstSlice(const MultiArrayBase<T,AnonymousRange>& ma, SIZET<SRanges>... sizes);
2019-05-17 15:10:33 +02:00
virtual const T& operator[](const IType& i) const final;
virtual const T& at(const typename IType::MetaType& meta) const override;
virtual const T* data() const override;
virtual bool isSlice() const override;
virtual auto begin() const -> IType override;
virtual auto end() const -> IType override;
2018-08-06 15:20:06 +02:00
virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymous(bool slice = false) const override;
auto define(const std::shared_ptr<typename SRanges::IndexType>&... inds)
2018-09-13 13:28:40 +02:00
-> ConstSliceDef<T,SRanges...>;
private:
2018-09-13 13:28:40 +02:00
friend ConstSliceDef<T,SRanges...>;
void format(const std::array<size_t,sizeof...(SRanges)+1>& blocks);
const T* mData;
};
template <typename T, class... SRanges>
class Slice : public MutableMultiArrayBase<T,SRanges...>
{
public:
typedef ContainerRange<SRanges...> CRange;
typedef MultiArrayBase<T,SRanges...> MAB;
2021-05-28 17:29:13 +02:00
typedef ConstContainerIndex<T,typename SRanges::IndexType...> IType;
2018-09-16 18:53:28 +02:00
using MultiArrayBase<T,SRanges...>::operator();
using MutableMultiArrayBase<T,SRanges...>::operator();
using MultiArrayBase<T,SRanges...>::operator[];
using MutableMultiArrayBase<T,SRanges...>::operator[];
DEFAULT_MEMBERS(Slice);
2019-02-13 21:59:13 +01:00
Slice(const std::tuple<std::shared_ptr<SRanges>...>& ranges,
T* data = nullptr);
Slice(const std::shared_ptr<SRanges>&... ranges, T* data = nullptr);
2019-05-17 15:10:33 +02:00
Slice& operator=(T val);
virtual const T& operator[](const IType& i) const final;
virtual T& operator[](const IType& i) final;
2018-03-05 00:04:50 +01:00
virtual const T& at(const typename IType::MetaType& meta) const override;
virtual T& at(const typename IType::MetaType& meta) override;
virtual const T* data() const override;
virtual T* data() override;
virtual bool isSlice() const override;
2018-03-05 00:04:50 +01:00
virtual auto begin() const -> IType override;
virtual auto end() const -> IType override;
2018-03-05 18:46:29 +01:00
2018-08-06 15:20:06 +02:00
virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymous(bool slice = false) const override;
//virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymousMove() override;
auto define(const std::shared_ptr<typename SRanges::IndexType>&... inds)
-> SliceDef<T,SRanges...>;
private:
friend SliceDef<T,SRanges...>;
void format(const std::array<size_t,sizeof...(SRanges)+1>& blocks);
T* mData;
};
2018-03-05 21:52:12 +01:00
template <typename T, class... SRanges>
class SliceDef
{
public:
2021-05-28 17:29:13 +02:00
typedef ConstContainerIndex<T,typename SRanges::IndexType...> IType;
2018-09-08 18:46:04 +02:00
template <class Op>
2018-09-13 13:28:40 +02:00
static Slice<T,SRanges...> mkSlice( const typename Slice<T,SRanges...>::IndexType& ind,
2021-05-04 18:26:17 +02:00
const Op& op )
2018-09-08 18:46:04 +02:00
{
2018-09-13 13:28:40 +02:00
Slice<T,SRanges...> out(ind->range()->space(), &*ind);
2018-09-08 18:46:04 +02:00
std::array<size_t,sizeof...(SRanges)+1> ff;
2021-06-08 16:41:28 +02:00
sfor_pn<0,sizeof...(SRanges)>
( [&](auto i) {
std::get<i+1>(ff) =
op.rootSteps(reinterpret_cast<std::intptr_t>
( ind.template getPtr<i>().get())).val();
return 0; } );
2018-09-08 18:46:04 +02:00
out.format(ff);
return out;
}
2018-03-05 21:52:12 +01:00
private:
IType mIndex;
2018-09-13 13:28:40 +02:00
Slice<T,SRanges...>& mSl;
2018-03-05 21:52:12 +01:00
SliceDef() = default;
2018-03-05 21:52:12 +01:00
public:
2018-03-05 21:52:12 +01:00
SliceDef(Slice<T,SRanges...>& sl,
const std::shared_ptr<typename SRanges::IndexType>&... inds);
template <class... ORanges>
SliceDef& operator=(const OperationRoot<T,ORanges...>& op);
2018-03-05 21:52:12 +01:00
};
2018-09-13 13:28:40 +02:00
template <typename T, class... SRanges>
class ConstSliceDef
{
public:
2021-05-28 17:29:13 +02:00
typedef ConstContainerIndex<T,typename SRanges::IndexType...> IType;
2018-09-13 13:28:40 +02:00
template <class Op>
static ConstSlice<T,SRanges...> mkSlice( const typename ConstSlice<T,SRanges...>::IndexType& ind,
const Op& op )
{
ConstSlice<T,SRanges...> out(ind->range()->space(), &*ind);
std::array<size_t,sizeof...(SRanges)+1> ff;
2021-06-08 16:41:28 +02:00
sfor_pn<0,sizeof...(SRanges)>
( [&](auto i) {
std::get<i+1>(ff) =
op.rootSteps(reinterpret_cast<std::intptr_t>
( ind.template getPtr<i>().get())).val();
return 0; } );
2018-09-13 13:28:40 +02:00
out.format(ff);
return out;
}
private:
IType mIndex;
ConstSlice<T,SRanges...>& mSl;
ConstSliceDef() = default;
public:
ConstSliceDef(ConstSlice<T,SRanges...>& csl,
const std::shared_ptr<typename SRanges::IndexType>&... inds);
template <class... ORanges>
ConstSliceDef& operator=(const ConstOperationRoot<T,ORanges...>& op);
template <class... ORanges>
ConstSliceDef& operator=(const OperationRoot<T,ORanges...>& op);
};
2018-09-08 18:46:04 +02:00
template <typename T, class Op, class... Ranges>
ConstSlice<T,Ranges...> mkSlice( const typename ConstSlice<T,Ranges...>::IndexType& ind,
const Op& op )
2018-09-13 13:28:40 +02:00
{
return ConstSliceDef<T,Ranges...>::mkSlice(ind, op);
}
template <typename T, class Op, class... Ranges>
Slice<T,Ranges...> mkSlice( const typename Slice<T,Ranges...>::IndexType& ind,
const Op& op )
2018-09-08 18:46:04 +02:00
{
return SliceDef<T,Ranges...>::mkSlice(ind, op);
}
2018-09-13 13:28:40 +02:00
} // end namespace MultiArrayTools
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
#endif