Claw  1.7.3
iterator.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
31 #ifndef __CLAW_ITERATOR_HPP__
32 #define __CLAW_ITERATOR_HPP__
33 
34 #include <iterator>
35 
36 namespace claw
37 {
38  /*-------------------------------------------------------------------------*/
43  template< typename Category, typename Value, typename Iterator,
44  typename Function >
46  {
47 
48  }; // class wrapped_iterator_by_category
49 
50  /*-------------------------------------------------------------------------*/
55  template<typename Value, typename Iterator, typename Function>
57  <std::forward_iterator_tag, Value, Iterator, Function>
58  {
59  public:
60  typedef typename std::iterator_traits<Iterator>::difference_type
61  difference_type;
62  typedef Value value_type;
63  typedef value_type* pointer;
64  typedef value_type& reference;
65  typedef typename std::iterator_traits<Iterator>::iterator_category
66  iterator_category;
67 
68  typedef
70  <std::forward_iterator_tag, Value, Iterator, Function>
71  self_type;
72 
73  public:
75  wrapped_iterator_by_category( const Iterator& it )
76  : m_it(it)
77  { }
78  wrapped_iterator_by_category( const Iterator& it, const Function& f )
79  : m_it(it), m_fun(f)
80  { }
81  template<typename C, typename V, typename I, typename F>
84  : m_it(that.get_iterator()), m_fun(that.get_function())
85  { }
86 
87  const Iterator& get_iterator() const { return m_it; }
88  const Function& get_function() const { return m_fun; }
89 
90  self_type& operator++()
91  {
92  ++m_it;
93  return *this;
94  }
95 
96  self_type operator++(int)
97  {
98  self_type tmp(*this);
99  ++m_it;
100  return tmp;
101  }
102 
103  reference operator*() const { return m_fun(*m_it); }
104  pointer operator->() const { return &m_fun(*m_it); }
105 
106  bool operator==( const self_type& that ) const { return m_it == that.m_it; }
107  bool operator!=( const self_type& that ) const { return m_it != that.m_it; }
108  bool operator==( const Iterator& it ) const { return m_it == it; }
109  bool operator!=( const Iterator& it ) const { return m_it != it; }
110 
111  private:
113  Iterator m_it;
114 
116  Function m_fun;
117 
118  }; // class wrapped_iterator_by_category [forward_iterator_tag]
119 
120  /*-------------------------------------------------------------------------*/
126  template<typename Value, typename Iterator, typename Function>
128  <std::bidirectional_iterator_tag, Value, Iterator, Function>
129  {
130  public:
131  typedef typename std::iterator_traits<Iterator>::difference_type
132  difference_type;
133  typedef Value value_type;
134  typedef value_type* pointer;
135  typedef value_type& reference;
136  typedef typename std::iterator_traits<Iterator>::iterator_category
137  iterator_category;
138 
139  typedef
141  <std::bidirectional_iterator_tag, Value, Iterator, Function> self_type;
142 
143  public:
145  wrapped_iterator_by_category( const Iterator& it )
146  : m_it(it)
147  { }
148  wrapped_iterator_by_category( const Iterator& it, const Function& f )
149  : m_it(it), m_fun(f)
150  { }
151  template<typename C, typename V, typename I, typename F>
154  : m_it(that.get_iterator()), m_fun(that.get_function())
155  { }
156 
157  const Iterator& get_iterator() const { return m_it; }
158  const Function& get_function() const { return m_fun; }
159 
160  self_type& operator++()
161  {
162  ++m_it;
163  return *this;
164  }
165 
166  self_type operator++(int)
167  {
168  self_type tmp(*this);
169  ++m_it;
170  return tmp;
171  }
172 
173  self_type& operator--()
174  {
175  --m_it;
176  return *this;
177  }
178 
179  self_type operator--(int)
180  {
181  self_type tmp(*this);
182  --m_it;
183  return tmp;
184  }
185 
186  reference operator*() const { return m_fun(*m_it); }
187  pointer operator->() const { return &m_fun(*m_it); }
188 
189  bool operator==( const self_type& that ) const { return m_it == that.m_it; }
190  bool operator!=( const self_type& that ) const { return m_it != that.m_it; }
191  bool operator==( const Iterator& it ) const { return m_it == it; }
192  bool operator!=( const Iterator& it ) const { return m_it != it; }
193 
194  private:
196  Iterator m_it;
197 
199  Function m_fun;
200 
201  }; // class wrapped_iterator_by_category [bidirectional_iterator_tag]
202 
203  /*-------------------------------------------------------------------------*/
208  template<typename Value, typename Iterator, typename Function>
210  <std::random_access_iterator_tag, Value, Iterator, Function>
211  {
212  public:
213  typedef typename std::iterator_traits<Iterator>::difference_type
214  difference_type;
215  typedef Value value_type;
216  typedef value_type* pointer;
217  typedef value_type& reference;
218  typedef typename std::iterator_traits<Iterator>::iterator_category
219  iterator_category;
220 
221  typedef
223  <std::random_access_iterator_tag, Value, Iterator, Function>
224  self_type;
225 
226  public:
228  wrapped_iterator_by_category( const Iterator& it )
229  : m_it(it)
230  { }
231  wrapped_iterator_by_category( const Iterator& it, const Function& f )
232  : m_it(it), m_fun(f)
233  { }
234  template<typename V, typename I>
237  <std::random_access_iterator_tag, V, I, Function>& that )
238  : m_it(that.m_it), m_fun(that.m_fun)
239  { }
240  template<typename C, typename V, typename I, typename F>
243  : m_it(that.get_iterator()), m_fun(that.get_function())
244  { }
245 
246  const Iterator& get_iterator() const { return m_it; }
247  const Function& get_function() const { return m_fun; }
248 
249  self_type& operator++()
250  {
251  ++m_it;
252  return *this;
253  }
254 
255  self_type operator++(int)
256  {
257  self_type tmp(*this);
258  ++m_it;
259  return tmp;
260  }
261 
262  self_type& operator--()
263  {
264  --m_it;
265  return *this;
266  }
267 
268  self_type operator--(int)
269  {
270  self_type tmp(*this);
271  --m_it;
272  return tmp;
273  }
274 
275  reference operator*() const { return m_fun(*m_it); }
276  pointer operator->() const { return &m_fun(*m_it); }
277 
278  bool operator==( const self_type& that ) const { return m_it == that.m_it; }
279  bool operator!=( const self_type& that ) const { return m_it != that.m_it; }
280  bool operator==( const Iterator& it ) const { return m_it == it; }
281  bool operator!=( const Iterator& it ) const { return m_it != it; }
282  bool operator<( const self_type& that ) const { return m_it < that.m_it; }
283  bool operator<=( const self_type& that ) const { return m_it <= that.m_it; }
284  bool operator>( const self_type& that ) const { return m_it > that.m_it; }
285  bool operator>=( const self_type& that ) const { return m_it >= that.m_it; }
286 
287  difference_type operator-( const self_type& that ) const
288  {
289  return m_it - that.m_it;
290  }
291 
292  self_type& operator+=(int n)
293  {
294  m_it += n;
295  return *this;
296  }
297 
298  self_type operator+(int n) const
299  {
300  self_type result(*this);
301  result += n;
302  return result;
303  }
304 
305  self_type& operator-=(int n) { return *this += -n; }
306 
307  self_type operator-(int n) const
308  {
309  self_type result(*this);
310  result -= n;
311  return result;
312  }
313 
314  reference operator[](int n) { return m_fun(m_it[n]); }
315 
316  private:
318  Iterator m_it;
319 
321  Function m_fun;
322 
323  }; // class wrapped_iterator_by_category [random_access_iterator_tag]
324 
325  template<typename Value, typename Iterator, typename Function>
327  <std::random_access_iterator_tag, Value, Iterator, Function>
328  operator+
329  ( int n,
331  < std::random_access_iterator_tag, Value, Iterator, Function >& it )
332  {
333  return it + n;
334  }
335 
336  template<typename Value, typename Iterator, typename Function>
337  wrapped_iterator_by_category
338  <std::random_access_iterator_tag, Value, Iterator, Function>
339  operator-
340  ( int n,
341  const wrapped_iterator_by_category
342  < std::random_access_iterator_tag, Value, Iterator, Function >& it )
343  {
344  return it - n;
345  }
346 
347  /*-------------------------------------------------------------------------*/
360  template <typename Value, typename Iterator, typename Function>
362  {
363  public:
366  < typename std::iterator_traits<Iterator>::iterator_category,
367  Value, Iterator, Function >
369 
370  }; // class wrapped_iterator
371 } // namespace claw
372 
373 #endif // __CLAW_ITERATOR_HPP__