BALL  1.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MATHS/LINALG/randomAccessIterator.h
Go to the documentation of this file.
1 #ifndef BALL_LINALG_RANDOMACCESSITERATOR_H
2 #define BALL_LINALG_RANDOMACCESSITERATOR_H
3 
4 #ifndef BALL_CONCEPT_BIDIRECTIONALITERATOR_H
6 #endif
7 
8 namespace BALL
9 {
11  typedef int Distance;
13  typedef int Index;
14 
19  template <typename Container, typename DataType, typename Position, typename Traits>
20  class ConstRandomAccessIterator
21  : public ConstBidirectionalIterator<Container, DataType, Position, Traits>
22  {
23  public:
24 
28 
30  typedef std::random_access_iterator_tag iterator_category;
31  // convenience typedef
34 
38 
41 
44  : Base(iterator)
45  {
46  }
47 
51 
52 
53 
57 
62 
67 
73 
80 
87  Distance operator - (const ConstRandomAccessIterator& iterator) const;
88 
93  static ConstRandomAccessIterator begin(const Container& container);
94 
99  static ConstRandomAccessIterator end(const Container& container);
100 
105  static ConstRandomAccessIterator rbegin(const Container& container);
106 
111  static ConstRandomAccessIterator rend(const Container& container);
112 
114 
118 
120  bool operator + () const { return Base::getTraits().isValid(); }
121 
123  bool operator - () const { return !Base::getTraits().isValid(); }
124 
131  bool operator < (const ConstRandomAccessIterator& iterator) const;
132 
140  bool operator <= (const ConstRandomAccessIterator& iterator) const;
141 
149  bool operator >= (const ConstRandomAccessIterator& iterator) const;
150 
157  bool operator > (const ConstRandomAccessIterator& iterator) const;
158 
160 
161 
162 
169  const DataType& operator [] (Index index) const;
171 
172  protected:
173 
174  ConstRandomAccessIterator(const Container& container)
175  : Base(container)
176  {
177  }
178  };
180 
186  template <typename Container, typename DataType, typename Position, typename Traits>
187  ConstRandomAccessIterator<Container, DataType, Position, Traits> operator +
188  (Distance distance, const ConstRandomAccessIterator<Container, DataType, Position, Traits>& iterator)
189  {
190  ConstRandomAccessIterator<Container, DataType, Position, Traits> tmp_iterator(iterator);
191  return (tmp_iterator += distance);
192  }
193 
200  template <typename Container, typename DataType, typename Position, typename Traits>
201  Distance ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator -
202  (const ConstRandomAccessIterator<Container, DataType, Position, Traits>& b) const
203  {
204  if (!Base::getTraits().isValid())
205  {
206  Exception::InvalidIterator e;
207  throw e;
208  }
209  if (!b.getTraits().isValid())
210  {
211  Exception::InvalidIterator e;
212  throw e;
213  }
214  if (Base::getTraits().getContainer() != b.getTraits().getContainer())
215  {
216  Exception::IncompatibleIterators e;
217  throw e;
218  }
219  return Base::getTraits().getDistance(b.getTraits());
220  }
221 
222  template <typename Container, typename DataType, typename Position, typename Traits>
223  ConstRandomAccessIterator<Container, DataType, Position, Traits>&
225  {
226  if (!Base::getTraits().isValid())
227  {
228  Exception::InvalidIterator e;
229  throw e;
230  }
231  if (distance < (Distance)0)
232  {
233  return (*this -= -distance);
234  }
235  Base::getTraits().forward(distance);
236  return *this;
237  }
238 
239  template <typename Container, typename DataType, typename Position, typename Traits>
240  ConstRandomAccessIterator<Container, DataType, Position, Traits>&
242  {
243  if (Base::getTraits().isSingular())
244  {
245  Exception::InvalidIterator e;
246  throw e;
247  }
248  if (distance < (Distance)0)
249  {
250  return (*this += -distance);
251  }
252  if (Base::getTraits().isEnd() == true)
253  {
254  Base::getTraits().toRBegin();
255  Base::getTraits().backward(distance - 1);
256  }
257  else
258  {
259  Base::getTraits().backward(distance);
260  }
261  return *this;
262  }
263 
264  template <typename Container, typename DataType, typename Position, typename Traits>
265  ConstRandomAccessIterator<Container, DataType, Position, Traits>
267  {
268  ConstRandomAccessIterator iterator(*this);
269  return (iterator += distance);
270  }
271 
272  template <typename Container, typename DataType, typename Position, typename Traits>
273  ConstRandomAccessIterator<Container, DataType, Position, Traits>
275  {
276  ConstRandomAccessIterator iterator(*this);
277  return (iterator -= distance);
278  }
279 
280  template <typename Container, typename DataType, typename Position, typename Traits>
281  bool ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator <
282  (const ConstRandomAccessIterator& iterator) const
283  {
284  if (!Base::getTraits().isValid())
285  {
286  Exception::InvalidIterator e;
287  throw e;
288  }
289  if (!iterator.isValid())
290  {
291  Exception::InvalidIterator e;
292  throw e;
293  }
294  if (Base::getTraits().getContainer() != iterator.getContainer())
295  {
296  Exception::IncompatibleIterators e;
297  throw e;
298  }
299 
300  return (Base::getTraits().operator < (iterator.getTraits()));
301  }
302 
303  template <typename Container, typename DataType, typename Position, typename Traits>
304  bool ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator <=
305  (const ConstRandomAccessIterator& iterator) const
306  {
307  if (!Base::getTraits().isValid())
308  {
309  Exception::InvalidIterator e;
310  throw e;
311  }
312  if (!iterator.isValid())
313  {
314  Exception::InvalidIterator e;
315  throw e;
316  }
317  if (Base::getTraits().getContainer() != iterator.getContainer())
318  {
319  Exception::IncompatibleIterators e;
320  throw e;
321  }
322  return !(Base::getTraits().operator > (iterator.getTraits()));
323  }
324 
325  template <typename Container, typename DataType, typename Position, typename Traits>
326  bool ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator >=
327  (const ConstRandomAccessIterator& iterator) const
328  {
329  if (!Base::getTraits().isValid())
330  {
331  Exception::InvalidIterator e;
332  throw e;
333  }
334  if (!iterator.isValid())
335  {
336  Exception::InvalidIterator e;
337  throw e;
338  }
339  if (Base::getTraits().getContainer() != iterator.getContainer())
340  {
341  Exception::IncompatibleIterators e;
342  throw e;
343  }
344 
345  return !(Base::getTraits().operator < (iterator.getTraits()));
346  }
347 
348  template <typename Container, typename DataType, typename Position, typename Traits>
349  bool ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator >
350  (const ConstRandomAccessIterator& iterator) const
351  {
352  if (!Base::getTraits().isValid())
353  {
354  Exception::InvalidIterator e;
355  throw e;
356  }
357  if (!iterator.isValid())
358  {
359  Exception::InvalidIterator e;
360  throw e;
361  }
362  if (Base::getTraits().getContainer() != iterator.getContainer())
363  {
364  Exception::IncompatibleIterators e;
365  throw e;
366  }
367 
368  return (Base::getTraits().operator > (iterator.getTraits()));
369  }
370 
371 
372  template <typename Container, typename DataType, typename Position, typename Traits>
374  {
375  if (!Base::getTraits().isValid())
376  {
377  Exception::InvalidIterator e;
378  throw e;
379  }
380 
381  return Base::getTraits().getData(index);
382  }
383 
384  template <typename Container, typename DataType, typename Position, typename Traits>
385  ConstRandomAccessIterator<Container, DataType, Position, Traits>
387  {
388  ConstRandomAccessIterator iterator(container);
389  iterator.toBegin();
390  return iterator;
391  }
392 
393  template <typename Container, typename DataType, typename Position, typename Traits>
394  ConstRandomAccessIterator<Container, DataType, Position, Traits>
396  {
397  ConstRandomAccessIterator iterator(container);
398  iterator.toEnd();
399  return iterator;
400  }
401 
402  template <typename Container, typename DataType, typename Position, typename Traits>
403  ConstRandomAccessIterator<Container, DataType, Position, Traits>
405  {
406  ConstRandomAccessIterator iterator(container);
407  iterator.toRBegin();
408  return iterator;
409  }
410 
411  template <typename Container, typename DataType, typename Position, typename Traits>
412  ConstRandomAccessIterator<Container, DataType, Position, Traits>
414  {
415  ConstRandomAccessIterator iterator(container);
416  iterator.toREnd();
417  return iterator;
418  }
419 
424 
427  template <typename Container, typename DataType, typename Position, typename Traits>
428  class RandomAccessIterator
429  : public ConstRandomAccessIterator<Container, DataType, Position, Traits>
430  {
431  public:
432 
436 
437  typedef DataType& reference;
439  typedef DataType* pointer;
443 
447 
450 
453  : Base(iterator)
454  {
455  }
456 
460 
467  reference operator [] (Index index) const { return const_cast<reference>(Base::getTraits().getData(index)); }
469  reference operator * () const { return const_cast<reference>(Base::getTraits().getData()); }
471  pointer operator -> () const { return const_cast<pointer>(&Base::getTraits().getData()); }
473 
481  static RandomAccessIterator begin(const Container& container);
482 
487  static RandomAccessIterator end(const Container& container);
488 
493  static RandomAccessIterator rbegin(const Container& container);
494 
499  static RandomAccessIterator rend(const Container& container);
501 
502  protected:
503 
504  RandomAccessIterator(const Container& container)
505  : Base(container)
506  {
507  }
508 
509  };
511 
512  template <typename Container, typename DataType, typename Position, typename Traits>
513  RandomAccessIterator<Container, DataType, Position, Traits>
515  {
516  RandomAccessIterator iterator(container);
517  iterator.toBegin();
518  return iterator;
519  }
520 
521  template <typename Container, typename DataType, typename Position, typename Traits>
522  RandomAccessIterator<Container, DataType, Position, Traits>
524  {
525  RandomAccessIterator iterator(container);
526  iterator.toEnd();
527  return iterator;
528  }
529 
530  template <typename Container, typename DataType, typename Position, typename Traits>
531  RandomAccessIterator<Container, DataType, Position, Traits>
533  {
534  RandomAccessIterator iterator(container);
535  iterator.toRBegin();
536  return iterator;
537  }
538 
539  template <typename Container, typename DataType, typename Position, typename Traits>
540  RandomAccessIterator<Container, DataType, Position, Traits>
542  {
543  RandomAccessIterator iterator(container);
544  iterator.toREnd();
545  return iterator;
546  }
547 
548 } // namespace BALL
549 
550 #endif // BALL_KERNEL_RANDOMACCESSITERATOR_H