im com (sort code)

This commit is contained in:
Christian Zimmermann 2017-12-17 17:40:55 +01:00
parent 346101b994
commit 5f319cf1d5
25 changed files with 1069 additions and 834 deletions

View file

@ -22,21 +22,17 @@ endif()
include_directories(src) include_directories(src)
#add_executable(utest src/unit_test.cc) add_executable(iutest src/ranges/tests/index_unit_test.cc src/ranges/range_base.cc)
#target_link_libraries(utest ${GTEST_BOTH_LIBRARIES})
#add_test(NAME utest COMMAND utest)
add_executable(iutest src/index_unit_test.cc)
target_link_libraries(iutest ${GTEST_BOTH_LIBRARIES}) target_link_libraries(iutest ${GTEST_BOTH_LIBRARIES})
add_test(NAME iutest COMMAND iutest) add_test(NAME iutest COMMAND iutest)
add_executable(mautest src/ma_unit_test.cc) #add_executable(mautest src/ma_unit_test.cc)
target_link_libraries(mautest ${GTEST_BOTH_LIBRARIES}) #target_link_libraries(mautest ${GTEST_BOTH_LIBRARIES})
add_test(NAME mautest COMMAND mautest) #add_test(NAME mautest COMMAND mautest)
add_executable(oputest src/op_unit_test.cc) #add_executable(oputest src/op_unit_test.cc)
target_link_libraries(oputest ${GTEST_BOTH_LIBRARIES}) #target_link_libraries(oputest ${GTEST_BOTH_LIBRARIES})
add_test(NAME oputest COMMAND oputest) #add_test(NAME oputest COMMAND oputest)
#install(TARGETS testm DESTINATION install) #install(TARGETS testm DESTINATION install)

View file

@ -32,220 +32,4 @@
__class_name__& operator=(__class_name__&& in) = default __class_name__& operator=(__class_name__&& in) = default
namespace MultiArrayTools
{
template <class MultiArray>
void checkPtr(const MultiArray& in, bool check = false)
{
static MultiArray const* ptr = nullptr;
if(not check){
ptr = &in;
}
else {
assert(ptr == &in);
}
}
/***********************
* Provided Types *
***********************/
// range_base.h
enum class SpaceType;
// range_base.h
class RangeFactoryBase;
// range_base.h
class RangeBase;
// range_base.h
template <class Index>
class RangeInterface;
// index_base.h
class VirtualIndexWrapperBase;
typedef VirtualIndexWrapperBase VIWB;
// index_base.h
template <class I>
class IndexWrapper;
// index_base.h
template <class I, typename MetaType>
class IndexInterface;
// single_range.h
template <typename U, SpaceType TYPE>
class SingleRange;
// single_range.h
template <typename U, SpaceType TYPE>
class SingleRangeFactory;
// single_range.h
template <typename U, SpaceType TYPE>
class SingleIndex;
// multi_range.h
template <class... Ranges>
class MultiRangeFactory;
// multi_range.h
template <class... Ranges>
class MultiRange;
// multi_range.h
template <class... Indices>
class MultiIndex;
// container_range.h
template <class... Ranges>
class ContainerRangeFactory;
// container_range.h
template <class... Ranges>
class ContainerRange;
// container_range.h
template <class... Indices>
class ContainerIndex;
// anonymous_range.h
class AnonymousRangeFactory;
// anonymous_range.h
class AnonymousRange;
// multi_array.h
template <typename T, class... SRanges>
class MultiArrayBase;
// multi_array.h
template <typename T, class... SRanges>
class MutableMultiArrayBase;
// multi_array.h
template <typename T, class... SRanges>
class MultiArray;
// multi_array_operation.h
template <typename T>
class OperationBase;
// multi_array_operation.h
template <typename T>
class MutableOperationBase;
// multi_array_operation.h
template <typename T, class OperationClass>
class OperationTemplate;
// multi_array_operation.h
template <typename T, class OpClass, class... Ranges>
class OperationMaster;
// multi_array_operation.h
template <typename T, class... Ranges>
class OperationRoot;
// multi_array_operation.h
template <typename T, class... Ranges>
class ConstOperationRoot;
// multi_array_operation.h
template <typename T, class OpFunction, class... Ops>
class Operation;
// multi_array_operation.h
template <typename T, class Op, class IndexType>
class Contraction;
/*
// multi_array_operation.h
template <typename T, class InRange, class TotalInRange, class OutRange, class TotalRange>
class MultiArrayOperationMap;
// multi_array_operation.h
template <typename T, class ContractOperation, class Range, class... MAOps>
class MultiArrayContraction;
// slice.h
template <typename T, class Range, class MARange>
class Slice;
// slice.h
template <typename T, class Range, class MARange>
class ConstSlice;
// manipulator.h
template <typename T>
class ManipulatorBase;
// manipulator.h
template <typename T>
class BinReader;
// ma_functional.h
template <class InRange, class OutRange>
class IndexMapFunction;
// ma_functional.h
class vec3d2Function;
// ma_functional.h
class pyProjFunction;
// ma_functional.h
class pyProjNoSymFunction;
*/
}
namespace MultiArrayHelper
{
// block.h
template <typename T, class OpFunc, class BlockClass1, class BlockClass2>
class BlockBinaryOp;
// block.h
enum class BlockType;
// block.h
template <typename T>
class BlockBase;
// block.h
template <typename T>
class MutableBlockBase;
// block.h
template <typename T>
class Block;
// block.h
template <typename T>
class MBlock;
// block.h
template <typename T>
class BlockValue;
// block.h
template <typename T>
class MBlockValue;
// block.h
template <typename T>
class SplitBlock;
// block.h
template <typename T>
class MSplitBlock;
// block.h
template <typename T>
class BlockResult;
}
#endif #endif

View file

