2018-02-16 18:17:22 +01:00
|
|
|
|
|
|
|
#ifndef __slice_h__
|
|
|
|
#define __slice_h__
|
|
|
|
|
|
|
|
#include "multi_array_base.h"
|
2018-03-05 18:46:29 +01:00
|
|
|
#include "multi_array_operation.h"
|
2018-02-16 18:17:22 +01:00
|
|
|
|
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
2018-03-22 11:13:00 +01:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
class ConstSlice : public MultiArrayBase<T,SRanges...>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef ContainerRange<T,SRanges...> CRange;
|
|
|
|
typedef MultiArrayBase<T,SRanges...> MAB;
|
|
|
|
typedef ContainerIndex<T,typename SRanges::IndexType...> IType;
|
|
|
|
|
|
|
|
DEFAULT_MEMBERS(ConstSlice);
|
|
|
|
|
|
|
|
ConstSlice(const std::shared_ptr<SRanges>&... ranges, const T* data = nullptr);
|
|
|
|
|
|
|
|
virtual const T& operator[](const IType& i) const override;
|
|
|
|
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-07-16 18:52:02 +02:00
|
|
|
virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymous() const override;
|
|
|
|
|
2018-03-22 11:13:00 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
const T* mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
class Slice : public MutableMultiArrayBase<T,SRanges...>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef ContainerRange<T,SRanges...> CRange;
|
|
|
|
typedef MultiArrayBase<T,SRanges...> MAB;
|
2018-03-05 18:46:29 +01:00
|
|
|
typedef ContainerIndex<T,typename SRanges::IndexType...> IType;
|
2018-02-16 18:17:22 +01:00
|
|
|
|
|
|
|
DEFAULT_MEMBERS(Slice);
|
|
|
|
|
2018-03-06 13:58:17 +01:00
|
|
|
Slice(const std::shared_ptr<SRanges>&... ranges, T* data = nullptr);
|
2018-03-05 18:46:29 +01:00
|
|
|
|
2018-03-05 00:04:50 +01:00
|
|
|
virtual const T& operator[](const IType& i) const override;
|
|
|
|
virtual T& operator[](const IType& i) override;
|
|
|
|
virtual const T& at(const typename IType::MetaType& meta) const override;
|
|
|
|
virtual T& at(const typename IType::MetaType& meta) override;
|
2018-02-16 18:17:22 +01:00
|
|
|
|
|
|
|
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-07-16 18:52:02 +02:00
|
|
|
virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymous() const override;
|
|
|
|
virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymousMove() override;
|
|
|
|
|
2018-03-06 13:58:17 +01:00
|
|
|
auto define(const std::shared_ptr<typename SRanges::IndexType>&... inds)
|
|
|
|
-> SliceDef<T,SRanges...>;
|
2018-02-16 18:17:22 +01:00
|
|
|
|
|
|
|
private:
|
2018-03-06 13:58:17 +01:00
|
|
|
friend SliceDef<T,SRanges...>;
|
|
|
|
|
|
|
|
void format(const std::array<size_t,sizeof...(SRanges)+1>& blocks);
|
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
T* mData;
|
|
|
|
};
|
2018-03-05 21:52:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
class SliceDef
|
|
|
|
{
|
2018-03-06 13:58:17 +01:00
|
|
|
public:
|
|
|
|
typedef ContainerIndex<T,typename SRanges::IndexType...> IType;
|
|
|
|
|
2018-03-05 21:52:12 +01:00
|
|
|
private:
|
2018-03-06 13:58:17 +01:00
|
|
|
IType mIndex;
|
2018-03-22 11:13:00 +01:00
|
|
|
Slice<T,SRanges...>* mSlPtr = nullptr;
|
|
|
|
ConstSlice<T,SRanges...>* mCSlPtr = nullptr;
|
2018-03-05 21:52:12 +01:00
|
|
|
|
2018-03-06 13:58:17 +01:00
|
|
|
SliceDef() = default;
|
2018-03-05 21:52:12 +01:00
|
|
|
public:
|
2018-03-22 11:13:00 +01:00
|
|
|
SliceDef(ConstSlice<T,SRanges...>& csl,
|
|
|
|
const std::shared_ptr<typename SRanges::IndexType>&... inds);
|
|
|
|
|
2018-03-05 21:52:12 +01:00
|
|
|
SliceDef(Slice<T,SRanges...>& sl,
|
|
|
|
const std::shared_ptr<typename SRanges::IndexType>&... inds);
|
|
|
|
|
|
|
|
template <class... ORanges>
|
2018-03-06 13:58:17 +01:00
|
|
|
SliceDef& operator=(const OperationRoot<T,ORanges...>& op);
|
2018-03-05 21:52:12 +01:00
|
|
|
};
|
2018-02-16 18:17:22 +01:00
|
|
|
|
|
|
|
} // end namespace MultiArrayTools
|
|
|
|
|
|
|
|
/* ========================= *
|
|
|
|
* --- TEMPLATE CODE --- *
|
|
|
|
* ========================= */
|
|
|
|
|
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
2018-03-22 11:13:00 +01:00
|
|
|
|
|
|
|
/*******************
|
|
|
|
* ConstSlice *
|
|
|
|
*******************/
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
void ConstSlice<T,SRanges...>::format(const std::array<size_t,sizeof...(SRanges)+1>& blocks)
|
|
|
|
{
|
|
|
|
MAB::mProtoI->format(blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
ConstSlice<T,SRanges...>::ConstSlice(const std::shared_ptr<SRanges>&... ranges, const T* data) :
|
|
|
|
MultiArrayBase<T,SRanges...>(ranges...),
|
|
|
|
mData(data) {}
|
2018-02-16 18:17:22 +01:00
|
|
|
|
2018-03-22 11:13:00 +01:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T& ConstSlice<T,SRanges...>::operator[](const IType& i) const
|
|
|
|
{
|
|
|
|
//assert(i.sliceMode()); // -> compare objects !!!!!
|
|
|
|
assert(i.container() == reinterpret_cast<std::intptr_t>(this));
|
|
|
|
return mData[ i.pos() ];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T& ConstSlice<T,SRanges...>::at(const typename IType::MetaType& meta) const
|
|
|
|
{
|
|
|
|
//auto x = begin().at(meta);
|
|
|
|
//VCHECK(x.pos());
|
|
|
|
return mData[ begin().at(meta).pos() ];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T* ConstSlice<T,SRanges...>::data() const
|
|
|
|
{
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool ConstSlice<T,SRanges...>::isSlice() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
auto ConstSlice<T,SRanges...>::begin() const -> ConstSlice<T,SRanges...>::IType
|
|
|
|
{
|
|
|
|
IType i(*MAB::mProtoI);
|
|
|
|
i = 0;
|
|
|
|
//i = mStartPos;
|
|
|
|
return i.setData(data());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
auto ConstSlice<T,SRanges...>::end() const -> ConstSlice<T,SRanges...>::IType
|
|
|
|
{
|
|
|
|
IType i(*MAB::mProtoI);
|
|
|
|
i = i.max(); // CHECK !!!
|
|
|
|
//i = std::get<sizeof...(SRanges)>(mBlockSizes);
|
|
|
|
return i.setData(data());
|
|
|
|
}
|
|
|
|
|
2018-07-16 18:52:02 +02:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
std::shared_ptr<MultiArrayBase<T,AnonymousRange> > ConstSlice<T,SRanges...>::anonymous() const
|
|
|
|
{
|
|
|
|
assert(0); // think about carefully!!!!
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-22 11:13:00 +01:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
auto ConstSlice<T,SRanges...>::define(const std::shared_ptr<typename SRanges::IndexType>&... inds)
|
|
|
|
-> SliceDef<T,SRanges...>
|
|
|
|
{
|
|
|
|
return SliceDef<T,SRanges...>(*this, inds...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**************
|
|
|
|
* Slice *
|
|
|
|
**************/
|
2018-03-05 18:46:29 +01:00
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
template <typename T, class... SRanges>
|
2018-03-06 13:58:17 +01:00
|
|
|
void Slice<T,SRanges...>::format(const std::array<size_t,sizeof...(SRanges)+1>& blocks)
|
|
|
|
{
|
|
|
|
MAB::mProtoI->format(blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
Slice<T,SRanges...>::Slice(const std::shared_ptr<SRanges>&... ranges, T* data) :
|
2018-03-05 18:46:29 +01:00
|
|
|
MutableMultiArrayBase<T,SRanges...>(ranges...),
|
|
|
|
mData(data) {}
|
2018-03-05 00:04:50 +01:00
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
template <typename T, class... SRanges>
|
2018-03-05 00:04:50 +01:00
|
|
|
const T& Slice<T,SRanges...>::operator[](const IType& i) const
|
2018-02-16 18:17:22 +01:00
|
|
|
{
|
2018-03-06 13:58:17 +01:00
|
|
|
//assert(i.sliceMode()); // -> compare objects !!!!!
|
2018-03-05 00:04:50 +01:00
|
|
|
assert(i.container() == reinterpret_cast<std::intptr_t>(this));
|
2018-02-16 18:17:22 +01:00
|
|
|
return mData[ i.pos() ];
|
|
|
|
}
|
2018-03-05 18:46:29 +01:00
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
template <typename T, class... SRanges>
|
2018-03-05 00:04:50 +01:00
|
|
|
T& Slice<T,SRanges...>::operator[](const IType& i)
|
2018-02-16 18:17:22 +01:00
|
|
|
{
|
2018-03-06 13:58:17 +01:00
|
|
|
//assert(i.sliceMode());
|
2018-03-05 00:04:50 +01:00
|
|
|
assert(i.container() == reinterpret_cast<std::intptr_t>(this));
|
2018-02-16 18:17:22 +01:00
|
|
|
return mData[ i.pos() ];
|
|
|
|
}
|
2018-03-05 18:46:29 +01:00
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
template <typename T, class... SRanges>
|
2018-03-05 00:04:50 +01:00
|
|
|
const T& Slice<T,SRanges...>::at(const typename IType::MetaType& meta) const
|
2018-02-16 18:17:22 +01:00
|
|
|
{
|
2018-03-06 13:58:17 +01:00
|
|
|
//auto x = begin().at(meta);
|
|
|
|
//VCHECK(x.pos());
|
2018-02-16 18:17:22 +01:00
|
|
|
return mData[ begin().at(meta).pos() ];
|
|
|
|
}
|
2018-03-05 18:46:29 +01:00
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
template <typename T, class... SRanges>
|
2018-03-05 00:04:50 +01:00
|
|
|
T& Slice<T,SRanges...>::at(const typename IType::MetaType& meta)
|
2018-02-16 18:17:22 +01:00
|
|
|
{
|
2018-03-06 13:58:17 +01:00
|
|
|
//auto x = begin().at(meta);
|
|
|
|
//VCHECK(x.pos());
|
2018-02-16 18:17:22 +01:00
|
|
|
return mData[ begin().at(meta).pos() ];
|
|
|
|
}
|
2018-03-05 18:46:29 +01:00
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T* Slice<T,SRanges...>::data() const
|
|
|
|
{
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
T* Slice<T,SRanges...>::data()
|
|
|
|
{
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool Slice<T,SRanges...>::isSlice() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
2018-03-05 00:04:50 +01:00
|
|
|
auto Slice<T,SRanges...>::begin() const -> Slice<T,SRanges...>::IType
|
2018-02-16 18:17:22 +01:00
|
|
|
{
|
2018-03-06 13:58:17 +01:00
|
|
|
IType i(*MAB::mProtoI);
|
2018-03-05 00:04:50 +01:00
|
|
|
i = 0;
|
|
|
|
//i = mStartPos;
|
2018-02-16 18:17:22 +01:00
|
|
|
return i.setData(data());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
2018-03-05 00:04:50 +01:00
|
|
|
auto Slice<T,SRanges...>::end() const -> Slice<T,SRanges...>::IType
|
2018-02-16 18:17:22 +01:00
|
|
|
{
|
2018-03-06 13:58:17 +01:00
|
|
|
IType i(*MAB::mProtoI);
|
2018-03-05 00:04:50 +01:00
|
|
|
i = i.max(); // CHECK !!!
|
|
|
|
//i = std::get<sizeof...(SRanges)>(mBlockSizes);
|
2018-02-16 18:17:22 +01:00
|
|
|
return i.setData(data());
|
|
|
|
}
|
2018-03-06 13:58:17 +01:00
|
|
|
|
2018-07-16 18:52:02 +02:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
std::shared_ptr<MultiArrayBase<T,AnonymousRange> > Slice<T,SRanges...>::anonymous() const
|
|
|
|
{
|
|
|
|
assert(0); // think about carefully!!!!
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
std::shared_ptr<MultiArrayBase<T,AnonymousRange> > Slice<T,SRanges...>::anonymousMove()
|
|
|
|
{
|
|
|
|
assert(0); // think about carefully!!!!
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:58:17 +01:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
auto Slice<T,SRanges...>::define(const std::shared_ptr<typename SRanges::IndexType>&... inds)
|
|
|
|
-> SliceDef<T,SRanges...>
|
|
|
|
{
|
|
|
|
return SliceDef<T,SRanges...>(*this, inds...);
|
|
|
|
}
|
2018-02-16 18:17:22 +01:00
|
|
|
|
2018-03-22 11:13:00 +01:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
SliceDef<T,SRanges...>::SliceDef(ConstSlice<T,SRanges...>& csl,
|
|
|
|
const std::shared_ptr<typename SRanges::IndexType>&... inds) :
|
|
|
|
mIndex(csl.begin()),
|
|
|
|
mCSlPtr(&csl)
|
|
|
|
{
|
|
|
|
mIndex(inds...);
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:58:17 +01:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
SliceDef<T,SRanges...>::SliceDef(Slice<T,SRanges...>& sl,
|
|
|
|
const std::shared_ptr<typename SRanges::IndexType>&... inds) :
|
|
|
|
mIndex(sl.begin()),
|
2018-03-22 11:13:00 +01:00
|
|
|
mSlPtr(&sl)
|
2018-03-06 13:58:17 +01:00
|
|
|
{
|
|
|
|
mIndex(inds...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
2018-03-05 21:52:12 +01:00
|
|
|
template <class... ORanges>
|
2018-03-06 13:58:17 +01:00
|
|
|
SliceDef<T,SRanges...>& SliceDef<T,SRanges...>::operator=(const OperationRoot<T,ORanges...>& op)
|
|
|
|
{
|
|
|
|
std::array<size_t,sizeof...(SRanges)+1> blocks;
|
|
|
|
PackNum<sizeof...(SRanges)-1>::
|
|
|
|
template mkSliceBlocks<T,OperationRoot<T,ORanges...>,SRanges...>(blocks, mIndex, op);
|
2018-03-22 11:13:00 +01:00
|
|
|
if(mCSlPtr){
|
|
|
|
mCSlPtr->format(blocks);
|
|
|
|
mCSlPtr->mData = op.data();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mSlPtr->format(blocks);
|
|
|
|
mSlPtr->mData = op.data();
|
|
|
|
}
|
2018-03-06 13:58:17 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-02-16 18:17:22 +01:00
|
|
|
|
|
|
|
} // end namespace MultiArrayTools
|
|
|
|
|
|
|
|
#endif
|