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 { 00048 } 00049 00050 VectorIteratorTraits() 00051 : bound_(0), 00052 position_(0) 00053 { 00054 } 00055 00056 VectorIteratorTraits(const Vector<valuetype>& vector) 00057 : bound_(const_cast<Vector<valuetype>*>(&vector)), 00058 position_(0) 00059 { 00060 } 00061 00062 VectorIteratorTraits(const VectorIteratorTraits& traits) 00063 : bound_(traits.bound_), 00064 position_(traits.position_) 00065 { 00066 } 00067 00068 VectorIteratorTraits& operator = (const VectorIteratorTraits& traits) 00069 { 00070 bound_ = traits.bound_; 00071 position_ = traits.position_; 00072 00073 return *this; 00074 } 00075 00076 Vector<valuetype>* getContainer() 00077 { 00078 return bound_; 00079 } 00080 00081 const Vector<valuetype>* getContainer() const 00082 { 00083 return bound_; 00084 } 00085 00086 bool isSingular() const 00087 { 00088 return (bound_ == 0); 00089 } 00090 00091 IteratorPosition& getPosition() 00092 { 00093 return position_; 00094 } 00095 00096 const IteratorPosition& getPosition() const 00097 { 00098 return position_; 00099 } 00100 00101 bool operator == (const VectorIteratorTraits& traits) const 00102 { 00103 return (position_ == traits.position_); 00104 } 00105 00106 bool operator != (const VectorIteratorTraits& traits) const 00107 { 00108 return (position_ != traits.position_); 00109 } 00110 00111 bool operator < (const VectorIteratorTraits& traits) const 00112 { 00113 return (position_ < traits.position_); 00114 } 00115 00116 Distance getDistance(const VectorIteratorTraits& traits) const 00117 { 00118 return (Distance)(position_ - traits.position_); 00119 } 00120 00121 bool isValid() const 00122 { 00123 return ((bound_ != 0) && (position_ >= 0) && (position_ < (int)bound_->data_.size())); 00124 } 00125 00126 void invalidate() 00127 { 00128 bound_ = 0; 00129 position_ = -1; 00130 } 00131 00132 void toBegin() 00133 { 00134 position_ = 0; 00135 } 00136 00137 bool isBegin() const 00138 { 00139 return ( position_ == 0 ); 00140 } 00141 00142 void toEnd() 00143 { 00144 position_ = bound_->data_.size(); 00145 } 00146 00147 bool isEnd() const 00148 { 00149 return ( position_ == (int)bound_->data_.size()); 00150 } 00151 00152 ValueType& getData() 00153 { 00154 return (*bound_)[position_]; 00155 } 00156 00157 const ValueType& getData() const 00158 { 00159 return (*bound_)[position_]; 00160 } 00161 00162 void forward() 00163 { 00164 position_++; 00165 } 00166 00167 friend std::ostream& operator << (std::ostream& s, const VectorIteratorTraits& traits) 00168 { 00169 return (s << traits.position_ << ' '); 00170 } 00171 00172 void dump(std::ostream& s) const 00173 { 00174 s << position_ << std::endl; 00175 } 00176 00177 void toRBegin() 00178 { 00179 position_ = bound_->data_.size() - 1; 00180 } 00181 00182 bool isRBegin() const 00183 { 00184 return (position_ == bound_->data_.size() - 1); 00185 } 00186 00187 void toREnd() 00188 { 00189 position_ = -1; 00190 } 00191 00192 bool isREnd() const 00193 { 00194 return (position_ <= -1); 00195 } 00196 00197 void backward() 00198 { 00199 position_--; 00200 } 00201 00202 void backward(Distance distance) 00203 { 00204 position_ -= distance; 00205 } 00206 00207 void forward(Distance distance) 00208 { 00209 position_ += distance; 00210 } 00211 00212 ValueType& getData(Index index) 00213 { 00214 return (*bound_)[index]; 00215 } 00216 00217 const ValueType& getData(Index index) const 00218 { 00219 return (*bound_)[index]; 00220 } 00221 00222 00223 protected: 00224 00225 Vector<valuetype>* bound_; 00226 IteratorPosition position_; 00227 }; 00228 00229 00230 } // namespace BALL 00231 00232 #endif