@ -11,6 +11,47 @@
namespace MultiArrayHelper namespace MultiArrayHelper
{ {
template <typename T, class OpFunc, class BlockClass1, class BlockClass2>
class BlockBinaryOp;
template <typename T>
class BlockBase;
template <typename T>
class MutableBlockBase;
template <typename T>
class Block;
template <typename T>
class MBlock;
template <typename T>
class BlockValue;
template <typename T>
class MBlockValue;
template <typename T>
class SplitBlock;
template <typename T>
class MSplitBlock;
template <typename T>
class BlockResult;
enum class BlockType { enum class BlockType {
INDEF = 0, INDEF = 0,
BLOCK = 1, BLOCK = 1,

123
src/mbase_def.h Normal file
View file

@ -0,0 +1,123 @@
#ifndef __mbase_def_h__
#define __mbase_def_h__
namespace MultiArrayTools
{
/***********************
* Provided Types *
***********************/
// range_base.h
enum class SpaceType;
// range_base.h
class RangeFactoryBase;
// range_base.h
class RangeBase;
// range_base.h
template <class Index>
class RangeInterface;
// index_base.h
class VirtualIndexWrapperBase;
typedef VirtualIndexWrapperBase VIWB;
// index_base.h
template <class I>
class IndexWrapper;
// index_base.h
template <class I, typename MetaType>
class IndexInterface;
// single_range.h
template <typename U, SpaceType TYPE>
class SingleRange;
// single_range.h
template <typename U, SpaceType TYPE>
class SingleRangeFactory;
// single_range.h
template <typename U, SpaceType TYPE>
class SingleIndex;
// multi_range.h
template <class... Ranges>
class MultiRangeFactory;
// multi_range.h
template <class... Ranges>
class MultiRange;
// multi_range.h
template <class... Indices>
class MultiIndex;
// container_range.h
template <class... Ranges>
class ContainerRangeFactory;
// container_range.h
template <class... Ranges>
class ContainerRange;
// container_range.h
template <class... Indices>
class ContainerIndex;
// anonymous_range.h
class AnonymousRangeFactory;
// anonymous_range.h
class AnonymousRange;
// multi_array.h
template <typename T, class... SRanges>
class MultiArrayBase;
// multi_array.h
template <typename T, class... SRanges>
class MutableMultiArrayBase;
// multi_array.h
template <typename T, class... SRanges>
class MultiArray;
// multi_array_operation.h
template <typename T>
class OperationBase;
// multi_array_operation.h
template <typename T>
class MutableOperationBase;
// multi_array_operation.h
template <typename T, class OperationClass>
class OperationTemplate;
// multi_array_operation.h
template <typename T, class OpClass, class... Ranges>
class OperationMaster;
// multi_array_operation.h
template <typename T, class... Ranges>
class OperationRoot;
// multi_array_operation.h
template <typename T, class... Ranges>
class ConstOperationRoot;
// multi_array_operation.h
template <typename T, class OpFunction, class... Ops>
class Operation;
// multi_array_operation.h
template <typename T, class Op, class IndexType>
class Contraction;
}
#endif

View file

@ -16,219 +16,10 @@ namespace MultiArrayHelper
template <typename... T> template <typename... T>
std::ostream& operator<<(std::ostream& out, const std::tuple<T...>& tp); std::ostream& operator<<(std::ostream& out, const std::tuple<T...>& tp);
template <size_t N> template <size_t N>
struct PackNum struct PackNum
{ {
template <class IndexType>
static std::shared_ptr<VIWB> getIndex(const IndexType& in, size_t n)
{
if(n == N){
return make_viwb( in.template get<N>() );
}
else {
return PackNum<N-1>::getIndex(in, n);
}
}
/*
template <class IndexType>
static const IndexBase& getIndex(const IndexType& in, size_t n)
{
if(n == N){
return in.template get<N>();
}
else {
return PackNum<N-1>::getIndex(in, n);
}
}*/
template <class IndexType>
static std::shared_ptr<VIWB> getIndexPtr(const IndexType& in, size_t n)
{
if(n == N){
return make_viwb( in.template getPtr<N>() );
}
else {
return PackNum<N-1>::getIndexPtr(in, n);
}
}
/*
template <class... Indices>
static void lock(std::tuple<std::shared_ptr<Indices>...>& ip,
std::shared_ptr<IndexBase> toLock)
{
std::get<N>(ip)->lock(toLock);
PackNum<N-1>::lock(ip, toLock);
}
*/
template <class... Indices>
static void initBlockSizes(std::array<size_t,sizeof...(Indices)+1>& bs,
std::tuple<std::shared_ptr<Indices>...>& ip)
{
std::get<N>(bs) = PackNum<sizeof...(Indices)-N-1>::blockSize(ip);
PackNum<N-1>::initBlockSizes(bs, ip);
}
template <class... Indices>
static inline void pp(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<N>(ip);
if(si.last()){
si = 0;
PackNum<N-1>::pp(ip);
}
else {
++si;
}
}
template <class... Indices>
static inline int pp(std::tuple<std::shared_ptr<Indices>...>& ip,
std::array<size_t,sizeof...(Indices)+1>& bs,
std::intptr_t idxPtrNum)
{
auto& siPtr = std::get<N>(ip);
//VCHECK(siPtr.id());
if(reinterpret_cast<std::intptr_t>(siPtr.get()) == idxPtrNum){
return PackNum<N-1>::pp(ip, bs, idxPtrNum);
}
else {
int tmp = siPtr->pp(idxPtrNum);
if(siPtr->pos() == siPtr->max()){
(*siPtr) = 0;
return PackNum<N-1>::pp(ip, bs, idxPtrNum) - siPtr->max() + 1;
}
else {
return tmp * std::get<N+1>(bs);
}
}
}
template <class... Indices>
static inline void mm(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<N>(ip);
if(si.first()){
si = si.max() - 1;
PackNum<N-1>::mm(ip);
}
else {
--si;
}
}
// !!!!
template <class... Indices>
static inline int mm(std::tuple<std::shared_ptr<Indices>...>& ip,
std::array<size_t,sizeof...(Indices)+1>& bs,
std::intptr_t idxPtrNum)
{
auto& siPtr = std::get<N>(ip);
if(reinterpret_cast<std::intptr_t>(siPtr.get()) == idxPtrNum){
return std::get<N>(bs) + PackNum<N-1>::mm(ip, bs, idxPtrNum);
}
else {
if(siPtr->first()){
(*siPtr) = siPtr->max() - 1;
return PackNum<N-1>::mm(ip, bs, idxPtrNum) - siPtr->max() + 1;
}
else {
return siPtr->mm(idxPtrNum);
}
}
}
template <class RangeTuple>
static size_t getSize(const RangeTuple& rt)
{
return std::get<N>(rt)->size() * PackNum<N-1>::getSize(rt);
}
template <class IndexPack, class MetaType>
static void getMetaPos(MetaType& target,
const IndexPack& source)
{
std::get<N>(target) = std::get<N>(source)->meta();
PackNum<N-1>::getMetaPos(target, source);
}
template <class IndexPack, typename MetaType>
static void setMeta(IndexPack& target, const MetaType& source)
{
std::get<N>(target)->at( std::get<N>(source) );
PackNum<N-1>::setMeta(target, source);
}
template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos)
{
auto& i = *std::get<N>(iPack).get();
const size_t ownPos = pos % i.max();
i = ownPos;
PackNum<N-1>::setIndexPack(iPack, (pos - ownPos) / i.max() );
}
template <class MRange, class... Indices>
static void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range)
{
typedef typename std::remove_reference<decltype(range.template get<N>())>::type SubRangeType;
typedef typename SubRangeType::IndexType SubIndexType;
typedef typename std::remove_reference<decltype(*std::get<N>(ip).get())>::type TypeFromIndexPack;
static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value,
"inconsiśtent types");
std::get<N>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( range.template getPtr<N>() ) );
PackNum<N-1>::construct(ip, range);
}
/*
template <template<class...> class IndexType, class... Indices>
static void copy(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind)
{
typedef typename std::remove_reference<decltype(ind.template get<N>())>::type SubIndexType;
std::get<N>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( ind.template get<N>() ) );
PackNum<N-1>::copy(ip, ind);
}
*/
template <template<class...> class IndexType, class... Indices>
static void copyInst(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind)
{
std::get<N>(ip) = ind.template getPtr<N>() ;
PackNum<N-1>::copyInst(ip, ind);
}
template <class... Indices>
static size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{
auto ID = std::get<N>(iPtrTup)->id();
//VCHECK(std::get<N>(iPtrTup)->pos());
//VCHECK(std::get<N>(iPtrTup)->max());
return std::get<N>(iPtrTup)->pos() +
PackNum<N-1>::makePos(iPtrTup) * std::get<N>(iPtrTup)->max();
}
template <class Pack, class IndexType, class... Indices>
static void swapIndices(Pack& ipack, const std::shared_ptr<IndexType>& nind,
const std::shared_ptr<Indices>&... ninds)
{
std::get<std::tuple_size<Pack>::value-N-1>(ipack) = nind;
PackNum<N-1>::swapIndices(ipack, ninds...);
}
template <class... Indices>
static size_t blockSize(const std::tuple<std::shared_ptr<Indices>...>& pack)
{
return std::get<sizeof...(Indices)-N-1>(pack)->max() * PackNum<N-1>::blockSize(pack);
}
template <typename T, class Func, class ArgTuple, class... Args>
static BlockResult<T> unpackArgs(const ArgTuple& tp, const Args&... args)
{
return PackNum<N-1>::template unpackArgs<T,Func>(tp, std::get<N>(tp).get(), args...);
}
template <class... Ops> template <class... Ops>
static void makeBlockTypeVec(std::vector<std::pair<BlockType,size_t> >& btv, static void makeBlockTypeVec(std::vector<std::pair<BlockType,size_t> >& btv,
@ -240,190 +31,23 @@ namespace MultiArrayHelper
PackNum<N-1>::makeBlockTypeVec(btv, ops, idxPtr); PackNum<N-1>::makeBlockTypeVec(btv, ops, idxPtr);
} }
template <typename T, class Func, class ArgTuple, class... Args>
static BlockResult<T> unpackArgs(const ArgTuple& tp, const Args&... args)
{
return PackNum<N-1>::template unpackArgs<T,Func>(tp, std::get<N>(tp).get(), args...);
}
template <typename... T> template <typename... T>
static void printTuple(std::ostream& out, const std::tuple<T...>& tp){ static void printTuple(std::ostream& out, const std::tuple<T...>& tp){
out << std::get<sizeof...(T)-N-1>(tp) << ", "; out << std::get<sizeof...(T)-N-1>(tp) << ", ";
PackNum<N-1>::printTuple(out, tp); PackNum<N-1>::printTuple(out, tp);
} }
template <class... Ranges>
static void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<RangeBase> v)
{
v[N] = std::get<N>(rst);
PackNum<N-1>::RangesToVec(rst, v);
}
template <class... Indices>
static void printIndex(const std::tuple<std::shared_ptr<Indices>...>& ip, size_t offset)
{
std::get<N>(ip)->print(offset);
PackNum<N-1>::printIndex(ip, offset);
}
template <class Range, class... Ranges>
static void checkDefaultable()
{
static_assert( Range::defaultable, "not defaultable" );
PackNum<N-1>::template checkDefaultable<Ranges...>();
}
}; };
template<> template<>
struct PackNum<0> struct PackNum<0>
{ {
template <class IndexType>
static std::shared_ptr<VIWB> getIndex(const IndexType& in, size_t n)
{
return make_viwb( in.template get<0>() );
}
/*
template <class MultiIndex>
static const IndexBase& getIndex(const MultiIndex& in, size_t n)
{
return in.template get<0>();
}
*/
template <class IndexType>
static std::shared_ptr<VIWB> getIndexPtr(const IndexType& in, size_t n)
{
return make_viwb( in.template getPtr<0>() );
}
/*
template <class... Indices>
static void lock(std::tuple<std::shared_ptr<Indices>...>& ip,
std::shared_ptr<IndexBase> toLock)
{
std::get<0>(ip)->lock(toLock);
}
*/
template <class... Indices>
static void initBlockSizes(std::array<size_t,sizeof...(Indices)+1>& bs,
std::tuple<std::shared_ptr<Indices>...>& ip)
{
std::get<0>(bs) = PackNum<sizeof...(Indices)-1>::blockSize(ip);
}
template <class... Indices>
static inline void pp(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<0>(ip);
++si;
}
template <class... Indices>
static inline int pp(std::tuple<std::shared_ptr<Indices>...>& ip,
std::array<size_t,sizeof...(Indices)+1>& bs,
std::intptr_t idxPtrNum)
{
auto& siPtr = std::get<0>(ip);
if(reinterpret_cast<std::intptr_t>(siPtr.get()) == idxPtrNum){
return std::get<0>(bs);
}
else {
int tmp = siPtr->pp(idxPtrNum);
return tmp * std::get<1>(bs);
}
}
template <class... Indices>
static inline void mm(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<0>(ip);
--si;
}
template <class... Indices>
static inline int mm(std::tuple<std::shared_ptr<Indices>...>& ip,
std::array<size_t,sizeof...(Indices)+1>& bs,
std::intptr_t idxPtrNum)
{
auto& siPtr = std::get<0>(ip);
if(reinterpret_cast<std::intptr_t>(siPtr.get()) == idxPtrNum){
return std::get<0>(bs);
//return 1;
}
else {
return siPtr->mm(idxPtrNum);
}
}
template <class RangeTuple>
static size_t getSize(const RangeTuple& rt)
{
return std::get<0>(rt)->size();
}
template <class IndexPack, class MetaType>
static void getMetaPos(MetaType& target,
const IndexPack& source)
{
std::get<0>(target) = std::get<0>(source)->meta();
}
template <class IndexPack, typename MetaType>
static void setMeta(IndexPack& target, const MetaType& source)
{
std::get<0>(target)->at( std::get<0>( source ) );
}
template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos)
{
auto& i = *std::get<0>(iPack);
const size_t ownPos = pos % i.max();
i = ownPos;
}
template <class MRange, class... Indices>
static void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range)
{
typedef typename std::remove_reference<decltype(range.template get<0>())>::type SubRangeType;
typedef typename SubRangeType::IndexType SubIndexType;
typedef typename std::remove_reference<decltype(*std::get<0>(ip).get())>::type TypeFromIndexPack;
static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value,
"inconsiśtent types");
std::get<0>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( range.template getPtr<0>() ) );
}
/*
template <template<class...> class IndexType, class... Indices>
static void copy(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind)
{
typedef typename std::remove_reference<decltype(ind.template get<0>())>::type SubIndexType;
std::get<0>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( ind.template get<0>() ) );
}
*/
template <template<class...> class IndexType, class... Indices>
static void copyInst(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind)
{
std::get<0>(ip) = ind.template getPtr<0>();
}
template <class... Indices>
static size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{
auto ID = std::get<0>(iPtrTup)->id();
//VCHECK(std::get<0>(iPtrTup)->pos());
//VCHECK(std::get<0>(iPtrTup)->max());
return std::get<0>(iPtrTup)->pos();
}
template <class Pack, class IndexType>
static void swapIndices(Pack& ipack, const std::shared_ptr<IndexType>& nind)
{
std::get<std::tuple_size<Pack>::value-1>(ipack) = nind;
}
template <class... Indices>
static size_t blockSize(const std::tuple<std::shared_ptr<Indices>...>& pack)
{
return std::get<sizeof...(Indices)-1>(pack)->max();
}
template <typename T, class Func, class ArgTuple, class... Args> template <typename T, class Func, class ArgTuple, class... Args>
static BlockResult<T> unpackArgs(const ArgTuple& tp, const Args&... args) static BlockResult<T> unpackArgs(const ArgTuple& tp, const Args&... args)
@ -447,25 +71,6 @@ namespace MultiArrayHelper
static void printTuple(std::ostream& out, const std::tuple<T...>& tp){ static void printTuple(std::ostream& out, const std::tuple<T...>& tp){
out << std::get<sizeof...(T)-1>(tp); out << std::get<sizeof...(T)-1>(tp);
} }
template <class... Ranges>
static void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<RangeBase> v)
{
v[0] = std::get<0>(rst);
}
template <class... Indices>
static void printIndex(const std::tuple<std::shared_ptr<Indices>...>& ip, size_t offset)
{
std::get<0>(ip)->print(offset);
}
template <class Range>
static void checkDefaultable()
{
static_assert( Range::defaultable, "not defaultable" );
}
}; };

