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

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

merging release 0.6 into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1#ifndef _theplu_yat_utility_stride_iterator_
2#define _theplu_yat_utility_stride_iterator_
3
4// $Id: StrideIterator.h 2202 2010-02-21 18:39:13Z peter $
5
6/*
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2010 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include "iterator_traits.h"
27
28#include <boost/concept_check.hpp>
29#include <boost/iterator/iterator_adaptor.hpp>
30
31namespace theplu {
32namespace yat {
33namespace utility {
34
35  /**
36     @brief Adaptor using a stride on underlying iterator
37
38     Works as underlying iterator except that all arithmetic uses the
39     stride, so e.g., ++StrideIterator returns underlying
40     _iterator+stride
41
42     The stride must be positive. Negative stride can be accomplished
43     by combining StrideIterator with a reverse_iterator (from
44     e.g. std or boost).
45  */
46  template<typename RandomAccessIterator>
47  class StrideIterator 
48    : public boost::iterator_adaptor<StrideIterator<RandomAccessIterator>
49                                     , RandomAccessIterator>
50  {
51    typedef boost::iterator_adaptor<StrideIterator<RandomAccessIterator>,
52                                    RandomAccessIterator> super_t;
53
54  public:
55    /// type of underlying iterator
56    typedef RandomAccessIterator iterator_type;
57
58    /**
59       \brief default constructor
60
61       Using default constructor of BASE iterator.
62     */
63    explicit StrideIterator(size_t stride=1)
64      : StrideIterator::iterator_adaptor_(), stride_(stride) 
65    {
66      BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<RandomAccessIterator>));
67    }
68
69    /**
70       \brief Constructor
71    */
72    explicit StrideIterator(RandomAccessIterator p, size_t stride=1)
73      : StrideIterator::iterator_adaptor_(p), stride_(stride) {}
74
75   
76    /**
77       \brief Copy constructor
78     */
79    StrideIterator(const StrideIterator& other)
80      : StrideIterator::iterator_adaptor_(other.base()), stride_(other.stride()) 
81    {}
82
83    /**
84       \brief Conversion constructor.
85
86       Create a StrideIterator<RandomAccessIterator> from a
87       StrideIterator<I2>. Possible if I2 is convertible to a
88       RandomAccessIterator. Constructor allows implicit conversions
89       such as iterator to const_iterator.
90     */
91    template<typename I2>
92    StrideIterator(StrideIterator<I2> other,
93                   typename boost::enable_if_convertible<I2
94                   , RandomAccessIterator>::type* = 0 )
95      : StrideIterator::iterator_adaptor_(other.base()), 
96        stride_(other.stride()) {}
97
98    /**
99        \brief Assignment operator
100     */
101    StrideIterator& operator=(const StrideIterator& rhs)
102    {
103      stride_ = rhs.stride();
104      this->base_reference() = rhs.base();
105      return *this;
106    }
107
108    /**
109       \return stride
110     */
111    inline size_t stride(void) const { return stride_; }
112
113  private:
114    // to give base class access to private parts
115    friend class boost::iterator_core_access;
116   
117    size_t stride_;
118
119    //typedef typename StrideIterator::iterator_adaptor_::difference_type
120    typedef typename StrideIterator::iterator_adaptor_::difference_type
121    difference_t;
122
123    void advance(typename super_t::difference_type n)
124    { this->base_reference() += stride_*n; }
125
126    void decrement(void) { this->base_reference()-=stride_; }
127
128    template <class OtherIterator>
129    typename super_t::difference_type
130    distance_to(const StrideIterator<OtherIterator>& other) const
131    { 
132      // casting to int to avoid loss of sign in numerator
133      return (other.base() - this->base() )/static_cast<int>(stride_); 
134    }
135
136    void increment(void) { this->base_reference()+=stride_; }
137  };
138
139}}} // of namespace utility, yat, and theplu
140
141#endif
Note: See TracBrowser for help on using the repository browser.