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

Last change on this file since 1047 was 1047, checked in by Peter, 15 years ago

generalized a bit

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1#ifndef _theplu_yat_utility_stride_iterator_
2#define _theplu_yat_utility_stride_iterator_
3
4// $Id: StrideIterator.h 1047 2008-02-07 13:02:05Z 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 typename std::iterator_traits<Iter>::value_type value_type;
66    typedef typename std::iterator_traits<Iter>::difference_type difference_type;
67    typedef typename std::iterator_traits<Iter>::reference reference;
68    typedef typename std::iterator_traits<Iter>::pointer pointer;
69    typedef typename yat::utility::weighted_iterator_traits<Iter>::type w_type;
70
71    /**
72       \brief Constructor
73    */
74    explicit StrideIterator(Iter p, difference_type stride=1)
75      : iterator_(p), stride_(stride) {}
76
77   
78    /**
79     */
80    // Peter, this is ugly hack to provide iteartor to const_iterator
81    // for our gsl_iterators - solution? use Boost:iterator_adaptor
82    operator StrideIterator<const value_type*>()
83    { return StrideIterator<const value_type*>(base(), stride_); }
84
85    /**
86       \return underlying iterator
87     */
88    inline Iter base(void) const { return iterator_; }
89
90    /**
91       \return stride
92     */
93    inline size_t stride(void) const { return stride_; }
94
95    /**
96       \return element
97     */
98    reference operator*(void) const 
99    { return *iterator_; }
100
101    /**
102     */
103    pointer operator->() const { return &(operator*()); }
104
105    /**
106       \return element \a n steps forward
107     */
108    reference operator[](difference_type n) const 
109    { 
110      return iterator_[stride_*n];
111    }
112
113    /**
114       \brief pre-increment
115
116       \return reference to *this
117     */
118    StrideIterator& operator++(void) { iterator_+=stride_; return *this; }
119
120    /**
121       \brief post-increment
122
123       \return copy of iterator prior increment
124     */
125    StrideIterator operator++(int) 
126    { StrideIterator tmp(*this); iterator_+=stride_; return tmp;}
127
128    /**
129       \brief iterate \f$ n \f$ steps forward
130
131       \return reference to resulting iterator
132     */
133    StrideIterator& operator+=(int n) { iterator_+=stride_*n; return *this; }
134
135    /**
136       \brief post-decrement
137
138       \return copy of iterator prior decrement
139     */
140    StrideIterator operator--(int) 
141    { StrideIterator tmp(*this); iterator_-=stride_; return tmp;}
142
143    /**
144       \brief pre-decrement
145
146       \return reference to resulting iterator
147     */
148    StrideIterator& operator--(void) { iterator_-=stride_; return *this; }
149
150    /**
151       \brief iterate \f$ n \f$ steps backwards
152
153       \return reference to resulting iterator
154     */
155    StrideIterator& operator-=(int n) { iterator_-=n*stride_; return *this; }
156
157    /**
158       \brief addition operator
159
160       \return copy of resulting iterator
161     */
162    friend StrideIterator operator+(const StrideIterator& lhs,difference_type n) 
163    { StrideIterator tmp(lhs); tmp+=n; return tmp; }
164
165    /**
166       \brief subtraction operator
167
168       \return copy of resulting iterator
169     */
170    friend StrideIterator operator-(const StrideIterator& lhs, 
171                                    difference_type n) 
172    { StrideIterator tmp(lhs); tmp-=n; return tmp; }
173
174    /**
175       \brief difference operator
176
177       \return distance between \a lhs and \a rhs
178     */
179    friend difference_type operator-(const StrideIterator& lhs, 
180                                     const StrideIterator& rhs) 
181    {
182      yat_assert<std::runtime_error>(lhs.stride_==rhs.stride_, 
183        "StrideIterator::operator- different stride in lhs and rhs.");
184      return static_cast<difference_type>( (lhs.iterator_-rhs.iterator_)/
185                                           lhs.stride_);
186    }
187
188   
189    /**
190       \brief Equality operator
191
192       \return True if \a lhs and \a rhs are pointing to same element
193     */
194    friend bool operator==(const StrideIterator& lhs, const StrideIterator& rhs)
195    { return lhs.iterator_==rhs.iterator_; }
196   
197    /**
198       \brief Non-equality operator
199
200       \return False if \a lhs and \a rhs are pointing to same element
201     */
202    friend bool operator!=(const StrideIterator& lhs, const StrideIterator& rhs)
203    { return lhs.iterator_!=rhs.iterator_; }
204   
205    /**
206       \brief Less operator
207     */
208    friend bool operator<(const StrideIterator& lhs, const StrideIterator& rhs)
209    { return lhs.iterator_<rhs.iterator_; }
210   
211    /**
212       \brief Less equal operator
213     */
214    friend bool operator<=(const StrideIterator& lhs, const StrideIterator& rhs)
215    { return lhs.iterator_<=rhs.iterator_; }
216   
217    /**
218       \brief Larger operator
219     */
220    friend bool operator>(const StrideIterator& lhs, const StrideIterator& rhs)
221    { return lhs.iterator_>rhs.iterator_; }
222   
223    /**
224       \brief Larger equal operator
225     */
226    friend bool operator>=(const StrideIterator& lhs, const StrideIterator& rhs)
227    { return lhs.iterator_>=rhs.iterator_; }
228   
229  private:
230    Iter iterator_;
231    size_t stride_;
232
233    // Using compiler generated copy
234    //StrideIterator(const StrideIterator&);
235    //StrideIterator& operator=(const StrideIterator&);
236  };
237
238
239
240
241
242}}} // of namespace utility, yat, and theplu
243
244#endif
Note: See TracBrowser for help on using the repository browser.