im com (slice)

This commit is contained in:
Christian Zimmermann 2018-03-05 18:46:29 +01:00
parent 4df82f263e
commit 058d7503d7
3 changed files with 22 additions and 90 deletions

View file

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8)
project(multi_array)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -Wpedantic -Wno-uninitialized -O3 -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -Wpedantic -O3 -g")
enable_testing()

View file

@ -168,13 +168,13 @@ namespace MultiArrayTools
template <size_t N>
auto getPtr() const -> decltype( std::get<N>( mSpace ) )&;
virtual size_t dim() const override;
virtual size_t size() const override;
virtual size_t dim() const final;
virtual size_t size() const final;
const SpaceType& space() const;
virtual IndexType begin() const override;
virtual IndexType end() const override;
virtual IndexType begin() const final;
virtual IndexType end() const final;
friend MultiRangeFactory<Ranges...>;

View file

@ -3,10 +3,10 @@
#define __slice_h__
#include "multi_array_base.h"
#include "multi_array_operation.h"
namespace MultiArrayTools
{
template <typename T, class... SRanges>
class Slice : public MutableMultiArrayBase<T,SRanges...>
{
@ -14,15 +14,12 @@ namespace MultiArrayTools
typedef ContainerRange<T,SRanges...> CRange;
typedef MultiArrayBase<T,SRanges...> MAB;
typedef typename CRange::IndexType IType;
typedef ContainerIndex<T,typename SRanges::IndexType...> IType;
DEFAULT_MEMBERS(Slice);
// RITypes = Range XOR Index depending on whether const XOR opern
template <class... RITypes>
Slice(MutableMultiArrayBase<T,typename RITypes::RangeType...>& ma,
const std::shared_ptr<RITypes>&... ris);
Slice(T* data, const std::shared_ptr<SRanges>&... ranges);
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;
@ -35,6 +32,10 @@ namespace MultiArrayTools
virtual auto begin() const -> IType override;
virtual auto end() const -> IType override;
template <class... MARanges>
auto define(const std::shared_ptr<typename SRanges::IType>&... inds)
-> SliceDef<T,OperationRoot<T,MARanges...> >;
private:
T* mData;
@ -48,84 +49,15 @@ namespace MultiArrayTools
namespace MultiArrayTools
{
namespace
{
//size_t sum(size_t arg)
//{
// return arg;
//}
template <typename... SizeTypes>
size_t sum(size_t arg, SizeTypes... args)
{
return arg + sum(args...);
}
template <bool ISINDEX>
struct XX
{
template <class RI>
static auto ri_to_tuple(const std::shared_ptr<RI>& ri)
-> std::tuple<RI>
{
return std::make_tuple(ri);
}
template <class RI>
static size_t ri_to_start_pos(const std::shared_ptr<RI>& ri)
{
return 0;
}
};
template <>
struct XX<true>
{
template <class RI>
static auto ri_to_tuple(const std::shared_ptr<RI>& ri)
-> std::tuple<>
{
return std::make_tuple();
}
template <class RI>
static size_t ri_to_start_pos(const std::shared_ptr<RI>& ri)
{
return ri->pos();
}
};
template <class... RITypes>
auto mkSliceRange(const std::shared_ptr<RITypes>&... ris)
-> decltype(std::tuple_cat(XX<RITypes::ISINDEX>::ri_to_tuple(ris)...))
{
return std::tuple_cat(XX<RITypes::ISINDEX>::ri_to_tuple(ris)...);
}
template <class... RITypes>
size_t mkStartPos(const std::shared_ptr<RITypes>&... ris)
{
return sum(ri_to_start_pos(ris)...);
}
}
/*************
* Slice *
* Slice *
*************/
template <typename T, class... SRanges>
template <class... RITypes>
Slice<T,SRanges...>::Slice(MutableMultiArrayBase<T,typename RITypes::RangeType...>& ma,
const std::shared_ptr<RITypes>&... ris) :
MutableMultiArrayBase<T,SRanges...>( mkSliceRange(ris...) ),
mData(ma.data() + mkStartPos(ris...))
{
MAB::mProtoI.format( mBlockSizes(mkSliceBlocks(ris...)) );
}
Slice<T,SRanges...>::Slice(T* data, const std::shared_ptr<SRanges>&... ranges) :
MutableMultiArrayBase<T,SRanges...>(ranges...),
mData(data) {}
template <typename T, class... SRanges>
const T& Slice<T,SRanges...>::operator[](const IType& i) const
@ -134,7 +66,7 @@ namespace MultiArrayTools
assert(i.container() == reinterpret_cast<std::intptr_t>(this));
return mData[ i.pos() ];
}
template <typename T, class... SRanges>
T& Slice<T,SRanges...>::operator[](const IType& i)
{
@ -142,19 +74,19 @@ namespace MultiArrayTools
assert(i.container() == reinterpret_cast<std::intptr_t>(this));
return mData[ i.pos() ];
}
template <typename T, class... SRanges>
const T& Slice<T,SRanges...>::at(const typename IType::MetaType& meta) const
{
return mData[ begin().at(meta).pos() ];
}
template <typename T, class... SRanges>
T& Slice<T,SRanges...>::at(const typename IType::MetaType& meta)
{
return mData[ begin().at(meta).pos() ];
}
template <typename T, class... SRanges>
const T* Slice<T,SRanges...>::data() const
{