00001 #ifndef BALL_LINALG_ROWITERATOR_H
00002 #define BALL_LINALG_ROWITERATOR_H
00003
00004 #ifndef BALL_LINALG_MATRIX_IH
00005 # include <BALL/MATHS/LINALG/matrix.ih>
00006 #endif
00007
00008 #ifndef BALL_LINALG_VECTOR_IH
00009 # include <BALL/MATHS/LINALG/vector.ih>
00010 #endif
00011
00012 #ifndef BALL_CONCEPT_RANDOMACCESSITERATOR_H
00013 #include <BALL/CONCEPT/randomAccessIterator.h>
00014 #endif
00015
00016
00017 typedef unsigned int uint;
00018
00019 namespace BALL {
00020
00021
00022 template <class valuetype, class mtraits>
00023 class Matrix;
00024
00025 template <class valuetype, class mtraits=StandardTraits>
00026 class RowIteratorTraits
00027 {
00028
00031 typedef valuetype ValueType;
00032
00035 typedef valuetype* PointerType;
00036
00039 typedef int IteratorPosition;
00040
00043 typedef int Distance;
00044
00047 typedef int Index;
00048
00049 friend class Matrix<valuetype, mtraits>;
00050 public:
00051
00052 virtual ~RowIteratorTraits()
00053 throw()
00054 {
00055 }
00056
00057 RowIteratorTraits()
00058 throw()
00059 : bound_(0),
00060 position_(0),
00061 vector_(0)
00062 {
00063 }
00064
00065 RowIteratorTraits(const Matrix<valuetype, mtraits>& matrix) throw()
00066 : bound_(const_cast<Matrix<valuetype, mtraits>*>(&matrix)),
00067 position_(0),
00068 vector_(bound_->m_)
00069 {
00070 }
00071
00072 RowIteratorTraits(const RowIteratorTraits& traits) throw()
00073 : bound_(traits.bound_),
00074 position_(traits.position_),
00075 vector_(bound_->m_)
00076 {
00077 }
00078
00079 RowIteratorTraits& operator = (const RowIteratorTraits& traits) throw()
00080 {
00081 bound_ = traits.bound_;
00082 position_ = traits.position_;
00083 vector_ = traits.vector_;
00084
00085 return *this;
00086 }
00087
00088 Matrix<valuetype, mtraits>* getContainer() throw()
00089 {
00090 return bound_;
00091 }
00092
00093 const Matrix<valuetype, mtraits>* getContainer() const throw()
00094 {
00095 return bound_;
00096 }
00097
00098 bool isSingular() const throw()
00099 {
00100 return (bound_ == 0);
00101 }
00102
00103 IteratorPosition& getPosition() throw()
00104 {
00105 return position_;
00106 }
00107
00108 const IteratorPosition& getPosition() const throw()
00109 {
00110 return position_;
00111 }
00112
00113 bool operator == (const RowIteratorTraits& traits) const throw()
00114 {
00115 return (position_ == traits.position_);
00116 }
00117
00118 bool operator != (const RowIteratorTraits& traits) const throw()
00119 {
00120 return (position_ != traits.position_);
00121 }
00122
00123 bool operator < (const RowIteratorTraits& traits) const throw()
00124 {
00125 return (position_ < traits.position_);
00126 }
00127
00128 Distance getDistance(const RowIteratorTraits& traits) const throw()
00129 {
00130 return (Distance)(position_ - traits.position_);
00131 }
00132
00133 bool isValid() const throw()
00134 {
00135 return ((bound_ != 0) && (position_ >= 0) && (position_ < (int)bound_->data_.size()));
00136 }
00137
00138 void invalidate() throw()
00139 {
00140 bound_ = 0;
00141 position_ = -1;
00142 }
00143
00144 void toBegin() throw()
00145 {
00146 position_ = 0;
00147 }
00148
00149 bool isBegin() const throw()
00150 {
00151 return ( position_ == 0 );
00152 }
00153
00154 void toEnd() throw()
00155 {
00156 position_ = bound_->data_.size();
00157 }
00158
00159 bool isEnd() const throw()
00160 {
00161 return ( position_ == (int)bound_->data_.size());
00162 }
00163
00164 Vector<valuetype>& getData() throw()
00165 {
00166
00167 if (bound_->row_major_)
00168 {
00169 for (uint i = 0; i < bound_->m_; i++)
00170 {
00171 vector_[i]=&(*bound_)[position_+i];
00172 }
00173 }
00174 else
00175 {
00176 uint j = 0;
00177 for (uint i = 0; i < bound_->data_.size(); i+=bound_->n_)
00178 {
00179 vector_[j++]=&(*bound_)[position_+i];
00180 }
00181 }
00182
00183 return vector_;
00184 }
00185
00186 const Vector<valuetype>& getData() const throw()
00187 {
00188
00189 if (bound_->row_major_)
00190 {
00191 for (uint i = 0; i < bound_->m_; i++)
00192 {
00193 vector_[i]=(*bound_)[position_+i];
00194 }
00195 }
00196 else
00197 {
00198 uint j = 0;
00199 for (uint i = 0; i < bound_->data_.size(); i+=bound_->n_)
00200 {
00201 vector_[j++]=(*bound_)[position_+i];
00202 }
00203 }
00204 return vector_;
00205
00206 }
00207
00208 void forward() throw()
00209 {
00210 if (bound_->row_major_)
00211 {
00212 position_ += bound_->m_;
00213 }
00214 else
00215 {
00216 position_++;
00217 if (position_ == (int)bound_->n_)
00218 position_ = bound_->data_.size();
00219 }
00220 }
00221
00222 friend std::ostream& operator << (std::ostream& s, const RowIteratorTraits& traits)
00223 throw()
00224 {
00225 return (s << traits.position_ << ' ');
00226 }
00227
00228 void dump(std::ostream& s) const
00229 throw()
00230 {
00231 s << position_ << std::endl;
00232 }
00233
00234 void toRBegin()
00235 throw()
00236 {
00237 position_ = bound_->data_.size() - 1;
00238 }
00239
00240 bool isRBegin() const
00241 throw()
00242 {
00243 return (position_ == bound_->data_.size() - 1);
00244 }
00245
00246 void toREnd()
00247 throw()
00248 {
00249 position_ = -1;
00250 }
00251
00252 bool isREnd() const
00253 throw()
00254 {
00255 return (position_ <= -1);
00256 }
00257
00258 void backward()
00259 throw()
00260 {
00261 if (bound_->row_major_)
00262 {
00263 if (position_ == 0)
00264 position_--;
00265 else
00266 position_ -= bound_->m_;
00267 }
00268 else
00269 {
00270 if (position_ == (int)bound_->data_.size())
00271 position_ = bound_->n_;
00272 position_--;
00273 }
00274 }
00275
00276 void backward(Distance distance)
00277 throw()
00278 {
00279 if (bound_->row_major_)
00280 {
00281 if (position_-(distance * (int)bound_->m_) < 0)
00282 {
00283 position_ = -1;
00284 return;
00285 }
00286 position_ -= (distance * (int)bound_->m_);
00287
00288 }
00289 else
00290 {
00291 if (position_ == (int)bound_->data_.size())
00292 position_ = bound_->n_;
00293 if (position_-distance < 0)
00294 {
00295 position_ = -1;
00296 return;
00297 }
00298 position_ -= distance;
00299 }
00300 }
00301
00302 void forward(Distance distance)
00303 throw()
00304 {
00305
00306 if (bound_->row_major_)
00307 {
00308 if (position_+(distance * bound_->m_) > bound_->data_.size())
00309 {
00310 position_ = bound_->data_.size();
00311 return;
00312 }
00313 position_ += (distance * bound_->m_);
00314 }
00315 else
00316 {
00317 position_ += distance;
00318 if (position_ >= (int)bound_->n_)
00319 position_ = bound_->data_.size();
00320 }
00321 }
00322
00323 Vector<valuetype>& getData(Index index) throw()
00324 {
00325
00326 if (bound_->row_major_)
00327 {
00328 for (uint i = 0; i < bound_->m_; i++)
00329 vector_[i]=(*bound_)[index+i];
00330 }
00331 else
00332 {
00333 for (uint i = 0; i < bound_->m_; i+=bound_->n_)
00334 vector_[i]=(*bound_)[index+i];
00335 }
00336
00337 return vector_;
00338 }
00339
00340 const Vector<valuetype>& getData(Index index) const throw()
00341 {
00342
00343 if (bound_->row_major_)
00344 {
00345 for (uint i = 0; i < bound_->m_; i++)
00346 vector_[i]=(*bound_)[index+i];
00347 }
00348 else
00349 {
00350 for (uint i = 0; i < bound_->m_; i+=bound_->n_)
00351 vector_[i]=(*bound_)[index+i];
00352 }
00353
00354 return vector_;
00355 }
00356
00357
00358 protected:
00359
00360 Matrix<valuetype, mtraits>* bound_;
00361 IteratorPosition position_;
00362 mutable Vector<valuetype> vector_;
00363 };
00364
00365
00366 }
00367
00368 #endif