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