BALL  1.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CONCEPT/randomAccessIterator.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_CONCEPT_RANDOMACCESSITERATOR_H
6 #define BALL_CONCEPT_RANDOMACCESSITERATOR_H
7 
8 #ifndef BALL_COMMON_H
9 # include <BALL/common.h>
10 #endif
11 
12 #ifndef BALL_CONCEPT_BIDIRECTIONALITERATOR_H
14 #endif
15 
16 namespace BALL
17 {
18 
23  template <typename Container, typename DataType, typename Position, typename Traits>
25  : public ConstBidirectionalIterator<Container, DataType, Position, Traits>
26  {
27  public:
28 
32 
34  typedef std::random_access_iterator_tag iterator_category;
35  // convenience typedef
38 
42 
45 
48  : Base(iterator)
49  {
50  }
51 
55 
56 
57 
61 
66 
71 
77 
84 
91  Distance operator - (const ConstRandomAccessIterator& iterator) const;
92 
97  static ConstRandomAccessIterator begin(const Container& container);
98 
103  static ConstRandomAccessIterator end(const Container& container);
104 
109  static ConstRandomAccessIterator rbegin(const Container& container);
110 
115  static ConstRandomAccessIterator rend(const Container& container);
116 
118 
122 
124  BALL_INLINE bool operator + () const { return Base::getTraits().isValid(); }
125 
127  BALL_INLINE bool operator - () const { return !Base::getTraits().isValid(); }
128 
135  bool operator < (const ConstRandomAccessIterator& iterator) const;
136 
144  bool operator <= (const ConstRandomAccessIterator& iterator) const;
145 
153  bool operator >= (const ConstRandomAccessIterator& iterator) const;
154 
161  bool operator > (const ConstRandomAccessIterator& iterator) const;
162 
164 
165 
166 
173  const DataType& operator [] (Index index) const;
175 
176  protected:
177 
178  ConstRandomAccessIterator(const Container& container)
179  : Base(container)
180  {
181  }
182  };
184 
190  template <typename Container, typename DataType, typename Position, typename Traits>
191  ConstRandomAccessIterator<Container, DataType, Position, Traits> operator +
193  {
195  return (tmp_iterator += distance);
196  }
197 
203  template <typename Container, typename DataType, typename Position, typename Traits>
204  Distance ConstRandomAccessIterator<Container, DataType, Position, Traits>::operator -
205  (const ConstRandomAccessIterator<Container, DataType, Position, Traits>& b) const
206  {
207  if (!Base::getTraits().isValid())
208  {
209  throw Exception::InvalidIterator(__FILE__, __LINE__);
210  }
211  if (!b.getTraits().isValid())
212  {
213  throw Exception::InvalidIterator(__FILE__, __LINE__);
214  }
215  if (Base::getTraits().getContainer() != b.getTraits().getContainer())
216  {
217  throw Exception::IncompatibleIterators(__FILE__, __LINE__);
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  throw Exception::InvalidIterator(__FILE__, __LINE__);
229  }
230  if (distance < (Distance)0)
231  {
232  return (*this -= -distance);
233  }
234  Base::getTraits().forward(distance);
235  return *this;
236  }
237 
238  template <typename Container, typename DataType, typename Position, typename Traits>
241  {
242  if (Base::getTraits().isSingular())
243  {
244  throw Exception::InvalidIterator(__FILE__, __LINE__);
245  }
246  if (distance < (Distance)0)
247  {
248  return (*this += -distance);
249  }
250  if (Base::getTraits().isEnd() == true)
251  {
252  Base::getTraits().toRBegin();
253  Base::getTraits().backward(distance - 1);
254  }
255  else
256  {
257  Base::getTraits().backward(distance);
258  }
259  return *this;
260  }
261 
262  template <typename Container, typename DataType, typename Position, typename Traits>
265  {
266  ConstRandomAccessIterator iterator(*this);
267  return (iterator += distance);
268  }
269 
270  template <typename Container, typename DataType, typename Position, typename Traits>
273  {
274  ConstRandomAccessIterator iterator(*this);
275  return (iterator -= distance);
276  }
277 
278  template <typename Container, typename DataType, typename Position, typename Traits>
280  (const ConstRandomAccessIterator& iterator) const
281  {
282  if (!Base::getTraits().isValid())
283  {
284  throw Exception::InvalidIterator(__FILE__, __LINE__);
285  }
286  if (!iterator.isValid())
287  {
288  throw Exception::InvalidIterator(__FILE__, __LINE__);
289  }
290  if (Base::getTraits().getContainer() != iterator.getContainer())
291  {
292  throw Exception::IncompatibleIterators(__FILE__, __LINE__);
293  }
294 
295  return (Base::getTraits().operator < (iterator.getTraits()));
296  }
297 
298  template <typename Container, typename DataType, typename Position, typename Traits>
300  (const ConstRandomAccessIterator& iterator) const
301  {
302  if (!Base::getTraits().isValid())
303  {
304  throw Exception::InvalidIterator(__FILE__, __LINE__);
305  }
306  if (!iterator.isValid())
307  {
308  throw Exception::InvalidIterator(__FILE__, __LINE__);
309  }
310  if (Base::getTraits().getContainer() != iterator.getContainer())
311  {
312  throw Exception::IncompatibleIterators(__FILE__, __LINE__);
313  }
314  return !(Base::getTraits().operator > (iterator.getTraits()));
315  }
316 
317  template <typename Container, typename DataType, typename Position, typename Traits>
319  (const ConstRandomAccessIterator& iterator) const
320  {
321  if (!Base::getTraits().isValid())
322  {
323  throw Exception::InvalidIterator(__FILE__, __LINE__);
324  }
325  if (!iterator.isValid())
326  {
327  throw Exception::InvalidIterator(__FILE__, __LINE__);
328  }
329  if (Base::getTraits().getContainer() != iterator.getContainer())
330  {
331  throw Exception::IncompatibleIterators(__FILE__, __LINE__);
332  }
333 
334  return !(Base::getTraits().operator < (iterator.getTraits()));
335  }
336 
337  template <typename Container, typename DataType, typename Position, typename Traits>
339  (const ConstRandomAccessIterator& iterator) const
340  {
341  if (!Base::getTraits().isValid())
342  {
343  throw Exception::InvalidIterator(__FILE__, __LINE__);
344  }
345  if (!iterator.isValid())
346  {
347  throw Exception::InvalidIterator(__FILE__, __LINE__);
348  }
349  if (Base::getTraits().getContainer() != iterator.getContainer())
350  {
351  throw Exception::IncompatibleIterators(__FILE__, __LINE__);
352  }
353 
354  return (Base::getTraits().operator > (iterator.getTraits()));
355  }
356 
357 
358  template <typename Container, typename DataType, typename Position, typename Traits>
360  {
361  if (!Base::getTraits().isValid())
362  {
363  throw Exception::InvalidIterator(__FILE__, __LINE__);
364  }
365 
366  return Base::getTraits().getData(index);
367  }
368 
369  template <typename Container, typename DataType, typename Position, typename Traits>
372  {
373  ConstRandomAccessIterator iterator(container);
374  iterator.toBegin();
375  return iterator;
376  }
377 
378  template <typename Container, typename DataType, typename Position, typename Traits>
381  {
382  ConstRandomAccessIterator iterator(container);
383  iterator.toEnd();
384  return iterator;
385  }
386 
387  template <typename Container, typename DataType, typename Position, typename Traits>
390  {
391  ConstRandomAccessIterator iterator(container);
392  iterator.toRBegin();
393  return iterator;
394  }
395 
396  template <typename Container, typename DataType, typename Position, typename Traits>
399  {
400  ConstRandomAccessIterator iterator(container);
401  iterator.toREnd();
402  return iterator;
403  }
404 
409 
412  template <typename Container, typename DataType, typename Position, typename Traits>
414  : public ConstRandomAccessIterator<Container, DataType, Position, Traits>
415  {
416  public:
417 
421 
422  typedef DataType& reference;
424  typedef DataType* pointer;
428 
432 
435 
438  : Base(iterator)
439  {
440  }
441 
445 
452  BALL_INLINE reference operator [] (Index index) const { return const_cast<reference>(Base::getTraits().getData(index)); }
454  BALL_INLINE reference operator * () const { return const_cast<reference>(Base::getTraits().getData()); }
456  BALL_INLINE pointer operator -> () const { return const_cast<pointer>(&Base::getTraits().getData()); }
458 
466  static RandomAccessIterator begin(const Container& container);
467 
472  static RandomAccessIterator end(const Container& container);
473 
478  static RandomAccessIterator rbegin(const Container& container);
479 
484  static RandomAccessIterator rend(const Container& container);
486 
487  protected:
488 
489  BALL_INLINE RandomAccessIterator(const Container& container)
490  : Base(container)
491  {
492  }
493 
494  };
496 
497  template <typename Container, typename DataType, typename Position, typename Traits>
498  RandomAccessIterator<Container, DataType, Position, Traits>
500  {
501  RandomAccessIterator iterator(container);
502  iterator.toBegin();
503  return iterator;
504  }
505 
506  template <typename Container, typename DataType, typename Position, typename Traits>
509  {
510  RandomAccessIterator iterator(container);
511  iterator.toEnd();
512  return iterator;
513  }
514 
515  template <typename Container, typename DataType, typename Position, typename Traits>
518  {
519  RandomAccessIterator iterator(container);
520  iterator.toRBegin();
521  return iterator;
522  }
523 
524  template <typename Container, typename DataType, typename Position, typename Traits>
527  {
528  RandomAccessIterator iterator(container);
529  iterator.toREnd();
530  return iterator;
531  }
532 
533 } // namespace BALL
534 
535 #endif // BALL_CONCEPT_RANDOMACCESSITERATOR_H