View file

@ -1,26 +0,0 @@
// -*- C++ -*-
#ifndef __range_factory_h__
#define __range_factory_h__
#include <cstdlib>
#include <memory>
#include "base_def.h"
namespace MultiArrayTools
{
class RangeFactoryBase
{
public:
DEFAULT_MEMBERS(RangeFactoryBase);
virtual std::shared_ptr<IndefinitRangeBase> create() const = 0;
};
} //namespace MultiArrayTools
// === NO TEMPLATE CODE HERE ===
#endif

View file

@ -4,14 +4,15 @@
#define __anonymous_range_h__ #define __anonymous_range_h__
#include <cstdlib> #include <cstdlib>
#include "base_def.h" //#include "base_def.h"
#include "ranges/range_base.h"
#include "ranges/single_range.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
typedef SingleIndex<size_t,RangeType::NONE> AnonymousIndex; typedef SingleIndex<size_t,SpaceType::NONE> AnonymousIndex;
template <typename U, RangeType TYPE>
class AnonymousRangeFactory : public RangeFactoryBase class AnonymousRangeFactory : public RangeFactoryBase
{ {
public: public:
@ -20,16 +21,15 @@ namespace MultiArrayTools
AnonymousRangeFactory() = delete; AnonymousRangeFactory() = delete;
template <class... Ranges> template <class... RangeTypes>
AnonymousRangeFactory(const std::tuple<std::shared_ptr<Ranges>...>& origs); AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
template <class... Ranges> template <class... RangeTypes>
AnonymousRangeFactory(std::shared_ptr<Ranges>... origs); AnonymousRangeFactory(std::shared_ptr<RangeTypes>... origs);
std::shared_ptr<RangeBase> create(); std::shared_ptr<RangeBase> create();
}; };
class AnonymousRange : public RangeInterface<AnonymousIndex> class AnonymousRange : public RangeInterface<AnonymousIndex>
{ {
@ -53,11 +53,11 @@ namespace MultiArrayTools
AnonymousRange() = delete; AnonymousRange() = delete;
AnonymousRange(const AnonymousRange& in) = delete; AnonymousRange(const AnonymousRange& in) = delete;
template <class Ranges...> template <class... RangeTypes>
AnonymousRange(const std::tuple<std::shared_ptr<Ranges>...>& origs) AnonymousRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs);
template <class Ranges...> template <class... RangeTypes>
AnonymousRange(std::shared_ptr<Ranges>... origs); AnonymousRange(std::shared_ptr<RangeTypes>... origs);
size_t mSize; size_t mSize;
@ -77,14 +77,14 @@ namespace MultiArrayTools
* AnonymousRange * * AnonymousRange *
***********************/ ***********************/
template <class... Ranges> template <class... RangeTypes>
AnonymousRangeFactory::AnonymousRangeFactory(const std::tuple<std::shared_ptr<Ranges>...>& origs) AnonymousRangeFactory::AnonymousRangeFactory(const std::tuple<std::shared_ptr<RangeTypes>...>& origs)
{ {
mProd = std::shared_ptr<oType>( new AnonymousRange( origs ) ); mProd = std::shared_ptr<oType>( new AnonymousRange( origs ) );
} }
template <class... Ranges> template <class... RangeTypes>
AnonymousRangeFactory::AnonymousRangeFactory(std::shared_ptr<Ranges>... origs) AnonymousRangeFactory::AnonymousRangeFactory(std::shared_ptr<RangeTypes>... origs)
{ {
mProd = std::shared_ptr<oType>( new AnonymousRange( origs... ) ); mProd = std::shared_ptr<oType>( new AnonymousRange( origs... ) );
} }
@ -99,23 +99,23 @@ namespace MultiArrayTools
* AnonymousRange * * AnonymousRange *
***********************/ ***********************/
template <class... Ranges> template <class... RangeTypes>
AnonymousRange::AnonymousRange(const std::tuple<std::shared_ptr<Ranges>...>& origs) : AnonymousRange::AnonymousRange(const std::tuple<std::shared_ptr<RangeTypes>...>& origs) :
RangeInterface<AnonymousIndex>() RangeInterface<AnonymousIndex>()
{ {
mOrig.resize(sizeof...(Ranges)); mOrig.resize(sizeof...(RangeTypes));
PackNum<sizeof...(Ranges)-1>::RangesToVec( origs, mOrig ); RPackNum<sizeof...(RangeTypes)-1>::RangesToVec( origs, mOrig );
PackNum<sizeof...(Ranges)-1>::getSize( rst ); RPackNum<sizeof...(RangeTypes)-1>::getSize( origs );
} }
template <class... Ranges> template <class... RangeTypes>
AnonymousRange::AnonymousRange(std::shared_ptr<Ranges>... origs) : AnonymousRange::AnonymousRange(std::shared_ptr<RangeTypes>... origs) :
RangeInterface<AnonymousIndex>() RangeInterface<AnonymousIndex>()
{ {
auto rst = std::make_tuple(origs...); auto rst = std::make_tuple(origs...);
mOrig.resize(sizeof...(Ranges)); mOrig.resize(sizeof...(RangeTypes));
PackNum<sizeof...(Ranges)-1>::RangesToVec( rst, mOrig ); RPackNum<sizeof...(RangeTypes)-1>::RangesToVec( rst, mOrig );
PackNum<sizeof...(Ranges)-1>::getSize( rst ); RPackNum<sizeof...(RangeTypes)-1>::getSize( rst );
} }
size_t AnonymousRange::get(size_t pos) const size_t AnonymousRange::get(size_t pos) const
@ -140,16 +140,16 @@ namespace MultiArrayTools
typename AnonymousRange::IndexType AnonymousRange::begin() const typename AnonymousRange::IndexType AnonymousRange::begin() const
{ {
SingleIndex i( std::dynamic_pointer_cast<AnonymousRange> AnonymousIndex i( std::dynamic_pointer_cast<AnonymousRange>
( std::shared_ptr<RangeBase>( RB::mThis ) ) ); ( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = 0; i = 0;
return i; return i;
} }
typename AnonymousRange::IndexType AnonymousRange::end() const typename AnonymousRange::IndexType AnonymousRange::end() const
{ {
SingleIndex i( std::dynamic_pointer_cast<AnonymousRange> AnonymousIndex i( std::dynamic_pointer_cast<AnonymousRange>
( std::shared_ptr<RangeBase>( RB::mThis ) ) ); ( std::shared_ptr<RangeBase>( RB::mThis ) ) );
i = size(); i = size();
return i; return i;
} }
@ -157,8 +157,9 @@ namespace MultiArrayTools
// put this in the interface class !!! // put this in the interface class !!!
std::shared_ptr<VIWB> AnonymousRange::index() const std::shared_ptr<VIWB> AnonymousRange::index() const
{ {
return std::make_shared<VIWB> typedef IndexWrapper<IndexType> IW;
(std::make_shared<SingleIndex > return std::make_shared<IW>
(std::make_shared<IndexType>
( std::dynamic_pointer_cast<AnonymousRange> ( std::dynamic_pointer_cast<AnonymousRange>
( std::shared_ptr<RangeBase>( RB::mThis ) ) ) ); ( std::shared_ptr<RangeBase>( RB::mThis ) ) ) );
} }

View file

@ -7,11 +7,11 @@
#include <tuple> #include <tuple>
#include <memory> #include <memory>
#include "base_def.h" //#include "base_def.h"
#include "range_base.h" #include "ranges/range_base.h"
#include "index_base.h" #include "ranges/index_base.h"
#include "pack_num.h" #include "rpack_num.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -167,17 +167,17 @@ namespace MultiArrayTools
ContainerIndex<Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range) : ContainerIndex<Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range) :
IndexInterface<ContainerIndex<Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0) IndexInterface<ContainerIndex<Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0)
{ {
PackNum<sizeof...(Indices)-1>::construct(mIPack, *range); RPackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
std::get<sizeof...(Indices)>(mBlockSizes) = 1; std::get<sizeof...(Indices)>(mBlockSizes) = 1;
PackNum<sizeof...(Indices)-1>::initBlockSizes(mBlockSizes, mIPack); RPackNum<sizeof...(Indices)-1>::initBlockSizes(mBlockSizes, mIPack);
} }
template <class... Indices> template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::sync() ContainerIndex<Indices...>& ContainerIndex<Indices...>::sync()
{ {
if(mExternControl){ if(mExternControl){
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
//VCHECK(id()); //VCHECK(id());
//VCHECK(sizeof...(Indices)); //VCHECK(sizeof...(Indices));
//assert(IB::mPos < IB::max()); //assert(IB::mPos < IB::max());
@ -202,7 +202,7 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()(const std::shared_ptr<Indices>&... inds) ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()(const std::shared_ptr<Indices>&... inds)
{ {
PackNum<sizeof...(Indices)-1>::swapIndices(mIPack, inds...); RPackNum<sizeof...(Indices)-1>::swapIndices(mIPack, inds...);
mExternControl = true; mExternControl = true;
return sync(); return sync();
} }
@ -220,9 +220,9 @@ namespace MultiArrayTools
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator++() ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator++()
{ {
if(mExternControl){ if(mExternControl){
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
} }
PackNum<sizeof...(Indices)-1>::pp( mIPack ); RPackNum<sizeof...(Indices)-1>::pp( mIPack );
++IB::mPos; ++IB::mPos;
return *this; return *this;
} }
@ -231,9 +231,9 @@ namespace MultiArrayTools
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator--() ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator--()
{ {
if(mExternControl){ if(mExternControl){
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
} }
PackNum<sizeof...(Indices)-1>::mm( mIPack ); RPackNum<sizeof...(Indices)-1>::mm( mIPack );
--IB::mPos; --IB::mPos;
return *this; return *this;
@ -243,14 +243,14 @@ namespace MultiArrayTools
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator=(size_t pos) ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator=(size_t pos)
{ {
IB::mPos = pos; IB::mPos = pos;
PackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, pos); RPackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, pos);
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
int ContainerIndex<Indices...>::pp(std::intptr_t idxPtrNum) int ContainerIndex<Indices...>::pp(std::intptr_t idxPtrNum)
{ {
int tmp = PackNum<sizeof...(Indices)-1>::pp(mIPack, mBlockSizes, idxPtrNum); int tmp = RPackNum<sizeof...(Indices)-1>::pp(mIPack, mBlockSizes, idxPtrNum);
IB::mPos += tmp; IB::mPos += tmp;
return tmp; return tmp;
} }
@ -258,7 +258,7 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
int ContainerIndex<Indices...>::mm(std::intptr_t idxPtrNum) int ContainerIndex<Indices...>::mm(std::intptr_t idxPtrNum)
{ {
int tmp = PackNum<sizeof...(Indices)-1>::mm(mIPack, mBlockSizes, idxPtrNum); int tmp = RPackNum<sizeof...(Indices)-1>::mm(mIPack, mBlockSizes, idxPtrNum);
IB::mPos -= tmp; IB::mPos -= tmp;
return tmp; return tmp;
} }
@ -267,15 +267,15 @@ namespace MultiArrayTools
typename ContainerIndex<Indices...>::MetaType ContainerIndex<Indices...>::meta() typename ContainerIndex<Indices...>::MetaType ContainerIndex<Indices...>::meta()
{ {
MetaType metaTuple; MetaType metaTuple;
PackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack); RPackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack);
return metaTuple; return metaTuple;
} }
template <class... Indices> template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::at(const MetaType& metaPos) ContainerIndex<Indices...>& ContainerIndex<Indices...>::at(const MetaType& metaPos)
{ {
PackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos); RPackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this; return *this;
} }
@ -319,7 +319,7 @@ namespace MultiArrayTools
// throw !! // throw !!
} }
ContainerIndex<Indices...> const* t = this; ContainerIndex<Indices...> const* t = this;
return PackNum<sizeof...(Indices)-1>::getIndexPtr(*t, n); return RPackNum<sizeof...(Indices)-1>::getIndexPtr(*t, n);
} }
template <class... Indices> template <class... Indices>
@ -347,7 +347,7 @@ namespace MultiArrayTools
for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; }
std::cout << id() << "[" << reinterpret_cast<std::intptr_t>(this) << "]" std::cout << id() << "[" << reinterpret_cast<std::intptr_t>(this) << "]"
<< "(" << IB::mRangePtr << "): " << meta() << std::endl; << "(" << IB::mRangePtr << "): " << meta() << std::endl;
PackNum<sizeof...(Indices)-1>::printIndex(mIPack, offset+1); RPackNum<sizeof...(Indices)-1>::printIndex(mIPack, offset+1);
} }
@ -395,7 +395,7 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
size_t ContainerRange<Ranges...>::size() const size_t ContainerRange<Ranges...>::size() const
{ {
return PackNum<sizeof...(Ranges)-1>::getSize(mSpace); return RPackNum<sizeof...(Ranges)-1>::getSize(mSpace);
} }
template <class... Ranges> template <class... Ranges>

