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