MorphoGraphX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CircIterator.hpp
1 #ifndef CIRC_ITERATOR_H
2 #define CIRC_ITERATOR_H
3 
4 #include <Config.hpp>
5 
6 #include <iterator>
7 
8 namespace mgx {
9 namespace util {
15 template <typename ForwardIterator> class CircIterator {
16 public:
17  typedef std::forward_iterator_tag iterator_category;
18  typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
19  typedef typename std::iterator_traits<ForwardIterator>::difference_type difference_type;
20  typedef typename std::iterator_traits<ForwardIterator>::pointer pointer;
21  typedef typename std::iterator_traits<ForwardIterator>::reference reference;
22 
23  CircIterator() {
24  }
25 
26  CircIterator(const ForwardIterator& f, const ForwardIterator& l, const ForwardIterator& c)
27  : first(f)
28  , last(l)
29  , init(c)
30  , cur(c)
31  {
32  }
33 
34  CircIterator(const ForwardIterator& f, const ForwardIterator& l)
35  : first(f)
36  , last(l)
37  , init(l)
38  , cur(l)
39  {
40  }
41 
42  CircIterator(const CircIterator& copy)
43  : first(copy.first)
44  , last(copy.last)
45  , init(copy.init)
46  , cur(copy.cur)
47  {
48  }
49 
50  CircIterator& operator++()
51  {
52  ++cur;
53  if(cur == last)
54  cur = first;
55  if(cur == init)
56  cur = last;
57  return *this;
58  }
59 
60  CircIterator operator++(int)
61  {
62  CircIterator temp(*this);
63  this->operator++();
64  return temp;
65  }
66 
67  reference operator*() {
68  return *cur;
69  }
70  pointer operator->() {
71  return cur.operator->();
72  }
73 
74  bool operator==(const ForwardIterator& other) const {
75  return cur == other;
76  }
77 
78  bool operator==(const CircIterator& other) const {
79  return cur == other.cur;
80  }
81 
82  bool operator!=(const ForwardIterator& other) const {
83  return cur != other;
84  }
85 
86  bool operator!=(const CircIterator& other) const {
87  return cur != other.cur;
88  }
89 
90  ForwardIterator base() const {
91  return cur;
92  }
93 
94 protected:
95  ForwardIterator first, last, init, cur;
96 };
97 } // namespace util
98 } // namespace mgx
99 #endif
Creates a circular iterator from a range of forward iterators.
Definition: CircIterator.hpp:15