diff --git a/src/anonymous_range.cc b/src/anonymous_range.cc deleted file mode 100644 index 9363cdc..0000000 --- a/src/anonymous_range.cc +++ /dev/null @@ -1,13 +0,0 @@ -// -*- C++ -*- - -#include "anonymous_range.h" - -namespace MultiArrayTools -{ - - - - - - -} diff --git a/src/anonymous_range.h b/src/anonymous_range.h index efe21c2..19bcba0 100644 --- a/src/anonymous_range.h +++ b/src/anonymous_range.h @@ -18,6 +18,10 @@ namespace MultiArrayTools } -#include "anonymous_range.cc" +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +// ... #endif diff --git a/src/block.cc b/src/block.cc deleted file mode 100644 index a351545..0000000 --- a/src/block.cc +++ /dev/null @@ -1,216 +0,0 @@ -// -*- C++ -*- - -#include "block.h" - -namespace MultiArrayHelper -{ - - /********************* - * BlockBinaryOp * - *********************/ - - template - BlockResult - BlockBinaryOp::operator()(const BlockClass1& arg1, - const BlockClass2& arg2) - { - static OpFunc f; - BlockResult res(arg1.size()); - assert(arg1.size() == arg2.size()); - for(size_t i = 0; i != arg1.size(); ++i){ - res[i] = f(arg1[i], arg2[i]); - } - return res; - } - - template - BlockBinaryOpSelf::BlockBinaryOpSelf(BlockResult& res) : mRes(res) {} - - template - void BlockBinaryOpSelf::operator()(const BlockClass& arg) - { - static OpFunc f; - if(mRes.size() == 0) { mRes.assign(arg.size(), static_cast(0)); } - assert(mRes.size() == arg.size()); - for(size_t i = 0; i != arg.size(); ++i){ - mRes[i] = f(mRes[i], arg[i]); - } - } - - - /***************** - * BlockBase * - *****************/ - - template - BlockBase::BlockBase(size_t size) : mSize(size) {} - - template - size_t BlockBase::size() const - { - return mSize; - } - - /************************ - * MutableBlockBase * - ************************/ - - template - MutableBlockBase::MutableBlockBase(size_t size) : BlockBase(size) {} - - - /************* - * Block * - *************/ - - template - Block::Block(const std::vector& data, - size_t begPos, size_t size, size_t stepSize) : - BlockBase(size), - mData(&data), - mBegPtr(data.data() + begPos), - mStepSize(stepSize) {} - - template - BlockType Block::type() const - { - return mStepSize == 0 ? BlockType::VALUE : - ( mStepSize == 1 ? BlockType::BLOCK : BlockType::SPLIT ); - } - - template - const T& Block::operator[](size_t i) const - { - - return *(mBegPtr + i * mStepSize); - } - - template - Block& Block::set(size_t npos) - { - mBegPtr = &(*mData)[npos]; - return *this; - } - - template - size_t Block::stepSize() const - { - return 1; - } - - /************** - * MBlock * - **************/ - - template - MBlock::MBlock(std::vector& data, - size_t begPos, size_t size, size_t stepSize) : - MutableBlockBase(size), - mData(&data), - mBegPtr(data.data() + begPos), - mStepSize(stepSize) {} - - template - template - MBlock& MBlock::operator=(const BlockClass& in) - { - for(size_t i = 0; i != BlockBase::mSize; ++i){ - (*this)[i] = in[i]; - } - return *this; - } - - template - BlockType MBlock::type() const - { - return mStepSize == 0 ? BlockType::VALUE : - ( mStepSize == 1 ? BlockType::BLOCK : BlockType::SPLIT ); - } - - template - const T& MBlock::operator[](size_t i) const - { - - return *(mBegPtr + i * mStepSize); - } - - template - T& MBlock::operator[](size_t i) - { - - return *(mBegPtr + i * mStepSize); - } - - template - MBlock& MBlock::set(size_t npos) - { - mBegPtr = &(*mData)[npos]; - return *this; - } - - template - size_t MBlock::stepSize() const - { - return 1; - } - - /******************* - * BlockResult * - *******************/ - - template - BlockResult::BlockResult(size_t size) : - MutableBlockBase(size), - mRes(size) {} - - template - template - BlockResult& BlockResult::operator=(const BlockClass& in) - { - //CHECK; - for(size_t i = 0; i != BlockBase::mSize; ++i){ - (*this)[i] = in[i]; - } - return *this; - } - - template - BlockType BlockResult::type() const - { - return BlockType::RESULT; - } - - template - const T& BlockResult::operator[](size_t i) const - { - return mRes[i]; - } - - template - T& BlockResult::operator[](size_t i) - { - - return mRes[i]; - } - - template - BlockResult& BlockResult::set(size_t npos) - { - return *this; - } - - template - size_t BlockResult::stepSize() const - { - return 1; - } - - template - BlockResult& BlockResult::assign(size_t size, const T& val) - { - BB::mSize = size; - mRes.assign(BB::mSize, val); - return *this; - } - -} // end namespace MultiArrayHelper diff --git a/src/block.h b/src/block.h index e007350..cf19632 100644 --- a/src/block.h +++ b/src/block.h @@ -153,6 +153,221 @@ namespace MultiArrayHelper } // end namespace MultiArrayHelper -#include "block.cc" +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayHelper +{ + + /********************* + * BlockBinaryOp * + *********************/ + + template + BlockResult + BlockBinaryOp::operator()(const BlockClass1& arg1, + const BlockClass2& arg2) + { + static OpFunc f; + BlockResult res(arg1.size()); + assert(arg1.size() == arg2.size()); + for(size_t i = 0; i != arg1.size(); ++i){ + res[i] = f(arg1[i], arg2[i]); + } + return res; + } + + template + BlockBinaryOpSelf::BlockBinaryOpSelf(BlockResult& res) : mRes(res) {} + + template + void BlockBinaryOpSelf::operator()(const BlockClass& arg) + { + static OpFunc f; + if(mRes.size() == 0) { mRes.assign(arg.size(), static_cast(0)); } + assert(mRes.size() == arg.size()); + for(size_t i = 0; i != arg.size(); ++i){ + mRes[i] = f(mRes[i], arg[i]); + } + } + + + /***************** + * BlockBase * + *****************/ + + template + BlockBase::BlockBase(size_t size) : mSize(size) {} + + template + size_t BlockBase::size() const + { + return mSize; + } + + /************************ + * MutableBlockBase * + ************************/ + + template + MutableBlockBase::MutableBlockBase(size_t size) : BlockBase(size) {} + + + /************* + * Block * + *************/ + + template + Block::Block(const std::vector& data, + size_t begPos, size_t size, size_t stepSize) : + BlockBase(size), + mData(&data), + mBegPtr(data.data() + begPos), + mStepSize(stepSize) {} + + template + BlockType Block::type() const + { + return mStepSize == 0 ? BlockType::VALUE : + ( mStepSize == 1 ? BlockType::BLOCK : BlockType::SPLIT ); + } + + template + const T& Block::operator[](size_t i) const + { + + return *(mBegPtr + i * mStepSize); + } + + template + Block& Block::set(size_t npos) + { + mBegPtr = &(*mData)[npos]; + return *this; + } + + template + size_t Block::stepSize() const + { + return 1; + } + + /************** + * MBlock * + **************/ + + template + MBlock::MBlock(std::vector& data, + size_t begPos, size_t size, size_t stepSize) : + MutableBlockBase(size), + mData(&data), + mBegPtr(data.data() + begPos), + mStepSize(stepSize) {} + + template + template + MBlock& MBlock::operator=(const BlockClass& in) + { + for(size_t i = 0; i != BlockBase::mSize; ++i){ + (*this)[i] = in[i]; + } + return *this; + } + + template + BlockType MBlock::type() const + { + return mStepSize == 0 ? BlockType::VALUE : + ( mStepSize == 1 ? BlockType::BLOCK : BlockType::SPLIT ); + } + + template + const T& MBlock::operator[](size_t i) const + { + + return *(mBegPtr + i * mStepSize); + } + + template + T& MBlock::operator[](size_t i) + { + + return *(mBegPtr + i * mStepSize); + } + + template + MBlock& MBlock::set(size_t npos) + { + mBegPtr = &(*mData)[npos]; + return *this; + } + + template + size_t MBlock::stepSize() const + { + return 1; + } + + /******************* + * BlockResult * + *******************/ + + template + BlockResult::BlockResult(size_t size) : + MutableBlockBase(size), + mRes(size) {} + + template + template + BlockResult& BlockResult::operator=(const BlockClass& in) + { + //CHECK; + for(size_t i = 0; i != BlockBase::mSize; ++i){ + (*this)[i] = in[i]; + } + return *this; + } + + template + BlockType BlockResult::type() const + { + return BlockType::RESULT; + } + + template + const T& BlockResult::operator[](size_t i) const + { + return mRes[i]; + } + + template + T& BlockResult::operator[](size_t i) + { + + return mRes[i]; + } + + template + BlockResult& BlockResult::set(size_t npos) + { + return *this; + } + + template + size_t BlockResult::stepSize() const + { + return 1; + } + + template + BlockResult& BlockResult::assign(size_t size, const T& val) + { + BB::mSize = size; + mRes.assign(BB::mSize, val); + return *this; + } + +} // end namespace MultiArrayHelper #endif diff --git a/src/container_range.cc b/src/container_range.cc deleted file mode 100644 index c0fbdd6..0000000 --- a/src/container_range.cc +++ /dev/null @@ -1,296 +0,0 @@ -// -*- C++ -*- - -#include "container_range.h" -#include "pack_num.h" - -namespace MultiArrayTools -{ - namespace - { - using namespace MultiArrayHelper; - } - - - /********************** - * ContainerIndex * - **********************/ - /* - template - ContainerIndex::ContainerIndex(const ContainerIndex& in) : - IndexInterface >(in) - { - PackNum::copy(mIPack, in); - IB::mPos = PackNum::makePos(mIPack); - } - - template - ContainerIndex& ContainerIndex::operator=(const ContainerIndex& in) - { - IndexI::operator=(in); - PackNum::copy(mIPack, in); - IB::mPos = PackNum::makePos(mIPack); - return *this; - } - */ - template - template - ContainerIndex::ContainerIndex(const std::shared_ptr& range) : - IndexInterface >(range, 0) - { - PackNum::construct(mIPack, *range); - IB::mPos = PackNum::makePos(mIPack); - std::get(mBlockSizes) = 1; - PackNum::initBlockSizes(mBlockSizes, mIPack); - } - - template - IndexType ContainerIndex::type() const - { - return IndexType::CONT; - } - - template - ContainerIndex& ContainerIndex::operator++() - { - if(mExternControl){ - IB::mPos = PackNum::makePos(mIPack); - } - PackNum::pp( mIPack ); - ++IB::mPos; - return *this; - } - - template - ContainerIndex& ContainerIndex::operator--() - { - if(mExternControl){ - IB::mPos = PackNum::makePos(mIPack); - } - PackNum::mm( mIPack ); - --IB::mPos; - return *this; - - } - - template - ContainerIndex& ContainerIndex::operator=(size_t pos) - { - IB::mPos = pos; - PackNum::setIndexPack(mIPack, pos); - return *this; - } - - template - int ContainerIndex::pp(std::shared_ptr& idxPtr) - { - int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtr); - IB::mPos += tmp; - return tmp; - } - - template - int ContainerIndex::mm(std::shared_ptr& idxPtr) - { - int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtr); - IB::mPos -= tmp; - return tmp; - } - - template - typename ContainerIndex::MetaType ContainerIndex::meta() const - { - MetaType metaTuple; - PackNum::getMetaPos(metaTuple, mIPack); - return metaTuple; - } - - template - ContainerIndex& ContainerIndex::at(const MetaType& metaPos) - { - PackNum::setMeta(mIPack, metaPos); - IB::mPos = PackNum::makePos(mIPack); - return *this; - } - - template - size_t ContainerIndex::dim() const - { - return sizeof...(Indices); - } - - template - ContainerIndex& ContainerIndex::sync() - { - if(mExternControl){ - IB::mPos = PackNum::makePos(mIPack); - //VCHECK(id()); - //VCHECK(sizeof...(Indices)); - //assert(IB::mPos < IB::max()); - } - return *this; - } - - template - template - auto ContainerIndex::get() const -> decltype( *std::get( mIPack ) )& - { - return *std::get( mIPack ); - } - - template - template - auto ContainerIndex::getPtr() const -> decltype( std::get( mIPack ) )& - { - return std::get( mIPack ); - } - - template - std::shared_ptr ContainerIndex::getPtr(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - ContainerIndex const* t = this; - return PackNum::getIndexPtr(*t, n); - } - - - template - size_t ContainerIndex::getStepSize(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - return mBlockSizes[n+1]; - } - - template - bool ContainerIndex::first() const - { - return IB::pos() == 0; - } - - template - bool ContainerIndex::last() const - { - return IB::pos() == IB::mRangePtr->size() - 1; - } - - template - ContainerIndex& ContainerIndex::operator()(const std::shared_ptr&... inds) - { - PackNum::swapIndices(mIPack, inds...); - mExternControl = true; - return sync(); - } - - template - ContainerIndex& ContainerIndex::operator()() - { - return sync(); - } - - template - std::shared_ptr::RangeType> ContainerIndex::range() const - { - return std::dynamic_pointer_cast( IB::mRangePtr ); - } - - /***************************** - * ContainerRangeFactory * - *****************************/ - - template - ContainerRangeFactory::ContainerRangeFactory(const std::shared_ptr&... rs) - { - mProd = std::shared_ptr >( new ContainerRange( rs... ) ); - } - - template - ContainerRangeFactory:: - ContainerRangeFactory(const typename ContainerRange::SpaceType& space) - { - mProd = std::shared_ptr >( new ContainerRange( space ) ); - } - - template - std::shared_ptr ContainerRangeFactory::create() - { - setSelf(); - return mProd; - } - - /********************** - * ContainerRange * - **********************/ - - template - ContainerRange::ContainerRange(const std::shared_ptr&... rs) : - mSpace( std::make_tuple( rs... ) ) {} - - template - ContainerRange::ContainerRange(const SpaceType& space) : mSpace( space ) {} - - template - size_t ContainerRange::dim() const - { - return sizeof...(Ranges); - } - - template - size_t ContainerRange::size() const - { - return PackNum::getSize(mSpace); - } - - template - template - auto ContainerRange::get() const -> decltype( *std::get( mSpace ) )& - { - return *std::get( mSpace ); - } - - template - template - auto ContainerRange::getPtr() const -> decltype( std::get( mSpace ) )& - { - return std::get( mSpace ); - } - - template - const typename ContainerRange::SpaceType& ContainerRange::space() const - { - return mSpace; - } - - template - typename ContainerRange::IndexType ContainerRange::begin() const - { - ContainerIndex - i( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - i = 0; - return i; - } - - template - typename ContainerRange::IndexType ContainerRange::end() const - { - ContainerIndex - i( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - i = size(); - return i; - } - - template - std::shared_ptr ContainerRange::index() const - { - return std::make_shared > - ( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - } - -} // end namespace MultiArrayTools diff --git a/src/container_range.h b/src/container_range.h index 4482567..902ce65 100644 --- a/src/container_range.h +++ b/src/container_range.h @@ -11,6 +11,8 @@ #include "range_base.h" #include "index_base.h" +#include "pack_num.h" + namespace MultiArrayTools { @@ -137,6 +139,283 @@ namespace MultiArrayTools } // end namespace MultiArrayTools -#include "container_range.cc" +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayTools +{ + namespace + { + using namespace MultiArrayHelper; + } + + /********************** + * ContainerIndex * + **********************/ + + template + template + ContainerIndex::ContainerIndex(const std::shared_ptr& range) : + IndexInterface >(range, 0) + { + PackNum::construct(mIPack, *range); + IB::mPos = PackNum::makePos(mIPack); + std::get(mBlockSizes) = 1; + PackNum::initBlockSizes(mBlockSizes, mIPack); + } + + template + IndexType ContainerIndex::type() const + { + return IndexType::CONT; + } + + template + ContainerIndex& ContainerIndex::operator++() + { + if(mExternControl){ + IB::mPos = PackNum::makePos(mIPack); + } + PackNum::pp( mIPack ); + ++IB::mPos; + return *this; + } + + template + ContainerIndex& ContainerIndex::operator--() + { + if(mExternControl){ + IB::mPos = PackNum::makePos(mIPack); + } + PackNum::mm( mIPack ); + --IB::mPos; + return *this; + + } + + template + ContainerIndex& ContainerIndex::operator=(size_t pos) + { + IB::mPos = pos; + PackNum::setIndexPack(mIPack, pos); + return *this; + } + + template + int ContainerIndex::pp(std::shared_ptr& idxPtr) + { + int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtr); + IB::mPos += tmp; + return tmp; + } + + template + int ContainerIndex::mm(std::shared_ptr& idxPtr) + { + int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtr); + IB::mPos -= tmp; + return tmp; + } + + template + typename ContainerIndex::MetaType ContainerIndex::meta() const + { + MetaType metaTuple; + PackNum::getMetaPos(metaTuple, mIPack); + return metaTuple; + } + + template + ContainerIndex& ContainerIndex::at(const MetaType& metaPos) + { + PackNum::setMeta(mIPack, metaPos); + IB::mPos = PackNum::makePos(mIPack); + return *this; + } + + template + size_t ContainerIndex::dim() const + { + return sizeof...(Indices); + } + + template + ContainerIndex& ContainerIndex::sync() + { + if(mExternControl){ + IB::mPos = PackNum::makePos(mIPack); + //VCHECK(id()); + //VCHECK(sizeof...(Indices)); + //assert(IB::mPos < IB::max()); + } + return *this; + } + + template + template + auto ContainerIndex::get() const -> decltype( *std::get( mIPack ) )& + { + return *std::get( mIPack ); + } + + template + template + auto ContainerIndex::getPtr() const -> decltype( std::get( mIPack ) )& + { + return std::get( mIPack ); + } + + template + std::shared_ptr ContainerIndex::getPtr(size_t n) const + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + ContainerIndex const* t = this; + return PackNum::getIndexPtr(*t, n); + } + + + template + size_t ContainerIndex::getStepSize(size_t n) const + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + return mBlockSizes[n+1]; + } + + template + bool ContainerIndex::first() const + { + return IB::pos() == 0; + } + + template + bool ContainerIndex::last() const + { + return IB::pos() == IB::mRangePtr->size() - 1; + } + + template + ContainerIndex& ContainerIndex::operator()(const std::shared_ptr&... inds) + { + PackNum::swapIndices(mIPack, inds...); + mExternControl = true; + return sync(); + } + + template + ContainerIndex& ContainerIndex::operator()() + { + return sync(); + } + + template + std::shared_ptr::RangeType> ContainerIndex::range() const + { + return std::dynamic_pointer_cast( IB::mRangePtr ); + } + + /***************************** + * ContainerRangeFactory * + *****************************/ + + template + ContainerRangeFactory::ContainerRangeFactory(const std::shared_ptr&... rs) + { + mProd = std::shared_ptr >( new ContainerRange( rs... ) ); + } + + template + ContainerRangeFactory:: + ContainerRangeFactory(const typename ContainerRange::SpaceType& space) + { + mProd = std::shared_ptr >( new ContainerRange( space ) ); + } + + template + std::shared_ptr ContainerRangeFactory::create() + { + setSelf(); + return mProd; + } + + /********************** + * ContainerRange * + **********************/ + + template + ContainerRange::ContainerRange(const std::shared_ptr&... rs) : + mSpace( std::make_tuple( rs... ) ) {} + + template + ContainerRange::ContainerRange(const SpaceType& space) : mSpace( space ) {} + + template + size_t ContainerRange::dim() const + { + return sizeof...(Ranges); + } + + template + size_t ContainerRange::size() const + { + return PackNum::getSize(mSpace); + } + + template + template + auto ContainerRange::get() const -> decltype( *std::get( mSpace ) )& + { + return *std::get( mSpace ); + } + + template + template + auto ContainerRange::getPtr() const -> decltype( std::get( mSpace ) )& + { + return std::get( mSpace ); + } + + template + const typename ContainerRange::SpaceType& ContainerRange::space() const + { + return mSpace; + } + + template + typename ContainerRange::IndexType ContainerRange::begin() const + { + ContainerIndex + i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = 0; + return i; + } + + template + typename ContainerRange::IndexType ContainerRange::end() const + { + ContainerIndex + i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = size(); + return i; + } + + template + std::shared_ptr ContainerRange::index() const + { + return std::make_shared > + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + } + +} // end namespace MultiArrayTools + #endif diff --git a/src/helper_tools.h b/src/helper_tools.h index db85569..c0f41ee 100644 --- a/src/helper_tools.h +++ b/src/helper_tools.h @@ -13,9 +13,9 @@ namespace MultiArrayTools } -// ===================== -// === C O D E === -// ===================== +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ namespace MultiArrayTools { diff --git a/src/index_base.cc b/src/index_base.cc deleted file mode 100644 index 6d31d79..0000000 --- a/src/index_base.cc +++ /dev/null @@ -1,67 +0,0 @@ -// -*- C++ -*- -#include "index_base.h" - -namespace MultiArrayTools -{ - /***************** - * IndexBase * - *****************/ - - IndexBase::IndexBase(const std::shared_ptr& range, - size_t pos) : mRangePtr(range), - mPos(pos) - { - mId = indexId(); - } - - bool IndexBase::operator==(const IndexBase& in) const - { - return in.mPos == mPos and in.mRangePtr.get() == mRangePtr.get(); - } - - bool IndexBase::operator!=(const IndexBase& in) const - { - return in.mPos != mPos or in.mRangePtr.get() != mRangePtr.get(); - } - - size_t IndexBase::pos() const - { - return mPos; - } - - size_t IndexBase::max() const - { - return mRangePtr->size(); - } - /* - bool IndexBase::locked() const - { - return mLocked; - } - - IndexBase& IndexBase::lock(std::shared_ptr& idx) - { - mLocked = (idx.get() == this); - return *this; - } - */ - - std::shared_ptr IndexBase::rangePtr() const - { - return mRangePtr; - } - - IndexBase::operator size_t() const - { - return pos(); - } - - /********************** - * IndexInterface * - **********************/ - - template - IndexInterface::IndexInterface(const std::shared_ptr& rangePtr, size_t pos) : - IndexBase(rangePtr, pos) {} - -} diff --git a/src/index_base.h b/src/index_base.h index a45f904..0cce418 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -90,6 +90,73 @@ namespace MultiArrayTools } -#include "index_base.cc" +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayTools +{ + /***************** + * IndexBase * + *****************/ + + IndexBase::IndexBase(const std::shared_ptr& range, + size_t pos) : mRangePtr(range), + mPos(pos) + { + mId = indexId(); + } + + bool IndexBase::operator==(const IndexBase& in) const + { + return in.mPos == mPos and in.mRangePtr.get() == mRangePtr.get(); + } + + bool IndexBase::operator!=(const IndexBase& in) const + { + return in.mPos != mPos or in.mRangePtr.get() != mRangePtr.get(); + } + + size_t IndexBase::pos() const + { + return mPos; + } + + size_t IndexBase::max() const + { + return mRangePtr->size(); + } + /* + bool IndexBase::locked() const + { + return mLocked; + } + + IndexBase& IndexBase::lock(std::shared_ptr& idx) + { + mLocked = (idx.get() == this); + return *this; + } + */ + + std::shared_ptr IndexBase::rangePtr() const + { + return mRangePtr; + } + + IndexBase::operator size_t() const + { + return pos(); + } + + /********************** + * IndexInterface * + **********************/ + + template + IndexInterface::IndexInterface(const std::shared_ptr& rangePtr, size_t pos) : + IndexBase(rangePtr, pos) {} + +} #endif diff --git a/src/manipulator.cc b/src/manipulator.cc deleted file mode 100644 index 43f8a60..0000000 --- a/src/manipulator.cc +++ /dev/null @@ -1,56 +0,0 @@ -// -*- C++ -*- - -#include "manipulator.h" - -namespace MultiArrayTools -{ - - template - void ManipulatorBase::setup(std::vector& targetRef, size_t begin, size_t end) - { - mBegin = begin; - mEnd = end; - mTargetPtr = &targetRef; - } - - template - void ManipulatorBase::reset() - { - mLastPos = 0; - } - - template - BinReader::BinReader(const std::string& fname, size_t readBegin): - mFs(fname, std::fstream::in | std::fstream::binary), - mReadBegin(readBegin) {} - - template - BinReader::~BinReader() - { - mFs.close(); - } - - template - void BinReader::execute() - { - const size_t readSize = MB::mEnd - MB::mBegin; - const size_t blockSize = sizeof(T) * readSize; - char* buffer = new char[blockSize]; - - mFs.seekg(mReadBegin + MB::mLastPos, mFs.beg); - mFs.read(buffer, blockSize); - MB::mLastPos += blockSize; - - T* content = reinterpret_cast( buffer ); - - if(MB::mTargetPtr->size() < MB::mEnd){ - assert(0); - // throw - } - - std::copy(&content[0], &content[readSize], MB::mTargetPtr->begin() + MB::mBegin); - - delete[] buffer; - } - -} diff --git a/src/manipulator.h b/src/manipulator.h deleted file mode 100644 index f884d0d..0000000 --- a/src/manipulator.h +++ /dev/null @@ -1,56 +0,0 @@ -// -*- C++ -*- - -#ifndef __manipulator_h__ -#define __manipulator_h__ - -#include -#include -#include -#include - -#include "base_def.h" - -namespace MultiArrayTools -{ - - // some kind of input stream or sth similar... - template - class ManipulatorBase - { - public: - virtual ~ManipulatorBase() {} - - virtual void setup(std::vector& targetRef, size_t begin, size_t end); - virtual void execute(size_t pos) = 0; - virtual void reset(); - - protected: - - size_t mLastPos = 0; - size_t mBegin = 0; - size_t mEnd = 0; - std::vector* mTargetPtr = nullptr; - }; - - template - class BinReader : public ManipulatorBase - { - public: - typedef ManipulatorBase MB; - - BinReader(const std::string& fname, size_t readBegin = 0); - virtual ~BinReader(); - - - virtual void execute() override; - - private: - size_t mReadBegin; - std::fstream mFs; - }; - -} - -#include "manipulator.cc" - -#endif diff --git a/src/multi_array.cc b/src/multi_array.cc deleted file mode 100644 index 58df5ec..0000000 --- a/src/multi_array.cc +++ /dev/null @@ -1,595 +0,0 @@ -// -*- C++ -*- - -#include "multi_array.h" - -namespace MultiArrayTools -{ - /************************************** - * MultiArrayBase::const_iterator * - **************************************/ - - template - MultiArrayBase::const_iterator::const_iterator(const MultiArrayBase& ma): - mMAPtr(&ma), mPos(0) { } - - template - MultiArrayBase::const_iterator::const_iterator(const MultiArrayBase& ma, - const typename CRange::IndexType& index): - mMAPtr(&ma), mPos(index.pos()) { } - - template - bool MultiArrayBase::const_iterator::operator==(const const_iterator& it) const - { - return mMAPtr == it.mMAPtr and mPos == it.mPos; - } - - template - bool MultiArrayBase::const_iterator::operator!=(const const_iterator& it) const - { - return mMAPtr != it.mMAPtr or mPos != it.mPos; - } - - template - const T& MultiArrayBase::const_iterator::operator*() const - { - return mMAPtr->data()[mPos]; - } - - template - T const* MultiArrayBase::const_iterator::operator->() const - { - return &mMAPtr->data()[mPos]; - } - - template - typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator++() - { - ++mPos; - return *this; - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator++(int) - { - const_iterator tmp(*this); - ++mPos; - return tmp; - } - - template - typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator--() - { - --mPos; - return *this; - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator--(int) - { - const_iterator tmp(*this); - --mPos; - return tmp; - } - - template - typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator+=(int diff) - { - mPos += diff; - return *this; - } - - template - typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator-=(int diff) - { - mPos -= diff; - return *this; - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator+(int num) const - { - const_iterator tmp(*this); - tmp += num; - return tmp; - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator-(int num) const - { - const_iterator tmp(*this); - tmp -= num; - } - - template - int MultiArrayBase::const_iterator::operator-(const const_iterator& it) const - { - return mPos - it.mPos; - } - - template - const T& MultiArrayBase::const_iterator::operator[](int num) const - { - return *(operator+(num)); - } - - template - bool MultiArrayBase::const_iterator::operator<(const const_iterator& it) const - { - return mPos < it.mPos; - } - - template - bool MultiArrayBase::const_iterator::operator>(const const_iterator& it) const - { - return mPos > it.mPos; - } - - template - bool MultiArrayBase::const_iterator::operator<=(const const_iterator& it) const - { - return mPos <= it.mPos; - } - - template - bool MultiArrayBase::const_iterator::operator>=(const const_iterator& it) const - { - return mPos >= it.mPos; - } - - template - typename MultiArrayBase::IndexType - MultiArrayBase::const_iterator::index() const - { - auto i = mMAPtr->beginIndex(); - i = mPos; - return i; - } - - /********************** - * MultiArrayBase * - **********************/ - - template - MultiArrayBase::MultiArrayBase(const std::shared_ptr&... ranges) - { - ContainerRangeFactory crf(ranges...); - mRange = std::dynamic_pointer_cast >( crf.create() ); - } - - template - size_t MultiArrayBase::size() const - { - return mRange->size(); - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::begin() const - { - return const_iterator(*this, beginIndex()); - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::end() const - { - return const_iterator(*this, endIndex()); - } - - template - typename MultiArrayBase::IndexType - MultiArrayBase::beginIndex() const - { - return mRange->begin(); - } - - template - typename MultiArrayBase::IndexType - MultiArrayBase::endIndex() const - { - return mRange->end(); - } - - template - const std::shared_ptr::CRange>& - MultiArrayBase::range() const - { - return mRange; - } - - template - bool MultiArrayBase::isConst() const - { - return true; - } - - template - ConstOperationRoot - MultiArrayBase::operator()(std::shared_ptr&... inds) const - { - return ConstOperationRoot(*this, inds...); - } - - template - bool MultiArrayBase::isInit() const - { - return mInit; - } - - /**************************************** - * MutableMultiArrayBase::iterator * - ****************************************/ - - template - MutableMultiArrayBase::iterator::iterator(MutableMultiArrayBase& ma): - mMAPtr(&ma), mPos(0) - { } - - template - MutableMultiArrayBase::iterator::iterator(MutableMultiArrayBase& ma, - const typename CRange::IndexType& index): - mMAPtr(&ma), mPos(index.pos()) - { } - - template - bool MutableMultiArrayBase::iterator::operator==(const iterator& it) const - { - return mMAPtr == it.mMAPtr and mPos == it.mPos; - } - - template - bool MutableMultiArrayBase::iterator::operator!=(const iterator& it) const - { - return mMAPtr != it.mMAPtr or mPos != it.mPos; - } - - template - const T& MutableMultiArrayBase::iterator::operator*() const - { - return mMAPtr->data()[mPos]; - } - - template - T const* MutableMultiArrayBase::iterator::operator->() const - { - return &mMAPtr->data()[mPos]; - } - - template - T& MutableMultiArrayBase::iterator::operator*() - { - return mMAPtr->data()[mPos]; - } - - template - T* MutableMultiArrayBase::iterator::operator->() - { - return &mMAPtr->data()[mPos]; - } - - template - typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator++() - { - ++mPos; - return *this; - } - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator++(int) - { - iterator tmp(*this); - ++mPos; - return tmp; - } - - template - typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator--() - { - --mPos; - return *this; - } - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator--(int) - { - iterator tmp(*this); - --mPos; - return tmp; - } - - template - typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator+=(int diff) - { - mPos += diff; - return *this; - } - - template - typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator-=(int diff) - { - mPos -= diff; - return *this; - } - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator+(int num) const - { - iterator tmp(*this); - tmp += num; - return tmp; - } - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator-(int num) const - { - iterator tmp(*this); - tmp -= num; - } - - template - int MutableMultiArrayBase::iterator::operator-(const iterator& it) const - { - return mPos - it.mPos; - } - - template - const T& MutableMultiArrayBase::iterator::operator[](int num) const - { - return *(operator+(num)); - } - - template - T& MutableMultiArrayBase::iterator::operator[](int num) - { - return *(operator+(num)); - } - - template - bool MutableMultiArrayBase::iterator::operator<(const iterator& it) const - { - return mPos < it.mPos; - } - - template - bool MutableMultiArrayBase::iterator::operator>(const iterator& it) const - { - return mPos > it.mPos; - } - - template - bool MutableMultiArrayBase::iterator::operator<=(const iterator& it) const - { - return mPos <= it.mPos; - } - - template - bool MutableMultiArrayBase::iterator::operator>=(const iterator& it) const - { - return mPos >= it.mPos; - } - - template - typename MutableMultiArrayBase::IndexType - MutableMultiArrayBase::iterator::index() const - { - auto i = mMAPtr->beginIndex(); - i = mPos; - return i; - } - - /****************************** - * MutableMultiArrayBase * - ******************************/ - - template - MutableMultiArrayBase::MutableMultiArrayBase(const std::shared_ptr&... ranges) : - MultiArrayBase(ranges...) {} - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::begin() - { - return iterator(*this, MAB::beginIndex()); - } - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::end() - { - return iterator(*this, MAB::endIndex()); - } - - template - bool MutableMultiArrayBase::isConst() const - { - return false; - } - - template - OperationRoot - MutableMultiArrayBase::operator()(std::shared_ptr&... inds) - { - return OperationRoot(*this, inds...); - } - - template - ConstOperationRoot - MutableMultiArrayBase::operator()(std::shared_ptr&... inds) const - { - return ConstOperationRoot(*this, inds...); - } - - - /******************* - * MultiArray * - *******************/ - - template - MultiArray::MultiArray(const std::shared_ptr&... ranges) : - MutableMultiArrayBase(ranges...), - mCont(MAB::mRange->size()) - { - MAB::mInit = true; - } - - template - MultiArray::MultiArray(const std::shared_ptr&... ranges, const std::vector& vec) : - MutableMultiArrayBase(ranges...), - mCont(vec) - { - MAB::mInit = true; - if(mCont.size() > MAB::mRange->size()){ - mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); - } - } - - template - MultiArray::MultiArray(const std::shared_ptr&... ranges, std::vector&& vec) : - MutableMultiArrayBase(ranges...), - mCont(vec) - { - MAB::mInit = true; - if(mCont.size() > MAB::mRange->size()){ - mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); - } - } - /* - template - template - MultiArray::MultiArray(const MultiArray,Range3> in) : - MutableMultiArrayBase(merge(in.range(), in[ in.beginIndex() ].range())) - // assert that Range2 has always same extension - { - MAB::mInit = true; - mCont.clear(); - for(auto i = in.beginIndex(); i != in.endIndex(); ++i){ - mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end()); - } - assert(mCont.size() == MAB::mRange->size()); - } - */ - /* - template - template - MultiArray& MultiArray::operator=(const MultiArray,Range3> in) - { - MAB::mRange.reset(new Range(merge(in.range(), in[ in.beginIndex() ].range()))); - // assert that Range2 has always same extension - mCont.clear(); - for(auto i = in.beginIndex(); i != in.endIndex(); ++i){ - mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end()); - } - assert(mCont.size() == MAB::mRange->size()); - return *this; - } */ - - template - T& MultiArray::operator[](const typename CRange::IndexType& i) - { - return mCont[ i.pos() ]; - } - - template - const T& MultiArray::operator[](const typename CRange::IndexType& i) const - { - return mCont[ i.pos() ]; - } - - template - T& MultiArray::at(const typename CRange::IndexType::MetaType& meta) - { - return mCont[ MAB::beginIndex().at(meta).pos() ]; - } - - template - const T& MultiArray::at(const typename CRange::IndexType::MetaType& meta) const - { - return mCont[ MAB::beginIndex().at(meta).pos() ]; - } - - template - bool MultiArray::isConst() const - { - return false; - } - - template - bool MultiArray::isSlice() const - { - return false; - } - - template - template - MultiArray MultiArray::format(const std::shared_ptr&... nrs) - { - return MultiArray( nrs... , std::move(mCont) ); - } - - template - const T* MultiArray::data() const - { - return mCont.data(); - } - - template - T* MultiArray::data() - { - return mCont.data(); - } - - template - const std::vector& MultiArray::datav() const - { - return mCont; - } - - template - std::vector& MultiArray::datav() - { - return mCont; - } - - - /* - template - void MultiArray::manipulate(ManipulatorBase& mb, - const typename Range::IndexType& manBegin, - const typename Range::IndexType& manEnd) - { - mb.setup(mCont, manBegin.pos(), manEnd.pos()); - mb.execute(); - } - */ - - - /**************************** - * FunctionalMultiArray * - ****************************/ - - /* - template - FunctionalMultiArray::FunctionalMultiArray(const Range& range) : - MultiArrayBase(range), mFunc() {} - */ - template - FunctionalMultiArray::FunctionalMultiArray(const std::shared_ptr&... ranges, - const Function& func) : - MultiArrayBase(ranges...), mFunc(func) {} - - template - const T& FunctionalMultiArray::operator[](const typename CRange::IndexType& i) const - { - mVal = mFunc(i); - return mVal; - } - - template - bool FunctionalMultiArray::isConst() const - { - return true; - } - - template - bool FunctionalMultiArray::isSlice() const - { - return false; - } - -} diff --git a/src/multi_array.h b/src/multi_array.h index 4e37e70..90bedcd 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -264,6 +264,600 @@ namespace MultiArrayTools } -#include "multi_array.cc" +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayTools +{ + /************************************** + * MultiArrayBase::const_iterator * + **************************************/ + + template + MultiArrayBase::const_iterator::const_iterator(const MultiArrayBase& ma): + mMAPtr(&ma), mPos(0) { } + + template + MultiArrayBase::const_iterator::const_iterator(const MultiArrayBase& ma, + const typename CRange::IndexType& index): + mMAPtr(&ma), mPos(index.pos()) { } + + template + bool MultiArrayBase::const_iterator::operator==(const const_iterator& it) const + { + return mMAPtr == it.mMAPtr and mPos == it.mPos; + } + + template + bool MultiArrayBase::const_iterator::operator!=(const const_iterator& it) const + { + return mMAPtr != it.mMAPtr or mPos != it.mPos; + } + + template + const T& MultiArrayBase::const_iterator::operator*() const + { + return mMAPtr->data()[mPos]; + } + + template + T const* MultiArrayBase::const_iterator::operator->() const + { + return &mMAPtr->data()[mPos]; + } + + template + typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator++() + { + ++mPos; + return *this; + } + + template + typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator++(int) + { + const_iterator tmp(*this); + ++mPos; + return tmp; + } + + template + typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator--() + { + --mPos; + return *this; + } + + template + typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator--(int) + { + const_iterator tmp(*this); + --mPos; + return tmp; + } + + template + typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator+=(int diff) + { + mPos += diff; + return *this; + } + + template + typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator-=(int diff) + { + mPos -= diff; + return *this; + } + + template + typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator+(int num) const + { + const_iterator tmp(*this); + tmp += num; + return tmp; + } + + template + typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator-(int num) const + { + const_iterator tmp(*this); + tmp -= num; + } + + template + int MultiArrayBase::const_iterator::operator-(const const_iterator& it) const + { + return mPos - it.mPos; + } + + template + const T& MultiArrayBase::const_iterator::operator[](int num) const + { + return *(operator+(num)); + } + + template + bool MultiArrayBase::const_iterator::operator<(const const_iterator& it) const + { + return mPos < it.mPos; + } + + template + bool MultiArrayBase::const_iterator::operator>(const const_iterator& it) const + { + return mPos > it.mPos; + } + + template + bool MultiArrayBase::const_iterator::operator<=(const const_iterator& it) const + { + return mPos <= it.mPos; + } + + template + bool MultiArrayBase::const_iterator::operator>=(const const_iterator& it) const + { + return mPos >= it.mPos; + } + + template + typename MultiArrayBase::IndexType + MultiArrayBase::const_iterator::index() const + { + auto i = mMAPtr->beginIndex(); + i = mPos; + return i; + } + + /********************** + * MultiArrayBase * + **********************/ + + template + MultiArrayBase::MultiArrayBase(const std::shared_ptr&... ranges) + { + ContainerRangeFactory crf(ranges...); + mRange = std::dynamic_pointer_cast >( crf.create() ); + } + + template + size_t MultiArrayBase::size() const + { + return mRange->size(); + } + + template + typename MultiArrayBase::const_iterator MultiArrayBase::begin() const + { + return const_iterator(*this, beginIndex()); + } + + template + typename MultiArrayBase::const_iterator MultiArrayBase::end() const + { + return const_iterator(*this, endIndex()); + } + + template + typename MultiArrayBase::IndexType + MultiArrayBase::beginIndex() const + { + return mRange->begin(); + } + + template + typename MultiArrayBase::IndexType + MultiArrayBase::endIndex() const + { + return mRange->end(); + } + + template + const std::shared_ptr::CRange>& + MultiArrayBase::range() const + { + return mRange; + } + + template + bool MultiArrayBase::isConst() const + { + return true; + } + + template + ConstOperationRoot + MultiArrayBase::operator()(std::shared_ptr&... inds) const + { + return ConstOperationRoot(*this, inds...); + } + + template + bool MultiArrayBase::isInit() const + { + return mInit; + } + + /**************************************** + * MutableMultiArrayBase::iterator * + ****************************************/ + + template + MutableMultiArrayBase::iterator::iterator(MutableMultiArrayBase& ma): + mMAPtr(&ma), mPos(0) + { } + + template + MutableMultiArrayBase::iterator::iterator(MutableMultiArrayBase& ma, + const typename CRange::IndexType& index): + mMAPtr(&ma), mPos(index.pos()) + { } + + template + bool MutableMultiArrayBase::iterator::operator==(const iterator& it) const + { + return mMAPtr == it.mMAPtr and mPos == it.mPos; + } + + template + bool MutableMultiArrayBase::iterator::operator!=(const iterator& it) const + { + return mMAPtr != it.mMAPtr or mPos != it.mPos; + } + + template + const T& MutableMultiArrayBase::iterator::operator*() const + { + return mMAPtr->data()[mPos]; + } + + template + T const* MutableMultiArrayBase::iterator::operator->() const + { + return &mMAPtr->data()[mPos]; + } + + template + T& MutableMultiArrayBase::iterator::operator*() + { + return mMAPtr->data()[mPos]; + } + + template + T* MutableMultiArrayBase::iterator::operator->() + { + return &mMAPtr->data()[mPos]; + } + + template + typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator++() + { + ++mPos; + return *this; + } + + template + typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator++(int) + { + iterator tmp(*this); + ++mPos; + return tmp; + } + + template + typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator--() + { + --mPos; + return *this; + } + + template + typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator--(int) + { + iterator tmp(*this); + --mPos; + return tmp; + } + + template + typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator+=(int diff) + { + mPos += diff; + return *this; + } + + template + typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator-=(int diff) + { + mPos -= diff; + return *this; + } + + template + typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator+(int num) const + { + iterator tmp(*this); + tmp += num; + return tmp; + } + + template + typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator-(int num) const + { + iterator tmp(*this); + tmp -= num; + } + + template + int MutableMultiArrayBase::iterator::operator-(const iterator& it) const + { + return mPos - it.mPos; + } + + template + const T& MutableMultiArrayBase::iterator::operator[](int num) const + { + return *(operator+(num)); + } + + template + T& MutableMultiArrayBase::iterator::operator[](int num) + { + return *(operator+(num)); + } + + template + bool MutableMultiArrayBase::iterator::operator<(const iterator& it) const + { + return mPos < it.mPos; + } + + template + bool MutableMultiArrayBase::iterator::operator>(const iterator& it) const + { + return mPos > it.mPos; + } + + template + bool MutableMultiArrayBase::iterator::operator<=(const iterator& it) const + { + return mPos <= it.mPos; + } + + template + bool MutableMultiArrayBase::iterator::operator>=(const iterator& it) const + { + return mPos >= it.mPos; + } + + template + typename MutableMultiArrayBase::IndexType + MutableMultiArrayBase::iterator::index() const + { + auto i = mMAPtr->beginIndex(); + i = mPos; + return i; + } + + /****************************** + * MutableMultiArrayBase * + ******************************/ + + template + MutableMultiArrayBase::MutableMultiArrayBase(const std::shared_ptr&... ranges) : + MultiArrayBase(ranges...) {} + + template + typename MutableMultiArrayBase::iterator MutableMultiArrayBase::begin() + { + return iterator(*this, MAB::beginIndex()); + } + + template + typename MutableMultiArrayBase::iterator MutableMultiArrayBase::end() + { + return iterator(*this, MAB::endIndex()); + } + + template + bool MutableMultiArrayBase::isConst() const + { + return false; + } + + template + OperationRoot + MutableMultiArrayBase::operator()(std::shared_ptr&... inds) + { + return OperationRoot(*this, inds...); + } + + template + ConstOperationRoot + MutableMultiArrayBase::operator()(std::shared_ptr&... inds) const + { + return ConstOperationRoot(*this, inds...); + } + + + /******************* + * MultiArray * + *******************/ + + template + MultiArray::MultiArray(const std::shared_ptr&... ranges) : + MutableMultiArrayBase(ranges...), + mCont(MAB::mRange->size()) + { + MAB::mInit = true; + } + + template + MultiArray::MultiArray(const std::shared_ptr&... ranges, const std::vector& vec) : + MutableMultiArrayBase(ranges...), + mCont(vec) + { + MAB::mInit = true; + if(mCont.size() > MAB::mRange->size()){ + mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); + } + } + + template + MultiArray::MultiArray(const std::shared_ptr&... ranges, std::vector&& vec) : + MutableMultiArrayBase(ranges...), + mCont(vec) + { + MAB::mInit = true; + if(mCont.size() > MAB::mRange->size()){ + mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); + } + } + /* + template + template + MultiArray::MultiArray(const MultiArray,Range3> in) : + MutableMultiArrayBase(merge(in.range(), in[ in.beginIndex() ].range())) + // assert that Range2 has always same extension + { + MAB::mInit = true; + mCont.clear(); + for(auto i = in.beginIndex(); i != in.endIndex(); ++i){ + mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end()); + } + assert(mCont.size() == MAB::mRange->size()); + } + */ + /* + template + template + MultiArray& MultiArray::operator=(const MultiArray,Range3> in) + { + MAB::mRange.reset(new Range(merge(in.range(), in[ in.beginIndex() ].range()))); + // assert that Range2 has always same extension + mCont.clear(); + for(auto i = in.beginIndex(); i != in.endIndex(); ++i){ + mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end()); + } + assert(mCont.size() == MAB::mRange->size()); + return *this; + } */ + + template + T& MultiArray::operator[](const typename CRange::IndexType& i) + { + return mCont[ i.pos() ]; + } + + template + const T& MultiArray::operator[](const typename CRange::IndexType& i) const + { + return mCont[ i.pos() ]; + } + + template + T& MultiArray::at(const typename CRange::IndexType::MetaType& meta) + { + return mCont[ MAB::beginIndex().at(meta).pos() ]; + } + + template + const T& MultiArray::at(const typename CRange::IndexType::MetaType& meta) const + { + return mCont[ MAB::beginIndex().at(meta).pos() ]; + } + + template + bool MultiArray::isConst() const + { + return false; + } + + template + bool MultiArray::isSlice() const + { + return false; + } + + template + template + MultiArray MultiArray::format(const std::shared_ptr&... nrs) + { + return MultiArray( nrs... , std::move(mCont) ); + } + + template + const T* MultiArray::data() const + { + return mCont.data(); + } + + template + T* MultiArray::data() + { + return mCont.data(); + } + + template + const std::vector& MultiArray::datav() const + { + return mCont; + } + + template + std::vector& MultiArray::datav() + { + return mCont; + } + + + /* + template + void MultiArray::manipulate(ManipulatorBase& mb, + const typename Range::IndexType& manBegin, + const typename Range::IndexType& manEnd) + { + mb.setup(mCont, manBegin.pos(), manEnd.pos()); + mb.execute(); + } + */ + + + /**************************** + * FunctionalMultiArray * + ****************************/ + + /* + template + FunctionalMultiArray::FunctionalMultiArray(const Range& range) : + MultiArrayBase(range), mFunc() {} + */ + template + FunctionalMultiArray::FunctionalMultiArray(const std::shared_ptr&... ranges, + const Function& func) : + MultiArrayBase(ranges...), mFunc(func) {} + + template + const T& FunctionalMultiArray::operator[](const typename CRange::IndexType& i) const + { + mVal = mFunc(i); + return mVal; + } + + template + bool FunctionalMultiArray::isConst() const + { + return true; + } + + template + bool FunctionalMultiArray::isSlice() const + { + return false; + } + +} #endif diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc deleted file mode 100644 index bad3d9f..0000000 --- a/src/multi_array_operation.cc +++ /dev/null @@ -1,377 +0,0 @@ -// -*- C++ -*- - -#include "multi_array_operation.h" - -namespace MultiArrayTools -{ - namespace - { - using namespace MultiArrayHelper; - } - - void seekIndexInst(std::shared_ptr i, std::vector >& ivec) - { - for(size_t inum = 0; inum != i->rangePtr()->dim(); ++inum){ - auto ii = i->getPtr(inum); - if(ii->type() == IndexType::MULTI or - ii->type() == IndexType::CONT){ - seekIndexInst(ii, ivec); - } - ivec.push_back(ii); - } - } - - BTSS getBlockType(std::shared_ptr i, - std::shared_ptr j, - bool first, size_t higherStepSize) - { - // returning BlockType and step size is redundant (change in the future) - // stepSize == 0 => VALUE - // stepSize == 1 => BLOCK - // stepSize > 1 => SPLIT :) - BTSS out(BlockType::VALUE, 0); - size_t lastNum = i->rangePtr()->dim(); - for(size_t inum = 0; inum != lastNum; ++inum){ - auto ii = i->getPtr(inum); - if(ii == j){ - - if(inum == lastNum - 1 and first){ - out = BTSS(BlockType::BLOCK, 1); - } - else { - first = false; - out = BTSS(BlockType::SPLIT, i->getStepSize(inum) * higherStepSize + out.second); - } - continue; - } - - if(ii->type() == IndexType::MULTI or - ii->type() == IndexType::CONT){ - - BTSS tmp = getBlockType(ii, j, inum == lastNum - 1, i->getStepSize(inum) * higherStepSize); - if(tmp.first != BlockType::VALUE){ - out = tmp; - } - } - } - return out; - } - - template - std::shared_ptr > makeBlock(const std::vector& vec, size_t stepSize, size_t blockSize) - { - return std::make_shared >(vec, 0, blockSize, stepSize); - } - - template - std::shared_ptr > makeBlock(std::vector& vec, size_t stepSize, size_t blockSize) - { - return std::make_shared >(vec, 0, blockSize, stepSize); - } - - size_t getBTNum(const std::vector& mp, BlockType bt) - { - size_t out = 0; - for(auto& xx: mp){ - if(xx.first == bt){ - ++out; - } - } - return out; - } - - void minimizeAppearanceOfType(std::map, std::vector >& mp, - BlockType bt) - { - size_t minNum = getBTNum( mp.begin()->second, bt ); - for(auto& mm: mp){ - size_t tmp = getBTNum( mm.second, bt ); - if(tmp < minNum){ - minNum = tmp; - } - } - - for(auto mit = mp.begin(); mit != mp.end(); ){ - size_t tmp = getBTNum( mit->second, bt ); - if(tmp > minNum){ - mit = mp.erase(mit); - } - else { - ++mit; - } - } - - } - - template - std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, - const OpClass& second) - { - std::vector > ivec; - seekIndexInst(ownIdx, ivec); - std::map, std::vector > mp; - - for(auto& xx: ivec){ - mp[xx] = second.block(xx); - } - // seek minimal number of VALUEs => guarantees absence of conflicting blocks - minimizeAppearanceOfType(mp, BlockType::VALUE); - // seek mininmal number of SPLITs => maximal vectorization possible - minimizeAppearanceOfType(mp, BlockType::SPLIT); - - return mp.begin()->first; - } - - /*************************** - * OperationTemplate * - ***************************/ - - template - OperationTemplate::OperationTemplate(OperationClass* oc) : mOc(oc) {} - - template - template - auto OperationTemplate::operator+(const Second& in) const - -> Operation,OperationClass,Second> - { - return Operation,OperationClass,Second>(*mOc, in); - } - - template - template - auto OperationTemplate::operator-(const Second& in) const - -> Operation,OperationClass,Second> - { - return Operation,OperationClass,Second>(*mOc, in); - } - - template - template - auto OperationTemplate::operator*(const Second& in) const - -> Operation,OperationClass,Second> - { - return Operation,OperationClass,Second>(*mOc, in); - } - - template - template - auto OperationTemplate::operator/(const Second& in) const - -> Operation,OperationClass,Second> - { - return Operation,OperationClass,Second>(*mOc, in); - } - - template - template - auto OperationTemplate::c(std::shared_ptr& ind) const - -> Contraction - { - return Contraction(*mOc, ind); - } - - - /************************* - * OperationMaster * - *************************/ - - template - OperationMaster:: - OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, - std::shared_ptr& index) : - mSecond(second), mArrayRef(ma), mIndex() - { - MultiRangeFactory mrf( index->range() ); - std::shared_ptr > mr = - std::dynamic_pointer_cast >( mrf.create() ); - mIndex = std::make_shared( mr->begin() ); - (*mIndex) = *index; - - auto blockIndex = seekBlockIndex( mIndex, second); - - block(blockIndex); - second.block(blockIndex); - - for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndex) ){ - get() = mSecond.get(); - } - } - - template - MBlock& OperationMaster::get() - { - block(); - return *mBlockPtr; - } - - template - const Block& OperationMaster::get() const - { - block(); - return *mBlockPtr; - } - - template - std::vector OperationMaster::block(const std::shared_ptr blockIndex) const - { - std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); - mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); - return btv; - } - - template - const OperationMaster& OperationMaster::block() const - { - mBlockPtr->set( mIndex->pos() ); - return *this; - } - - /**************************** - * ConstOperationRoot * - ****************************/ - - template - ConstOperationRoot:: - ConstOperationRoot(const MultiArrayBase& ma, - const std::shared_ptr&... indices) : - OperationTemplate >(this), - mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) - { - (*mIndex)(indices...); - } - - template - const Block& ConstOperationRoot::get() const - { - block(); - return *mBlockPtr; - } - - template - std::vector ConstOperationRoot::block(const std::shared_ptr blockIndex) const - { - std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); - mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); - return btv; - } - - template - const ConstOperationRoot& ConstOperationRoot::block() const - { - mBlockPtr->set( (*mIndex)().pos() ); - return *this; - } - - /*********************** - * OperationRoot * - ***********************/ - - template - OperationRoot:: - OperationRoot(MutableMultiArrayBase& ma, - const std::shared_ptr&... indices) : - OperationTemplate >(this), - mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) - { - (*mIndex)(indices...); - } - - template - template - OperationMaster OperationRoot::operator=(const OpClass& in) - { - return OperationMaster(mArrayRef, in, mIndex); - } - - template - const MBlock& OperationRoot::get() const - { - block(); - return *mBlockPtr; - } - - template - MBlock& OperationRoot::get() - { - block(); - return *mBlockPtr; - } - - template - std::vector OperationRoot::block(const std::shared_ptr blockIndex) const - { - std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); - mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); - return btv; - } - - template - const OperationRoot& OperationRoot::block() const - { - mBlockPtr->set( (*mIndex)().pos() ); - return *this; - } - - /******************* - * Operation * - *******************/ - - template - Operation::Operation(const Ops&... ops) : - OperationTemplate >(this), - mOps(ops...) {} - - template - const BlockResult& Operation::get() const - { - mRes = std::move( PackNum::template unpackArgs(mOps) ); - return mRes; - } - - template - std::vector Operation::block(const std::shared_ptr blockIndex) const - { - std::vector btv; - PackNum::makeBlockTypeVec(btv, mOps, blockIndex); - return btv; - } - - template - const Operation& Operation::block() const - { - //mBlockPtr->set( mIndex->pos() ); - return *this; - } - - /********************* - * Contraction * - *********************/ - - template - Contraction::Contraction(const Op& op, std::shared_ptr ind) : - OperationTemplate >(this), - mOp(op), - mInd(ind) {} - - template - const BlockResult& Contraction::get() const - { - BlockBinaryOpSelf,decltype(mOp.get())> f(mRes); - for(*mInd = 0; mInd->pos() != mInd->max(); ++(*mInd)){ - f(mOp.get()); - } - return mRes; - } - - template - std::vector Contraction::block(const std::shared_ptr blockIndex) const - { - return mOp.block(blockIndex); - } - - template - const Contraction& Contraction::block() const - { - return *this; - } - -} diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 6e17fa0..90537b7 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -251,6 +251,382 @@ namespace MultiArrayTools } -#include "multi_array_operation.cc" +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayTools +{ + namespace + { + using namespace MultiArrayHelper; + } + + void seekIndexInst(std::shared_ptr i, std::vector >& ivec) + { + for(size_t inum = 0; inum != i->rangePtr()->dim(); ++inum){ + auto ii = i->getPtr(inum); + if(ii->type() == IndexType::MULTI or + ii->type() == IndexType::CONT){ + seekIndexInst(ii, ivec); + } + ivec.push_back(ii); + } + } + + BTSS getBlockType(std::shared_ptr i, + std::shared_ptr j, + bool first, size_t higherStepSize) + { + // returning BlockType and step size is redundant (change in the future) + // stepSize == 0 => VALUE + // stepSize == 1 => BLOCK + // stepSize > 1 => SPLIT :) + BTSS out(BlockType::VALUE, 0); + size_t lastNum = i->rangePtr()->dim(); + for(size_t inum = 0; inum != lastNum; ++inum){ + auto ii = i->getPtr(inum); + if(ii == j){ + + if(inum == lastNum - 1 and first){ + out = BTSS(BlockType::BLOCK, 1); + } + else { + first = false; + out = BTSS(BlockType::SPLIT, i->getStepSize(inum) * higherStepSize + out.second); + } + continue; + } + + if(ii->type() == IndexType::MULTI or + ii->type() == IndexType::CONT){ + + BTSS tmp = getBlockType(ii, j, inum == lastNum - 1, i->getStepSize(inum) * higherStepSize); + if(tmp.first != BlockType::VALUE){ + out = tmp; + } + } + } + return out; + } + + template + std::shared_ptr > makeBlock(const std::vector& vec, size_t stepSize, size_t blockSize) + { + return std::make_shared >(vec, 0, blockSize, stepSize); + } + + template + std::shared_ptr > makeBlock(std::vector& vec, size_t stepSize, size_t blockSize) + { + return std::make_shared >(vec, 0, blockSize, stepSize); + } + + size_t getBTNum(const std::vector& mp, BlockType bt) + { + size_t out = 0; + for(auto& xx: mp){ + if(xx.first == bt){ + ++out; + } + } + return out; + } + + void minimizeAppearanceOfType(std::map, std::vector >& mp, + BlockType bt) + { + size_t minNum = getBTNum( mp.begin()->second, bt ); + for(auto& mm: mp){ + size_t tmp = getBTNum( mm.second, bt ); + if(tmp < minNum){ + minNum = tmp; + } + } + + for(auto mit = mp.begin(); mit != mp.end(); ){ + size_t tmp = getBTNum( mit->second, bt ); + if(tmp > minNum){ + mit = mp.erase(mit); + } + else { + ++mit; + } + } + + } + + template + std::shared_ptr seekBlockIndex(std::shared_ptr ownIdx, + const OpClass& second) + { + std::vector > ivec; + seekIndexInst(ownIdx, ivec); + std::map, std::vector > mp; + + for(auto& xx: ivec){ + mp[xx] = second.block(xx); + } + // seek minimal number of VALUEs => guarantees absence of conflicting blocks + minimizeAppearanceOfType(mp, BlockType::VALUE); + // seek mininmal number of SPLITs => maximal vectorization possible + minimizeAppearanceOfType(mp, BlockType::SPLIT); + + return mp.begin()->first; + } + + /*************************** + * OperationTemplate * + ***************************/ + + template + OperationTemplate::OperationTemplate(OperationClass* oc) : mOc(oc) {} + + template + template + auto OperationTemplate::operator+(const Second& in) const + -> Operation,OperationClass,Second> + { + return Operation,OperationClass,Second>(*mOc, in); + } + + template + template + auto OperationTemplate::operator-(const Second& in) const + -> Operation,OperationClass,Second> + { + return Operation,OperationClass,Second>(*mOc, in); + } + + template + template + auto OperationTemplate::operator*(const Second& in) const + -> Operation,OperationClass,Second> + { + return Operation,OperationClass,Second>(*mOc, in); + } + + template + template + auto OperationTemplate::operator/(const Second& in) const + -> Operation,OperationClass,Second> + { + return Operation,OperationClass,Second>(*mOc, in); + } + + template + template + auto OperationTemplate::c(std::shared_ptr& ind) const + -> Contraction + { + return Contraction(*mOc, ind); + } + + + /************************* + * OperationMaster * + *************************/ + + template + OperationMaster:: + OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, + std::shared_ptr& index) : + mSecond(second), mArrayRef(ma), mIndex() + { + MultiRangeFactory mrf( index->range() ); + std::shared_ptr > mr = + std::dynamic_pointer_cast >( mrf.create() ); + mIndex = std::make_shared( mr->begin() ); + (*mIndex) = *index; + + auto blockIndex = seekBlockIndex( mIndex, second); + + block(blockIndex); + second.block(blockIndex); + + for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndex) ){ + get() = mSecond.get(); + } + } + + template + MBlock& OperationMaster::get() + { + block(); + return *mBlockPtr; + } + + template + const Block& OperationMaster::get() const + { + block(); + return *mBlockPtr; + } + + template + std::vector OperationMaster::block(const std::shared_ptr blockIndex) const + { + std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); + mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); + return btv; + } + + template + const OperationMaster& OperationMaster::block() const + { + mBlockPtr->set( mIndex->pos() ); + return *this; + } + + /**************************** + * ConstOperationRoot * + ****************************/ + + template + ConstOperationRoot:: + ConstOperationRoot(const MultiArrayBase& ma, + const std::shared_ptr&... indices) : + OperationTemplate >(this), + mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) + { + (*mIndex)(indices...); + } + + template + const Block& ConstOperationRoot::get() const + { + block(); + return *mBlockPtr; + } + + template + std::vector ConstOperationRoot::block(const std::shared_ptr blockIndex) const + { + std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); + mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); + return btv; + } + + template + const ConstOperationRoot& ConstOperationRoot::block() const + { + mBlockPtr->set( (*mIndex)().pos() ); + return *this; + } + + /*********************** + * OperationRoot * + ***********************/ + + template + OperationRoot:: + OperationRoot(MutableMultiArrayBase& ma, + const std::shared_ptr&... indices) : + OperationTemplate >(this), + mArrayRef(ma), mIndex( std::make_shared( mArrayRef.range() ) ) + { + (*mIndex)(indices...); + } + + template + template + OperationMaster OperationRoot::operator=(const OpClass& in) + { + return OperationMaster(mArrayRef, in, mIndex); + } + + template + const MBlock& OperationRoot::get() const + { + block(); + return *mBlockPtr; + } + + template + MBlock& OperationRoot::get() + { + block(); + return *mBlockPtr; + } + + template + std::vector OperationRoot::block(const std::shared_ptr blockIndex) const + { + std::vector btv(1, getBlockType(mIndex, blockIndex, true) ); + mBlockPtr = makeBlock(mArrayRef.datav(), btv[0].second, blockIndex->max()); + return btv; + } + + template + const OperationRoot& OperationRoot::block() const + { + mBlockPtr->set( (*mIndex)().pos() ); + return *this; + } + + /******************* + * Operation * + *******************/ + + template + Operation::Operation(const Ops&... ops) : + OperationTemplate >(this), + mOps(ops...) {} + + template + const BlockResult& Operation::get() const + { + mRes = std::move( PackNum::template unpackArgs(mOps) ); + return mRes; + } + + template + std::vector Operation::block(const std::shared_ptr blockIndex) const + { + std::vector btv; + PackNum::makeBlockTypeVec(btv, mOps, blockIndex); + return btv; + } + + template + const Operation& Operation::block() const + { + //mBlockPtr->set( mIndex->pos() ); + return *this; + } + + /********************* + * Contraction * + *********************/ + + template + Contraction::Contraction(const Op& op, std::shared_ptr ind) : + OperationTemplate >(this), + mOp(op), + mInd(ind) {} + + template + const BlockResult& Contraction::get() const + { + BlockBinaryOpSelf,decltype(mOp.get())> f(mRes); + for(*mInd = 0; mInd->pos() != mInd->max(); ++(*mInd)){ + f(mOp.get()); + } + return mRes; + } + + template + std::vector Contraction::block(const std::shared_ptr blockIndex) const + { + return mOp.block(blockIndex); + } + + template + const Contraction& Contraction::block() const + { + return *this; + } + +} #endif diff --git a/src/multi_range.cc b/src/multi_range.cc deleted file mode 100644 index 049ab48..0000000 --- a/src/multi_range.cc +++ /dev/null @@ -1,320 +0,0 @@ - -#include "multi_range.h" -#include "pack_num.h" - -namespace MultiArrayTools -{ - - namespace - { - using namespace MultiArrayHelper; - } - - /****************** - * MultiIndex * - ******************/ - - /* - template - MultiIndex::MultiIndex(const MultiIndex& in) : - IndexInterface >(in) - { - PackNum::copy(mIPack, in); - IB::mPos = PackNum::makePos(mIPack); - } - - template - MultiIndex& MultiIndex::operator=(const MultiIndex& in) - { - IndexI::operator=(in); - PackNum::copy(mIPack, in); - IB::mPos = PackNum::makePos(mIPack); - return *this; - } - */ - template - MultiIndex& MultiIndex::operator=(ContainerIndex& ci) - { - PackNum::copyInst(mIPack, ci); - IB::mPos = PackNum::makePos(mIPack); - return *this; - } - - template - template - MultiIndex::MultiIndex(const std::shared_ptr& range) : - IndexInterface >(range, 0) - { - PackNum::construct(mIPack, *range); - IB::mPos = PackNum::makePos(mIPack); - std::get(mBlockSizes) = 1; - PackNum::initBlockSizes(mBlockSizes, mIPack); // has one more element! - } - - template - IndexType MultiIndex::type() const - { - return IndexType::MULTI; - } - - template - MultiIndex& MultiIndex::operator++() - { - PackNum::pp( mIPack ); - ++IB::mPos; - return *this; - } - - template - MultiIndex& MultiIndex::operator--() - { - PackNum::mm( mIPack ); - --IB::mPos; - return *this; - } - - template - MultiIndex& MultiIndex::operator=(size_t pos) - { - IB::mPos = pos; - PackNum::setIndexPack(mIPack, pos); - return *this; - } - - template - int MultiIndex::pp(std::shared_ptr& idxPtr) - { - int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtr); - IB::mPos += tmp; - return tmp; - } - - template - int MultiIndex::mm(std::shared_ptr& idxPtr) - { - int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtr); - IB::mPos -= tmp; - return tmp; - } - - template - template - MultiIndex& MultiIndex::up() - { - static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); - IB::mPos += PackNum::blockSize( mIPack ); - PackNum::pp( mIPack ); - return *this; - } - - template - template - MultiIndex& MultiIndex::down() - { - static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); - IB::mPos -= PackNum::blockSize( mIPack ); - PackNum::mm( mIPack ); - return *this; - } - - template - size_t MultiIndex::dim() const - { - return sizeof...(Indices); - } - - template - template - auto MultiIndex::get() const -> decltype( *std::get( mIPack ) )& - { - return *std::get(mIPack); - } - - template - template - auto MultiIndex::getPtr() const -> decltype( std::get( mIPack ) )& - { - return std::get(mIPack); - } - - template - const IndexBase& MultiIndex::get(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - MultiIndex const* t = this; - return PackNum::getIndex(*t, n); - } - - template - std::shared_ptr MultiIndex::getPtr(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - MultiIndex const* t = this; - return PackNum::getIndexPtr(*t, n); - } - - template - size_t MultiIndex::getStepSize(size_t n) const - { - if(n >= sizeof...(Indices)){ - assert(0); - // throw !! - } - return mBlockSizes[n+1]; - } - - template - typename MultiIndex::MetaType MultiIndex::meta() const - { - MetaType metaTuple; - PackNum::getMetaPos(metaTuple, mIPack); - return metaTuple; - } - - template - MultiIndex& MultiIndex::at(const MultiIndex::MetaType& metaPos) - { - PackNum::setMeta(mIPack, metaPos); - IB::mPos = PackNum::makePos(mIPack); - return *this; - } - - template - bool MultiIndex::first() const - { - return IB::mPos == 0; - } - - template - bool MultiIndex::last() const - { - return IB::mPos == IB::mRangePtr->size() - 1; - } - - template - std::shared_ptr::RangeType> MultiIndex::range() const - { - return std::dynamic_pointer_cast( IB::mRangePtr ); - } - /* - template - MultiIndex& MultiIndex::lock(std::shared_ptr& idx) - { - IB::mLocked = (idx.get() == this); - PackNum::lock(mIPack, idx); - return *this; - }*/ - - template - MultiIndex& MultiIndex::operator()(std::shared_ptr&... indices) - { - PackNum::swapIndices(mIPack, indices...); - PackNum::setIndexPack(mIPack, IB::mPos); - return *this; - } - - /************************* - * MultiRangeFactory * - *************************/ - - template - MultiRangeFactory::MultiRangeFactory(const std::shared_ptr&... rs) - { - mProd = std::shared_ptr< MultiRange >( new MultiRange( rs... ) ); - } - - template - MultiRangeFactory::MultiRangeFactory(const typename MultiRange::SpaceType& st) - { - mProd = std::shared_ptr< MultiRange >( new MultiRange( st ) ); - } - - template - MultiRangeFactory::MultiRangeFactory(const std::shared_ptr >& cr) - { - mProd = std::shared_ptr< MultiRange >( new MultiRange( cr->space() ) ); - } - - template - std::shared_ptr MultiRangeFactory::create() - { - setSelf(); - return mProd; - } - - /****************** - * MultiRange * - ******************/ - - template - MultiRange::MultiRange(const std::shared_ptr&... rs) : mSpace(std::make_tuple(rs...)) {} - - template - MultiRange::MultiRange(const SpaceType& space) : mSpace( space ) {} - - template - template - auto MultiRange::get() const -> decltype( *std::get( mSpace ) )& - { - return *std::get(mSpace); - } - - template - template - auto MultiRange::getPtr() const -> decltype( std::get( mSpace ) )& - { - return std::get(mSpace); - } - - template - size_t MultiRange::dim() const - { - return sdim; - } - - template - size_t MultiRange::size() const - { - return PackNum::getSize(mSpace); - } - - template - const typename MultiRange::SpaceType& MultiRange::space() const - { - return mSpace; - } - - template - typename MultiRange::IndexType MultiRange::begin() const - { - MultiIndex - i( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - i = 0; - return i; - } - - template - typename MultiRange::IndexType MultiRange::end() const - { - MultiIndex - i( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis )) ); - i = size(); - return i; - } - - template - std::shared_ptr MultiRange::index() const - { - return std::make_shared > - ( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - } -} diff --git a/src/multi_range.h b/src/multi_range.h index bfd1285..ecfe670 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -11,6 +11,8 @@ #include "range_base.h" #include "index_base.h" +#include "pack_num.h" + namespace MultiArrayTools { @@ -152,6 +154,325 @@ namespace MultiArrayTools } -#include "multi_range.cc" +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayTools +{ + + namespace + { + using namespace MultiArrayHelper; + } + + /****************** + * MultiIndex * + ******************/ + + /* + template + MultiIndex::MultiIndex(const MultiIndex& in) : + IndexInterface >(in) + { + PackNum::copy(mIPack, in); + IB::mPos = PackNum::makePos(mIPack); + } + + template + MultiIndex& MultiIndex::operator=(const MultiIndex& in) + { + IndexI::operator=(in); + PackNum::copy(mIPack, in); + IB::mPos = PackNum::makePos(mIPack); + return *this; + } + */ + template + MultiIndex& MultiIndex::operator=(ContainerIndex& ci) + { + PackNum::copyInst(mIPack, ci); + IB::mPos = PackNum::makePos(mIPack); + return *this; + } + + template + template + MultiIndex::MultiIndex(const std::shared_ptr& range) : + IndexInterface >(range, 0) + { + PackNum::construct(mIPack, *range); + IB::mPos = PackNum::makePos(mIPack); + std::get(mBlockSizes) = 1; + PackNum::initBlockSizes(mBlockSizes, mIPack); // has one more element! + } + + template + IndexType MultiIndex::type() const + { + return IndexType::MULTI; + } + + template + MultiIndex& MultiIndex::operator++() + { + PackNum::pp( mIPack ); + ++IB::mPos; + return *this; + } + + template + MultiIndex& MultiIndex::operator--() + { + PackNum::mm( mIPack ); + --IB::mPos; + return *this; + } + + template + MultiIndex& MultiIndex::operator=(size_t pos) + { + IB::mPos = pos; + PackNum::setIndexPack(mIPack, pos); + return *this; + } + + template + int MultiIndex::pp(std::shared_ptr& idxPtr) + { + int tmp = PackNum::pp(mIPack, mBlockSizes, idxPtr); + IB::mPos += tmp; + return tmp; + } + + template + int MultiIndex::mm(std::shared_ptr& idxPtr) + { + int tmp = PackNum::mm(mIPack, mBlockSizes, idxPtr); + IB::mPos -= tmp; + return tmp; + } + + template + template + MultiIndex& MultiIndex::up() + { + static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); + IB::mPos += PackNum::blockSize( mIPack ); + PackNum::pp( mIPack ); + return *this; + } + + template + template + MultiIndex& MultiIndex::down() + { + static_assert(DIR < sizeof...(Indices), "DIR exceeds number of sub-indices"); + IB::mPos -= PackNum::blockSize( mIPack ); + PackNum::mm( mIPack ); + return *this; + } + + template + size_t MultiIndex::dim() const + { + return sizeof...(Indices); + } + + template + template + auto MultiIndex::get() const -> decltype( *std::get( mIPack ) )& + { + return *std::get(mIPack); + } + + template + template + auto MultiIndex::getPtr() const -> decltype( std::get( mIPack ) )& + { + return std::get(mIPack); + } + + template + const IndexBase& MultiIndex::get(size_t n) const + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + MultiIndex const* t = this; + return PackNum::getIndex(*t, n); + } + + template + std::shared_ptr MultiIndex::getPtr(size_t n) const + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + MultiIndex const* t = this; + return PackNum::getIndexPtr(*t, n); + } + + template + size_t MultiIndex::getStepSize(size_t n) const + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + return mBlockSizes[n+1]; + } + + template + typename MultiIndex::MetaType MultiIndex::meta() const + { + MetaType metaTuple; + PackNum::getMetaPos(metaTuple, mIPack); + return metaTuple; + } + + template + MultiIndex& MultiIndex::at(const MultiIndex::MetaType& metaPos) + { + PackNum::setMeta(mIPack, metaPos); + IB::mPos = PackNum::makePos(mIPack); + return *this; + } + + template + bool MultiIndex::first() const + { + return IB::mPos == 0; + } + + template + bool MultiIndex::last() const + { + return IB::mPos == IB::mRangePtr->size() - 1; + } + + template + std::shared_ptr::RangeType> MultiIndex::range() const + { + return std::dynamic_pointer_cast( IB::mRangePtr ); + } + /* + template + MultiIndex& MultiIndex::lock(std::shared_ptr& idx) + { + IB::mLocked = (idx.get() == this); + PackNum::lock(mIPack, idx); + return *this; + }*/ + + template + MultiIndex& MultiIndex::operator()(std::shared_ptr&... indices) + { + PackNum::swapIndices(mIPack, indices...); + PackNum::setIndexPack(mIPack, IB::mPos); + return *this; + } + + /************************* + * MultiRangeFactory * + *************************/ + + template + MultiRangeFactory::MultiRangeFactory(const std::shared_ptr&... rs) + { + mProd = std::shared_ptr< MultiRange >( new MultiRange( rs... ) ); + } + + template + MultiRangeFactory::MultiRangeFactory(const typename MultiRange::SpaceType& st) + { + mProd = std::shared_ptr< MultiRange >( new MultiRange( st ) ); + } + + template + MultiRangeFactory::MultiRangeFactory(const std::shared_ptr >& cr) + { + mProd = std::shared_ptr< MultiRange >( new MultiRange( cr->space() ) ); + } + + template + std::shared_ptr MultiRangeFactory::create() + { + setSelf(); + return mProd; + } + + /****************** + * MultiRange * + ******************/ + + template + MultiRange::MultiRange(const std::shared_ptr&... rs) : mSpace(std::make_tuple(rs...)) {} + + template + MultiRange::MultiRange(const SpaceType& space) : mSpace( space ) {} + + template + template + auto MultiRange::get() const -> decltype( *std::get( mSpace ) )& + { + return *std::get(mSpace); + } + + template + template + auto MultiRange::getPtr() const -> decltype( std::get( mSpace ) )& + { + return std::get(mSpace); + } + + template + size_t MultiRange::dim() const + { + return sdim; + } + + template + size_t MultiRange::size() const + { + return PackNum::getSize(mSpace); + } + + template + const typename MultiRange::SpaceType& MultiRange::space() const + { + return mSpace; + } + + template + typename MultiRange::IndexType MultiRange::begin() const + { + MultiIndex + i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = 0; + return i; + } + + template + typename MultiRange::IndexType MultiRange::end() const + { + MultiIndex + i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis )) ); + i = size(); + return i; + } + + template + std::shared_ptr MultiRange::index() const + { + return std::make_shared > + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + } +} #endif diff --git a/src/range_base.cc b/src/range_base.cc deleted file mode 100644 index 133078c..0000000 --- a/src/range_base.cc +++ /dev/null @@ -1,30 +0,0 @@ - -#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 != ∈ - } - -} diff --git a/src/range_base.h b/src/range_base.h index e06f000..c977c99 100644 --- a/src/range_base.h +++ b/src/range_base.h @@ -81,6 +81,36 @@ namespace MultiArrayTools } -#include "range_base.cc" +/* ========================= * + * --- 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_factory.cc b/src/range_factory.cc deleted file mode 100644 index 4ab5e40..0000000 --- a/src/range_factory.cc +++ /dev/null @@ -1,8 +0,0 @@ -// -*- C++ -*- - -#include "range_factory.h" - -namespace MultiArrayTools -{ - -} //namespace MultiArrayTools diff --git a/src/range_factory.h b/src/range_factory.h index daca600..99a4714 100644 --- a/src/range_factory.h +++ b/src/range_factory.h @@ -21,6 +21,6 @@ namespace MultiArrayTools } //namespace MultiArrayTools -#include "range_factory.cc" +// === NO TEMPLATE CODE HERE === #endif diff --git a/src/single_range.cc b/src/single_range.cc deleted file mode 100644 index 7e0e98d..0000000 --- a/src/single_range.cc +++ /dev/null @@ -1,379 +0,0 @@ - -#include "single_range.h" - -namespace MultiArrayTools -{ - /****************** - * SingleIndex * - ******************/ - - template - SingleIndex::SingleIndex(const std::shared_ptr >& range) : - IndexInterface(range, 0) {} - - template - IndexType SingleIndex::type() const - { - return IndexType::SINGLE; - } - - template - SingleIndex& SingleIndex::operator=(size_t pos) - { - IB::mPos = pos; - return *this; - } - - template - SingleIndex& SingleIndex::operator++() - { - ++IB::mPos; - return *this; - } - - template - SingleIndex& SingleIndex::operator--() - { - --IB::mPos; - return *this; - } - - template - int SingleIndex::pp(std::shared_ptr& idxPtr) - { - ++(*this); - return 1; - } - - template - int SingleIndex::mm(std::shared_ptr& idxPtr) - { - --(*this); - return 1; - } - - template - U SingleIndex::meta() const - { - return std::dynamic_pointer_cast const>( IB::mRangePtr )->get( IB::pos() ); - } - - template - SingleIndex& SingleIndex::at(const U& metaPos) - { - operator=( std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ) ); - return *this; - } - - template - size_t SingleIndex::dim() const - { - return 1; - } - - template - bool SingleIndex::last() const - { - return IB::mPos == IB::mRangePtr->size() - 1; - } - - template - bool SingleIndex::first() const - { - return IB::mPos == 0; - } - - template - std::shared_ptr SingleIndex::getPtr(size_t n) const - { - return std::shared_ptr(); - } - - template - size_t SingleIndex::getStepSize(size_t n) const - { - return 1; - } - - /******************** - * SingleRange * - ********************/ - - template - SingleRangeFactory::SingleRangeFactory(const std::vector& space) - { - mProd = std::shared_ptr( new SingleRange( space ) ); - } - - template - std::shared_ptr SingleRangeFactory::create() - { - setSelf(); - return mProd; - } - - /******************** - * SingleRange * - ********************/ - - template - SingleRange::SingleRange(const std::vector& space) : RangeInterface >(), - mSpace(space) {} - - template - const U& SingleRange::get(size_t pos) const - { - return mSpace[pos]; - } - - template - size_t SingleRange::getMeta(const U& metaPos) const - { - size_t cnt = 0; - for(auto& x: mSpace){ - if(x == metaPos){ - return cnt; - } - ++cnt; - } - return cnt; - } - - template - size_t SingleRange::size() const - { - return mSpace.size(); - } - - template - size_t SingleRange::dim() const - { - return 1; - } - - template - typename SingleRange::IndexType SingleRange::begin() const - { - SingleIndex i( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - i = 0; - return i; - } - - template - typename SingleRange::IndexType SingleRange::end() const - { - SingleIndex i( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - i = size(); - return i; - } - - // put this in the interface class !!! - template - std::shared_ptr SingleRange::index() const - { - return std::make_shared > - ( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - } - - /* - // specializations (not updated!!!) - - SingleRange::SingleRange(size_t ext) : - RangeBase >(), - mExt(ext) {} - - int SingleRange::get(size_t pos) const - { - return (pos <= mExt / 2) ? static_cast( pos ) : static_cast( pos ) - static_cast( mExt ); - } - - size_t SingleRange::getMeta(int metaPos) const - { - return (metaPos < 0) ? metaPos + mExt : metaPos; - } - - - size_t SingleRange::size() const - { - return mExt; - } - - - MultiRangeType SingleRange::type() const - { - return MultiRangeType(RangeType::SPACE); - } - - - SingleIndex SingleRange::begin() const - { - return SingleIndex(this, 0); - } - - - SingleIndex SingleRange::end() const - { - return SingleIndex(this, size()); - } - - // - - SingleRange::SingleRange(size_t ext) : - RangeBase >(), - mExt(ext) {} - - size_t SingleRange::get(size_t pos) const - { - return pos; - } - - size_t SingleRange::getMeta(size_t metaPos) const - { - return metaPos; - } - - size_t SingleRange::size() const - { - return mExt; - } - - MultiRangeType SingleRange::type() const - { - return MultiRangeType(RangeType::DISTANCE); - } - - SingleIndex SingleRange::begin() const - { - return SingleIndex(this, 0); - } - - SingleIndex SingleRange::end() const - { - return SingleIndex(this, size()); - } - - // - - SingleRange::SingleRange(size_t num) : - RangeBase >(), - mNum(num) {} - - - size_t SingleRange::get(size_t pos) const - { - return pos; - } - - - size_t SingleRange::getMeta(size_t metaPos) const - { - return metaPos; - } - - - size_t SingleRange::size() const - { - return mNum; - } - - - MultiRangeType SingleRange::type() const - { - return MultiRangeType(RangeType::ENSEMBLE); - } - - - SingleIndex SingleRange::begin() const - { - return SingleIndex(this, 0); - } - - - SingleIndex SingleRange::end() const - { - return SingleIndex(this, size()); - } - - // - - VET SingleRange::get(size_t pos) const - { - return static_cast( pos ); - } - - - size_t SingleRange::getMeta(VET metaPos) const - { - return static_cast( metaPos ); - } - - - size_t SingleRange::size() const - { - return 2; - } - - - MultiRangeType SingleRange::type() const - { - return MultiRangeType(RangeType::VALUE_ERROR); - } - - - SingleIndex SingleRange::begin() const - { - return SingleIndex(this, 0); - } - - - SingleIndex SingleRange::end() const - { - return SingleIndex(this, size()); - } - - // - - - size_t SingleRange::get(size_t pos) const - { - return pos; - } - - - size_t SingleRange::getMeta(size_t metaPos) const - { - return metaPos; - } - - - size_t SingleRange::size() const - { - #ifdef LORENTZ_DIMENSION - return LORENTZ_DIMENSION; - #else - return 4; // 4 - #endif - } - - - MultiRangeType SingleRange::type() const - { - return MultiRangeType(RangeType::LORENTZ); - } - - - SingleIndex SingleRange::begin() const - { - return SingleIndex(this, 0); - } - - - SingleIndex SingleRange::end() const - { - return SingleIndex(this, size()); - } - */ -} diff --git a/src/single_range.h b/src/single_range.h index 8cc4a23..100a60a 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -90,133 +90,190 @@ namespace MultiArrayTools std::vector mSpace; }; - /* - // specializaions - template <> - class SingleRange : public RangeBase > - { - public: - typedef typename RangeBase >::IndexType IndexType; - - static SingleRange oType() { return SingleRange(); } - - virtual size_t size() const override; - - int get(size_t pos) const; - size_t getMeta(int metaPos) const; - - virtual MultiRangeType type() const override; - - SingleIndex begin() const override; - SingleIndex end() const override; - - protected: - - SingleRange(size_t ext); - - size_t mExt; - }; - - template <> - class SingleRange : public RangeBase > - { - public: - typedef typename RangeBase >::IndexType IndexType; - - static SingleRange oType() { return SingleRange(); } - - virtual size_t size() const override; - - size_t get(size_t pos) const; - size_t getMeta(size_t metaPos) const; - - virtual MultiRangeType type() const override; - - SingleIndex begin() const override; - SingleIndex end() const override; - - protected: - - SingleRange(size_t ext); - - size_t mExt; - }; - - template <> - class SingleRange : public RangeBase > - { - public: - typedef typename RangeBase >::IndexType IndexType; - - static SingleRange oType() { return SingleRange(); } - - virtual size_t size() const override; - - size_t get(size_t pos) const; - size_t getMeta(size_t metaPos) const; - - virtual MultiRangeType type() const override; - - SingleIndex begin() const override; - SingleIndex end() const override; - - protected: - - SingleRange(size_t num); - - size_t mNum; - }; - - enum class VET - { - VALUE = 0, - ERROR = 1 - }; - - std::ostream& operator<<(std::ostream& os, VET vet); - - template <> - class SingleRange : public RangeBase > - { - public: - typedef typename RangeBase >::IndexType IndexType; - - static SingleRange oType() - { return SingleRange(); } - - virtual size_t size() const override; - - VET get(size_t pos) const; - size_t getMeta(VET metaPos) const; - - virtual MultiRangeType type() const override; - - SingleIndex begin() const override; - SingleIndex end() const override; - }; - - template <> - class SingleRange : public RangeBase > - { - public: - typedef typename RangeBase >::IndexType IndexType; - - static SingleRange oType() { return SingleRange(); } - - virtual size_t size() const override; - - size_t get(size_t pos) const; - size_t getMeta(size_t metaPos) const; - - virtual MultiRangeType type() const override; - - SingleIndex begin() const override; - SingleIndex end() const override; - }; - */ } -#include "single_range.cc" +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayTools +{ + /****************** + * SingleIndex * + ******************/ + + template + SingleIndex::SingleIndex(const std::shared_ptr >& range) : + IndexInterface(range, 0) {} + + template + IndexType SingleIndex::type() const + { + return IndexType::SINGLE; + } + + template + SingleIndex& SingleIndex::operator=(size_t pos) + { + IB::mPos = pos; + return *this; + } + + template + SingleIndex& SingleIndex::operator++() + { + ++IB::mPos; + return *this; + } + + template + SingleIndex& SingleIndex::operator--() + { + --IB::mPos; + return *this; + } + + template + int SingleIndex::pp(std::shared_ptr& idxPtr) + { + ++(*this); + return 1; + } + + template + int SingleIndex::mm(std::shared_ptr& idxPtr) + { + --(*this); + return 1; + } + + template + U SingleIndex::meta() const + { + return std::dynamic_pointer_cast const>( IB::mRangePtr )->get( IB::pos() ); + } + + template + SingleIndex& SingleIndex::at(const U& metaPos) + { + operator=( std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ) ); + return *this; + } + + template + size_t SingleIndex::dim() const + { + return 1; + } + + template + bool SingleIndex::last() const + { + return IB::mPos == IB::mRangePtr->size() - 1; + } + + template + bool SingleIndex::first() const + { + return IB::mPos == 0; + } + + template + std::shared_ptr SingleIndex::getPtr(size_t n) const + { + return std::shared_ptr(); + } + + template + size_t SingleIndex::getStepSize(size_t n) const + { + return 1; + } + + /******************** + * SingleRange * + ********************/ + + template + SingleRangeFactory::SingleRangeFactory(const std::vector& space) + { + mProd = std::shared_ptr( new SingleRange( space ) ); + } + + template + std::shared_ptr SingleRangeFactory::create() + { + setSelf(); + return mProd; + } + + /******************** + * SingleRange * + ********************/ + + template + SingleRange::SingleRange(const std::vector& space) : RangeInterface >(), + mSpace(space) {} + + template + const U& SingleRange::get(size_t pos) const + { + return mSpace[pos]; + } + + template + size_t SingleRange::getMeta(const U& metaPos) const + { + size_t cnt = 0; + for(auto& x: mSpace){ + if(x == metaPos){ + return cnt; + } + ++cnt; + } + return cnt; + } + + template + size_t SingleRange::size() const + { + return mSpace.size(); + } + + template + size_t SingleRange::dim() const + { + return 1; + } + + template + typename SingleRange::IndexType SingleRange::begin() const + { + SingleIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = 0; + return i; + } + + template + typename SingleRange::IndexType SingleRange::end() const + { + SingleIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = size(); + return i; + } + + // put this in the interface class !!! + template + std::shared_ptr SingleRange::index() const + { + return std::make_shared > + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + } + +} #endif