StdMatrix.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2010 Cassio Neri Moreira
00004  *
00005  * This file is part of the KeyValue library.
00006  *
00007  * The KeyValue library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or (at
00010  * your option) any later version.
00011  *
00012  * The KeyValue library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00015  * Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with KeyValue. If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  **************************************************************************/
00021 
00029 #ifndef KEYVALUE_KEY_CONVERTER_STDMATRIX_H_
00030 #define KEYVALUE_KEY_CONVERTER_STDMATRIX_H_
00031 
00032 #include "keyvalue/extern/SharedPtr.h"
00033 #include "keyvalue/extern/Vector.h"
00034 #include "keyvalue/sys/exception/KV_ASSERT.h"
00035 #include "keyvalue/value/Variant.h"
00036 #include "keyvalue/value/Matrix.h"
00037 
00038 namespace keyvalue {
00039 namespace key {
00040 
00058 template <typename ElementType>
00059 class StdMatrix {
00060 
00061 public:
00062 
00066   typedef value::Matrix InputType_;
00067 
00074   typedef shared_ptr<std::vector<std::vector<ElementType> > > OutputType_;
00075 
00088   StdMatrix(const InputType_& input, OutputType_* cache);
00089 
00096   bool
00097   isEmpty() const;
00098 
00104   value::Variant
00105   pop();
00106 
00113   void
00114   insert(const ElementType& element);
00115 
00121   OutputType_
00122   getOutput() const;
00123 
00130   bool
00131   mustUpdate() const;
00132 
00133 private:
00134 
00135   const InputType_& input_;
00136   OutputType_       output_;
00137   OutputType_*      cache_;
00138   size_t            nRows_;
00139   size_t            nCols_;
00140   size_t            i_;
00141   size_t            j_;
00142   bool              mustUpdate_;
00143 
00144 };
00145 
00146 /*--------------------------------------------------------------------------
00147  * StdMatrix()
00148  *------------------------------------------------------------------------*/
00149 
00150 template <typename ElementType>
00151 StdMatrix<ElementType>::StdMatrix(const InputType_& input,
00152   OutputType_* const cache) : input_(input), cache_(cache),
00153   output_(new std::vector<std::vector<ElementType> >),
00154   nRows_(input.getNRows()), nCols_(input.getNCols()), i_(0), j_(0),
00155   mustUpdate_(!cache) {
00156 
00157   KV_ASSERT(nRows_ != 0 && nCols_ != 0, "Empty matrix given to StdMatrix!");
00158 
00159   output_->reserve(nRows_);
00160   output_->push_back(std::vector<ElementType>());
00161   output_->back().reserve(nCols_);
00162 
00163   mustUpdate_ = mustUpdate_ || (*cache_)->size() != nRows_ ||
00164     (*(*cache_))[0].size() != nCols_;
00165 }
00166 
00167 /*--------------------------------------------------------------------------
00168  * end()
00169  *------------------------------------------------------------------------*/
00170 
00171 template <typename ElementType>
00172 bool
00173 StdMatrix<ElementType>::isEmpty() const {
00174   return (j_ == nCols_ && i_ == nRows_ - 1);
00175 }
00176 
00177 /*--------------------------------------------------------------------------
00178  * pop()
00179  *------------------------------------------------------------------------*/
00180 
00181 template <typename ElementType>
00182 value::Variant
00183 StdMatrix<ElementType>::pop() {
00184 
00185   KV_ASSERT(!isEmpty(), "All elements have been processed!");
00186 
00187   if (j_ == nCols_) {
00188     j_ = 0;
00189     ++i_;
00190   }
00191 
00192   return input_(i_, j_++);
00193 }
00194 
00195 /*--------------------------------------------------------------------------
00196  * insert()
00197  *------------------------------------------------------------------------*/
00198 
00199 template <typename ElementType>
00200 void
00201 StdMatrix<ElementType>::insert(const ElementType& element) {
00202 
00203   if (output_->back().size() == nCols_) {
00204     output_->push_back(std::vector<ElementType>());
00205     output_->back().reserve(nCols_);
00206   }
00207 
00208   output_->back().push_back(element);
00209 
00210   KV_ASSERT(i_ < nRows_, "Calls to StdMatrix.pop() and StdMatrix.insert() "
00211     "must be intercalated!");
00212 
00213   mustUpdate_ = mustUpdate_ ||
00214     (*(*cache_))[j_ = 0 ? i_-1 : i_][j_ = 0 ? nCols_-1 : j_-1] != element;
00215 }
00216 
00217 /*--------------------------------------------------------------------------
00218  * getOutput()
00219  *------------------------------------------------------------------------*/
00220 
00221 template <typename ElementType>
00222 typename StdMatrix<ElementType>::OutputType_
00223 StdMatrix<ElementType>::getOutput() const {
00224   return output_;
00225 }
00226 
00227 /*--------------------------------------------------------------------------
00228  * mustUpdate()
00229  *------------------------------------------------------------------------*/
00230 
00231 template <typename ElementType>
00232 bool
00233 StdMatrix<ElementType>::mustUpdate() const {
00234   return mustUpdate_;
00235 }
00236 
00237 } // namespace key
00238 } // namespace keyvalue
00239 
00240 #endif // KEYVALUE_KEY_CONVERTER_STDMATRIX_H_

Generated on Sat Mar 20 15:08:29 2010 for KeyValue by  doxygen 1.6.1