00001 #ifndef BALL_LINALG_FORWARDITERATOR_H
00002 #define BALL_LINALG_FORWARDITERATOR_H
00003
00004 #ifndef BALL_LINALG_BASEITERATOR_H
00005 # include <BALL/MATHS/LINALG/baseIterator.h>
00006 #endif
00007
00008 #include <BALL/COMMON/exception.h>
00009 #include <BALL/MATHS/LINALG/linalgException.h>
00010
00011 namespace BALL
00012 {
00013
00017
00020 template <typename Container, typename DataType, typename Position, typename Traits>
00021 class ConstForwardIterator
00022 : public BaseIterator<Container, DataType, Position, Traits>
00023 {
00024 public:
00025
00029
00031 typedef std::forward_iterator_tag iterator_category;
00032
00033 typedef BaseIterator<Container, DataType, Position, Traits> Base;
00035
00039
00041 ConstForwardIterator() {}
00042
00044 ConstForwardIterator(const ConstForwardIterator& iterator)
00045 : BaseIterator<Container, DataType, Position, Traits>(iterator)
00046 {
00047 }
00048
00050 ~ConstForwardIterator() {}
00051
00053
00057
00058 ConstForwardIterator& operator = (const ConstForwardIterator& iterator)
00059 {
00060 BaseIterator<Container, DataType, Position, Traits>::operator = (iterator);
00061 return *this;
00062 }
00063
00065 void swap(ConstForwardIterator& iterator) { std::swap(ConstForwardIterator<Container, DataType, Position, Traits>::getTraits(), iterator.getTraits()); }
00067
00071
00075 void toBegin();
00076
00078 bool isBegin() const;
00079
00083 void toEnd();
00084
00086 bool isEnd() const;
00087
00091 static ConstForwardIterator begin(const Container& container);
00092
00096 static ConstForwardIterator end(const Container& container);
00097
00101 ConstForwardIterator& operator ++ ()
00102 {
00103 if (!Base::isValid())
00104 {
00105 Exception::InvalidIterator e;
00106 throw(e);
00107 }
00108 Base::getTraits().forward();
00109 return *this;
00110 }
00111
00115 ConstForwardIterator operator ++ (int)
00116 {
00117 if (!Base::isValid())
00118 {
00119 Exception::InvalidIterator e;
00120 throw(e);
00121 }
00122 ConstForwardIterator tmp(*this);
00123 ++(*this);
00124 return tmp;
00125 }
00126
00128
00129 protected:
00130
00132 ConstForwardIterator(const Container& container)
00133 : BaseIterator<Container, DataType, Position, Traits>(container)
00134 {
00135 }
00136 };
00138
00139 template <typename Container, typename DataType, typename Position, typename Traits>
00140 void ConstForwardIterator<Container, DataType, Position, Traits>::toBegin()
00141 {
00142 if (Base::isSingular())
00143 {
00144 Exception::SingularIterator e;
00145 throw(e);
00146 }
00147 Base::getTraits().toBegin();
00148 }
00149
00150 template <typename Container, typename DataType, typename Position, typename Traits>
00151 ConstForwardIterator<Container, DataType, Position, Traits>
00152 ConstForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00153 {
00154 ConstForwardIterator<Container, DataType, Position, Traits> iterator(container);
00155 iterator.toBegin();
00156 return iterator;
00157 }
00158
00159 template <typename Container, typename DataType, typename Position, typename Traits>
00160 bool ConstForwardIterator<Container, DataType, Position, Traits>::isBegin() const
00161 {
00162 if (Base::getTraits().isSingular())
00163 {
00164 return false;
00165 }
00166 return Base::getTraits().isBegin();
00167 }
00168
00169 template <typename Container, typename DataType, typename Position, typename Traits>
00170 void ConstForwardIterator<Container, DataType, Position, Traits>::toEnd()
00171 {
00172 if (Base::isSingular())
00173 {
00174 Exception::SingularIterator e;
00175 throw(e);
00176 }
00177 Base::getTraits().toEnd();
00178 }
00179
00180 template <typename Container, typename DataType, typename Position, typename Traits>
00181 ConstForwardIterator<Container, DataType, Position, Traits>
00182 ConstForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00183 {
00184 ConstForwardIterator iterator(container);
00185 iterator.toEnd();
00186 return iterator;
00187 }
00188
00189 template <typename Container, typename DataType, typename Position, typename Traits>
00190 bool ConstForwardIterator<Container, DataType, Position, Traits>::isEnd() const
00191 {
00192 if (Base::isSingular())
00193 {
00194 return false;
00195 }
00196 return Base::getTraits().isEnd();
00197 }
00198
00201 template <typename Container, typename DataType, typename Position, typename Traits>
00202 class ForwardIterator
00203 : public ConstForwardIterator<Container, DataType, Position, Traits>
00204 {
00205 public:
00206
00210
00212 typedef DataType& reference;
00214 typedef DataType* pointer;
00215
00216 typedef ConstForwardIterator<Container, DataType, Position, Traits> Base;
00218
00222
00224 ForwardIterator() {}
00225
00227 ForwardIterator(const ForwardIterator& iterator)
00228 : ConstForwardIterator<Container, DataType, Position, Traits>(iterator)
00229 {
00230 }
00231
00233 ~ForwardIterator() {}
00235
00239
00240 ForwardIterator& operator = (const ForwardIterator& iterator)
00241 {
00242 ConstForwardIterator<Container, DataType, Position, Traits>::operator = (iterator);
00243 return *this;
00244 }
00245
00247 void swap(ForwardIterator& iterator) { std::swap(Base::getTraits(), iterator.getTraits()); }
00249
00253
00257 static ForwardIterator begin(const Container& container);
00258
00262 static ForwardIterator end(const Container& container);
00263
00265 reference operator * () const
00266 {
00267 return (reference)Base::getTraits().getData();
00268 }
00269
00271 pointer operator -> () const
00272 {
00273 return (pointer)&Base::getTraits().getData();
00274 }
00275
00279 ForwardIterator& operator ++ ()
00280 {
00281 if (!Base::isValid())
00282 {
00283 Exception::InvalidIterator e;
00284 throw(e);
00285 }
00286 Base::getTraits().forward();
00287 return *this;
00288 }
00289
00293 ForwardIterator operator ++ (int)
00294 {
00295 if (!Base::isValid())
00296 {
00297 Exception::InvalidIterator e;
00298 throw(e);
00299 }
00300 ForwardIterator tmp(*this);
00301 ++(*this);
00302 return tmp;
00303 }
00304
00306
00307 protected:
00308
00310 ForwardIterator(const Container& container)
00311 : Base(container)
00312 {
00313 }
00314 };
00315
00316
00317 template <typename Container, typename DataType, typename Position, typename Traits>
00318 ForwardIterator<Container, DataType, Position, Traits>
00319 ForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00320 {
00321 ForwardIterator iterator(container);
00322 iterator.toBegin();
00323 return iterator;
00324 }
00325
00326 template <typename Container, typename DataType, typename Position, typename Traits>
00327 ForwardIterator<Container, DataType, Position, Traits>
00328 ForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00329 {
00330 ForwardIterator iterator(container);
00331 iterator.toEnd();
00332 return iterator;
00333 }
00334
00335
00336 }
00337
00338 #endif // BALL_KERNEL_FORWARDITERATOR_H