View file

@ -7,8 +7,10 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base_def.h" #include "rbase_def.h"
#include "range_base.h" #include "range_base.h"
#include "index_type.h"
#include "vindex_wrapper.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -18,64 +20,6 @@ namespace MultiArrayTools
++id; ++id;
return id; return id;
} }
enum class IndexType{
SINGLE = 0,
MULTI = 1,
CONT = 2
};
class VirtualIndexWrapperBase
{
public:
DEFAULT_MEMBERS(VirtualIndexWrapperBase);
virtual IndexType type() const = 0;
virtual size_t dim() const = 0;
virtual size_t pos() const = 0;
virtual size_t max() const = 0;
virtual std::shared_ptr<RangeBase> rangePtr() const = 0;
virtual std::shared_ptr<VirtualIndexWrapperBase> getPtr(size_t n) const = 0;
virtual std::intptr_t getPtrNum() const = 0;
virtual size_t getStepSize(size_t n) const = 0;
};
template <class I>
std::shared_ptr<IndexWrapper<I> > make_viwb(std::shared_ptr<I> idxPtr)
{
return std::make_shared<IndexWrapper<I> >(idxPtr);
}
/*
template <class I>
std::shared_ptr<IndexWrapper<I> > make_viwb(const I& idxPtr)
{
return make_viwb( std::make_shared<I>(idxPtr) );
}*/
template <class I>
class IndexWrapper : public VirtualIndexWrapperBase
{
public:
DEFAULT_MEMBERS(IndexWrapper);
IndexWrapper(std::shared_ptr<I> idxPtr) : mIdxPtr(idxPtr) {}
virtual IndexType type() const override { return mIdxPtr->type(); }
virtual size_t dim() const override { return mIdxPtr->dim(); }
virtual size_t pos() const override { return mIdxPtr->pos(); }
virtual size_t max() const override { return mIdxPtr->max(); }
virtual std::shared_ptr<RangeBase> rangePtr() const override { return mIdxPtr->vrange(); }
virtual std::shared_ptr<VirtualIndexWrapperBase> getPtr(size_t n) const override
{ return mIdxPtr->getVPtr(n); }
virtual std::intptr_t getPtrNum() const override { return reinterpret_cast<std::intptr_t>( mIdxPtr.get() ); }
virtual size_t getStepSize(size_t n) const override { return mIdxPtr->getStepSize(n); }
std::shared_ptr<I> get() const { return mIdxPtr; } // unwrap
private:
std::shared_ptr<I> mIdxPtr;
};
template <class I, typename MetaType> template <class I, typename MetaType>
class IndexInterface class IndexInterface

