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() throw() {}
00042
00044 ConstForwardIterator(const ConstForwardIterator& iterator) throw()
00045 : BaseIterator<Container, DataType, Position, Traits>(iterator)
00046 {
00047 }
00048
00050 ~ConstForwardIterator() throw() {}
00051
00053
00057
00058 ConstForwardIterator& operator = (const ConstForwardIterator& iterator) throw()
00059 {
00060 BaseIterator<Container, DataType, Position, Traits>::operator = (iterator);
00061 return *this;
00062 }
00063
00065 void swap(ConstForwardIterator& iterator) throw() { std::swap(ConstForwardIterator<Container, DataType, Position, Traits>::getTraits(), iterator.getTraits()); }
00067
00071
00073 void toBegin() throw(Exception::SingularIterator);
00074
00076 bool isBegin() const throw();
00077
00079 void toEnd() throw(Exception::SingularIterator);
00080
00082 bool isEnd() const throw();
00083
00085 static ConstForwardIterator begin(const Container& container)
00086 throw(Exception::Precondition);
00087
00089 static ConstForwardIterator end(const Container& container)
00090 throw(Exception::Precondition);
00091
00093 ConstForwardIterator& operator ++ () throw(Exception::InvalidIterator)
00094 {
00095 if (!Base::isValid())
00096 {
00097 Exception::InvalidIterator e;
00098 throw(e);
00099 }
00100 Base::getTraits().forward();
00101 return *this;
00102 }
00103
00105 ConstForwardIterator operator ++ (int) throw(Exception::InvalidIterator)
00106 {
00107 if (!Base::isValid())
00108 {
00109 Exception::InvalidIterator e;
00110 throw(e);
00111 }
00112 ConstForwardIterator tmp(*this);
00113 ++(*this);
00114 return tmp;
00115 }
00116
00118
00119 protected:
00120
00122 ConstForwardIterator(const Container& container) throw()
00123 : BaseIterator<Container, DataType, Position, Traits>(container)
00124 {
00125 }
00126 };
00128
00129 template <typename Container, typename DataType, typename Position, typename Traits>
00130 void ConstForwardIterator<Container, DataType, Position, Traits>::toBegin()
00131 throw(Exception::SingularIterator)
00132 {
00133 if (Base::isSingular())
00134 {
00135 Exception::SingularIterator e;
00136 throw(e);
00137 }
00138 Base::getTraits().toBegin();
00139 }
00140
00141 template <typename Container, typename DataType, typename Position, typename Traits>
00142 ConstForwardIterator<Container, DataType, Position, Traits>
00143 ConstForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00144 throw(Exception::Precondition)
00145 {
00146 ConstForwardIterator<Container, DataType, Position, Traits> iterator(container);
00147 iterator.toBegin();
00148 return iterator;
00149 }
00150
00151 template <typename Container, typename DataType, typename Position, typename Traits>
00152 bool ConstForwardIterator<Container, DataType, Position, Traits>::isBegin() const
00153 throw()
00154 {
00155 if (Base::getTraits().isSingular())
00156 {
00157 return false;
00158 }
00159 return Base::getTraits().isBegin();
00160 }
00161
00162 template <typename Container, typename DataType, typename Position, typename Traits>
00163 void ConstForwardIterator<Container, DataType, Position, Traits>::toEnd()
00164 throw(Exception::SingularIterator)
00165 {
00166 if (Base::isSingular())
00167 {
00168 Exception::SingularIterator e;
00169 throw(e);
00170 }
00171 Base::getTraits().toEnd();
00172 }
00173
00174 template <typename Container, typename DataType, typename Position, typename Traits>
00175 ConstForwardIterator<Container, DataType, Position, Traits>
00176 ConstForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00177 throw(Exception::Precondition)
00178 {
00179 ConstForwardIterator iterator(container);
00180 iterator.toEnd();
00181 return iterator;
00182 }
00183
00184 template <typename Container, typename DataType, typename Position, typename Traits>
00185 bool ConstForwardIterator<Container, DataType, Position, Traits>::isEnd() const
00186 throw()
00187 {
00188 if (Base::isSingular())
00189 {
00190 return false;
00191 }
00192 return Base::getTraits().isEnd();
00193 }
00194
00197 template <typename Container, typename DataType, typename Position, typename Traits>
00198 class ForwardIterator
00199 : public ConstForwardIterator<Container, DataType, Position, Traits>
00200 {
00201 public:
00202
00206
00208 typedef DataType& reference;
00210 typedef DataType* pointer;
00211
00212 typedef ConstForwardIterator<Container, DataType, Position, Traits> Base;
00214
00218
00220 ForwardIterator() throw() {}
00221
00223 ForwardIterator(const ForwardIterator& iterator) throw()
00224 : ConstForwardIterator<Container, DataType, Position, Traits>(iterator)
00225 {
00226 }
00227
00229 ~ForwardIterator() throw() {}
00231
00235
00236 ForwardIterator& operator = (const ForwardIterator& iterator) throw()
00237 {
00238 ConstForwardIterator<Container, DataType, Position, Traits>::operator = (iterator);
00239 return *this;
00240 }
00241
00243 void swap(ForwardIterator& iterator) throw() { std::swap(Base::getTraits(), iterator.getTraits()); }
00245
00249
00251 static ForwardIterator begin(const Container& container) throw(Exception::Precondition);
00252
00254 static ForwardIterator end(const Container& container) throw(Exception::Precondition);
00255
00257 reference operator * () const throw()
00258 {
00259 return (reference)Base::getTraits().getData();
00260 }
00261
00263 pointer operator -> () const throw()
00264 {
00265 return (pointer)&Base::getTraits().getData();
00266 }
00267
00269 ForwardIterator& operator ++ () throw(Exception::InvalidIterator)
00270 {
00271 if (!Base::isValid())
00272 {
00273 Exception::InvalidIterator e;
00274 throw(e);
00275 }
00276 Base::getTraits().forward();
00277 return *this;
00278 }
00279
00281 ForwardIterator operator ++ (int) throw(Exception::InvalidIterator)
00282 {
00283 if (!Base::isValid())
00284 {
00285 Exception::InvalidIterator e;
00286 throw(e);
00287 }
00288 ForwardIterator tmp(*this);
00289 ++(*this);
00290 return tmp;
00291 }
00292
00294
00295 protected:
00296
00298 ForwardIterator(const Container& container) throw()
00299 : Base(container)
00300 {
00301 }
00302 };
00303
00304
00305 template <typename Container, typename DataType, typename Position, typename Traits>
00306 ForwardIterator<Container, DataType, Position, Traits>
00307 ForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00308 throw(Exception::Precondition)
00309 {
00310 ForwardIterator iterator(container);
00311 iterator.toBegin();
00312 return iterator;
00313 }
00314
00315 template <typename Container, typename DataType, typename Position, typename Traits>
00316 ForwardIterator<Container, DataType, Position, Traits>
00317 ForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00318 throw(Exception::Precondition)
00319 {
00320 ForwardIterator iterator(container);
00321 iterator.toEnd();
00322 return iterator;
00323 }
00324
00325
00326 }
00327
00328 #endif // BALL_KERNEL_FORWARDITERATOR_H