diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cc968a..d801d32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,21 +22,17 @@ endif() include_directories(src) -#add_executable(utest src/unit_test.cc) -#target_link_libraries(utest ${GTEST_BOTH_LIBRARIES}) -#add_test(NAME utest COMMAND utest) - -add_executable(iutest src/index_unit_test.cc) +add_executable(iutest src/ranges/tests/index_unit_test.cc src/ranges/range_base.cc) target_link_libraries(iutest ${GTEST_BOTH_LIBRARIES}) add_test(NAME iutest COMMAND iutest) -add_executable(mautest src/ma_unit_test.cc) -target_link_libraries(mautest ${GTEST_BOTH_LIBRARIES}) -add_test(NAME mautest COMMAND mautest) +#add_executable(mautest src/ma_unit_test.cc) +#target_link_libraries(mautest ${GTEST_BOTH_LIBRARIES}) +#add_test(NAME mautest COMMAND mautest) -add_executable(oputest src/op_unit_test.cc) -target_link_libraries(oputest ${GTEST_BOTH_LIBRARIES}) -add_test(NAME oputest COMMAND oputest) +#add_executable(oputest src/op_unit_test.cc) +#target_link_libraries(oputest ${GTEST_BOTH_LIBRARIES}) +#add_test(NAME oputest COMMAND oputest) #install(TARGETS testm DESTINATION install) diff --git a/src/base_def.h b/src/base_def.h index 327f4df..10f833b 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -32,220 +32,4 @@ __class_name__& operator=(__class_name__&& in) = default -namespace MultiArrayTools -{ - template - void checkPtr(const MultiArray& in, bool check = false) - { - static MultiArray const* ptr = nullptr; - if(not check){ - ptr = ∈ - } - 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 RangeInterface; - - // index_base.h - class VirtualIndexWrapperBase; - typedef VirtualIndexWrapperBase VIWB; - - // index_base.h - template - class IndexWrapper; - - // index_base.h - template - class IndexInterface; - - // single_range.h - template - class SingleRange; - - // single_range.h - template - class SingleRangeFactory; - - // single_range.h - template - class SingleIndex; - - // multi_range.h - template - class MultiRangeFactory; - - // multi_range.h - template - class MultiRange; - - // multi_range.h - template - class MultiIndex; - - // container_range.h - template - class ContainerRangeFactory; - - // container_range.h - template - class ContainerRange; - - // container_range.h - template - class ContainerIndex; - - // anonymous_range.h - class AnonymousRangeFactory; - - // anonymous_range.h - class AnonymousRange; - - // multi_array.h - template - class MultiArrayBase; - - // multi_array.h - template - class MutableMultiArrayBase; - - // multi_array.h - template - class MultiArray; - - // multi_array_operation.h - template - class OperationBase; - - // multi_array_operation.h - template - class MutableOperationBase; - - // multi_array_operation.h - template - class OperationTemplate; - - // multi_array_operation.h - template - class OperationMaster; - - // multi_array_operation.h - template - class OperationRoot; - - // multi_array_operation.h - template - class ConstOperationRoot; - - // multi_array_operation.h - template - class Operation; - - // multi_array_operation.h - template - class Contraction; - - /* - // multi_array_operation.h - template - class MultiArrayOperationMap; - - // multi_array_operation.h - template - class MultiArrayContraction; - - // slice.h - template - class Slice; - - // slice.h - template - class ConstSlice; - - // manipulator.h - template - class ManipulatorBase; - - // manipulator.h - template - class BinReader; - - // ma_functional.h - template - class IndexMapFunction; - - // ma_functional.h - class vec3d2Function; - - // ma_functional.h - class pyProjFunction; - - // ma_functional.h - class pyProjNoSymFunction; - */ -} - -namespace MultiArrayHelper -{ - // block.h - template - class BlockBinaryOp; - - // block.h - enum class BlockType; - - // block.h - template - class BlockBase; - - // block.h - template - class MutableBlockBase; - - // block.h - template - class Block; - - // block.h - template - class MBlock; - - // block.h - template - class BlockValue; - - // block.h - template - class MBlockValue; - - // block.h - template - class SplitBlock; - - // block.h - template - class MSplitBlock; - - // block.h - template - class BlockResult; - -} - #endif diff --git a/src/block.h b/src/block.h index 686ae5e..80957a4 100644 --- a/src/block.h +++ b/src/block.h @@ -11,6 +11,47 @@ namespace MultiArrayHelper { + + template + class BlockBinaryOp; + + + template + class BlockBase; + + + template + class MutableBlockBase; + + + template + class Block; + + + template + class MBlock; + + + template + class BlockValue; + + + template + class MBlockValue; + + + template + class SplitBlock; + + + template + class MSplitBlock; + + + template + class BlockResult; + + enum class BlockType { INDEF = 0, BLOCK = 1, diff --git a/src/mbase_def.h b/src/mbase_def.h new file mode 100644 index 0000000..23f4379 --- /dev/null +++ b/src/mbase_def.h @@ -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 RangeInterface; + + // index_base.h + class VirtualIndexWrapperBase; + typedef VirtualIndexWrapperBase VIWB; + + // index_base.h + template + class IndexWrapper; + + // index_base.h + template + class IndexInterface; + + // single_range.h + template + class SingleRange; + + // single_range.h + template + class SingleRangeFactory; + + // single_range.h + template + class SingleIndex; + + // multi_range.h + template + class MultiRangeFactory; + + // multi_range.h + template + class MultiRange; + + // multi_range.h + template + class MultiIndex; + + // container_range.h + template + class ContainerRangeFactory; + + // container_range.h + template + class ContainerRange; + + // container_range.h + template + class ContainerIndex; + + // anonymous_range.h + class AnonymousRangeFactory; + + // anonymous_range.h + class AnonymousRange; + + // multi_array.h + template + class MultiArrayBase; + + // multi_array.h + template + class MutableMultiArrayBase; + + // multi_array.h + template + class MultiArray; + + // multi_array_operation.h + template + class OperationBase; + + // multi_array_operation.h + template + class MutableOperationBase; + + // multi_array_operation.h + template + class OperationTemplate; + + // multi_array_operation.h + template + class OperationMaster; + + // multi_array_operation.h + template + class OperationRoot; + + // multi_array_operation.h + template + class ConstOperationRoot; + + // multi_array_operation.h + template + class Operation; + + // multi_array_operation.h + template + class Contraction; +} + +#endif diff --git a/src/pack_num.h b/src/pack_num.h index 16770c8..e9100ad 100644 --- a/src/pack_num.h +++ b/src/pack_num.h @@ -16,219 +16,10 @@ namespace MultiArrayHelper template std::ostream& operator<<(std::ostream& out, const std::tuple& tp); - + template struct PackNum { - template - static std::shared_ptr getIndex(const IndexType& in, size_t n) - { - if(n == N){ - return make_viwb( in.template get() ); - } - else { - return PackNum::getIndex(in, n); - } - } - /* - template - static const IndexBase& getIndex(const IndexType& in, size_t n) - { - if(n == N){ - return in.template get(); - } - else { - return PackNum::getIndex(in, n); - } - }*/ - - template - static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) - { - if(n == N){ - return make_viwb( in.template getPtr() ); - } - else { - return PackNum::getIndexPtr(in, n); - } - } - /* - template - static void lock(std::tuple...>& ip, - std::shared_ptr toLock) - { - std::get(ip)->lock(toLock); - PackNum::lock(ip, toLock); - } - */ - template - static void initBlockSizes(std::array& bs, - std::tuple...>& ip) - { - std::get(bs) = PackNum::blockSize(ip); - PackNum::initBlockSizes(bs, ip); - } - - template - static inline void pp(std::tuple...>& ip) - { - auto& si = *std::get(ip); - if(si.last()){ - si = 0; - PackNum::pp(ip); - } - else { - ++si; - } - } - - template - static inline int pp(std::tuple...>& ip, - std::array& bs, - std::intptr_t idxPtrNum) - { - auto& siPtr = std::get(ip); - //VCHECK(siPtr.id()); - if(reinterpret_cast(siPtr.get()) == idxPtrNum){ - return PackNum::pp(ip, bs, idxPtrNum); - } - else { - int tmp = siPtr->pp(idxPtrNum); - if(siPtr->pos() == siPtr->max()){ - (*siPtr) = 0; - return PackNum::pp(ip, bs, idxPtrNum) - siPtr->max() + 1; - } - else { - return tmp * std::get(bs); - } - } - } - - template - static inline void mm(std::tuple...>& ip) - { - auto& si = *std::get(ip); - if(si.first()){ - si = si.max() - 1; - PackNum::mm(ip); - } - else { - --si; - } - } - - // !!!! - template - static inline int mm(std::tuple...>& ip, - std::array& bs, - std::intptr_t idxPtrNum) - { - auto& siPtr = std::get(ip); - if(reinterpret_cast(siPtr.get()) == idxPtrNum){ - return std::get(bs) + PackNum::mm(ip, bs, idxPtrNum); - } - else { - if(siPtr->first()){ - (*siPtr) = siPtr->max() - 1; - return PackNum::mm(ip, bs, idxPtrNum) - siPtr->max() + 1; - } - else { - return siPtr->mm(idxPtrNum); - } - } - } - - template - static size_t getSize(const RangeTuple& rt) - { - return std::get(rt)->size() * PackNum::getSize(rt); - } - - template - static void getMetaPos(MetaType& target, - const IndexPack& source) - { - std::get(target) = std::get(source)->meta(); - PackNum::getMetaPos(target, source); - } - - template - static void setMeta(IndexPack& target, const MetaType& source) - { - std::get(target)->at( std::get(source) ); - PackNum::setMeta(target, source); - } - - template - static void setIndexPack(IndexPack& iPack, size_t pos) - { - auto& i = *std::get(iPack).get(); - const size_t ownPos = pos % i.max(); - i = ownPos; - PackNum::setIndexPack(iPack, (pos - ownPos) / i.max() ); - } - - template - static void construct(std::tuple...>& ip, - const MRange& range) - { - typedef typename std::remove_reference())>::type SubRangeType; - typedef typename SubRangeType::IndexType SubIndexType; - typedef typename std::remove_reference(ip).get())>::type TypeFromIndexPack; - - static_assert(std::is_same::value, - "inconsiśtent types"); - - std::get(ip) = std::shared_ptr( new SubIndexType( range.template getPtr() ) ); - PackNum::construct(ip, range); - } - /* - template class IndexType, class... Indices> - static void copy(std::tuple...>& ip, - const IndexType& ind) - { - typedef typename std::remove_reference())>::type SubIndexType; - std::get(ip) = std::shared_ptr( new SubIndexType( ind.template get() ) ); - PackNum::copy(ip, ind); - } - */ - template class IndexType, class... Indices> - static void copyInst(std::tuple...>& ip, - const IndexType& ind) - { - std::get(ip) = ind.template getPtr() ; - PackNum::copyInst(ip, ind); - } - - template - static size_t makePos(const std::tuple...>& iPtrTup) - { - auto ID = std::get(iPtrTup)->id(); - //VCHECK(std::get(iPtrTup)->pos()); - //VCHECK(std::get(iPtrTup)->max()); - return std::get(iPtrTup)->pos() + - PackNum::makePos(iPtrTup) * std::get(iPtrTup)->max(); - } - - template - static void swapIndices(Pack& ipack, const std::shared_ptr& nind, - const std::shared_ptr&... ninds) - { - std::get::value-N-1>(ipack) = nind; - PackNum::swapIndices(ipack, ninds...); - } - - template - static size_t blockSize(const std::tuple...>& pack) - { - return std::get(pack)->max() * PackNum::blockSize(pack); - } - - template - static BlockResult unpackArgs(const ArgTuple& tp, const Args&... args) - { - return PackNum::template unpackArgs(tp, std::get(tp).get(), args...); - } template static void makeBlockTypeVec(std::vector >& btv, @@ -240,190 +31,23 @@ namespace MultiArrayHelper PackNum::makeBlockTypeVec(btv, ops, idxPtr); } + template + static BlockResult unpackArgs(const ArgTuple& tp, const Args&... args) + { + return PackNum::template unpackArgs(tp, std::get(tp).get(), args...); + } + template static void printTuple(std::ostream& out, const std::tuple& tp){ out << std::get(tp) << ", "; PackNum::printTuple(out, tp); } - template - static void RangesToVec(const std::tuple...>& rst, - std::vector v) - { - v[N] = std::get(rst); - PackNum::RangesToVec(rst, v); - } - - template - static void printIndex(const std::tuple...>& ip, size_t offset) - { - std::get(ip)->print(offset); - PackNum::printIndex(ip, offset); - } - - template - static void checkDefaultable() - { - static_assert( Range::defaultable, "not defaultable" ); - PackNum::template checkDefaultable(); - } }; template<> struct PackNum<0> { - template - static std::shared_ptr getIndex(const IndexType& in, size_t n) - { - return make_viwb( in.template get<0>() ); - } - /* - template - static const IndexBase& getIndex(const MultiIndex& in, size_t n) - { - return in.template get<0>(); - } - */ - template - static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) - { - return make_viwb( in.template getPtr<0>() ); - } - /* - template - static void lock(std::tuple...>& ip, - std::shared_ptr toLock) - { - std::get<0>(ip)->lock(toLock); - } - */ - template - static void initBlockSizes(std::array& bs, - std::tuple...>& ip) - { - std::get<0>(bs) = PackNum::blockSize(ip); - } - - template - static inline void pp(std::tuple...>& ip) - { - auto& si = *std::get<0>(ip); - ++si; - } - - template - static inline int pp(std::tuple...>& ip, - std::array& bs, - std::intptr_t idxPtrNum) - { - auto& siPtr = std::get<0>(ip); - if(reinterpret_cast(siPtr.get()) == idxPtrNum){ - return std::get<0>(bs); - } - else { - int tmp = siPtr->pp(idxPtrNum); - return tmp * std::get<1>(bs); - } - } - - template - static inline void mm(std::tuple...>& ip) - { - auto& si = *std::get<0>(ip); - --si; - } - - template - static inline int mm(std::tuple...>& ip, - std::array& bs, - std::intptr_t idxPtrNum) - { - auto& siPtr = std::get<0>(ip); - if(reinterpret_cast(siPtr.get()) == idxPtrNum){ - return std::get<0>(bs); - //return 1; - } - else { - return siPtr->mm(idxPtrNum); - } - } - - template - static size_t getSize(const RangeTuple& rt) - { - return std::get<0>(rt)->size(); - } - - template - static void getMetaPos(MetaType& target, - const IndexPack& source) - { - std::get<0>(target) = std::get<0>(source)->meta(); - } - - template - static void setMeta(IndexPack& target, const MetaType& source) - { - std::get<0>(target)->at( std::get<0>( source ) ); - } - - template - static void setIndexPack(IndexPack& iPack, size_t pos) - { - auto& i = *std::get<0>(iPack); - const size_t ownPos = pos % i.max(); - i = ownPos; - } - - template - static void construct(std::tuple...>& ip, - const MRange& range) - { - typedef typename std::remove_reference())>::type SubRangeType; - typedef typename SubRangeType::IndexType SubIndexType; - typedef typename std::remove_reference(ip).get())>::type TypeFromIndexPack; - - static_assert(std::is_same::value, - "inconsiśtent types"); - - std::get<0>(ip) = std::shared_ptr( new SubIndexType( range.template getPtr<0>() ) ); - } - /* - template class IndexType, class... Indices> - static void copy(std::tuple...>& ip, - const IndexType& ind) - { - typedef typename std::remove_reference())>::type SubIndexType; - std::get<0>(ip) = std::shared_ptr( new SubIndexType( ind.template get<0>() ) ); - } - */ - template class IndexType, class... Indices> - static void copyInst(std::tuple...>& ip, - const IndexType& ind) - { - std::get<0>(ip) = ind.template getPtr<0>(); - } - - template - static size_t makePos(const std::tuple...>& 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 - static void swapIndices(Pack& ipack, const std::shared_ptr& nind) - { - std::get::value-1>(ipack) = nind; - } - - template - static size_t blockSize(const std::tuple...>& pack) - { - return std::get(pack)->max(); - } template static BlockResult unpackArgs(const ArgTuple& tp, const Args&... args) @@ -447,25 +71,6 @@ namespace MultiArrayHelper static void printTuple(std::ostream& out, const std::tuple& tp){ out << std::get(tp); } - - template - static void RangesToVec(const std::tuple...>& rst, - std::vector v) - { - v[0] = std::get<0>(rst); - } - - template - static void printIndex(const std::tuple...>& ip, size_t offset) - { - std::get<0>(ip)->print(offset); - } - - template - static void checkDefaultable() - { - static_assert( Range::defaultable, "not defaultable" ); - } }; diff --git a/src/range_factory.h b/src/range_factory.h deleted file mode 100644 index 99a4714..0000000 --- a/src/range_factory.h +++ /dev/null @@ -1,26 +0,0 @@ -// -*- C++ -*- - -#ifndef __range_factory_h__ -#define __range_factory_h__ - -#include -#include -#include "base_def.h" - -namespace MultiArrayTools -{ - - class RangeFactoryBase - { - public: - DEFAULT_MEMBERS(RangeFactoryBase); - virtual std::shared_ptr create() const = 0; - - }; - - -} //namespace MultiArrayTools - -// === NO TEMPLATE CODE HERE === - -#endif diff --git a/src/anonymous_range.h b/src/ranges/anonymous_range.h similarity index 64% rename from src/anonymous_range.h rename to src/ranges/anonymous_range.h index cbf6e82..110a369 100644 --- a/src/anonymous_range.h +++ b/src/ranges/anonymous_range.h @@ -4,14 +4,15 @@ #define __anonymous_range_h__ #include -#include "base_def.h" +//#include "base_def.h" +#include "ranges/range_base.h" +#include "ranges/single_range.h" namespace MultiArrayTools { - typedef SingleIndex AnonymousIndex; + typedef SingleIndex AnonymousIndex; - template class AnonymousRangeFactory : public RangeFactoryBase { public: @@ -20,16 +21,15 @@ namespace MultiArrayTools AnonymousRangeFactory() = delete; - template - AnonymousRangeFactory(const std::tuple...>& origs); + template + AnonymousRangeFactory(const std::tuple...>& origs); - template - AnonymousRangeFactory(std::shared_ptr... origs); + template + AnonymousRangeFactory(std::shared_ptr... origs); std::shared_ptr create(); }; - class AnonymousRange : public RangeInterface { @@ -53,11 +53,11 @@ namespace MultiArrayTools AnonymousRange() = delete; AnonymousRange(const AnonymousRange& in) = delete; - template - AnonymousRange(const std::tuple...>& origs) + template + AnonymousRange(const std::tuple...>& origs); - template - AnonymousRange(std::shared_ptr... origs); + template + AnonymousRange(std::shared_ptr... origs); size_t mSize; @@ -77,14 +77,14 @@ namespace MultiArrayTools * AnonymousRange * ***********************/ - template - AnonymousRangeFactory::AnonymousRangeFactory(const std::tuple...>& origs) + template + AnonymousRangeFactory::AnonymousRangeFactory(const std::tuple...>& origs) { mProd = std::shared_ptr( new AnonymousRange( origs ) ); } - template - AnonymousRangeFactory::AnonymousRangeFactory(std::shared_ptr... origs) + template + AnonymousRangeFactory::AnonymousRangeFactory(std::shared_ptr... origs) { mProd = std::shared_ptr( new AnonymousRange( origs... ) ); } @@ -99,23 +99,23 @@ namespace MultiArrayTools * AnonymousRange * ***********************/ - template - AnonymousRange::AnonymousRange(const std::tuple...>& origs) : + template + AnonymousRange::AnonymousRange(const std::tuple...>& origs) : RangeInterface() { - mOrig.resize(sizeof...(Ranges)); - PackNum::RangesToVec( origs, mOrig ); - PackNum::getSize( rst ); + mOrig.resize(sizeof...(RangeTypes)); + RPackNum::RangesToVec( origs, mOrig ); + RPackNum::getSize( origs ); } - template - AnonymousRange::AnonymousRange(std::shared_ptr... origs) : + template + AnonymousRange::AnonymousRange(std::shared_ptr... origs) : RangeInterface() { auto rst = std::make_tuple(origs...); - mOrig.resize(sizeof...(Ranges)); - PackNum::RangesToVec( rst, mOrig ); - PackNum::getSize( rst ); + mOrig.resize(sizeof...(RangeTypes)); + RPackNum::RangesToVec( rst, mOrig ); + RPackNum::getSize( rst ); } size_t AnonymousRange::get(size_t pos) const @@ -140,16 +140,16 @@ namespace MultiArrayTools typename AnonymousRange::IndexType AnonymousRange::begin() const { - SingleIndex i( std::dynamic_pointer_cast - ( std::shared_ptr( RB::mThis ) ) ); + AnonymousIndex i( std::dynamic_pointer_cast + ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } typename AnonymousRange::IndexType AnonymousRange::end() const { - SingleIndex i( std::dynamic_pointer_cast - ( std::shared_ptr( RB::mThis ) ) ); + AnonymousIndex i( std::dynamic_pointer_cast + ( std::shared_ptr( RB::mThis ) ) ); i = size(); return i; } @@ -157,8 +157,9 @@ namespace MultiArrayTools // put this in the interface class !!! std::shared_ptr AnonymousRange::index() const { - return std::make_shared - (std::make_shared + typedef IndexWrapper IW; + return std::make_shared + (std::make_shared ( std::dynamic_pointer_cast ( std::shared_ptr( RB::mThis ) ) ) ); } diff --git a/src/container_range.h b/src/ranges/container_range.h similarity index 89% rename from src/container_range.h rename to src/ranges/container_range.h index bdbfcd5..0f7ad5e 100644 --- a/src/container_range.h +++ b/src/ranges/container_range.h @@ -7,11 +7,11 @@ #include #include -#include "base_def.h" -#include "range_base.h" -#include "index_base.h" +//#include "base_def.h" +#include "ranges/range_base.h" +#include "ranges/index_base.h" -#include "pack_num.h" +#include "rpack_num.h" namespace MultiArrayTools { @@ -167,17 +167,17 @@ namespace MultiArrayTools ContainerIndex::ContainerIndex(const std::shared_ptr& range) : IndexInterface,std::tuple >(range, 0) { - PackNum::construct(mIPack, *range); - IB::mPos = PackNum::makePos(mIPack); + RPackNum::construct(mIPack, *range); + IB::mPos = RPackNum::makePos(mIPack); std::get(mBlockSizes) = 1; - PackNum::initBlockSizes(mBlockSizes, mIPack); + RPackNum::initBlockSizes(mBlockSizes, mIPack); } template ContainerIndex& ContainerIndex::sync() { if(mExternControl){ - IB::mPos = PackNum::makePos(mIPack); + IB::mPos = RPackNum::makePos(mIPack); //VCHECK(id()); //VCHECK(sizeof...(Indices)); //assert(IB::mPos < IB::max()); @@ -202,7 +202,7 @@ namespace MultiArrayTools template ContainerIndex& ContainerIndex::operator()(const std::shared_ptr&... inds) { - PackNum::swapIndices(mIPack, inds...); + RPackNum::swapIndices(mIPack, inds...); mExternControl = true; return sync(); } @@ -220,9 +220,9 @@ namespace MultiArrayTools ContainerIndex& ContainerIndex::operator++() { if(mExternControl){ - IB::mPos = PackNum::makePos(mIPack); + IB::mPos = RPackNum::makePos(mIPack); } - PackNum::pp( mIPack ); + RPackNum::pp( mIPack ); ++IB::mPos; return *this; } @@ -231,9 +231,9 @@ namespace MultiArrayTools ContainerIndex& ContainerIndex::operator--() { if(mExternControl){ - IB::mPos = PackNum::makePos(mIPack); + IB::mPos = RPackNum::makePos(mIPack); } - PackNum::mm( mIPack ); + RPackNum::mm( mIPack ); --IB::mPos; return *this; @@ -243,14 +243,14 @@ namespace MultiArrayTools ContainerIndex& ContainerIndex::operator=(size_t pos) { IB::mPos = pos; - PackNum::setIndexPack(mIPack, pos); + RPackNum::setIndexPack(mIPack, pos); return *this; } template int ContainerIndex::pp(std::intptr_t idxPtrNum) { - int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtrNum); + int tmp = RPackNum::pp(mIPack, mBlockSizes, idxPtrNum); IB::mPos += tmp; return tmp; } @@ -258,7 +258,7 @@ namespace MultiArrayTools template int ContainerIndex::mm(std::intptr_t idxPtrNum) { - int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtrNum); + int tmp = RPackNum::mm(mIPack, mBlockSizes, idxPtrNum); IB::mPos -= tmp; return tmp; } @@ -267,15 +267,15 @@ namespace MultiArrayTools typename ContainerIndex::MetaType ContainerIndex::meta() { MetaType metaTuple; - PackNum::getMetaPos(metaTuple, mIPack); + RPackNum::getMetaPos(metaTuple, mIPack); return metaTuple; } template ContainerIndex& ContainerIndex::at(const MetaType& metaPos) { - PackNum::setMeta(mIPack, metaPos); - IB::mPos = PackNum::makePos(mIPack); + RPackNum::setMeta(mIPack, metaPos); + IB::mPos = RPackNum::makePos(mIPack); return *this; } @@ -319,7 +319,7 @@ namespace MultiArrayTools // throw !! } ContainerIndex const* t = this; - return PackNum::getIndexPtr(*t, n); + return RPackNum::getIndexPtr(*t, n); } template @@ -347,7 +347,7 @@ namespace MultiArrayTools for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } std::cout << id() << "[" << reinterpret_cast(this) << "]" << "(" << IB::mRangePtr << "): " << meta() << std::endl; - PackNum::printIndex(mIPack, offset+1); + RPackNum::printIndex(mIPack, offset+1); } @@ -395,7 +395,7 @@ namespace MultiArrayTools template size_t ContainerRange::size() const { - return PackNum::getSize(mSpace); + return RPackNum::getSize(mSpace); } template diff --git a/src/index_base.h b/src/ranges/index_base.h similarity index 65% rename from src/index_base.h rename to src/ranges/index_base.h index b3c66b4..3cf3537 100644 --- a/src/index_base.h +++ b/src/ranges/index_base.h @@ -7,8 +7,10 @@ #include #include -#include "base_def.h" +#include "rbase_def.h" #include "range_base.h" +#include "index_type.h" +#include "vindex_wrapper.h" namespace MultiArrayTools { @@ -18,64 +20,6 @@ namespace MultiArrayTools ++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 rangePtr() const = 0; - virtual std::shared_ptr getPtr(size_t n) const = 0; - virtual std::intptr_t getPtrNum() const = 0; - virtual size_t getStepSize(size_t n) const = 0; - }; - - template - std::shared_ptr > make_viwb(std::shared_ptr idxPtr) - { - return std::make_shared >(idxPtr); - } - /* - template - std::shared_ptr > make_viwb(const I& idxPtr) - { - return make_viwb( std::make_shared(idxPtr) ); - }*/ - - template - class IndexWrapper : public VirtualIndexWrapperBase - { - public: - - DEFAULT_MEMBERS(IndexWrapper); - - IndexWrapper(std::shared_ptr 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 rangePtr() const override { return mIdxPtr->vrange(); } - virtual std::shared_ptr getPtr(size_t n) const override - { return mIdxPtr->getVPtr(n); } - virtual std::intptr_t getPtrNum() const override { return reinterpret_cast( mIdxPtr.get() ); } - virtual size_t getStepSize(size_t n) const override { return mIdxPtr->getStepSize(n); } - - std::shared_ptr get() const { return mIdxPtr; } // unwrap - - private: - std::shared_ptr mIdxPtr; - }; template class IndexInterface diff --git a/src/ranges/index_type.h b/src/ranges/index_type.h new file mode 100644 index 0000000..6d8c52a --- /dev/null +++ b/src/ranges/index_type.h @@ -0,0 +1,14 @@ + +#ifndef __index_type_h__ +#define __index_type_h__ + +namespace MultiArrayTools +{ + enum class IndexType{ + SINGLE = 0, + MULTI = 1, + CONT = 2 + }; +} + +#endif diff --git a/src/multi_range.h b/src/ranges/multi_range.h similarity index 87% rename from src/multi_range.h rename to src/ranges/multi_range.h index 34b5afb..ed376e3 100644 --- a/src/multi_range.h +++ b/src/ranges/multi_range.h @@ -7,11 +7,11 @@ #include #include -#include "base_def.h" -#include "range_base.h" -#include "index_base.h" +//#include "base_def.h" +#include "ranges/range_base.h" +#include "ranges/index_base.h" -#include "pack_num.h" +#include "rpack_num.h" namespace MultiArrayTools { @@ -187,24 +187,24 @@ namespace MultiArrayTools MultiIndex::MultiIndex(const MultiIndex& in) : IndexInterface >(in) { - PackNum::copy(mIPack, in); - IB::mPos = PackNum::makePos(mIPack); + RPackNum::copy(mIPack, in); + IB::mPos = RPackNum::makePos(mIPack); } template MultiIndex& MultiIndex::operator=(const MultiIndex& in) { IndexI::operator=(in); - PackNum::copy(mIPack, in); - IB::mPos = PackNum::makePos(mIPack); + RPackNum::copy(mIPack, in); + IB::mPos = RPackNum::makePos(mIPack); return *this; } */ template MultiIndex& MultiIndex::operator=(ContainerIndex& ci) { - PackNum::copyInst(mIPack, ci); - IB::mPos = PackNum::makePos(mIPack); + RPackNum::copyInst(mIPack, ci); + IB::mPos = RPackNum::makePos(mIPack); return *this; } @@ -213,10 +213,10 @@ namespace MultiArrayTools MultiIndex::MultiIndex(const std::shared_ptr& range) : IndexInterface,std::tuple >(range, 0) { - PackNum::construct(mIPack, *range); - IB::mPos = PackNum::makePos(mIPack); + RPackNum::construct(mIPack, *range); + IB::mPos = RPackNum::makePos(mIPack); std::get(mBlockSizes) = 1; - PackNum::initBlockSizes(mBlockSizes, mIPack); // has one more element! + RPackNum::initBlockSizes(mBlockSizes, mIPack); // has one more element! } template @@ -224,8 +224,8 @@ namespace MultiArrayTools MultiIndex& MultiIndex::up() { static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); - IB::mPos += PackNum::blockSize( mIPack ); - PackNum::pp( mIPack ); + IB::mPos += RPackNum::blockSize( mIPack ); + RPackNum::pp( mIPack ); return *this; } @@ -234,8 +234,8 @@ namespace MultiArrayTools MultiIndex& MultiIndex::down() { static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); - IB::mPos -= PackNum::blockSize( mIPack ); - PackNum::mm( mIPack ); + IB::mPos -= RPackNum::blockSize( mIPack ); + RPackNum::mm( mIPack ); return *this; } @@ -256,8 +256,8 @@ namespace MultiArrayTools template MultiIndex& MultiIndex::operator()(std::shared_ptr&... indices) { - PackNum::swapIndices(mIPack, indices...); - PackNum::setIndexPack(mIPack, IB::mPos); + RPackNum::swapIndices(mIPack, indices...); + RPackNum::setIndexPack(mIPack, IB::mPos); return *this; } @@ -271,14 +271,14 @@ namespace MultiArrayTools MultiIndex& MultiIndex::operator=(size_t pos) { IB::mPos = pos; - PackNum::setIndexPack(mIPack, pos); + RPackNum::setIndexPack(mIPack, pos); return *this; } template MultiIndex& MultiIndex::operator++() { - PackNum::pp( mIPack ); + RPackNum::pp( mIPack ); ++IB::mPos; return *this; } @@ -286,7 +286,7 @@ namespace MultiArrayTools template MultiIndex& MultiIndex::operator--() { - PackNum::mm( mIPack ); + RPackNum::mm( mIPack ); --IB::mPos; return *this; } @@ -294,7 +294,7 @@ namespace MultiArrayTools template int MultiIndex::pp(std::intptr_t idxPtrNum) { - int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtrNum); + int tmp = RPackNum::pp(mIPack, mBlockSizes, idxPtrNum); IB::mPos += tmp; return tmp; } @@ -302,7 +302,7 @@ namespace MultiArrayTools template int MultiIndex::mm(std::intptr_t idxPtrNum) { - int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtrNum); + int tmp = RPackNum::mm(mIPack, mBlockSizes, idxPtrNum); IB::mPos -= tmp; return tmp; } @@ -311,15 +311,15 @@ namespace MultiArrayTools typename MultiIndex::MetaType MultiIndex::meta() { MetaType metaTuple; - PackNum::getMetaPos(metaTuple, mIPack); + RPackNum::getMetaPos(metaTuple, mIPack); return metaTuple; } template MultiIndex& MultiIndex::at(const MetaType& metaPos) { - PackNum::setMeta(mIPack, metaPos); - IB::mPos = PackNum::makePos(mIPack); + RPackNum::setMeta(mIPack, metaPos); + IB::mPos = RPackNum::makePos(mIPack); return *this; } @@ -363,7 +363,7 @@ namespace MultiArrayTools // throw !! } MultiIndex const* t = this; - return PackNum::getIndexPtr(*t, n); + return RPackNum::getIndexPtr(*t, n); } template @@ -391,7 +391,7 @@ namespace MultiArrayTools for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; } std::cout << id() << "[" << reinterpret_cast(this) << "]" << "(" << IB::mRangePtr << "): " << meta() << std::endl; - PackNum::printIndex(mIPack, offset+1); + RPackNum::printIndex(mIPack, offset+1); } @@ -457,7 +457,7 @@ namespace MultiArrayTools template size_t MultiRange::size() const { - return PackNum::getSize(mSpace); + return RPackNum::getSize(mSpace); } template diff --git a/src/ranges/range_base.cc b/src/ranges/range_base.cc new file mode 100644 index 0000000..fc2b7fb --- /dev/null +++ b/src/ranges/range_base.cc @@ -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 == ∈ + } + + bool RangeBase::operator!=(const RangeBase& in) const + { + return this != ∈ + } + + +} // end namespace MultiArrayTools diff --git a/src/range_base.h b/src/ranges/range_base.h similarity index 71% rename from src/range_base.h rename to src/ranges/range_base.h index 364799d..4f13724 100644 --- a/src/range_base.h +++ b/src/ranges/range_base.h @@ -9,6 +9,13 @@ #include "base_def.h" +namespace MultiArrayTools +{ + class RangeBase; +} + +#include "vindex_base.h" + 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 == ∈ - } - - bool RangeBase::operator!=(const RangeBase& in) const - { - return this != ∈ - } - -} - #endif diff --git a/src/range_types/header.h b/src/ranges/range_types/header.h similarity index 100% rename from src/range_types/header.h rename to src/ranges/range_types/header.h diff --git a/src/range_types/spin_range.h b/src/ranges/range_types/spin_range.h similarity index 100% rename from src/range_types/spin_range.h rename to src/ranges/range_types/spin_range.h diff --git a/src/ranges/rbase_def.h b/src/ranges/rbase_def.h new file mode 100644 index 0000000..5521720 --- /dev/null +++ b/src/ranges/rbase_def.h @@ -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 RangeInterface; + + // index_base.h + class VirtualIndexWrapperBase; + typedef VirtualIndexWrapperBase VIWB; + + // index_base.h + template + class IndexWrapper; + + // index_base.h + template + class IndexInterface; + + // single_range.h + template + class SingleRange; + + // single_range.h + template + class SingleRangeFactory; + + // single_range.h + template + class SingleIndex; + + // multi_range.h + template + class MultiRangeFactory; + + // multi_range.h + template + class MultiRange; + + // multi_range.h + template + class MultiIndex; + + // container_range.h + template + class ContainerRangeFactory; + + // container_range.h + template + class ContainerRange; + + // container_range.h + template + class ContainerIndex; + + // anonymous_range.h + class AnonymousRangeFactory; + + // anonymous_range.h + class AnonymousRange; +} + +#endif diff --git a/src/ranges/rheader.h b/src/ranges/rheader.h new file mode 100644 index 0000000..cc0795f --- /dev/null +++ b/src/ranges/rheader.h @@ -0,0 +1,5 @@ + +#include "single_range.h" +#include "multi_range.h" +#include "container_range.h" +//#include "anonymous_range.h" have to fix bug !! diff --git a/src/ranges/rpack_num.h b/src/ranges/rpack_num.h new file mode 100644 index 0000000..974524f --- /dev/null +++ b/src/ranges/rpack_num.h @@ -0,0 +1,371 @@ + +#ifndef __pack_num_h__ +#define __pack_num_h__ + +#include +#include "vindex_wrapper.h" + +namespace MultiArrayHelper +{ + using namespace MultiArrayTools; + + template + struct RPackNum + { + template + static std::shared_ptr getIndex(const IndexType& in, size_t n) + { + if(n == N){ + return make_viwb( in.template get() ); + } + else { + return RPackNum::getIndex(in, n); + } + } + + template + static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) + { + if(n == N){ + return make_viwb( in.template getPtr() ); + } + else { + return RPackNum::getIndexPtr(in, n); + } + } + + template + static void initBlockSizes(std::array& bs, + std::tuple...>& ip) + { + std::get(bs) = RPackNum::blockSize(ip); + RPackNum::initBlockSizes(bs, ip); + } + + template + static inline void pp(std::tuple...>& ip) + { + auto& si = *std::get(ip); + if(si.last()){ + si = 0; + RPackNum::pp(ip); + } + else { + ++si; + } + } + + template + static inline int pp(std::tuple...>& ip, + std::array& bs, + std::intptr_t idxPtrNum) + { + auto& siPtr = std::get(ip); + //VCHECK(siPtr.id()); + if(reinterpret_cast(siPtr.get()) == idxPtrNum){ + return RPackNum::pp(ip, bs, idxPtrNum); + } + else { + int tmp = siPtr->pp(idxPtrNum); + if(siPtr->pos() == siPtr->max()){ + (*siPtr) = 0; + return RPackNum::pp(ip, bs, idxPtrNum) - siPtr->max() + 1; + } + else { + return tmp * std::get(bs); + } + } + } + + template + static inline void mm(std::tuple...>& ip) + { + auto& si = *std::get(ip); + if(si.first()){ + si = si.max() - 1; + RPackNum::mm(ip); + } + else { + --si; + } + } + + // !!!! + template + static inline int mm(std::tuple...>& ip, + std::array& bs, + std::intptr_t idxPtrNum) + { + auto& siPtr = std::get(ip); + if(reinterpret_cast(siPtr.get()) == idxPtrNum){ + return std::get(bs) + RPackNum::mm(ip, bs, idxPtrNum); + } + else { + if(siPtr->first()){ + (*siPtr) = siPtr->max() - 1; + return RPackNum::mm(ip, bs, idxPtrNum) - siPtr->max() + 1; + } + else { + return siPtr->mm(idxPtrNum); + } + } + } + + template + static size_t getSize(const RangeTuple& rt) + { + return std::get(rt)->size() * RPackNum::getSize(rt); + } + + template + static void getMetaPos(MetaType& target, + const IndexPack& source) + { + std::get(target) = std::get(source)->meta(); + RPackNum::getMetaPos(target, source); + } + + template + static void setMeta(IndexPack& target, const MetaType& source) + { + std::get(target)->at( std::get(source) ); + RPackNum::setMeta(target, source); + } + + template + static void setIndexPack(IndexPack& iPack, size_t pos) + { + auto& i = *std::get(iPack).get(); + const size_t ownPos = pos % i.max(); + i = ownPos; + RPackNum::setIndexPack(iPack, (pos - ownPos) / i.max() ); + } + + template + static void construct(std::tuple...>& ip, + const MRange& range) + { + typedef typename std::remove_reference())>::type SubRangeType; + typedef typename SubRangeType::IndexType SubIndexType; + typedef typename std::remove_reference(ip).get())>::type TypeFromIndexPack; + + static_assert(std::is_same::value, + "inconsiśtent types"); + + std::get(ip) = std::shared_ptr( new SubIndexType( range.template getPtr() ) ); + RPackNum::construct(ip, range); + } + + template class IndexType, class... Indices> + static void copyInst(std::tuple...>& ip, + const IndexType& ind) + { + std::get(ip) = ind.template getPtr() ; + RPackNum::copyInst(ip, ind); + } + + template + static size_t makePos(const std::tuple...>& iPtrTup) + { + auto ID = std::get(iPtrTup)->id(); + return std::get(iPtrTup)->pos() + + RPackNum::makePos(iPtrTup) * std::get(iPtrTup)->max(); + } + + template + static void swapIndices(Pack& ipack, const std::shared_ptr& nind, + const std::shared_ptr&... ninds) + { + std::get::value-N-1>(ipack) = nind; + RPackNum::swapIndices(ipack, ninds...); + } + + template + static size_t blockSize(const std::tuple...>& pack) + { + return std::get(pack)->max() * RPackNum::blockSize(pack); + } + + + template + static void RangesToVec(const std::tuple...>& rst, + std::vector v) + { + v[N] = std::get(rst); + RPackNum::RangesToVec(rst, v); + } + + template + static void printIndex(const std::tuple...>& ip, size_t offset) + { + std::get(ip)->print(offset); + RPackNum::printIndex(ip, offset); + } + + template + static void checkDefaultable() + { + static_assert( Range::defaultable, "not defaultable" ); + RPackNum::template checkDefaultable(); + } + }; + + + template<> + struct RPackNum<0> + { + template + static std::shared_ptr getIndex(const IndexType& in, size_t n) + { + return make_viwb( in.template get<0>() ); + } + + template + static std::shared_ptr getIndexPtr(const IndexType& in, size_t n) + { + return make_viwb( in.template getPtr<0>() ); + } + + template + static void initBlockSizes(std::array& bs, + std::tuple...>& ip) + { + std::get<0>(bs) = RPackNum::blockSize(ip); + } + + template + static inline void pp(std::tuple...>& ip) + { + auto& si = *std::get<0>(ip); + ++si; + } + + template + static inline int pp(std::tuple...>& ip, + std::array& bs, + std::intptr_t idxPtrNum) + { + auto& siPtr = std::get<0>(ip); + if(reinterpret_cast(siPtr.get()) == idxPtrNum){ + return std::get<0>(bs); + } + else { + int tmp = siPtr->pp(idxPtrNum); + return tmp * std::get<1>(bs); + } + } + + template + static inline void mm(std::tuple...>& ip) + { + auto& si = *std::get<0>(ip); + --si; + } + + template + static inline int mm(std::tuple...>& ip, + std::array& bs, + std::intptr_t idxPtrNum) + { + auto& siPtr = std::get<0>(ip); + if(reinterpret_cast(siPtr.get()) == idxPtrNum){ + return std::get<0>(bs); + //return 1; + } + else { + return siPtr->mm(idxPtrNum); + } + } + + template + static size_t getSize(const RangeTuple& rt) + { + return std::get<0>(rt)->size(); + } + + template + static void getMetaPos(MetaType& target, + const IndexPack& source) + { + std::get<0>(target) = std::get<0>(source)->meta(); + } + + template + static void setMeta(IndexPack& target, const MetaType& source) + { + std::get<0>(target)->at( std::get<0>( source ) ); + } + + template + static void setIndexPack(IndexPack& iPack, size_t pos) + { + auto& i = *std::get<0>(iPack); + const size_t ownPos = pos % i.max(); + i = ownPos; + } + + template + static void construct(std::tuple...>& ip, + const MRange& range) + { + typedef typename std::remove_reference())>::type SubRangeType; + typedef typename SubRangeType::IndexType SubIndexType; + typedef typename std::remove_reference(ip).get())>::type TypeFromIndexPack; + + static_assert(std::is_same::value, + "inconsiśtent types"); + + std::get<0>(ip) = std::shared_ptr( new SubIndexType( range.template getPtr<0>() ) ); + } + + template class IndexType, class... Indices> + static void copyInst(std::tuple...>& ip, + const IndexType& ind) + { + std::get<0>(ip) = ind.template getPtr<0>(); + } + + template + static size_t makePos(const std::tuple...>& iPtrTup) + { + auto ID = std::get<0>(iPtrTup)->id(); + return std::get<0>(iPtrTup)->pos(); + } + + template + static void swapIndices(Pack& ipack, const std::shared_ptr& nind) + { + std::get::value-1>(ipack) = nind; + } + + template + static size_t blockSize(const std::tuple...>& pack) + { + return std::get(pack)->max(); + } + + template + static void RangesToVec(const std::tuple...>& rst, + std::vector v) + { + v[0] = std::get<0>(rst); + } + + template + static void printIndex(const std::tuple...>& ip, size_t offset) + { + std::get<0>(ip)->print(offset); + } + + template + static void checkDefaultable() + { + static_assert( Range::defaultable, "not defaultable" ); + } + + }; + + +} // end namespace MultiArrayHelper + +#endif diff --git a/src/single_range.h b/src/ranges/single_range.h similarity index 98% rename from src/single_range.h rename to src/ranges/single_range.h index fd3e67d..4d51ebc 100644 --- a/src/single_range.h +++ b/src/ranges/single_range.h @@ -7,9 +7,9 @@ #include #include -#include "base_def.h" -#include "index_base.h" -#include "range_base.h" +//#include "base_def.h" +#include "ranges/index_base.h" +#include "ranges/range_base.h" namespace MultiArrayTools { diff --git a/src/index_unit_test.cc b/src/ranges/tests/index_unit_test.cc similarity index 98% rename from src/index_unit_test.cc rename to src/ranges/tests/index_unit_test.cc index a4c325d..dabb383 100644 --- a/src/index_unit_test.cc +++ b/src/ranges/tests/index_unit_test.cc @@ -4,7 +4,9 @@ #include "gtest/gtest.h" #include -#include "multi_array_header.h" +#include "ranges/rheader.h" + +//#include "multi_array_header.h" namespace MAT = MultiArrayTools; diff --git a/src/ranges/tests/index_unit_test.cc~ b/src/ranges/tests/index_unit_test.cc~ new file mode 100644 index 0000000..34a3a8b --- /dev/null +++ b/src/ranges/tests/index_unit_test.cc~ @@ -0,0 +1,216 @@ +// -*- C++ -*- + +#include +#include "gtest/gtest.h" +#include + +#include "rheader.h" + +//#include "multi_array_header.h" + +namespace MAT = MultiArrayTools; + +namespace { + + using namespace MAT; + + template + void swapFactory(std::shared_ptr& fptr, std::initializer_list ilist) + { + std::vector tmp = ilist; + auto nptr = std::make_shared( tmp ); + fptr = nptr; + } + + template + void swapMFactory(std::shared_ptr& fptr, const Rs&... rs) + { + auto nptr = std::make_shared( rs... ); + fptr = nptr; + } + + template + auto mkt(Ts&&... ts) -> decltype(std::make_tuple(ts...)) + { + return std::make_tuple(ts...); + } + + class IndexTest : public ::testing::Test + { + protected: + + typedef SingleRangeFactory SRF; + typedef SRF::oType SRange; + + typedef MultiRangeFactory M3RF; + typedef M3RF::oType M3Range; + + typedef MultiRangeFactory MasterRF; + typedef MasterRF::oType MasterRange; + + typedef ContainerRangeFactory CRF; + typedef CRF::oType CRange; + + IndexTest() + { + swapFactory(rfbptr, { 'e', 'b', 'n' } ); + sr1ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapFactory(rfbptr, { 'x', 'y', 'l', 'f' } ); + sr2ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapFactory(rfbptr, { 'a', 'b' } ); + std::shared_ptr temp1 = std::dynamic_pointer_cast( rfbptr->create() ); + swapFactory(rfbptr, { '1' } ); + std::shared_ptr temp2 = std::dynamic_pointer_cast( rfbptr->create() ); + swapFactory(rfbptr, { '0', '7' } ); + std::shared_ptr temp3 = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, temp1, temp2, temp3 ); + m3rptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, sr1ptr, m3rptr, sr2ptr); + mstrptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, m3rptr, sr2ptr); + cr1ptr = std::dynamic_pointer_cast( rfbptr->create() ); + + swapMFactory(rfbptr, m3rptr, sr1ptr); + cr2ptr = std::dynamic_pointer_cast( rfbptr->create() ); + } + + std::shared_ptr rfbptr; + std::shared_ptr sr1ptr; + std::shared_ptr sr2ptr; + std::shared_ptr m3rptr; + std::shared_ptr mstrptr; + std::shared_ptr cr1ptr; + std::shared_ptr 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(); +} diff --git a/src/ranges/vindex_base.h b/src/ranges/vindex_base.h new file mode 100644 index 0000000..088aa90 --- /dev/null +++ b/src/ranges/vindex_base.h @@ -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 rangePtr() const = 0; + virtual std::shared_ptr 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 diff --git a/src/ranges/vindex_wrapper.h b/src/ranges/vindex_wrapper.h new file mode 100644 index 0000000..705269f --- /dev/null +++ b/src/ranges/vindex_wrapper.h @@ -0,0 +1,43 @@ + +#ifndef __vindex_wrapper_h__ +#define __vindex_wrapper_h__ + +#include "ranges/vindex_base.h" + +namespace MultiArrayTools +{ + + template + std::shared_ptr > make_viwb(std::shared_ptr idxPtr) + { + return std::make_shared >(idxPtr); + } + + template + class IndexWrapper : public VirtualIndexWrapperBase + { + public: + + DEFAULT_MEMBERS(IndexWrapper); + + IndexWrapper(std::shared_ptr 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 rangePtr() const override { return mIdxPtr->vrange(); } + virtual std::shared_ptr getPtr(size_t n) const override + { return mIdxPtr->getVPtr(n); } + virtual std::intptr_t getPtrNum() const override { return reinterpret_cast( mIdxPtr.get() ); } + virtual size_t getStepSize(size_t n) const override { return mIdxPtr->getStepSize(n); } + + std::shared_ptr get() const { return mIdxPtr; } // unwrap + + private: + std::shared_ptr mIdxPtr; + }; + +} + +#endif diff --git a/src/ma_unit_test.cc b/src/tests/ma_unit_test.cc similarity index 100% rename from src/ma_unit_test.cc rename to src/tests/ma_unit_test.cc diff --git a/src/op_unit_test.cc b/src/tests/op_unit_test.cc similarity index 100% rename from src/op_unit_test.cc rename to src/tests/op_unit_test.cc