Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00035 #ifndef KEYVALUE_KEY_CONVERTER_STDMATRIX_H_
00036 #define KEYVALUE_KEY_CONVERTER_STDMATRIX_H_
00037
00038 #include "keyvalue/extern/SharedPtr.h"
00039 #include "keyvalue/extern/Vector.h"
00040 #include "keyvalue/sys/exception/KV_ASSERT.h"
00041 #include "keyvalue/value/Variant.h"
00042 #include "keyvalue/value/Matrix.h"
00043
00044 namespace keyvalue {
00045 namespace key {
00046
00064 template <typename ElementType>
00065 class StdMatrix {
00066
00067 public:
00068
00072 typedef value::Matrix InputType_;
00073
00080 typedef shared_ptr<std::vector<std::vector<ElementType> > > OutputType_;
00081
00094 StdMatrix(const InputType_& input, OutputType_* cache);
00095
00102 bool
00103 isEmpty() const;
00104
00110 value::Variant
00111 pop();
00112
00119 void
00120 insert(const ElementType& element);
00121
00127 OutputType_
00128 getOutput() const;
00129
00136 bool
00137 mustUpdate() const;
00138
00139 private:
00140
00141 const InputType_& input_;
00142 OutputType_ output_;
00143 OutputType_* cache_;
00144 size_t nRows_;
00145 size_t nCols_;
00146 size_t i_;
00147 size_t j_;
00148 bool mustUpdate_;
00149
00150 };
00151
00152
00153
00154
00155
00156 template <typename ElementType>
00157 StdMatrix<ElementType>::StdMatrix(const InputType_& input,
00158 OutputType_* const cache) :
00159 input_(input),
00160 output_(new std::vector<std::vector<ElementType> >),
00161 cache_(cache),
00162 nRows_(input.getNRows()),
00163 nCols_(input.getNCols()),
00164 i_(0),
00165 j_(0),
00166 mustUpdate_(!cache) {
00167
00168 KV_ASSERT(nRows_ != 0 && nCols_ != 0, "Empty matrix given to StdMatrix!");
00169
00170 output_->reserve(nRows_);
00171 output_->push_back(std::vector<ElementType>());
00172 output_->back().reserve(nCols_);
00173
00174 mustUpdate_ = mustUpdate_ || (*cache_)->size() != nRows_ ||
00175 (*(*cache_))[0].size() != nCols_;
00176 }
00177
00178
00179
00180
00181
00182 template <typename ElementType>
00183 bool
00184 StdMatrix<ElementType>::isEmpty() const {
00185 return (j_ == nCols_ && i_ == nRows_ - 1);
00186 }
00187
00188
00189
00190
00191
00192 template <typename ElementType>
00193 value::Variant
00194 StdMatrix<ElementType>::pop() {
00195
00196 KV_ASSERT(!isEmpty(), "All elements have been processed!");
00197
00198 if (j_ == nCols_) {
00199 j_ = 0;
00200 ++i_;
00201 }
00202
00203 return input_(i_, j_++);
00204 }
00205
00206
00207
00208
00209
00210 template <typename ElementType>
00211 void
00212 StdMatrix<ElementType>::insert(const ElementType& element) {
00213
00214 if (output_->back().size() == nCols_) {
00215 output_->push_back(std::vector<ElementType>());
00216 output_->back().reserve(nCols_);
00217 }
00218
00219 output_->back().push_back(element);
00220
00221 KV_ASSERT(i_ < nRows_, "Calls to StdMatrix.pop() and StdMatrix.insert() "
00222 "must be intercalated!");
00223
00224 mustUpdate_ = mustUpdate_ ||
00225 (*(*cache_))[j_ == 0 ? i_-1 : i_][j_ == 0 ? nCols_-1 : j_-1] != element;
00226 }
00227
00228
00229
00230
00231
00232 template <typename ElementType>
00233 typename StdMatrix<ElementType>::OutputType_
00234 StdMatrix<ElementType>::getOutput() const {
00235 return output_;
00236 }
00237
00238
00239
00240
00241
00242 template <typename ElementType>
00243 bool
00244 StdMatrix<ElementType>::mustUpdate() const {
00245 return mustUpdate_;
00246 }
00247
00248 }
00249 }
00250
00251 #endif // KEYVALUE_KEY_CONVERTER_STDMATRIX_H_