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