OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
circular_iterator.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2020 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef CIRCULAR_ITERATOR_HH
20 #define CIRCULAR_ITERATOR_HH
21 
22 #include <iterator>
23 
24 template<typename T>
25 class const_circular_iter : public std::iterator<std::bidirectional_iterator_tag,typename T::value_type>
26 {
27 typedef typename T::const_iterator iterator;
28 typedef typename T::value_type value_type;
29 protected:
30  iterator iter;
31  iterator begin;
32  iterator end;
33 public:
34  const_circular_iter(T& t) : iter(t.begin()), begin(t.begin()), end(t.end()) {};
35  const_circular_iter(iterator b,iterator e) : iter(b), begin(b), end(e) {};
36  const_circular_iter(iterator b,iterator e,iterator pos) : iter(pos), begin(b), end(e) {};
37  operator iterator(){
38  return iter;
39  }
41  {
42  ++iter;
43 
44  if (iter == end)
45  iter = begin;
46 
47  return(*this);
48  }
49 
51  {
52  const_circular_iter<T> t=*this;
53  ++(*this);
54  return(t);
55  }
57  {
58  if (iter == begin)
59  iter = end;
60 
61  --iter;
62 
63  return(*this);
64  }
65 
67  {
68  const_circular_iter<T> t=*this;
69  --(*this);
70  return(t);
71  }
72 
74  {
75  const_circular_iter<T> t=*this;
76  for(unsigned int i=0;i<n;++i)
77  --t;
78  return t;
79  }
81  {
82  const_circular_iter<T> t=*this;
83  for(unsigned int i=0;i<n;++i)
84  ++t;
85  return t;
86  }
87 
88  const value_type operator*() const
89  {return (*iter);}
90 
91  bool operator==(const const_circular_iter<T>& rhs) const
92  {return (iter == rhs.iter);}
93 
94  bool operator!=(const const_circular_iter<T>& rhs) const
95  {return ! operator==(rhs); }
96 };
97 
98 
99 template<typename T>
100 class circular_iter : public std::iterator<std::bidirectional_iterator_tag,typename T::value_type>
101 {
102 typedef typename T::iterator iterator;
103 typedef typename T::value_type value_type;
104 private:
105  iterator iter;
106  iterator begin;
107  iterator end;
108 public:
109  circular_iter(T& t) : iter(t.begin()), begin(t.begin()), end(t.end()) {};
110  circular_iter(iterator b, iterator e) : iter(b), begin(b), end(e) {};
111  circular_iter(iterator b, iterator e, iterator pos) : iter(pos), begin(b), end(e) {};
112 
114  {
115  ++iter;
116 
117  if (iter == end)
118  iter = begin;
119 
120  return(*this);
121  }
122 
124  {
125  circular_iter<T> t=*this;
126  ++(*this);
127  return(t);
128  }
130  {
131  if (iter == begin)
132  iter = end;
133 
134  --iter;
135 
136  return(*this);
137  }
138 
140  {
141  circular_iter<T> t=*this;
142  --(*this);
143  return(t);
144  }
146  {
147  circular_iter<T> t=*this;
148  for(unsigned int i=0;i<n;++i)
149  --t;
150  return t;
151  }
153  {
154  circular_iter<T> t=*this;
155  for(unsigned int i=0;i<n;++i)
156  ++t;
157  return t;
158  }
159 
160  const value_type operator*() const
161  {return (*iter);}
162 
163  value_type& operator*()
164  {return (*iter);}
165 
166  bool operator==(const circular_iter<T>& rhs) const
167  {return (iter == rhs.iter);}
168 
169  bool operator!=(const circular_iter<T>& rhs) const {return ! operator==(rhs); }
170  operator iterator(){
171  return iter;
172  }
173 };
174 
175 #endif
176 
circular_iter< T > operator--(int)
const_circular_iter< T > & operator--()
bool operator!=(const const_circular_iter< T > &rhs) const
const_circular_iter(iterator b, iterator e, iterator pos)
circular_iter(iterator b, iterator e, iterator pos)
const_circular_iter< T > operator-(int n)
value_type & operator*()
const_circular_iter(iterator b, iterator e)
bool operator==(const circular_iter< T > &rhs) const
const_circular_iter< T > & operator++()
circular_iter< T > operator++(int)
const_circular_iter< T > operator--(int)
bool operator==(const const_circular_iter< T > &rhs) const
const value_type operator*() const
circular_iter< T > & operator--()
const_circular_iter< T > operator++(int)
const value_type operator*() const
bool operator!=(const circular_iter< T > &rhs) const
circular_iter< T > operator-(int n)
const_circular_iter< T > operator+(int n)
circular_iter< T > operator+(int n)
circular_iter(iterator b, iterator e)
circular_iter< T > & operator++()