00001 #ifndef BALL_LINALG_VECTORITERATOR_H 00002 #define BALL_LINALG_VECTORITERATOR_H 00003 00004 #ifndef BALL_LINALG_VECTOR_IH 00005 # include <BALL/MATHS/LINALG/vector.ih> 00006 #endif 00007 00008 #ifndef BALL_CONCEPT_RANDOMACCESSITERATOR_H 00009 #include <BALL/CONCEPT/randomAccessIterator.h> 00010 #endif 00011 00012 namespace BALL { 00013 00014 // forward declaration 00015 template <typename valuetype> 00016 class Vector; 00017 00018 00019 template <typename valuetype> 00020 class VectorIteratorTraits 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 Vector<valuetype>; 00044 public: 00045 00046 virtual ~VectorIteratorTraits() 00047 throw() 00048 { 00049 } 00050 00051 VectorIteratorTraits() 00052 throw() 00053 : bound_(0), 00054 position_(0) 00055 { 00056 } 00057 00058 VectorIteratorTraits(const Vector<valuetype>& vector) throw() 00059 : bound_(const_cast<Vector<valuetype>*>(&vector)), 00060 position_(0) 00061 { 00062 } 00063 00064 VectorIteratorTraits(const VectorIteratorTraits& traits) throw() 00065 : bound_(traits.bound_), 00066 position_(traits.position_) 00067 { 00068 } 00069 00070 VectorIteratorTraits& operator = (const VectorIteratorTraits& traits) throw() 00071 { 00072 bound_ = traits.bound_; 00073 position_ = traits.position_; 00074 00075 return *this; 00076 } 00077 00078 Vector<valuetype>* getContainer() throw() 00079 { 00080 return bound_; 00081 } 00082 00083 const Vector<valuetype>* 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 VectorIteratorTraits& traits) const throw() 00104 { 00105 return (position_ == traits.position_); 00106 } 00107 00108 bool operator != (const VectorIteratorTraits& traits) const throw() 00109 { 00110 return (position_ != traits.position_); 00111 } 00112 00113 bool operator < (const VectorIteratorTraits& traits) const throw() 00114 { 00115 return (position_ < traits.position_); 00116 } 00117 00118 Distance getDistance(const VectorIteratorTraits& 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 position_++; 00167 } 00168 00169 friend std::ostream& operator << (std::ostream& s, const VectorIteratorTraits& traits) 00170 throw() 00171 { 00172 return (s << traits.position_ << ' '); 00173 } 00174 00175 void dump(std::ostream& s) const 00176 throw() 00177 { 00178 s << position_ << std::endl; 00179 } 00180 00181 void toRBegin() 00182 throw() 00183 { 00184 position_ = bound_->data_.size() - 1; 00185 } 00186 00187 bool isRBegin() const 00188 throw() 00189 { 00190 return (position_ == bound_->data_.size() - 1); 00191 } 00192 00193 void toREnd() 00194 throw() 00195 { 00196 position_ = -1; 00197 } 00198 00199 bool isREnd() const 00200 throw() 00201 { 00202 return (position_ <= -1); 00203 } 00204 00205 void backward() 00206 throw() 00207 { 00208 position_--; 00209 } 00210 00211 void backward(Distance distance) 00212 throw() 00213 { 00214 position_ -= distance; 00215 } 00216 00217 void forward(Distance distance) 00218 throw() 00219 { 00220 position_ += distance; 00221 } 00222 00223 ValueType& getData(Index index) throw() 00224 { 00225 return (*bound_)[index]; 00226 } 00227 00228 const ValueType& getData(Index index) const throw() 00229 { 00230 return (*bound_)[index]; 00231 } 00232 00233 00234 protected: 00235 00236 Vector<valuetype>* bound_; 00237 IteratorPosition position_; 00238 }; 00239 00240 00241 } // namespace BALL 00242 00243 #endif