diff --git a/latan/Sample.cpp b/latan/Sample.cpp index 73d3b4c..0f80484 100644 --- a/latan/Sample.cpp +++ b/latan/Sample.cpp @@ -34,10 +34,76 @@ DMatSample::DMatSample(const Index nSample, const Index nRow, resizeMat(nRow, nCol); } +DMatSample::DMatSample(ConstBlock &sampleBlock) +: DMatSample(sampleBlock.getSample().size(), sampleBlock.getNRow(), + sampleBlock.getNCol()) +{ + const DMatSample &sample = sampleBlock.getSample(); + + FOR_STAT_ARRAY(*this, s) + { + (*this)[s] = sample[s].block(sampleBlock.getStartRow(), + sampleBlock.getStartCol(), + sampleBlock.getNRow(), + sampleBlock.getNCol()); + } +} + +DMatSample::DMatSample(ConstBlock &&sampleBlock) +: DMatSample(sampleBlock) +{} + +// assignement operator //////////////////////////////////////////////////////// +DMatSample & DMatSample::operator=(Block &sampleBlock) +{ + DMatSample tmp(sampleBlock); + + this->swap(tmp); + + return *this; +} + +DMatSample & DMatSample::operator=(Block &&sampleBlock) +{ + *this = sampleBlock; + + return *this; +} + +DMatSample & DMatSample::operator=(ConstBlock &sampleBlock) +{ + DMatSample tmp(sampleBlock); + + this->swap(tmp); + + return *this; +} + +DMatSample & DMatSample::operator=(ConstBlock &&sampleBlock) +{ + *this = sampleBlock; + + return *this; +} + +// block access //////////////////////////////////////////////////////////////// +DMatSample::ConstBlock DMatSample::block(const Index i, const Index j, + const Index nRow, + const Index nCol) const +{ + return ConstBlock(*this, i, j, nRow, nCol); +} + +DMatSample::Block DMatSample::block(const Index i, const Index j, + const Index nRow, const Index nCol) +{ + return Block(*this, i, j, nRow, nCol); +} + // resize all matrices ///////////////////////////////////////////////////////// void DMatSample::resizeMat(const Index nRow, const Index nCol) { - FOR_VEC(*this, s) + FOR_STAT_ARRAY(*this, s) { (*this)[s].resize(nRow, nCol); } diff --git a/latan/Sample.hpp b/latan/Sample.hpp index 51404d4..0546e88 100644 --- a/latan/Sample.hpp +++ b/latan/Sample.hpp @@ -50,21 +50,154 @@ public: /****************************************************************************** * DMatSample class * ******************************************************************************/ + class DMatSample: public Sample, public IoObject { +public: + // block type template + template + class BlockTemplate + { + private: + typedef typename std::remove_const::type NonConstType; + public: + // constructors + BlockTemplate(S &sample, const Index i, const Index j, const Index nRow, + const Index nCol); + BlockTemplate(BlockTemplate &b); + BlockTemplate(BlockTemplate &&b); + // destructor + ~BlockTemplate(void) = default; + // access + S & getSample(void); + const S & getSample(void) const; + Index getStartRow(void) const; + Index getStartCol(void) const; + Index getNRow(void) const; + Index getNCol(void) const; + // assignement operators + BlockTemplate & operator=(const S &sample); + BlockTemplate & operator=(const S &&sample); + private: + S &sample_; + const Index i_, j_, nRow_, nCol_; + }; + // block types + typedef BlockTemplate Block; + typedef const BlockTemplate ConstBlock; public: // constructors DMatSample(void) = default; DMatSample(const Index nSample, const Index nRow, const Index nCol); + DMatSample(ConstBlock &sampleBlock); + DMatSample(ConstBlock &&sampleBlock); using Sample::Sample; // destructor virtual ~DMatSample(void) = default; + // assignement operator + DMatSample & operator=(Block &sampleBlock); + DMatSample & operator=(Block &&sampleBlock); + DMatSample & operator=(ConstBlock &sampleBlock); + DMatSample & operator=(ConstBlock &&sampleBlock); + // block access + ConstBlock block(const Index i, const Index j, const Index nRow, + const Index nCol) const; + Block block(const Index i, const Index j, const Index nRow, + const Index nCol); // resize all matrices void resizeMat(const Index nRow, const Index nCol); // IO type virtual IoType getType(void) const; }; +/****************************************************************************** + * Block template implementation * + ******************************************************************************/ +// constructors //////////////////////////////////////////////////////////////// +template +DMatSample::BlockTemplate::BlockTemplate(S &sample, const Index i, + const Index j, const Index nRow, + const Index nCol) +: sample_(sample) +, i_(i) +, j_(j) +, nRow_(nRow) +, nCol_(nCol) +{} + +template +DMatSample::BlockTemplate::BlockTemplate(BlockTemplate &b) +: sample_(b.getSample()) +, i_(b.getStartRow()) +, j_(b.getStartCol()) +, nRow_(b.getNRow()) +, nCol_(b.getNCol()) +{} + +template +DMatSample::BlockTemplate::BlockTemplate(BlockTemplate &&b) +: BlockTemplate(b) +{} + +// access ////////////////////////////////////////////////////////////////////// +template +S & DMatSample::BlockTemplate::getSample(void) +{ + return sample_; +} + +template +const S & DMatSample::BlockTemplate::getSample(void) const +{ + return sample_; +} + +template +Index DMatSample::BlockTemplate::getStartRow(void) const +{ + return i_; +} + +template +Index DMatSample::BlockTemplate::getStartCol(void) const +{ + return j_; +} + +template +Index DMatSample::BlockTemplate::getNRow(void) const +{ + return nRow_; +} + +template +Index DMatSample::BlockTemplate::getNCol(void) const +{ + return nCol_; +} + +// assignement operators /////////////////////////////////////////////////////// +template +DMatSample::BlockTemplate & +DMatSample::BlockTemplate::operator=(const S &sample) +{ + FOR_STAT_ARRAY(sample_, s) + { + sample_[s].block(i_, j_, nRow_, nCol_) = sample[s]; + } + + return *this; +} + +template +DMatSample::BlockTemplate & +DMatSample::BlockTemplate::operator=(const S &&sample) +{ + *this = sample; + + return *this; +} + END_NAMESPACE #endif // Latan_Sample_hpp_