1 | #ifndef _theplu_yat_utility_stride_iterator_ |
---|
2 | #define _theplu_yat_utility_stride_iterator_ |
---|
3 | |
---|
4 | // $Id: StrideIterator.h 1490 2008-09-11 22:10:26Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2008 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 | // debug |
---|
27 | #include <iostream> |
---|
28 | |
---|
29 | #include "iterator_traits.h" |
---|
30 | |
---|
31 | #include <boost/iterator/iterator_adaptor.hpp> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace utility { |
---|
36 | |
---|
37 | // forward declaration |
---|
38 | template<typename Iter> |
---|
39 | class StrideIterator; |
---|
40 | |
---|
41 | /** |
---|
42 | Specialization for StrideIterator that calls underlying iterator. |
---|
43 | */ |
---|
44 | template <typename Iter> |
---|
45 | struct weighted_iterator_traits<StrideIterator<Iter> > { |
---|
46 | /** |
---|
47 | StrideIterator is weighted if underlying iterator is weighted. |
---|
48 | */ |
---|
49 | typedef typename weighted_iterator_traits<Iter>::type type; |
---|
50 | }; |
---|
51 | |
---|
52 | /** |
---|
53 | Specialization for StrideIterator using iterator_traits with base() |
---|
54 | */ |
---|
55 | template <class Iter> |
---|
56 | struct iterator_traits<StrideIterator<Iter> > { |
---|
57 | /** |
---|
58 | data_reference same as for underlying iterator |
---|
59 | */ |
---|
60 | typedef typename iterator_traits<Iter>::data_reference data_reference; |
---|
61 | |
---|
62 | /** |
---|
63 | weight_reference same as for underlying iterator |
---|
64 | */ |
---|
65 | typedef typename iterator_traits<Iter>::weight_reference weight_reference; |
---|
66 | |
---|
67 | /** |
---|
68 | \return data of iterator |
---|
69 | */ |
---|
70 | data_reference data(StrideIterator<Iter> iter) const |
---|
71 | { return iterator_traits<Iter>().data(iter.base()); } |
---|
72 | |
---|
73 | /** |
---|
74 | \return weight of iterator |
---|
75 | */ |
---|
76 | weight_reference weight(StrideIterator<Iter> iter) const |
---|
77 | { return iterator_traits<Iter>().weight(iter.base()); } |
---|
78 | |
---|
79 | }; |
---|
80 | |
---|
81 | /** |
---|
82 | @brief Adaptor using a stride on underlying iterator |
---|
83 | |
---|
84 | Works as underlying iterator except that all arithmetic uses the |
---|
85 | stride, so e.g., ++StrideIterator returns underlying |
---|
86 | _iterator+stride |
---|
87 | |
---|
88 | */ |
---|
89 | template<typename Iter> |
---|
90 | class StrideIterator |
---|
91 | : public boost::iterator_adaptor<StrideIterator<Iter>, Iter> |
---|
92 | { |
---|
93 | typedef boost::iterator_adaptor<StrideIterator<Iter>,Iter> super_t; |
---|
94 | |
---|
95 | public: |
---|
96 | /// type of underlying iterator |
---|
97 | typedef Iter iterator_type; |
---|
98 | |
---|
99 | /* |
---|
100 | /// value type |
---|
101 | typedef typename std::iterator_traits<Iter>::value_type value_type; |
---|
102 | /// difference type |
---|
103 | typedef typename std::iterator_traits<Iter>::difference_type difference_type; |
---|
104 | /// reference |
---|
105 | typedef typename std::iterator_traits<Iter>::reference reference; |
---|
106 | /// pointer |
---|
107 | typedef typename std::iterator_traits<Iter>::pointer pointer; |
---|
108 | /// weighted_iterator_tag if iterator is weighted |
---|
109 | typedef typename yat::utility::weighted_iterator_traits<Iter>::type w_type; |
---|
110 | */ |
---|
111 | |
---|
112 | /** |
---|
113 | \brief default constructor |
---|
114 | |
---|
115 | Using default constructor of BASE iterator. |
---|
116 | */ |
---|
117 | explicit StrideIterator(size_t stride=1) |
---|
118 | : StrideIterator::iterator_adaptor_(), stride_(stride) {} |
---|
119 | |
---|
120 | /** |
---|
121 | \brief Constructor |
---|
122 | */ |
---|
123 | explicit StrideIterator(Iter p, size_t stride=1) |
---|
124 | : StrideIterator::iterator_adaptor_(p), stride_(stride) {} |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | \brief Copy constructor |
---|
129 | */ |
---|
130 | StrideIterator(const StrideIterator& other) |
---|
131 | : StrideIterator::iterator_adaptor_(other.base()), stride_(other.stride()) |
---|
132 | {} |
---|
133 | |
---|
134 | /** |
---|
135 | \brief Conversion constructor. |
---|
136 | |
---|
137 | Create a StrideIterator<Iter> from a StrideIterator<I2>. Possible |
---|
138 | if I2 is convertible to a Iter. Constructor allows implicit |
---|
139 | conversions such as iterator to const_iterator. |
---|
140 | */ |
---|
141 | template<typename I2> |
---|
142 | StrideIterator(StrideIterator<I2> other, |
---|
143 | typename boost::enable_if_convertible<I2, Iter>::type* = 0 ) |
---|
144 | : StrideIterator::iterator_adaptor_(other.base()), |
---|
145 | stride_(other.stride()) {} |
---|
146 | |
---|
147 | /** |
---|
148 | \brief Assignment operator |
---|
149 | */ |
---|
150 | StrideIterator& operator=(const StrideIterator& rhs) |
---|
151 | { |
---|
152 | stride_ = rhs.stride(); |
---|
153 | this->base_reference() = rhs.base(); |
---|
154 | return *this; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | \return stride |
---|
159 | */ |
---|
160 | inline size_t stride(void) const { return stride_; } |
---|
161 | |
---|
162 | private: |
---|
163 | // to give base class access to private parts |
---|
164 | friend class boost::iterator_core_access; |
---|
165 | |
---|
166 | size_t stride_; |
---|
167 | |
---|
168 | //typedef typename StrideIterator::iterator_adaptor_::difference_type |
---|
169 | typedef typename StrideIterator::iterator_adaptor_::difference_type |
---|
170 | difference_t; |
---|
171 | |
---|
172 | void advance(typename super_t::difference_type n) |
---|
173 | { this->base_reference() += stride_*n; } |
---|
174 | |
---|
175 | void decrement(void) { this->base_reference()-=stride_; } |
---|
176 | |
---|
177 | template <class OtherIterator> |
---|
178 | typename super_t::difference_type |
---|
179 | distance_to(const StrideIterator<OtherIterator>& other) const |
---|
180 | { |
---|
181 | // casting to int to avoid loss of sign in numerator |
---|
182 | return (other.base() - this->base() )/static_cast<int>(stride_); |
---|
183 | } |
---|
184 | |
---|
185 | void increment(void) { this->base_reference()+=stride_; } |
---|
186 | |
---|
187 | }; |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | }}} // of namespace utility, yat, and theplu |
---|
194 | |
---|
195 | #endif |
---|