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

Last change on this file since 1041 was 1041, checked in by Peter, 14 years ago

generalized StrideIterator? to work with Iterators as well - not only pointers

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