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