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

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

Closes #247. Removed IteratorWeighted? iterators over weighted container instead can use Iterator with a special Policy.

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