OpenStructure
Loading...
Searching...
No Matches
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
22template<typename T>
24{
25 using iterator = typename T::const_iterator;
26 using iterator_category = std::bidirectional_iterator_tag;
27 using value_type = typename T::value_type;
28 using difference_type = std::ptrdiff_t;
29 using pointer = value_type*;
30 using reference = value_type&;
31protected:
32 iterator iter;
33 iterator begin;
34 iterator end;
35public:
36 const_circular_iter(T& t) : iter(t.begin()), begin(t.begin()), end(t.end()) {};
37 const_circular_iter(iterator b,iterator e) : iter(b), begin(b), end(e) {};
38 const_circular_iter(iterator b,iterator e,iterator pos) : iter(pos), begin(b), end(e) {};
39 operator iterator(){
40 return iter;
41 }
43 {
44 ++iter;
45
46 if (iter == end)
47 iter = begin;
48
49 return(*this);
50 }
51
53 {
55 ++(*this);
56 return(t);
57 }
59 {
60 if (iter == begin)
61 iter = end;
62
63 --iter;
64
65 return(*this);
66 }
67
69 {
71 --(*this);
72 return(t);
73 }
74
76 {
78 for(unsigned int i=0;i<n;++i)
79 --t;
80 return t;
81 }
83 {
85 for(unsigned int i=0;i<n;++i)
86 ++t;
87 return t;
88 }
89
90 const value_type operator*() const
91 {return (*iter);}
92
93 bool operator==(const const_circular_iter<T>& rhs) const
94 {return (iter == rhs.iter);}
95
96 bool operator!=(const const_circular_iter<T>& rhs) const
97 {return ! operator==(rhs); }
98};
99
100
101template<typename T>
103{
104 using iterator = typename T::iterator;
105 using iterator_category = std::bidirectional_iterator_tag;
106 using value_type = typename T::value_type;
107 using difference_type = std::ptrdiff_t;
108 using pointer = value_type*;
109 using reference = value_type&;
110
111private:
112 iterator iter;
113 iterator begin;
114 iterator end;
115public:
116 circular_iter(T& t) : iter(t.begin()), begin(t.begin()), end(t.end()) {};
117 circular_iter(iterator b, iterator e) : iter(b), begin(b), end(e) {};
118 circular_iter(iterator b, iterator e, iterator pos) : iter(pos), begin(b), end(e) {};
119
121 {
122 ++iter;
123
124 if (iter == end)
125 iter = begin;
126
127 return(*this);
128 }
129
131 {
132 circular_iter<T> t=*this;
133 ++(*this);
134 return(t);
135 }
137 {
138 if (iter == begin)
139 iter = end;
140
141 --iter;
142
143 return(*this);
144 }
145
147 {
148 circular_iter<T> t=*this;
149 --(*this);
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 }
160 {
161 circular_iter<T> t=*this;
162 for(unsigned int i=0;i<n;++i)
163 ++t;
164 return t;
165 }
166
167 const value_type operator*() const
168 {return (*iter);}
169
170 value_type& operator*()
171 {return (*iter);}
172
173 bool operator==(const circular_iter<T>& rhs) const
174 {return (iter == rhs.iter);}
175
176 bool operator!=(const circular_iter<T>& rhs) const {return ! operator==(rhs); }
177 operator iterator(){
178 return iter;
179 }
180};
181
182#endif
183
circular_iter< T > & operator--()
circular_iter< T > operator++(int)
value_type & operator*()
circular_iter(iterator b, iterator e, iterator pos)
bool operator==(const circular_iter< T > &rhs) const
circular_iter< T > & operator++()
bool operator!=(const circular_iter< T > &rhs) const
circular_iter< T > operator-(int n)
const value_type operator*() const
circular_iter(iterator b, iterator e)
circular_iter< T > operator+(int n)
circular_iter< T > operator--(int)
const_circular_iter< T > operator+(int n)
const_circular_iter< T > & operator--()
const_circular_iter(iterator b, iterator e, iterator pos)
bool operator==(const const_circular_iter< T > &rhs) const
const_circular_iter< T > operator-(int n)
const_circular_iter< T > operator++(int)
const_circular_iter< T > & operator++()
const value_type operator*() const
bool operator!=(const const_circular_iter< T > &rhs) const
const_circular_iter(iterator b, iterator e)
const_circular_iter< T > operator--(int)