diff --git a/.gitignore b/.gitignore index 9800c39..14a35b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ */src/*~ */src/*# +build/ */build/ */install/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..526ed3e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 2.8) + +project(multi_array) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 --pedantic") + +find_package( GTest REQUIRED ) +if(GTest_FOUND) + include_directories(${GTEST_INCLUDE_DIRS}) +else() + message(FATAL_ERROR "GTest not found") +endif() + +include_directories(src) + +add_executable(utest src/unit_test.cc) +target_link_libraries(utest ${GTEST_BOTH_LIBRARIES}) +add_test(AllTests utest) + +#install(TARGETS testm DESTINATION install) + +set(CMAKE_INSTALL_PREFIX ..) diff --git a/src/index_base.cc b/src/index_base.cc index dcf6c42..fd884f5 100644 --- a/src/index_base.cc +++ b/src/index_base.cc @@ -17,7 +17,7 @@ namespace MultiArrayTools return mName; } - void name(const std::string& str) + void IndefinitIndexBase::name(const std::string& str) { mName = str; } @@ -86,64 +86,9 @@ namespace MultiArrayTools * IndexBase * **************/ - template - Index& IndexBase::operator=(const Index& in) - { - setPos( evaluate(in) ); - } - - template - Index& IndexBase::operator=(size_t pos) - { - setPos( pos ); - return *this; - } - - template - Index& IndexBase::operator++() - { - setPos( ++mPos ); - return *this; - } - - template - Index& IndexBase::operator--() - { - setPos( --mPos ); - return *this; - } - - template - Index& IndexBase::operator+=(int n) - { - setPos( mPos += n ); - return *this; - } - - template - Index& IndexBase::operator-=(int n) - { - setPos( mPos -= n ); - return *this; - } - - template - bool IndexBase::operator==(const Index& i) - { - return mRange == i.mRange and mPos == i.mPos; - } - - template - bool IndexBase::operator!=(const Index& i) - { - return mRange != i.mRange or mPos != i.mPos; - } - template size_t IndexBase::max() const { return mRange->size(); } - - } diff --git a/src/index_base.h b/src/index_base.h index 9e7143f..86ace64 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -17,16 +17,16 @@ namespace MultiArrayTools class IndefinitIndexBase { public: - virtual IndefinitIndexBase& operator=(const IndefinitIndexBase& in) = 0; virtual IndefinitIndexBase& operator=(size_t pos) = 0; virtual IndefinitIndexBase& operator++() = 0; virtual IndefinitIndexBase& operator--() = 0; virtual IndefinitIndexBase& operator+=(int n) = 0; virtual IndefinitIndexBase& operator-=(int n) = 0; - - virtual bool operator==(const IndefinitIndexBase& i) = 0; - virtual bool operator!=(const IndefinitIndexBase& i) = 0; + + // Make this somehow better... !!! + //virtual bool operator==(const IndefinitIndexBase& i) = 0; + //virtual bool operator!=(const IndefinitIndexBase& i) = 0; virtual size_t dim() const = 0; virtual size_t pos() const; // = mPos; implement !!! @@ -35,7 +35,7 @@ namespace MultiArrayTools virtual void name(const std::string& str); virtual void name(const Name& nm); - MultiRangeType rangeType() const = 0; + virtual MultiRangeType rangeType() const = 0; virtual bool link(IndefinitIndexBase* toLink); virtual void freeLinked(); @@ -61,18 +61,9 @@ namespace MultiArrayTools class IndexBase : public IndefinitIndexBase { public: - virtual Index& operator=(const Index& in) override; - virtual Index& operator=(size_t pos) override; - virtual Index& operator++() override; - virtual Index& operator--() override; - virtual Index& operator+=(int n) override; - virtual Index& operator-=(int n) override; - - virtual bool operator==(const IndexBase& i) override; - virtual bool operator!=(const IndexBase& i) override; - - virtual size_t pos() const override; // = mPos; implement !!! + //virtual size_t pos() const override; // = mPos; implement !!! + virtual size_t max() const override; protected: diff --git a/src/multi_array.cc b/src/multi_array.cc index c85e315..9e2c213 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -7,60 +7,56 @@ namespace MultiArrayTools * MultiArray * *******************/ - namespace - { - template - void giveNames(const std::string& name, /**/); - - template - void giveNames(const std::vector& names, /**/); - - } - /*!!!! giveNames(...) !!!!!*/ - template - MultiArray(const Range& range) : mRange(&range), mCont(mRange.size()) + MultiArray::MultiArray(const Range& range) : mRange(new Range(range)), mCont(mRange.size()) { mInit = true; } template - MultiArray(const Range& range, const std::vector& vec) : mRange(&range), mCont(vec) + MultiArray::MultiArray(const Range& range, const std::vector& vec) : mRange(new Range(range)), + mCont(vec) { mInit = true; - if(mCont.size() > mRange.size()){ - mCont.erase(mCont.begin() + mRange.size(), mCont.end()); + if(mCont.size() > mRange->size()){ + mCont.erase(mCont.begin() + mRange->size(), mCont.end()); } } template - MultiArray(const Range& range, std::vector&& vec) : mRange(&range), mCont(vec) + MultiArray::MultiArray(const Range& range, std::vector&& vec) : mRange(new Range(range)), mCont(vec) { mInit = true; - if(mCont.size() > mRange.size()){ - mCont.erase(mCont.begin() + mRange.size(), mCont.end()); + if(mCont.size() > mRange->size()){ + mCont.erase(mCont.begin() + mRange->size(), mCont.end()); } } template - T& MultiArray::operator()(const typename Range::indexType& i) + T& MultiArray::operator[](const typename Range::IndexType& i) { return mCont[ i.pos() ]; } template - const T& MultiArray::operator()(const typename Range::indexType& i) const + const T& MultiArray::operator[](const typename Range::IndexType& i) const { return mCont[ i.pos() ]; } template template - MultiArrayOperation& operator()(const NameTypes&... str) const + MultiArrayOperationBase MultiArray::operator()(const NameTypes&... str) { auto index = mRange->begin(); index.name(Name("master", str...)); - return MultiArrayOperation(*this, index); + return MultiArrayOperationBase(*this, index); } + template + size_t MultiArray::size() const + { + return mRange->size(); + } + } diff --git a/src/multi_array.h b/src/multi_array.h index 40b674b..ca60416 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -6,6 +6,7 @@ #include #include +#include #include "base_def.h" #include "multi_array_operation.h" @@ -25,10 +26,12 @@ namespace MultiArrayTools MultiArray(const Range& range, std::vector&& vec); template - MultiArrayOperation& operator()(const NameTypes&... str) const; + MultiArrayOperationBase operator()(const NameTypes&... str); - T& operator()(const typename Range::indexType& i); - const T& operator()(const typename Range::indexType& i) const; + T& operator[](const typename Range::IndexType& i); + const T& operator[](const typename Range::IndexType& i) const; + + size_t size() const; private: bool mInit = false; diff --git a/src/multi_array_header.h b/src/multi_array_header.h new file mode 100644 index 0000000..d1c1815 --- /dev/null +++ b/src/multi_array_header.h @@ -0,0 +1,14 @@ + +#ifndef __multi_array_header_h__ +#define __multi_array_header_h__ + +#include +#include "base_def.h" +#include "range_base.h" +#include "index_base.h" +#include "single_range.h" +#include "multi_range.h" +#include "multi_array_operation.h" +#include "multi_array.h" + +#endif diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index a05fc72..34cc536 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -1,5 +1,5 @@ -#include "mutli_array_operation.h" +#include "multi_array_operation.h" namespace MultiArrayTools { @@ -21,10 +21,10 @@ namespace MultiArrayTools template template - MultiArrayOperation + MultiArrayOperation MultiArrayOperationBase::operator()(Operation& op, MultiArrayOperationBase&... secs) { - return MultiArrayOperationBase(op, secs); + return MultiArrayOperation(op, secs...); } template @@ -48,44 +48,60 @@ namespace MultiArrayTools template T& MultiArrayOperationBase::get() { - return mArrayRef(*mIibPtr); + return mArrayRef[*mIibPtr]; } template const T& MultiArrayOperationBase::get() const { - return mArrayRef(*mIibPtr); + return mArrayRef[*mIibPtr]; } /***************************** * MultiArrayOperation * *****************************/ - template - void linkTupleIndicesTo(IndexTuple& itp, IndefinitIndexBase* target) + template + struct TupleIndicesLinker { - std::get(itp).linkTo(target); - linkTupleIndicesTo(itp, target); - } + template + static void linkTupleIndicesTo(IndexTuple& itp, IndefinitIndexBase* target) + { + std::get(itp).linkTo(target); + linkTupleIndicesTo(itp, target); + } + }; + + template <> + struct TupleIndicesLinker<0> + { + template + static void linkTupleIndicesTo(IndexTuple& itp, IndefinitIndexBase* target) + { + std::get<0>(itp).linkTo(target); + } + }; - template - void linkTupleIndicesTo<0>(IndexTuple& itp, IndefinitIndexBase* target) - { - std::get<0>(itp).linkTo(target); - } + template + struct OperationCall + { + template + auto callOperation(Operation& op, Tuple& tp, MBases&... secs) + -> decltype(callOperation(op, tp, std::get(tp), secs...)) + { + return callOperation(op, tp, std::get(tp), secs...); + } + }; - template - auto callOperation(Operation& op, Tuple& tp, MBases&... secs) - -> decltype(callOperation(op, tp, std::get(tp), secs...)) - { - return callOperation(op, tp, std::get(tp), secs...); - } - - template - auto callOperation<0>(Operation& op, Tuple& tp, MBases&... secs) -> decltype(op(secs.get()...)) - { - return op(secs.get()...); - } + template <> + struct OperationCall<0> + { + template + auto callOperation(Operation& op, Tuple& tp, MBases&... secs) -> decltype(op(secs.get()...)) + { + return op(secs.get()...); + } + }; template size_t MultiArrayOperation::argNum() const @@ -96,21 +112,21 @@ namespace MultiArrayTools template void MultiArrayOperation::linkIndicesTo(IndefinitIndexBase* target) { - mIibPtr->linkTo(target); - linkTupleIndicesTo(mSecs, target); + OB::mIibPtr->linkTo(target); + TupleIndicesLinker::linkTupleIndicesTo(mSecs, target); } template T& MultiArrayOperation::get() { - mVal = callOperation(mOp, mSecs); + mVal = OperationCall::callOperation(mOp, mSecs); return mVal; } template const T& MultiArrayOperation::get() const { - mVal = callOperation(mOp, mSecs); + mVal = OperationCall::callOperation(mOp, mSecs); return mVal; } diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index d188c9a..66b0a00 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -17,7 +17,7 @@ namespace MultiArrayTools { public: - MultiArrayOperation(MultiArray& ma, const IndefinitIndexBase& iib); + MultiArrayOperationBase(MultiArray& ma, const IndefinitIndexBase& iib); // execute AnyOperation // exception if range types are inconsitent with names @@ -26,19 +26,20 @@ namespace MultiArrayTools template - MultiArrayOperation operator()(Operation& op, MultiArrayOperationBase&... secs); + MultiArrayOperation + operator()(Operation& op, MultiArrayOperationBase&... secs); template - MultiArrayOperation,Range2> operator+(MultiArrayOperationBase& sec); + MultiArrayOperation,Range2> operator+(MultiArrayOperationBase& sec); template - MultiArrayOperation,Range2> operator-(MultiArrayOperationBase& sec); + MultiArrayOperation,Range2> operator-(MultiArrayOperationBase& sec); template - MultiArrayOperation,Range2> operator*(MultiArrayOperationBase& sec); + MultiArrayOperation,Range2> operator*(MultiArrayOperationBase& sec); template - MultiArrayOperation,Range2> operator/(MultiArrayOperationBase& sec); + MultiArrayOperation,Range2> operator/(MultiArrayOperationBase& sec); virtual size_t argNum() const; @@ -61,6 +62,8 @@ namespace MultiArrayTools { public: + typedef MultiArrayOperationBase OB; + MultiArrayOperation(Operation& op, MultiArrayOperationBase&... secs); virtual size_t argNum() const override; @@ -80,6 +83,6 @@ namespace MultiArrayTools } -#include "multi_array_operation.h" +#include "multi_array_operation.cc" #endif diff --git a/src/multi_range.cc b/src/multi_range.cc index 710425c..7115f0b 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -1,5 +1,5 @@ -#include "mutli_range.h" +#include "multi_range.h" namespace MultiArrayTools { @@ -9,36 +9,52 @@ namespace MultiArrayTools namespace { - template - IndefinitIndexBase& getIndex(MultiIndex& in, size_t n) - { - if(n == N){ - return in.getIndex(); + template + struct IndexGetter + { + template + static IndefinitIndexBase& getIndex(MultiIndex& in, size_t n) + { + if(n == N){ + return in.getIndex(); + } + else { + return getIndex(in, n); + } } - else { - return getIndex(in, n); - } - } - - template - IndefinitIndexBase& getIndex(MultiIndex& in, size_t n) - { - return in.getIndex<0>(); - } - - template - size_t evaluate_x(const MultiIndex& index) - { - const auto& subIndex = index.getIndex(); - return evaluate_x(index) * subIndex.size() + subIndex.pos(); - } + }; - template - size_t evaluate_x<0>(const MultiIndex& index) + template <> + struct IndexGetter<0> { - const auto& subIndex = index.getIndex<0>(); - return subIndex.pos(); - } + template + static IndefinitIndexBase& getIndex(MultiIndex& in, size_t n) + { + return in.getIndex<0>(); + } + }; + + template + struct Evaluation + { + template + static size_t evaluate(const MultiIndex& index) + { + const auto& subIndex = index.getIndex(0); + return Evaluation::evaluate(index) * subIndex.size() + subIndex.pos(); + } + }; + + template <> + struct Evaluation<0> + { + template + static size_t evaluate(const MultiIndex& index) + { + const auto& subIndex = index.getIndex<0>(0); + return subIndex.pos(); + } + }; template inline void plus(MultiIndex& index, size_t digit, int num) @@ -48,54 +64,63 @@ namespace MultiArrayTools size_t oor = si.outOfRange(); if(oor and digit != MultiIndex::mult - 1){ plus(index, digit + 1, 1); - plus(index, digit, oor - max()); + plus(index, digit, oor - si.max()); } } template - void nameTuple(IndexPack& iPack, Name& name) + struct TupleNamer { - std::get(iPack).name(name.get(N)); - nameTuple(iPack, name); - } - - template <> - void nameTuple<0>(IndexPack& iPack, Name& name) - { - std::get<0>(iPack).name(name.get(0)); - } + template + static void nameTuple(IndexPack& iPack, Name& name) + { + std::get(iPack).name(name.get(N)); + nameTuple(iPack, name); + } + }; + template <> + struct TupleNamer<0> + { + template + static void nameTuple(IndexPack& iPack, Name& name) + { + std::get<0>(iPack).name(name.get(0)); + } + }; } + template + MultiIndex::MultiIndex(Indices&&... inds) : mIPack(std::make_tuple(inds...)) {} template - MultiIndex& MultiIndex::operator++() + MultiIndex& MultiIndex::operator++() { - setPos( pos() + 1 ); + setPos( IIB::pos() + 1 ); plus(*this, 0, 1); return *this; } template - MultiIndex& MultiIndex::operator--() + MultiIndex& MultiIndex::operator--() { - setPos( pos() - 1 ); + setPos( IIB::pos() - 1 ); plus(*this, 0, -1); return *this; } template - MultiIndex& MultiIndex::operator+=(int n) + MultiIndex& MultiIndex::operator+=(int n) { - setPos( pos() + n ); + setPos( IIB::pos() + n ); plus(*this, 0, n); return *this; } template - MultiIndex& MultiIndex::operator-=(int n) + MultiIndex& MultiIndex::operator-=(int n) { - setPos( pos() - n ); + setPos( IIB::pos() - n ); plus(*this, 0, 0-n); return *this; } @@ -103,7 +128,7 @@ namespace MultiArrayTools template size_t MultiIndex::evaluate(const MultiIndex& in) const { - return evaluate_x(in); + return Evaluation::evaluate(in); } template @@ -111,12 +136,12 @@ namespace MultiArrayTools { name(nm.own()); if(nm.size() >= sizeof...(Indices)){ - nameTuple(mIPack, nm); + TupleNamer::nameTuple(mIPack, nm); } else { Name nm2 = nm; nm2.autoName(sizeof...(Indices)); - nameTuple(mIPack, nm); + TupleNamer::nameTuple(mIPack, nm); } } @@ -124,7 +149,7 @@ namespace MultiArrayTools size_t MultiIndex::dim() const { size_t res = 1; - for(size_t i = 0; i != sMult; ++i){ + for(size_t i = 0; i != sizeof...(Indices); ++i){ res *= getIndex(i).dim(); } return res; @@ -133,20 +158,20 @@ namespace MultiArrayTools template bool MultiIndex::link(IndefinitIndexBase* toLink) { - if(toLink->rangeType() != rangeType() and toLink->name() == name()){ + if(toLink->rangeType() != IIB::rangeType() and toLink->name() == name()){ // throw !! } - if(toLink->rangeType() == rangeType() and toLink->name() == name()){ - if(mLinked == toLink){ + if(toLink->rangeType() == IIB::rangeType() and toLink->name() == name()){ + if(IIB::mLinked == toLink){ return true; // dont link twice the same } - else if(mLinked == nullptr){ - mLinked = toLink; + else if(IIB::mLinked == nullptr){ + IIB::mLinked = toLink; return true; } else { - return mLinked->link(toLink); + return IIB::mLinked->link(toLink); } } else { @@ -154,43 +179,46 @@ namespace MultiArrayTools } } - template - auto& MultiIndex::getIndex() -> decltype(std::get(mIPack)) - { - return std::get(mIPack); - } - - template - const auto& MultiIndex::getIndex() const -> decltype(std::get(mIPack)); - { - return std::get(mIPack); - } - template - IndefinitIndexBase& MultiIndex::getIndex(size_t n) + template + auto MultiIndex::getIndex(size_t x) -> decltype(std::get(MultiIndex::IndexPack())) { - if(n >= sMult){ - // throw !! - } - MultiIndex* t = this; - return getIndex(*t, n); + return std::get(mIPack); } template - const IndefinitIndexBase& MultiIndex::getIndex(size_t n) const + template + auto MultiIndex::getIndex(size_t x) const -> + decltype(std::get(MultiIndex::IndexPack())) { - if(n >= sMult){ + return std::get(mIPack); + } + + template + IndefinitIndexBase& MultiIndex::get(size_t n) + { + if(n >= sizeof...(Indices)){ // throw !! } MultiIndex* t = this; - return getIndex(*t, n); + return IndexGetter::getIndex(*t, n); + } + + template + const IndefinitIndexBase& MultiIndex::get(size_t n) const + { + if(n >= sizeof...(Indices)){ + // throw !! + } + MultiIndex* t = this; + return IndexGetter::getIndex(*t, n); } template bool MultiIndex::linkLower(IndefinitIndexBase* toLink) { bool res = false; - for(size_t i = 0; i != sMult; ++i){ + for(size_t i = 0; i != sizeof...(Indices); ++i){ res |= getIndex(i).link(toLink); } return res; @@ -200,7 +228,7 @@ namespace MultiArrayTools void MultiIndex::linkTo(IndefinitIndexBase* target) { target->link(this); - for(size_t i = 0; i != sMult; ++i){ + for(size_t i = 0; i != sizeof...(Indices); ++i){ getIndex(i).linkTo(target); } } @@ -208,12 +236,30 @@ namespace MultiArrayTools /****************** * MultiRange * ******************/ - + + template template - auto MultiRange::get() -> decltype( std::get(mSpace) ) + auto MultiRange::get() -> decltype( std::get(MultiRange::SpaceType()) ) { return std::get(mSpace); } - - + + template + template + auto MultiRange::get() const -> decltype( std::get(MultiRange::SpaceType()) ) + { + return std::get(mSpace); + } + + template + MultiIndex MultiRange::begin() const + { + return MultiIndex(/*!!!!!!*/); + } + + template + MultiIndex MultiRange::end() const + { + return MultiIndex(/*!!!!!!*/); + } } diff --git a/src/multi_range.h b/src/multi_range.h index b722092..643cd59 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -19,9 +19,11 @@ namespace MultiArrayTools public: DEFAULT_MEMBERS(MultiIndex); + + MultiIndex(Indices&&... inds); typedef std::tuple IndexPack; - static size_t sMult = sizeof...(Indices); + typedef IndefinitIndexBase IIB; virtual MultiIndex& operator++() override; virtual MultiIndex& operator--() override; @@ -29,13 +31,13 @@ namespace MultiArrayTools virtual MultiIndex& operator-=(int n) override; template - auto& getIndex() -> decltype(std::get(mIPack)); + auto getIndex(size_t x) -> decltype(std::get(IndexPack())); template - const auto& getIndex() const -> decltype(std::get(mIPack)); + auto getIndex(size_t x) const -> decltype(std::get(IndexPack())); - IndefinitIndexBase& getIndex(size_t n); - const IndefinitIndexBase& getIndex(size_t n) const; + IndefinitIndexBase& get(size_t n); + const IndefinitIndexBase& get(size_t n) const; virtual void name(const Name& nm) override; @@ -54,18 +56,27 @@ namespace MultiArrayTools }; template - class MultiRange : public RangeBase > + class MultiRange : public RangeBase > { public: + typedef std::tuple SpaceType; + DEFAULT_MEMBERS(MultiRange); - static size_t dim = sizeof...(Ranges); + static const size_t dim = sizeof...(Ranges); template - auto get() -> decltype( std::get(mSpace) ); + auto get() -> decltype( std::get(SpaceType()) ); + + template + auto get() const -> decltype( std::get(SpaceType()) ); + + + virtual MultiIndex begin() const override; + virtual MultiIndex end() const override; protected: - std::tuple mSpace; + SpaceType mSpace; }; } diff --git a/src/name.cc b/src/name.cc index 2771986..3898263 100644 --- a/src/name.cc +++ b/src/name.cc @@ -6,6 +6,7 @@ namespace MultiArrayTools namespace { + /* template void giveNames(std::vector& nvec, const Name& name1, const Name& name2, const NameTypes&... names) { @@ -16,7 +17,7 @@ namespace MultiArrayTools void giveNames(std::vector& nvec, const Name& name) { nvec.push_back(name); - } + }*/ void giveNames(std::vector& nvec) { @@ -48,9 +49,14 @@ namespace MultiArrayTools mSub.resize(newSize); if(oldSize < newSize){ for(size_t i = oldSize; i != newSize; ++i){ - mSub[i] = mMain + to_string( i ); + mSub[i] = mMain + std::to_string( i ); } } } + + size_t Name::size() const + { + return mSub.size(); + } } diff --git a/src/name.h b/src/name.h index ff28e7c..853d8d9 100644 --- a/src/name.h +++ b/src/name.h @@ -23,6 +23,7 @@ namespace MultiArrayTools const Name& get(size_t n) const; void autoName(size_t newSize); + size_t size() const; private: std::string mMain; @@ -31,4 +32,6 @@ namespace MultiArrayTools } +#include "name.cc" + #endif diff --git a/src/range_base.cc b/src/range_base.cc index 9bbecc0..83d8b12 100644 --- a/src/range_base.cc +++ b/src/range_base.cc @@ -7,7 +7,19 @@ namespace MultiArrayTools * MultiRangeType * *********************/ - MultiRangeType& MultiRangeType::operator=(RangeType& type) + MultiRangeType::MultiRangeType(const RangeType& type) : mType(type), mMultiType(nullptr) {} + + MultiRangeType::MultiRangeType(const std::vector& multiType) : + mType(RangeType::NIL), + mMultiType(new std::vector(multiType)) {} + + MultiRangeType::~MultiRangeType() + { + delete mMultiType; + } + + + MultiRangeType& MultiRangeType::operator=(const RangeType& type) { setType(type); return *this; @@ -31,7 +43,7 @@ namespace MultiArrayTools bool MultiRangeType::multi() const { - return mType != nullptr; + return mMultiType != nullptr; } bool MultiRangeType::operator==(const MultiRangeType& in) const @@ -79,6 +91,12 @@ namespace MultiArrayTools return false; } + template + RangeBase* RangeBase::base() + { + return nullptr; + } + /********************* * SubRangeBase * *********************/ diff --git a/src/range_base.h b/src/range_base.h index b92bdff..db084a5 100644 --- a/src/range_base.h +++ b/src/range_base.h @@ -5,6 +5,8 @@ #include #include +#include + #include "base_def.h" namespace MultiArrayTools @@ -13,10 +15,11 @@ namespace MultiArrayTools enum class RangeType { NIL = 0, - SPACE = 1, - MOMENTUM = 2, - LORENTZ = 3, - SPIN = 4 + ANY = 1, + SPACE = 2, + MOMENTUM = 3, + LORENTZ = 4, + SPIN = 5 }; class MultiRangeType @@ -24,8 +27,13 @@ namespace MultiArrayTools public: DEFAULT_MEMBERS(MultiRangeType); + + MultiRangeType(const RangeType& type); + MultiRangeType(const std::vector& multiType); + + ~MultiRangeType(); - MultiRangeType& operator=(RangeType& type); + MultiRangeType& operator=(const RangeType& type); MultiRangeType& operator=(const std::vector& multiType); MultiRangeType& operator[](size_t num); @@ -51,21 +59,21 @@ namespace MultiArrayTools typedef Index IndexType; virtual size_t size() const = 0; - virtual Index begin() = 0; - virtual Index end() = 0; - virtual RangeBase* base() = 0; + virtual Index begin() const = 0; + virtual Index end() const = 0; + virtual RangeBase* base(); virtual bool isSubRange() const; protected: DEFAULT_MEMBERS(RangeBase); }; - - template - auto cross(const Range& r1, const Range& r2) -> /**/; + + //template + //auto cross(const Range& r1, const Range& r2) -> /**/; - template - auto cross(const Range1& r1, const Range2& r2) -> /**/; + //template + //auto cross(const Range1& r1, const Range2& r2) -> /**/; template class SubRangeBase : public RangeBase diff --git a/src/single_range.cc b/src/single_range.cc index 03a4d34..c2236b3 100644 --- a/src/single_range.cc +++ b/src/single_range.cc @@ -6,7 +6,11 @@ namespace MultiArrayTools /******************** * SingleRange * ********************/ - + + template + SingleRange::SingleRange(const std::vector& space) : RangeBase >(), + mSpace(space) {} + template const U& SingleRange::get(size_t pos) const { @@ -26,44 +30,122 @@ namespace MultiArrayTools return cnt; } - template + template size_t SingleRange::size() const { return mSpace.size(); } + + template + SingleIndex SingleRange::begin() const + { + return SingleIndex(0); + } + + template + SingleIndex SingleRange::end() const + { + return SingleIndex(mSpace.size()); + } /****************** * SingleIndex * ******************/ - - template - const U& SingleIndexBase::getMetaPos() const + + template + SingleIndex::SingleIndex(const U& upos, size_t disambig) : IndexBase >() { - return dynamic_cast( mRange )->get(mPos); + IIB::setPos( dynamic_cast*>( IB::mRange )->get(upos) ); } - template - size_t SingleIndexBase::dim() const + template + SingleIndex::SingleIndex(size_t pos) : IndexBase >() + { + IIB::setPos( pos ); + } + + template + SingleIndex& SingleIndex::operator=(size_t pos) + { + IIB::setPos( pos ); + return *this; + } + + template + SingleIndex& SingleIndex::operator++() + { + IIB::setPos( IIB::pos() + 1 ); + return *this; + } + + template + SingleIndex& SingleIndex::operator--() + { + IIB::setPos( IIB::pos() - 1 ); + return *this; + } + + template + SingleIndex& SingleIndex::operator+=(int n) + { + IIB::setPos( IIB::pos() + n ); + return *this; + } + + template + SingleIndex& SingleIndex::operator-=(int n) + { + IIB::setPos( IIB::pos() - n ); + return *this; + } + + template + bool SingleIndex::operator==(const SingleIndex& i) + { + return IB::mRange == i.mRange and IIB::pos() == i.mPos; + } + + template + bool SingleIndex::operator!=(const SingleIndex& i) + { + return IB::mRange != i.mRange or IIB::pos() != i.mPos; + } + + template + MultiRangeType SingleIndex::rangeType() const + { + return MultiRangeType(TYPE); + } + + template + const U& SingleIndex::getMetaPos() const + { + return dynamic_cast*>( IB::mRange )->get(IIB::pos()); + } + + template + size_t SingleIndex::dim() const { return 1; } - template - size_t SingleIndexBase::evaluate(const Index& in) + template + size_t SingleIndex::evaluate(const SingleIndex& in) const { - return in.mPos; + return in.pos(); } - template - void SingleIndexBase::linkTo(IndefinitIndexBase* target) + template + void SingleIndex::linkTo(IndefinitIndexBase* target) { target->link(this); } - template - SingleIndex& SingleIndexBase::operator=(const U& upos) + template + SingleIndex& SingleIndex::operator=(const U& upos) { - setPos( dynamic_cast( mRange )->get(upos) ); + IIB::setPos( dynamic_cast*>( IB::mRange )->get(upos) ); + return *this; } } diff --git a/src/single_range.h b/src/single_range.h index de6dbba..96ba2f0 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -18,16 +18,35 @@ namespace MultiArrayTools { public: + typedef IndexBase > IB; + typedef IndefinitIndexBase IIB; + DEFAULT_MEMBERS(SingleIndex); + // find better solution !!! + SingleIndex(const U& upos, size_t disambig); + SingleIndex(size_t pos); + virtual SingleIndex& operator=(const U& upos); + + virtual SingleIndex& operator=(size_t pos) override; + virtual SingleIndex& operator++() override; + virtual SingleIndex& operator--() override; + virtual SingleIndex& operator+=(int n) override; + virtual SingleIndex& operator-=(int n) override; + + virtual bool operator==(const SingleIndex& i); + virtual bool operator!=(const SingleIndex& i); + + virtual MultiRangeType rangeType() const override; + virtual const U& getMetaPos() const; virtual size_t dim() const override; // = 1 virtual void linkTo(IndefinitIndexBase* target) override; protected: - virtual size_t evaluate(const Index& in) const override; + virtual size_t evaluate(const SingleIndex& in) const override; }; template @@ -36,10 +55,15 @@ namespace MultiArrayTools public: DEFAULT_MEMBERS(SingleRange); + SingleRange(const std::vector& space); + virtual size_t size() const override; const U& get(size_t pos) const; size_t get(const U& metaPos) const; + + SingleIndex begin() const override; + SingleIndex end() const override; protected: std::vector mSpace; diff --git a/src/unit_test.cc b/src/unit_test.cc new file mode 100644 index 0000000..527ed5e --- /dev/null +++ b/src/unit_test.cc @@ -0,0 +1,49 @@ +// -*- C++ -*- + +#include +#include "gtest/gtest.h" +#include + +#include "multi_array_header.h" + +namespace MAT = MultiArrayTools; + +namespace { + + class OneDimTest : public ::testing::Test + { + protected: + + typedef MAT::SingleRange Range1dAny; + typedef MAT::MultiArray MultiArray1dAny; + + OneDimTest() : r({'a','b','c','d','e'}), ma(r, {-5,6,2,1,9}) {} + + //virtual void SetUp(); + + Range1dAny r; + MultiArray1dAny ma; + + }; + + TEST_F(OneDimTest, CorrectExtensions) + { + EXPECT_EQ(ma.size(), 5); + } + + TEST_F(OneDimTest, CorrectAssigned) + { + EXPECT_EQ(ma[0], -5); + EXPECT_EQ(ma[1], 6); + EXPECT_EQ(ma[2], 2); + EXPECT_EQ(ma[3], 1); + EXPECT_EQ(ma[4], 9); + } + +} // end namespace + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}