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

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

StrideIterator? - fixes ticket:303

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1#ifndef _theplu_yat_utility_stride_iterator_
2#define _theplu_yat_utility_stride_iterator_
3
4// $Id: StrideIterator.h 1038 2008-02-05 20:46:37Z 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 "yat_assert.h"
28
29#include <iterator>
30
31namespace theplu {
32namespace yat {
33namespace utility {
34
35  /**
36     @brief Stride Iterator
37
38     Works as a pointer except that all arithmetic is using the stride value
39  */
40  template<typename T>
41  class StrideIterator 
42    : public std::iterator<std::random_access_iterator_tag, T>
43  {
44  public:
45    typedef typename std::iterator_traits<T*>::difference_type difference_type;
46    typedef typename std::iterator_traits<T*>::reference reference;
47    typedef typename std::iterator_traits<T*>::pointer pointer;
48
49    /**
50       \brief Constructor
51    */
52    explicit StrideIterator(pointer p, difference_type stride=1)
53      : pointer_(p), stride_(stride) {}
54
55    operator StrideIterator<const T>()
56    { return StrideIterator<const T>(pointer_, stride_); }
57
58    /**
59       \return element
60     */
61    reference operator*(void) const 
62    { return *pointer_; }
63
64    /**
65     */
66    pointer operator->() const { return &(operator*()); }
67
68    /**
69       \return element \a n steps forward
70     */
71    reference operator[](difference_type n) const 
72    { 
73      return pointer_[stride_*n];
74    }
75
76    /**
77       \brief pre-increment
78
79       \return reference to *this
80     */
81    StrideIterator& operator++(void) { pointer_+=stride_; return *this; }
82
83    /**
84       \brief post-increment
85
86       \return copy of iterator prior increment
87     */
88    StrideIterator operator++(int) 
89    { StrideIterator tmp(*this); pointer_+=stride_; return tmp;}
90
91    /**
92       \brief iterate \f$ n \f$ steps forward
93
94       \return reference to resulting iterator
95     */
96    StrideIterator& operator+=(int n) { pointer_+=stride_*n; return *this; }
97
98    /**
99       \brief post-decrement
100
101       \return copy of iterator prior decrement
102     */
103    StrideIterator operator--(int) 
104    { StrideIterator tmp(*this); pointer_-=stride_; return tmp;}
105
106    /**
107       \brief pre-decrement
108
109       \return reference to resulting iterator
110     */
111    StrideIterator& operator--(void) { pointer_-=stride_; return *this; }
112
113    /**
114       \brief iterate \f$ n \f$ steps backwards
115
116       \return reference to resulting iterator
117     */
118    StrideIterator& operator-=(int n) { pointer_-=n*stride_; return *this; }
119
120    /**
121       \brief addition operator
122
123       \return copy of resulting iterator
124     */
125    friend StrideIterator operator+(const StrideIterator& lhs,difference_type n) 
126    { StrideIterator tmp(lhs); tmp+=n; return tmp; }
127
128    /**
129       \brief subtraction operator
130
131       \return copy of resulting iterator
132     */
133    friend StrideIterator operator-(const StrideIterator& lhs, 
134                                    difference_type n) 
135    { StrideIterator tmp(lhs); tmp-=n; return tmp; }
136
137    /**
138       \brief difference operator
139
140       \return distance between \a lhs and \a rhs
141     */
142    friend difference_type operator-(const StrideIterator& lhs, 
143                                     const StrideIterator& rhs) 
144    {
145      yat_assert<std::runtime_error>(lhs.stride_==rhs.stride_, 
146        "StrideIterator::operator- different stride in lhs and rhs.");
147      return static_cast<difference_type>( (lhs.pointer_-rhs.pointer_)/
148                                           lhs.stride_);
149    }
150
151   
152    /**
153       \brief Equality operator
154
155       \return True if \a lhs and \a rhs are pointing to same element
156     */
157    friend bool operator==(const StrideIterator& lhs, const StrideIterator& rhs)
158    { return lhs.pointer_==rhs.pointer_; }
159   
160    /**
161       \brief Non-equality operator
162
163       \return False if \a lhs and \a rhs are pointing to same element
164     */
165    friend bool operator!=(const StrideIterator& lhs, const StrideIterator& rhs)
166    { return !(lhs==rhs); }
167   
168    /**
169       \brief Less operator
170     */
171    friend bool operator<(const StrideIterator& lhs, const StrideIterator& rhs)
172    { return lhs.pointer_<rhs.pointer_; }
173   
174    /**
175       \brief Less equal operator
176     */
177    friend bool operator<=(const StrideIterator& lhs, const StrideIterator& rhs)
178    { return !(rhs<lhs); }
179   
180    /**
181       \brief Larger operator
182     */
183    friend bool operator>(const StrideIterator& lhs, const StrideIterator& rhs)
184    { return rhs<lhs; }
185   
186    /**
187       \brief Larger equal operator
188     */
189    friend bool operator>=(const StrideIterator& lhs, const StrideIterator& rhs)
190    { return !(lhs<rhs); }
191   
192  private:
193    T* pointer_;
194    size_t stride_;
195
196    // Using compiler generated copy
197    //StrideIterator(const StrideIterator&);
198    //StrideIterator& operator=(const StrideIterator&);
199  };
200}}} // of namespace utility, yat, and theplu
201
202#endif
Note: See TracBrowser for help on using the repository browser.