14
src/ranges/index_type.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef __index_type_h__
#define __index_type_h__
namespace MultiArrayTools
{
enum class IndexType{
SINGLE = 0,
MULTI = 1,
CONT = 2
};
}
#endif

View file

@ -7,11 +7,11 @@
#include <tuple> #include <tuple>
#include <memory> #include <memory>
#include "base_def.h" //#include "base_def.h"
#include "range_base.h" #include "ranges/range_base.h"
#include "index_base.h" #include "ranges/index_base.h"
#include "pack_num.h" #include "rpack_num.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -187,24 +187,24 @@ namespace MultiArrayTools
MultiIndex<Indices...>::MultiIndex(const MultiIndex<Indices...>& in) : MultiIndex<Indices...>::MultiIndex(const MultiIndex<Indices...>& in) :
IndexInterface<std::tuple<typename Indices::MetaType...> >(in) IndexInterface<std::tuple<typename Indices::MetaType...> >(in)
{ {
PackNum<sizeof...(Indices)-1>::copy(mIPack, in); RPackNum<sizeof...(Indices)-1>::copy(mIPack, in);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(const MultiIndex<Indices...>& in) MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(const MultiIndex<Indices...>& in)
{ {
IndexI::operator=(in); IndexI::operator=(in);
PackNum<sizeof...(Indices)-1>::copy(mIPack, in); RPackNum<sizeof...(Indices)-1>::copy(mIPack, in);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this; return *this;
} }
*/ */
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(ContainerIndex<Indices...>& ci) MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(ContainerIndex<Indices...>& ci)
{ {
PackNum<sizeof...(Indices)-1>::copyInst(mIPack, ci); RPackNum<sizeof...(Indices)-1>::copyInst(mIPack, ci);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this; return *this;
} }
@ -213,10 +213,10 @@ namespace MultiArrayTools
MultiIndex<Indices...>::MultiIndex(const std::shared_ptr<MRange>& range) : MultiIndex<Indices...>::MultiIndex(const std::shared_ptr<MRange>& range) :
IndexInterface<MultiIndex<Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0) IndexInterface<MultiIndex<Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0)
{ {
PackNum<sizeof...(Indices)-1>::construct(mIPack, *range); RPackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
std::get<sizeof...(Indices)>(mBlockSizes) = 1; std::get<sizeof...(Indices)>(mBlockSizes) = 1;
PackNum<sizeof...(Indices)-1>::initBlockSizes(mBlockSizes, mIPack); // has one more element! RPackNum<sizeof...(Indices)-1>::initBlockSizes(mBlockSizes, mIPack); // has one more element!
} }
template <class... Indices> template <class... Indices>
@ -224,8 +224,8 @@ namespace MultiArrayTools
MultiIndex<Indices...>& MultiIndex<Indices...>::up() MultiIndex<Indices...>& MultiIndex<Indices...>::up()
{ {
static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices");
IB::mPos += PackNum<sizeof...(Indices)-DIR-1>::blockSize( mIPack ); IB::mPos += RPackNum<sizeof...(Indices)-DIR-1>::blockSize( mIPack );
PackNum<DIR>::pp( mIPack ); RPackNum<DIR>::pp( mIPack );
return *this; return *this;
} }
@ -234,8 +234,8 @@ namespace MultiArrayTools
MultiIndex<Indices...>& MultiIndex<Indices...>::down() MultiIndex<Indices...>& MultiIndex<Indices...>::down()
{ {
static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices");
IB::mPos -= PackNum<sizeof...(Indices)-DIR-1>::blockSize( mIPack ); IB::mPos -= RPackNum<sizeof...(Indices)-DIR-1>::blockSize( mIPack );
PackNum<DIR>::mm( mIPack ); RPackNum<DIR>::mm( mIPack );
return *this; return *this;
} }
@ -256,8 +256,8 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator()(std::shared_ptr<Indices>&... indices) MultiIndex<Indices...>& MultiIndex<Indices...>::operator()(std::shared_ptr<Indices>&... indices)
{ {
PackNum<sizeof...(Indices)-1>::swapIndices(mIPack, indices...); RPackNum<sizeof...(Indices)-1>::swapIndices(mIPack, indices...);
PackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, IB::mPos); RPackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, IB::mPos);
return *this; return *this;
} }
@ -271,14 +271,14 @@ namespace MultiArrayTools
MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(size_t pos) MultiIndex<Indices...>& MultiIndex<Indices...>::operator=(size_t pos)
{ {
IB::mPos = pos; IB::mPos = pos;
PackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, pos); RPackNum<sizeof...(Indices)-1>::setIndexPack(mIPack, pos);
return *this; return *this;
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator++() MultiIndex<Indices...>& MultiIndex<Indices...>::operator++()
{ {
PackNum<sizeof...(Indices)-1>::pp( mIPack ); RPackNum<sizeof...(Indices)-1>::pp( mIPack );
++IB::mPos; ++IB::mPos;
return *this; return *this;
} }
@ -286,7 +286,7 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::operator--() MultiIndex<Indices...>& MultiIndex<Indices...>::operator--()
{ {
PackNum<sizeof...(Indices)-1>::mm( mIPack ); RPackNum<sizeof...(Indices)-1>::mm( mIPack );
--IB::mPos; --IB::mPos;
return *this; return *this;
} }
@ -294,7 +294,7 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
int MultiIndex<Indices...>::pp(std::intptr_t idxPtrNum) int MultiIndex<Indices...>::pp(std::intptr_t idxPtrNum)
{ {
int tmp = PackNum<sizeof...(Indices)-1>::pp(mIPack, mBlockSizes, idxPtrNum); int tmp = RPackNum<sizeof...(Indices)-1>::pp(mIPack, mBlockSizes, idxPtrNum);
IB::mPos += tmp; IB::mPos += tmp;
return tmp; return tmp;
} }
@ -302,7 +302,7 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
int MultiIndex<Indices...>::mm(std::intptr_t idxPtrNum) int MultiIndex<Indices...>::mm(std::intptr_t idxPtrNum)
{ {
int tmp = PackNum<sizeof...(Indices)-1>::mm(mIPack, mBlockSizes, idxPtrNum); int tmp = RPackNum<sizeof...(Indices)-1>::mm(mIPack, mBlockSizes, idxPtrNum);
IB::mPos -= tmp; IB::mPos -= tmp;
return tmp; return tmp;
} }
@ -311,15 +311,15 @@ namespace MultiArrayTools
typename MultiIndex<Indices...>::MetaType MultiIndex<Indices...>::meta() typename MultiIndex<Indices...>::MetaType MultiIndex<Indices...>::meta()
{ {
MetaType metaTuple; MetaType metaTuple;
PackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack); RPackNum<sizeof...(Indices)-1>::getMetaPos(metaTuple, mIPack);
return metaTuple; return metaTuple;
} }
template <class... Indices> template <class... Indices>
MultiIndex<Indices...>& MultiIndex<Indices...>::at(const MetaType& metaPos) MultiIndex<Indices...>& MultiIndex<Indices...>::at(const MetaType& metaPos)
{ {
PackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos); RPackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos);
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack); IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
return *this; return *this;
} }
@ -363,7 +363,7 @@ namespace MultiArrayTools
// throw !! // throw !!
} }
MultiIndex<Indices...> const* t = this; MultiIndex<Indices...> const* t = this;
return PackNum<sizeof...(Indices)-1>::getIndexPtr(*t, n); return RPackNum<sizeof...(Indices)-1>::getIndexPtr(*t, n);
} }
template <class... Indices> template <class... Indices>
@ -391,7 +391,7 @@ namespace MultiArrayTools
for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; }
std::cout << id() << "[" << reinterpret_cast<std::intptr_t>(this) std::cout << id() << "[" << reinterpret_cast<std::intptr_t>(this)
<< "]" << "(" << IB::mRangePtr << "): " << meta() << std::endl; << "]" << "(" << IB::mRangePtr << "): " << meta() << std::endl;
PackNum<sizeof...(Indices)-1>::printIndex(mIPack, offset+1); RPackNum<sizeof...(Indices)-1>::printIndex(mIPack, offset+1);
} }
@ -457,7 +457,7 @@ namespace MultiArrayTools
template <class... Ranges> template <class... Ranges>
size_t MultiRange<Ranges...>::size() const size_t MultiRange<Ranges...>::size() const
{ {
return PackNum<sizeof...(Ranges)-1>::getSize(mSpace); return RPackNum<sizeof...(Ranges)-1>::getSize(mSpace);
} }
template <class... Ranges> template <class... Ranges>

