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