All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
cycle_iterator.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Wygocki
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_CYCLE_ITERATOR_HPP
16 #define PAAL_CYCLE_ITERATOR_HPP
17 
19 
20 #include <boost/iterator/iterator_facade.hpp>
21 
22 namespace paal {
23 namespace data_structures {
24 
25 // could have been done by simple boost::join
26 // minor TODO class can specialized for randomaccess iterators
27 
47 template <typename Iter>
48 class cycle_iterator : public boost::iterator_facade<
49  cycle_iterator<Iter>, typename std::iterator_traits<Iter>::value_type,
50  typename boost::forward_traversal_tag,
51  typename std::iterator_traits<Iter>::reference,
52  typename std::iterator_traits<Iter>::difference_type> {
53 
54  typedef std::iterator_traits<Iter> IT;
55  typedef typename IT::reference ref;
56 
57  public:
58 
66  cycle_iterator(Iter start, Iter begin, Iter end)
67  : m_curr(start), m_start(start), m_begin(begin), m_end(end),
68  m_is_end(false) {}
69 
73  cycle_iterator() : m_is_end(true) {}
74 
75  private:
76  friend class boost::iterator_core_access;
81  void increment() {
82  ++m_curr;
83 
84  if (m_curr == m_end) {
85  m_curr = m_begin;
86  }
87 
88  if (m_curr == m_start) {
89  m_is_end = true;
90  m_curr = m_end;
91  }
92  }
93 
94  bool equal(cycle_iterator ei) const {
95  return (m_is_end && ei.m_is_end) || m_curr == ei.m_curr;
96  }
97 
98  ref dereference() const { return *m_curr; }
99 
100  Iter m_curr;
101  Iter m_start;
102  Iter m_begin;
103  Iter m_end;
104  bool m_is_end = false;
105 };
106 }
107 }
108 #endif // PAAL_CYCLE_ITERATOR_HPP
cycle_iterator(Iter start, Iter begin, Iter end)
constructing of cycle_iterator
For given collection (begin -&gt; end) and start iterator pointing to an element inside collection (begi...
cycle_iterator()
Points to end of the collection.