source: trunk/yat/utility/StrideIterator.h @ 1066

Last change on this file since 1066 was 1066, checked in by Peter, 13 years ago

Iterators for KernelLookup? - refs #267

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1#ifndef _theplu_yat_utility_stride_iterator_
2#define _theplu_yat_utility_stride_iterator_
3
4// $Id: StrideIterator.h 1066 2008-02-10 22:48:19Z peter $
5
6/*
7  Copyright (C) 2008 Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "iterator_traits.h"
28#include "yat_assert.h"
29
30#include <iterator>
31
32namespace theplu {
33namespace yat {
34namespace utility {
35
36  // forward declaration
37  template<typename Iter>
38  class StrideIterator;
39
40  /**
41     StrideIterator is weighted if underlying iterator is weighted.
42   */
43  template <typename Iter>
44  struct weighted_iterator_traits<StrideIterator<Iter> > {
45    typedef typename weighted_iterator_traits<Iter>::type type;
46  };
47
48  /**
49     @brief Adaptor using a stride on underlying iterator
50
51     Works as underlying iterator except that all arithmetic uses the
52     stride, so e.g., ++StrideIterator returns underlying
53     _iterator+stride
54
55  */
56  template<typename Iter>
57  class StrideIterator 
58    : public std::iterator<typename std::iterator_traits<Iter>::iterator_category, 
59                           typename std::iterator_traits<Iter>::value_type, 
60                           typename std::iterator_traits<Iter>::difference_type, 
61                           typename std::iterator_traits<Iter>::pointer, 
62                           typename std::iterator_traits<Iter>::reference>
63  {
64  public:
65    typedef Iter iterator_type;
66    typedef typename std::iterator_traits<Iter>::value_type value_type;
67    typedef typename std::iterator_traits<Iter>::difference_type difference_type;
68    typedef typename std::iterator_traits<Iter>::reference reference;
69    typedef typename std::iterator_traits<Iter>::pointer pointer;
70    typedef typename yat::utility::weighted_iterator_traits<Iter>::type w_type;
71
72    /**
73       \brief Constructor
74    */
75    explicit StrideIterator(Iter p, difference_type stride=1)
76      : iterator_(p), stride_(stride) {}
77
78   
79    /**
80     */
81    // Peter, this is ugly hack to provide iteartor to const_iterator
82    // for our gsl_iterators - solution? use Boost:iterator_adaptor
83    operator StrideIterator<const value_type*>()
84    { return StrideIterator<const value_type*>(base(), stride_); }
85
86    /**
87       \return underlying iterator
88     */
89    inline Iter base(void) const { return iterator_; }
90
91    /**
92       \return stride
93     */
94    inline size_t stride(void) const { return stride_; }
95
96    /**
97       \return element
98     */
99    reference operator*(void) const 
100    { return *iterator_; }
101
102    /**
103     */
104    pointer operator->() const { return &(operator*()); }
105
106    /**
107       \return element \a n steps forward
108     */
109    reference operator[](difference_type n) const 
110    { 
111      return iterator_[stride_*n];
112    }
113
114    /**
115       \brief pre-increment
116
117       \return reference to *this
118     */
119    StrideIterator& operator++(void) { iterator_+=stride_; return *this; }
120
121    /**
122       \brief post-increment
123
124       \return copy of iterator prior increment
125     */
126    StrideIterator operator++(int) 
127    { StrideIterator tmp(*this); iterator_+=stride_; return tmp;}
128
129    /**
130       \brief iterate \f$ n \f$ steps forward
131
132       \return reference to resulting iterator
133     */
134    StrideIterator& operator+=(int n) { iterator_+=stride_*n; return *this; }
135
136    /**
137       \brief post-decrement
138
139       \return copy of iterator prior decrement
140     */
141    StrideIterator operator--(int) 
142    { StrideIterator tmp(*this); iterator_-=stride_; return tmp;}
143
144    /**
145       \brief pre-decrement
146
147       \return reference to resulting iterator
148     */
149    StrideIterator& operator--(void) { iterator_-=stride_; return *this; }
150
151    /**
152       \brief iterate \f$ n \f$ steps backwards
153
154       \return reference to resulting iterator
155     */
156    StrideIterator& operator-=(int n) { iterator_-=n*stride_; return *this; }
157
158    /**
159       \brief addition operator
160
161       \return copy of resulting iterator
162     */
163    friend StrideIterator operator+(const StrideIterator& lhs,difference_type n) 
164    { StrideIterator tmp(lhs); tmp+=n; return tmp; }
165
166    /**
167       \brief subtraction operator
168
169       \return copy of resulting iterator
170     */
171    friend StrideIterator operator-(const StrideIterator& lhs, 
172                                    difference_type n) 
173    { StrideIterator tmp(lhs); tmp-=n; return tmp; }
174
175    /**
176       \brief difference operator
177
178       \return distance between \a lhs and \a rhs
179     */
180    friend difference_type operator-(const StrideIterator& lhs, 
181                                     const StrideIterator& rhs) 
182    {
183      yat_assert<std::runtime_error>(lhs.stride_==rhs.stride_, 
184        "StrideIterator::operator- different stride in lhs and rhs.");
185      return static_cast<difference_type>( (lhs.iterator_-rhs.iterator_)/
186                                           lhs.stride_);
187    }
188
189   
190    /**
191       \brief Equality operator
192
193       \return True if \a lhs and \a rhs are pointing to same element
194     */
195    friend bool operator==(const StrideIterator& lhs, const StrideIterator& rhs)
196    { return lhs.iterator_==rhs.iterator_; }
197   
198    /**
199       \brief Non-equality operator
200
201       \return False if \a lhs and \a rhs are pointing to same element
202     */
203    friend bool operator!=(const StrideIterator& lhs, const StrideIterator& rhs)
204    { return lhs.iterator_!=rhs.iterator_; }
205   
206    /**
207       \brief Less operator
208     */
209    friend bool operator<(const StrideIterator& lhs, const StrideIterator& rhs)
210    { return lhs.iterator_<rhs.iterator_; }
211   
212    /**
213       \brief Less equal operator
214     */
215    friend bool operator<=(const StrideIterator& lhs, const StrideIterator& rhs)
216    { return lhs.iterator_<=rhs.iterator_; }
217   
218    /**
219       \brief Larger operator
220     */
221    friend bool operator>(const StrideIterator& lhs, const StrideIterator& rhs)
222    { return lhs.iterator_>rhs.iterator_; }
223   
224    /**
225       \brief Larger equal operator
226     */
227    friend bool operator>=(const StrideIterator& lhs, const StrideIterator& rhs)
228    { return lhs.iterator_>=rhs.iterator_; }
229   
230  private:
231    Iter iterator_;
232    size_t stride_;
233
234    // Using compiler generated copy
235    //StrideIterator(const StrideIterator&);
236    //StrideIterator& operator=(const StrideIterator&);
237  };
238
239
240
241
242
243}}} // of namespace utility, yat, and theplu
244
245#endif
Note: See TracBrowser for help on using the repository browser.