31
src/ranges/range_base.cc Normal file
View file

@ -0,0 +1,31 @@
#include "range_base.h"
namespace MultiArrayTools
{
/*************************
* RangeFactoryBase *
*************************/
void RangeFactoryBase::setSelf()
{
mProd->mThis = mProd;
}
/******************
* RangeBase *
******************/
bool RangeBase::operator==(const RangeBase& in) const
{
return this == &in;
}
bool RangeBase::operator!=(const RangeBase& in) const
{
return this != &in;
}
} // end namespace MultiArrayTools

View file

@ -9,6 +9,13 @@
#include "base_def.h" #include "base_def.h"
namespace MultiArrayTools
{
class RangeBase;
}
#include "vindex_base.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -77,36 +84,4 @@ namespace MultiArrayTools
} }
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
namespace MultiArrayTools
{
/*************************
* RangeFactoryBase *
*************************/
void RangeFactoryBase::setSelf()
{
mProd->mThis = mProd;
}
/******************
* RangeBase *
******************/
bool RangeBase::operator==(const RangeBase& in) const
{
return this == &in;
}
bool RangeBase::operator!=(const RangeBase& in) const
{
return this != &in;
}
}
#endif #endif

80
src/ranges/rbase_def.h Normal file
View file

@ -0,0 +1,80 @@
// -*- C++ -*-
#ifndef __ranges_base_def_h__
#define __ranges_base_def_h__
namespace MultiArrayTools
{
/***********************
* Provided Types *
***********************/
// range_base.h
enum class SpaceType;
// range_base.h
class RangeFactoryBase;
// range_base.h
class RangeBase;
// range_base.h
template <class Index>
class RangeInterface;
// index_base.h
class VirtualIndexWrapperBase;
typedef VirtualIndexWrapperBase VIWB;
// index_base.h
template <class I>
class IndexWrapper;
// index_base.h
template <class I, typename MetaType>
class IndexInterface;
// single_range.h
template <typename U, SpaceType TYPE>
class SingleRange;
// single_range.h
template <typename U, SpaceType TYPE>
class SingleRangeFactory;
// single_range.h
template <typename U, SpaceType TYPE>
class SingleIndex;
// multi_range.h
template <class... Ranges>
class MultiRangeFactory;
// multi_range.h
template <class... Ranges>
class MultiRange;
// multi_range.h
template <class... Indices>
class MultiIndex;
// container_range.h
template <class... Ranges>
class ContainerRangeFactory;
// container_range.h
template <class... Ranges>
class ContainerRange;
// container_range.h
template <class... Indices>
class ContainerIndex;
// anonymous_range.h
class AnonymousRangeFactory;
// anonymous_range.h
class AnonymousRange;
}
#endif

5
src/ranges/rheader.h Normal file
View file

@ -0,0 +1,5 @@
#include "single_range.h"
#include "multi_range.h"
#include "container_range.h"
//#include "anonymous_range.h" have to fix bug !!

371
src/ranges/rpack_num.h Normal file
View file

