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