MorphoGraphX
CircIterator.hpp
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef CIRC_ITERATOR_H
12 #define CIRC_ITERATOR_H
13 
14 #include <Config.hpp>
15 
16 #include <iterator>
17 
18 namespace mgx {
19 namespace util {
25 template <typename ForwardIterator> class CircIterator {
26 public:
27  typedef std::forward_iterator_tag iterator_category;
28  typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
29  typedef typename std::iterator_traits<ForwardIterator>::difference_type difference_type;
30  typedef typename std::iterator_traits<ForwardIterator>::pointer pointer;
31  typedef typename std::iterator_traits<ForwardIterator>::reference reference;
32 
33  CircIterator() {
34  }
35 
36  CircIterator(const ForwardIterator& f, const ForwardIterator& l, const ForwardIterator& c)
37  : first(f)
38  , last(l)
39  , init(c)
40  , cur(c)
41  {
42  }
43 
44  CircIterator(const ForwardIterator& f, const ForwardIterator& l)
45  : first(f)
46  , last(l)
47  , init(l)
48  , cur(l)
49  {
50  }
51 
52  CircIterator(const CircIterator& copy)
53  : first(copy.first)
54  , last(copy.last)
55  , init(copy.init)
56  , cur(copy.cur)
57  {
58  }
59 
60  CircIterator& operator++()
61  {
62  ++cur;
63  if(cur == last)
64  cur = first;
65  if(cur == init)
66  cur = last;
67  return *this;
68  }
69 
70  CircIterator operator++(int)
71  {
72  CircIterator temp(*this);
73  this->operator++();
74  return temp;
75  }
76 
77  reference operator*() {
78  return *cur;
79  }
80  pointer operator->() {
81  return cur.operator->();
82  }
83 
84  bool operator==(const ForwardIterator& other) const {
85  return cur == other;
86  }
87 
88  bool operator==(const CircIterator& other) const {
89  return cur == other.cur;
90  }
91 
92  bool operator!=(const ForwardIterator& other) const {
93  return cur != other;
94  }
95 
96  bool operator!=(const CircIterator& other) const {
97  return cur != other.cur;
98  }
99 
100  ForwardIterator base() const {
101  return cur;
102  }
103 
104 protected:
105  ForwardIterator first, last, init, cur;
106 };
107 } // namespace util
108 } // namespace mgx
109 #endif
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Creates a circular iterator from a range of forward iterators.
Definition: CircIterator.hpp:25