KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
l_iterator.h
Go to the documentation of this file.
1 /* $Id: l_iterator.h 15196 2013-08-15 23:05:59Z ernesto $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== */
19 
20 #ifndef KJB_L_ITERATOR
21 #define KJB_L_ITERATOR
22 
29 #include <boost/concept_check.hpp>
30 #include <iterator>
31 namespace kjb
32 {
33 
39 template<class const_iterator>
41  : public std::iterator<std::bidirectional_iterator_tag,
42  typename std::iterator_traits<const_iterator>::value_type>
43 {
45 
46 protected:
47  const_iterator begin;
48  const_iterator end;
49  const_iterator iter;
50 
51 public:
52  typedef typename std::iterator_traits<const_iterator>::value_type value_type;
53 
55 
56  const_circular_iterator(const_iterator b, const_iterator e) :
57  begin(b),
58  end(e),
59  iter(begin)
60  {}
61 
62  template <class Container>
63  explicit const_circular_iterator(const Container& c) :
64  begin(c.begin()),
65  end(c.end()),
66  iter(begin)
67  {}
68 
69  const_circular_iterator(const Self& other) :
70  begin(other.begin),
71  end(other.end),
72  iter(other.iter)
73  {}
74 
75  Self& operator=(const Self& other)
76  {
77  if(&other != this)
78  {
79  Self tmp = other;
80  this->swap(tmp);
81  }
82  return *this;
83  }
84 
85  const const_iterator& get_iterator() const
86  {
87  return iter;
88  }
89 
90  void swap(Self& other)
91  {
92  using std::swap;
93  swap(begin, other.begin);
94  swap(end, other.end);
95  swap(iter, other.iter);
96  }
97 
99  {
100  // todo: use iterator tags to determine if we have a reverse
101  // iterator or a random-access iterator
102  /* const_iterator prev = begin;
103 
104  if(iter == begin)
105  {
106  iter = end;
107  }
108 
109  for(const_iterator cur = begin; cur != iter; cur++)
110  {
111  prev = cur;
112  }
113 
114  iter = prev;
115 
116  return(*this); */
117 
118  if(iter == begin)
119  {
120  iter = end;
121  }
122  iter--;
123  return *this;
124  }
125 
127  {
128  Self t = *this;
129  --(*this);
130  return(t);
131  }
132 
134  {
135  ++iter;
136  if(iter == end)
137  {
138  iter = begin;
139  }
140  return(*this);
141  }
142 
144  {
145  Self t = *this;
146  ++(*this);
147  return(t);
148  }
149 
150  const value_type& operator*() const
151  {
152  return (*iter);
153  }
154 
155  const value_type* operator->() const
156  {
157  return (iter.operator->());
158  }
159 
160  bool operator==(const Self& rhs) const
161  {
162  return (iter == rhs.iter);
163  }
164 
165  bool operator==(const const_iterator& rhs) const
166  {
167  return iter == rhs;
168  }
169 
170  bool operator!=(const Self& rhs) const
171  {
172  return !operator==(rhs);
173  }
174 
175  bool operator!=(const const_iterator& rhs) const
176  {
177  return !operator==(rhs);
178  }
179 
180  void reset()
181  {
182  iter = begin;
183  }
184 };
185 
198 template<class iterator>
200  : public std::iterator<std::bidirectional_iterator_tag,
201  typename std::iterator_traits<iterator>::value_type>
202 {
203  typedef circular_iterator Self;
204 
205 protected:
206  iterator begin;
207  iterator end;
208  iterator iter;
209 
210 public:
211  typedef typename std::iterator_traits<iterator>::value_type value_type;
212 
213  circular_iterator() : begin(), end(), iter() {};
214  circular_iterator(iterator b, iterator e) : begin(b), end(e), iter(b) {};
215 
217  begin(other.begin),
218  end(other.end),
219  iter(other.iter)
220  {}
221 
223  begin(other.begin),
224  end(other.end),
225  iter(other.iter)
226  {}
227 
228  template <class Container_>
229  explicit circular_iterator(Container_& c) :
230  begin(c.begin()),
231  end(c.end()),
232  iter(c.begin())
233  {}
234 
235 
236  Self& operator=(const Self& other)
237  {
238  if(&other != this)
239  {
240  Self tmp = other;
241  this->swap(tmp);
242  }
243  return *this;
244  }
245 
246  const iterator& get_iterator() const
247  {
248  return iter;
249  }
250 
251  void swap(Self& other)
252  {
253  using std::swap;
254  swap(begin, other.begin);
255  swap(end, other.end);
256  swap(iter, other.iter);
257  }
258 
259 
261  {
262  /* iterator prev = begin;
263 
264  if(begin == iter)
265  {
266  iter = end;
267  }
268 
269  // todo: use iterator tags to determine if we have a reverse iterator or a random-access iterator
270  for(iterator cur = begin; cur != iter; cur++)
271  {
272  prev = cur;
273  }
274 
275  iter = prev;
276 
277  return(*this); */
278 
279  if(iter == begin)
280  {
281  iter = end;
282  }
283  iter--;
284  return *this;
285  }
286 
288  {
289  Self t = *this;
290  this->operator--();
291  return(t);
292  }
293 
295  {
296  ++iter;
297  if(iter == end)
298  {
299  iter = begin;
300  }
301  return(*this);
302  }
303 
305  {
306  Self t(*this);
307  ++(*this);
308  return(t);
309  }
310 
312  {
313  return (*iter);
314  }
315 
317  {
318  return (iter.operator->());
319  }
320 
321  bool operator==(const Self& rhs) const
322  {
323  return (iter == rhs.iter);
324  }
325 
326  bool operator==(const iterator& rhs) const
327  {
328  return iter == rhs;
329  }
330 
331  bool operator!=(const Self& rhs) const
332  {
333  return !operator==(rhs);
334  }
335 
336  bool operator!=(const iterator& rhs) const
337  {
338  return !operator==(rhs);
339  }
340 
341  void reset()
342  {
343  iter = begin;
344  }
345 };
346 
347 template<class const_iterator> inline
349 {
350  return const_circular_iterator<const_iterator>(begin, end);
351 }
352 
353 template<class Container> inline
355 {
357 }
358 
359 template<class iterator> inline
361 {
362  return circular_iterator<iterator>(begin, end);
363 }
364 
365 template<class Container> inline
367 {
369 }
370 
371 
372 }
373 #endif
circular_iterator(Container_ &c)
Definition: l_iterator.h:229
const_iterator begin
Definition: l_iterator.h:47
Self & operator++()
Definition: l_iterator.h:294
value_type & operator*() const
Definition: l_iterator.h:311
const_iterator end
Definition: l_iterator.h:48
circular_iterator< iterator > make_circular_iterator(iterator begin, iterator end)
Definition: l_iterator.h:360
iterator begin
Definition: l_iterator.h:206
std::iterator_traits< const_iterator >::value_type value_type
Definition: l_iterator.h:52
bool operator==(const Self &rhs) const
Definition: l_iterator.h:160
bool operator==(const iterator &rhs) const
Definition: l_iterator.h:326
bool operator==(const Self &rhs) const
Definition: l_iterator.h:321
const_iterator iter
Definition: l_iterator.h:49
bool operator!=(const const_iterator &rhs) const
Definition: l_iterator.h:175
circular_iterator(Self &other)
Definition: l_iterator.h:222
iterator end
Definition: l_iterator.h:207
const_circular_iterator< const_iterator > make_const_circular_iterator(const_iterator begin, const_iterator end)
Definition: l_iterator.h:348
bool operator!=(const Self &rhs) const
Definition: l_iterator.h:331
value_type * operator->() const
Definition: l_iterator.h:316
void reset()
Definition: l_iterator.h:180
const_circular_iterator(const Self &other)
Definition: l_iterator.h:69
std::iterator_traits< iterator >::value_type value_type
Definition: l_iterator.h:211
Self operator++(int)
Definition: l_iterator.h:304
bool operator!=(const iterator &rhs) const
Definition: l_iterator.h:336
circular_iterator(const circular_iterator &other)
Definition: l_iterator.h:216
Self & operator=(const Self &other)
Definition: l_iterator.h:236
const const_iterator & get_iterator() const
Definition: l_iterator.h:85
circular_iterator(iterator b, iterator e)
Definition: l_iterator.h:214
const_circular_iterator(const Container &c)
Definition: l_iterator.h:63
circular_iterator()
Definition: l_iterator.h:213
Self operator++(int)
Definition: l_iterator.h:143
void swap(Self &other)
Definition: l_iterator.h:251
void reset()
Definition: l_iterator.h:341
const iterator & get_iterator() const
Definition: l_iterator.h:246
Self operator--(int)
Definition: l_iterator.h:287
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Self operator--(int)
Definition: l_iterator.h:126
const value_type & operator*() const
Definition: l_iterator.h:150
const value_type * operator->() const
Definition: l_iterator.h:155
Definition: l_iterator.h:40
Self & operator--()
Definition: l_iterator.h:260
bool operator==(const const_iterator &rhs) const
Definition: l_iterator.h:165
bool operator!=(const Self &rhs) const
Definition: l_iterator.h:170
Definition: l_iterator.h:199
void swap(Self &other)
Definition: l_iterator.h:90
Self & operator=(const Self &other)
Definition: l_iterator.h:75
Self & operator--()
Definition: l_iterator.h:98
iterator iter
Definition: l_iterator.h:208
const_circular_iterator(const_iterator b, const_iterator e)
Definition: l_iterator.h:56
const_circular_iterator()
Definition: l_iterator.h:54
Self & operator++()
Definition: l_iterator.h:133