@ -0,0 +1,371 @@
#ifndef __pack_num_h__
#define __pack_num_h__
#include <memory>
#include "vindex_wrapper.h"
namespace MultiArrayHelper
{
using namespace MultiArrayTools;
template <size_t N>
struct RPackNum
{
template <class IndexType>
static std::shared_ptr<VIWB> getIndex(const IndexType& in, size_t n)
{
if(n == N){
return make_viwb( in.template get<N>() );
}
else {
return RPackNum<N-1>::getIndex(in, n);
}
}
template <class IndexType>
static std::shared_ptr<VIWB> getIndexPtr(const IndexType& in, size_t n)
{
if(n == N){
return make_viwb( in.template getPtr<N>() );
}
else {
return RPackNum<N-1>::getIndexPtr(in, n);
}
}
template <class... Indices>
static void initBlockSizes(std::array<size_t,sizeof...(Indices)+1>& bs,
std::tuple<std::shared_ptr<Indices>...>& ip)
{
std::get<N>(bs) = RPackNum<sizeof...(Indices)-N-1>::blockSize(ip);
RPackNum<N-1>::initBlockSizes(bs, ip);
}
template <class... Indices>
static inline void pp(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<N>(ip);
if(si.last()){
si = 0;
RPackNum<N-1>::pp(ip);
}
else {
++si;
}
}
template <class... Indices>
static inline int pp(std::tuple<std::shared_ptr<Indices>...>& ip,
std::array<size_t,sizeof...(Indices)+1>& bs,
std::intptr_t idxPtrNum)
{
auto& siPtr = std::get<N>(ip);
//VCHECK(siPtr.id());
if(reinterpret_cast<std::intptr_t>(siPtr.get()) == idxPtrNum){
return RPackNum<N-1>::pp(ip, bs, idxPtrNum);
}
else {
int tmp = siPtr->pp(idxPtrNum);
if(siPtr->pos() == siPtr->max()){
(*siPtr) = 0;
return RPackNum<N-1>::pp(ip, bs, idxPtrNum) - siPtr->max() + 1;
}
else {
return tmp * std::get<N+1>(bs);
}
}
}
template <class... Indices>
static inline void mm(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<N>(ip);
if(si.first()){
si = si.max() - 1;
RPackNum<N-1>::mm(ip);
}
else {
--si;
}
}
// !!!!
template <class... Indices>
static inline int mm(std::tuple<std::shared_ptr<Indices>...>& ip,
std::array<size_t,sizeof...(Indices)+1>& bs,
std::intptr_t idxPtrNum)
{
auto& siPtr = std::get<N>(ip);
if(reinterpret_cast<std::intptr_t>(siPtr.get()) == idxPtrNum){
return std::get<N>(bs) + RPackNum<N-1>::mm(ip, bs, idxPtrNum);
}
else {
if(siPtr->first()){
(*siPtr) = siPtr->max() - 1;
return RPackNum<N-1>::mm(ip, bs, idxPtrNum) - siPtr->max() + 1;
}
else {
return siPtr->mm(idxPtrNum);
}
}
}
template <class RangeTuple>
static size_t getSize(const RangeTuple& rt)
{
return std::get<N>(rt)->size() * RPackNum<N-1>::getSize(rt);
}
template <class IndexPack, class MetaType>
static void getMetaPos(MetaType& target,
const IndexPack& source)
{
std::get<N>(target) = std::get<N>(source)->meta();
RPackNum<N-1>::getMetaPos(target, source);
}
template <class IndexPack, typename MetaType>
static void setMeta(IndexPack& target, const MetaType& source)
{
std::get<N>(target)->at( std::get<N>(source) );
RPackNum<N-1>::setMeta(target, source);
}
template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos)
{
auto& i = *std::get<N>(iPack).get();
const size_t ownPos = pos % i.max();
i = ownPos;
RPackNum<N-1>::setIndexPack(iPack, (pos - ownPos) / i.max() );
}
template <class MRange, class... Indices>
static void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range)
{
typedef typename std::remove_reference<decltype(range.template get<N>())>::type SubRangeType;
typedef typename SubRangeType::IndexType SubIndexType;
typedef typename std::remove_reference<decltype(*std::get<N>(ip).get())>::type TypeFromIndexPack;
static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value,
"inconsiśtent types");
std::get<N>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( range.template getPtr<N>() ) );
RPackNum<N-1>::construct(ip, range);
}
template <template<class...> class IndexType, class... Indices>
static void copyInst(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind)
{
std::get<N>(ip) = ind.template getPtr<N>() ;
RPackNum<N-1>::copyInst(ip, ind);
}
template <class... Indices>
static size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{
auto ID = std::get<N>(iPtrTup)->id();
return std::get<N>(iPtrTup)->pos() +
RPackNum<N-1>::makePos(iPtrTup) * std::get<N>(iPtrTup)->max();
}
template <class Pack, class IndexType, class... Indices>
static void swapIndices(Pack& ipack, const std::shared_ptr<IndexType>& nind,
const std::shared_ptr<Indices>&... ninds)
{
std::get<std::tuple_size<Pack>::value-N-1>(ipack) = nind;
RPackNum<N-1>::swapIndices(ipack, ninds...);
}
template <class... Indices>
static size_t blockSize(const std::tuple<std::shared_ptr<Indices>...>& pack)
{
return std::get<sizeof...(Indices)-N-1>(pack)->max() * RPackNum<N-1>::blockSize(pack);
}
template <class... Ranges>
static void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<RangeBase> v)
{
v[N] = std::get<N>(rst);
RPackNum<N-1>::RangesToVec(rst, v);
}
template <class... Indices>
static void printIndex(const std::tuple<std::shared_ptr<Indices>...>& ip, size_t offset)
{
std::get<N>(ip)->print(offset);
RPackNum<N-1>::printIndex(ip, offset);
}
template <class Range, class... Ranges>
static void checkDefaultable()
{
static_assert( Range::defaultable, "not defaultable" );
RPackNum<N-1>::template checkDefaultable<Ranges...>();
}
};
template<>
struct RPackNum<0>
{
template <class IndexType>
static std::shared_ptr<VIWB> getIndex(const IndexType& in, size_t n)
{
return make_viwb( in.template get<0>() );
}
template <class IndexType>
static std::shared_ptr<VIWB> getIndexPtr(const IndexType& in, size_t n)
{
return make_viwb( in.template getPtr<0>() );
}
template <class... Indices>
static void initBlockSizes(std::array<size_t,sizeof...(Indices)+1>& bs,
std::tuple<std::shared_ptr<Indices>...>& ip)
{
std::get<0>(bs) = RPackNum<sizeof...(Indices)-1>::blockSize(ip);
}
template <class... Indices>
static inline void pp(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<0>(ip);
++si;
}
template <class... Indices>
static inline int pp(std::tuple<std::shared_ptr<Indices>...>& ip,
std::array<size_t,sizeof...(Indices)+1>& bs,
std::intptr_t idxPtrNum)
{
auto& siPtr = std::get<0>(ip);
if(reinterpret_cast<std::intptr_t>(siPtr.get()) == idxPtrNum){
return std::get<0>(bs);
}
else {
int tmp = siPtr->pp(idxPtrNum);
return tmp * std::get<1>(bs);
}
}
template <class... Indices>
static inline void mm(std::tuple<std::shared_ptr<Indices>...>& ip)
{
auto& si = *std::get<0>(ip);
--si;
}
template <class... Indices>
static inline int mm(std::tuple<std::shared_ptr<Indices>...>& ip,
std::array<size_t,sizeof...(Indices)+1>& bs,
std::intptr_t idxPtrNum)
{
auto& siPtr = std::get<0>(ip);
if(reinterpret_cast<std::intptr_t>(siPtr.get()) == idxPtrNum){
return std::get<0>(bs);
//return 1;
}
else {
return siPtr->mm(idxPtrNum);
}
}
template <class RangeTuple>
static size_t getSize(const RangeTuple& rt)
{
return std::get<0>(rt)->size();
}
template <class IndexPack, class MetaType>
static void getMetaPos(MetaType& target,
const IndexPack& source)
{
std::get<0>(target) = std::get<0>(source)->meta();
}
template <class IndexPack, typename MetaType>
static void setMeta(IndexPack& target, const MetaType& source)
{
std::get<0>(target)->at( std::get<0>( source ) );
}
template <class IndexPack>
static void setIndexPack(IndexPack& iPack, size_t pos)
{
auto& i = *std::get<0>(iPack);
const size_t ownPos = pos % i.max();
i = ownPos;
}
template <class MRange, class... Indices>
static void construct(std::tuple<std::shared_ptr<Indices>...>& ip,
const MRange& range)
{
typedef typename std::remove_reference<decltype(range.template get<0>())>::type SubRangeType;
typedef typename SubRangeType::IndexType SubIndexType;
typedef typename std::remove_reference<decltype(*std::get<0>(ip).get())>::type TypeFromIndexPack;
static_assert(std::is_same<SubIndexType,TypeFromIndexPack>::value,
"inconsiśtent types");
std::get<0>(ip) = std::shared_ptr<SubIndexType>( new SubIndexType( range.template getPtr<0>() ) );
}
template <template<class...> class IndexType, class... Indices>
static void copyInst(std::tuple<std::shared_ptr<Indices>...>& ip,
const IndexType<Indices...>& ind)
{
std::get<0>(ip) = ind.template getPtr<0>();
}
template <class... Indices>
static size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup)
{
auto ID = std::get<0>(iPtrTup)->id();
return std::get<0>(iPtrTup)->pos();
}
template <class Pack, class IndexType>
static void swapIndices(Pack& ipack, const std::shared_ptr<IndexType>& nind)
{
std::get<std::tuple_size<Pack>::value-1>(ipack) = nind;
}
template <class... Indices>
static size_t blockSize(const std::tuple<std::shared_ptr<Indices>...>& pack)
{
return std::get<sizeof...(Indices)-1>(pack)->max();
}
template <class... Ranges>
static void RangesToVec(const std::tuple<std::shared_ptr<Ranges>...>& rst,
std::vector<RangeBase> v)
{
v[0] = std::get<0>(rst);
}
template <class... Indices>
static void printIndex(const std::tuple<std::shared_ptr<Indices>...>& ip, size_t offset)
{
std::get<0>(ip)->print(offset);
}
template <class Range>
static void checkDefaultable()
{
static_assert( Range::defaultable, "not defaultable" );
}
};
} // end namespace MultiArrayHelper
#endif

View file

@ -7,9 +7,9 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include "base_def.h" //#include "base_def.h"
#include "index_base.h" #include "ranges/index_base.h"
#include "range_base.h" #include "ranges/range_base.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {

View file

@ -4,7 +4,9 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <iostream> #include <iostream>
#include "multi_array_header.h" #include "ranges/rheader.h"
//#include "multi_array_header.h"
namespace MAT = MultiArrayTools; namespace MAT = MultiArrayTools;

View file

@ -0,0 +1,216 @@
// -*- C++ -*-
#include <cstdlib>
#include "gtest/gtest.h"
#include <iostream>
#include "rheader.h"
//#include "multi_array_header.h"
namespace MAT = MultiArrayTools;
namespace {
using namespace MAT;
template <class Factory, typename T>
void swapFactory(std::shared_ptr<RangeFactoryBase>& fptr, std::initializer_list<T> ilist)
{
std::vector<T> tmp = ilist;
auto nptr = std::make_shared<Factory>( tmp );
fptr = nptr;
}
template <class Factory, class... Rs>
void swapMFactory(std::shared_ptr<RangeFactoryBase>& fptr, const Rs&... rs)
{
auto nptr = std::make_shared<Factory>( rs... );
fptr = nptr;
}
template <typename... Ts>
auto mkt(Ts&&... ts) -> decltype(std::make_tuple(ts...))
{
return std::make_tuple(ts...);
}
class IndexTest : public ::testing::Test
{
protected:
typedef SingleRangeFactory<char,SpaceType::ANY> SRF;
typedef SRF::oType SRange;
typedef MultiRangeFactory<SRange,SRange,SRange> M3RF;
typedef M3RF::oType M3Range;
typedef MultiRangeFactory<SRange,M3Range,SRange> MasterRF;
typedef MasterRF::oType MasterRange;
typedef ContainerRangeFactory<M3Range,SRange> CRF;
typedef CRF::oType CRange;
IndexTest()
{
swapFactory<SRF>(rfbptr, { 'e', 'b', 'n' } );
sr1ptr = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
swapFactory<SRF>(rfbptr, { 'x', 'y', 'l', 'f' } );
sr2ptr = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
swapFactory<SRF>(rfbptr, { 'a', 'b' } );
std::shared_ptr<SRange> temp1 = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
swapFactory<SRF>(rfbptr, { '1' } );
std::shared_ptr<SRange> temp2 = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
swapFactory<SRF>(rfbptr, { '0', '7' } );
std::shared_ptr<SRange> temp3 = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
swapMFactory<M3RF>(rfbptr, temp1, temp2, temp3 );
m3rptr = std::dynamic_pointer_cast<M3Range>( rfbptr->create() );
swapMFactory<MasterRF>(rfbptr, sr1ptr, m3rptr, sr2ptr);
mstrptr = std::dynamic_pointer_cast<MasterRange>( rfbptr->create() );
swapMFactory<CRF>(rfbptr, m3rptr, sr2ptr);
cr1ptr = std::dynamic_pointer_cast<CRange>( rfbptr->create() );
swapMFactory<CRF>(rfbptr, m3rptr, sr1ptr);
cr2ptr = std::dynamic_pointer_cast<CRange>( rfbptr->create() );
}
std::shared_ptr<RangeFactoryBase> rfbptr;
std::shared_ptr<SRange> sr1ptr;
std::shared_ptr<SRange> sr2ptr;
std::shared_ptr<M3Range> m3rptr;
std::shared_ptr<MasterRange> mstrptr;
std::shared_ptr<CRange> cr1ptr;
std::shared_ptr<CRange> cr2ptr;
};
TEST_F(IndexTest, SingleIndex_SimpleCall)
{
auto si = sr1ptr->begin();
EXPECT_EQ(si.max(), 3u);
EXPECT_EQ(si.pos(), 0u);
EXPECT_EQ(si.first(), true);
EXPECT_EQ(si.last(), false);
EXPECT_EQ(si.meta(), 'e');
si.at('n');
EXPECT_EQ(si.pos(), si.max()-1);
EXPECT_EQ(si.first(), false);
EXPECT_EQ(si.last(), true);
si = 1;
EXPECT_EQ(si.meta(), 'b');
++si;
EXPECT_EQ(si.meta(), 'n');
auto si2 = sr1ptr->end();
--si2;
EXPECT_EQ(si == si2, true);
EXPECT_EQ(si != si2, false);
auto si3 = sr2ptr->end();
--si3;
EXPECT_EQ(si == si3, false);
EXPECT_EQ(si != si3, true);
}
TEST_F(IndexTest, MultiIndex_SimpleCall)
{
auto mi = m3rptr->begin();
EXPECT_EQ(mi.max(), 4u);
EXPECT_EQ(mi.pos(), 0u);
EXPECT_EQ(mi.first(), true);
EXPECT_EQ(mi.last(), false);
EXPECT_EQ(mi.meta() == mkt('a','1','0'), true);
mi.at( mkt('b','1','7') );
EXPECT_EQ(mi.pos(), mi.max()-1);
EXPECT_EQ(mi.first(), false);
EXPECT_EQ(mi.last(), true);
mi = 1;
EXPECT_EQ(mi.meta() == mkt('a','1','7'), true);
++mi;
EXPECT_EQ(mi.meta() == mkt('b','1','0'), true);
++mi;
auto mi2 = m3rptr->end();
--mi2;
EXPECT_EQ(mi == mi2, true);
EXPECT_EQ(mi != mi2, false);
--mi2;
EXPECT_EQ(mi == mi2, false);
EXPECT_EQ(mi != mi2, true);
auto si = sr1ptr->begin();
EXPECT_EQ(si == mi, false);
EXPECT_EQ(si != mi, true);
}
TEST_F(IndexTest, MasterRange_Check)
{
EXPECT_EQ(mstrptr->size(), 48u);
EXPECT_EQ(mstrptr->template get<0>().size(), 3u);
EXPECT_EQ(mstrptr->template get<1>().size(), 4u);
EXPECT_EQ(mstrptr->template get<2>().size(), 4u);
EXPECT_EQ(mstrptr->dim(), 3);
EXPECT_EQ(mstrptr->template get<0>().dim(), 1u);
EXPECT_EQ(mstrptr->template get<1>().dim(), 3u);
auto mi = mstrptr->begin();
EXPECT_EQ(mi.meta() == mkt( 'e' , mkt('a', '1', '0') , 'x' ), true);
mi = mi.max()-1;
EXPECT_EQ(mi.meta() == mkt( 'n' , mkt('b', '1', '7') , 'f' ), true);
mi.template down<1>();
EXPECT_EQ(mi.meta() == mkt( 'n' , mkt('b', '1', '0') , 'f' ), true);
mi.template down<0>();
EXPECT_EQ(mi.meta() == mkt( 'b' , mkt('b', '1', '0') , 'f' ), true);
mi.template down<2>();
EXPECT_EQ(mi.meta() == mkt( 'b' , mkt('b', '1', '0') , 'l' ), true);
mi.template up<1>();
EXPECT_EQ(mi.meta() == mkt( 'b' , mkt('b', '1', '7') , 'l' ), true);
auto& subI = mi.template get<0>();
EXPECT_EQ(subI.meta(), 'b');
mi.template up<0>();
EXPECT_EQ(subI.meta(), 'n');
auto& subI2 = mi.template get<1>();
EXPECT_EQ(subI2.meta() == mkt('b', '1', '7'), true);
mi.template down<1>();
EXPECT_EQ(subI2.meta() == mkt('b', '1', '0'), true);
}
TEST_F(IndexTest, ContainerRange_Check)
{
EXPECT_EQ(cr1ptr->size(), 16u);
EXPECT_EQ(cr2ptr->size(), 12u);
auto mi = mstrptr->begin();
auto ci1 = cr1ptr->begin();
auto ci2 = cr2ptr->begin();
EXPECT_EQ(ci1.max(), 16u);
EXPECT_EQ(ci2.max(), 12u);
ci1(mi.template getPtr<1>(), mi.template getPtr<2>());
ci2(mi.template getPtr<1>(), mi.template getPtr<0>());
EXPECT_EQ(ci1.pos(), 0u);
EXPECT_EQ(ci2.pos(), 0u);
++mi;
EXPECT_EQ(ci1().pos(), 1u);
EXPECT_EQ(ci2().pos(), 0u);
mi.template up<1>();
EXPECT_EQ(ci1().pos(), 5u);
EXPECT_EQ(ci2().pos(), 3u);
mi.template up<0>();
EXPECT_EQ(ci1().pos(), 5u);
EXPECT_EQ(ci2().pos(), 4u);
mi = mi.max()-1;
EXPECT_EQ(ci1().pos(), ci1.max()-1);
EXPECT_EQ(ci2().pos(), ci2.max()-1);
}
} // end namespace
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

30
src/ranges/vindex_base.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef __vindex_base_h__
#define __vindex_base_h__
//#include "ranges/range_base.h"
#include "ranges/index_type.h"
namespace MultiArrayTools
{
class VirtualIndexWrapperBase
{
public:
DEFAULT_MEMBERS(VirtualIndexWrapperBase);
virtual IndexType type() const = 0;
virtual size_t dim() const = 0;
virtual size_t pos() const = 0;
virtual size_t max() const = 0;
virtual std::shared_ptr<RangeBase> rangePtr() const = 0;
virtual std::shared_ptr<VirtualIndexWrapperBase> getPtr(size_t n) const = 0;
virtual std::intptr_t getPtrNum() const = 0;
virtual size_t getStepSize(size_t n) const = 0;
};
typedef VirtualIndexWrapperBase VIWB;
} // end namespace MultiArrayTools
#endif

View file

@ -0,0 +1,43 @@
#ifndef __vindex_wrapper_h__
#define __vindex_wrapper_h__
#include "ranges/vindex_base.h"
namespace MultiArrayTools
{
template <class I>
std::shared_ptr<IndexWrapper<I> > make_viwb(std::shared_ptr<I> idxPtr)
{
return std::make_shared<IndexWrapper<I> >(idxPtr);
}
template <class I>
class IndexWrapper : public VirtualIndexWrapperBase
{
public:
DEFAULT_MEMBERS(IndexWrapper);
IndexWrapper(std::shared_ptr<I> idxPtr) : mIdxPtr(idxPtr) {}
virtual IndexType type() const override { return mIdxPtr->type(); }
virtual size_t dim() const override { return mIdxPtr->dim(); }
virtual size_t pos() const override { return mIdxPtr->pos(); }
virtual size_t max() const override { return mIdxPtr->max(); }
virtual std::shared_ptr<RangeBase> rangePtr() const override { return mIdxPtr->vrange(); }
virtual std::shared_ptr<VirtualIndexWrapperBase> getPtr(size_t n) const override
{ return mIdxPtr->getVPtr(n); }
virtual std::intptr_t getPtrNum() const override { return reinterpret_cast<std::intptr_t>( mIdxPtr.get() ); }
virtual size_t getStepSize(size_t n) const override { return mIdxPtr->getStepSize(n); }
std::shared_ptr<I> get() const { return mIdxPtr; } // unwrap
private:
std::shared_ptr<I> mIdxPtr;
};
}
#endif