1 | // Boost.Range library |
---|
2 | // |
---|
3 | // Copyright Neil Groves 2010. Use, modification and |
---|
4 | // distribution is subject to the Boost Software License, Version |
---|
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | // |
---|
8 | // |
---|
9 | // For more information, see http://www.boost.org/libs/range/ |
---|
10 | // |
---|
11 | #ifndef BOOST_RANGE_IRANGE_HPP_INCLUDED |
---|
12 | #define BOOST_RANGE_IRANGE_HPP_INCLUDED |
---|
13 | |
---|
14 | #include <boost/assert.hpp> |
---|
15 | #include <boost/iterator/iterator_facade.hpp> |
---|
16 | #include <boost/range/iterator_range.hpp> |
---|
17 | |
---|
18 | namespace boost |
---|
19 | { |
---|
20 | namespace range_detail |
---|
21 | { |
---|
22 | // integer_iterator is an iterator over an integer sequence that |
---|
23 | // is bounded only by the limits of the underlying integer |
---|
24 | // representation. |
---|
25 | // |
---|
26 | // This is useful for implementing the irange(first, last) |
---|
27 | // function. |
---|
28 | // |
---|
29 | // Note: |
---|
30 | // This use of this iterator and irange is appreciably less |
---|
31 | // performant than the corresponding hand-written integer |
---|
32 | // loop on many compilers. |
---|
33 | template<typename Integer> |
---|
34 | class integer_iterator |
---|
35 | : public boost::iterator_facade< |
---|
36 | integer_iterator<Integer>, |
---|
37 | Integer, |
---|
38 | boost::random_access_traversal_tag, |
---|
39 | Integer, |
---|
40 | std::ptrdiff_t |
---|
41 | > |
---|
42 | { |
---|
43 | typedef boost::iterator_facade< |
---|
44 | integer_iterator<Integer>, |
---|
45 | Integer, |
---|
46 | boost::random_access_traversal_tag, |
---|
47 | Integer, |
---|
48 | std::ptrdiff_t |
---|
49 | > base_t; |
---|
50 | public: |
---|
51 | typedef typename base_t::value_type value_type; |
---|
52 | typedef typename base_t::difference_type difference_type; |
---|
53 | typedef typename base_t::reference reference; |
---|
54 | |
---|
55 | integer_iterator() : m_value() {} |
---|
56 | explicit integer_iterator(value_type x) : m_value(x) {} |
---|
57 | |
---|
58 | private: |
---|
59 | void increment() |
---|
60 | { |
---|
61 | ++m_value; |
---|
62 | } |
---|
63 | |
---|
64 | void decrement() |
---|
65 | { |
---|
66 | --m_value; |
---|
67 | } |
---|
68 | |
---|
69 | void advance(difference_type offset) |
---|
70 | { |
---|
71 | m_value += offset; |
---|
72 | } |
---|
73 | |
---|
74 | difference_type distance_to(const integer_iterator& other) const |
---|
75 | { |
---|
76 | return other.m_value - m_value; |
---|
77 | } |
---|
78 | |
---|
79 | bool equal(const integer_iterator& other) const |
---|
80 | { |
---|
81 | return m_value == other.m_value; |
---|
82 | } |
---|
83 | |
---|
84 | reference dereference() const |
---|
85 | { |
---|
86 | return m_value; |
---|
87 | } |
---|
88 | |
---|
89 | friend class ::boost::iterator_core_access; |
---|
90 | value_type m_value; |
---|
91 | }; |
---|
92 | |
---|
93 | // integer_iterator_with_step is similar in nature to the |
---|
94 | // integer_iterator but provides the ability to 'move' in |
---|
95 | // a number of steps specified at construction time. |
---|
96 | // |
---|
97 | // The three variable implementation provides the best guarantees |
---|
98 | // of loop termination upon various combinations of input. |
---|
99 | // |
---|
100 | // While this design is less performant than some less |
---|
101 | // safe alternatives, the use of ranges and iterators to |
---|
102 | // perform counting will never be optimal anyhow, hence |
---|
103 | // if optimal performance is desired a handcoded loop |
---|
104 | // is the solution. |
---|
105 | template<typename Integer> |
---|
106 | class integer_iterator_with_step |
---|
107 | : public boost::iterator_facade< |
---|
108 | integer_iterator_with_step<Integer>, |
---|
109 | Integer, |
---|
110 | boost::random_access_traversal_tag, |
---|
111 | Integer, |
---|
112 | std::ptrdiff_t |
---|
113 | > |
---|
114 | { |
---|
115 | typedef boost::iterator_facade< |
---|
116 | integer_iterator_with_step<Integer>, |
---|
117 | Integer, |
---|
118 | boost::random_access_traversal_tag, |
---|
119 | Integer, |
---|
120 | std::ptrdiff_t |
---|
121 | > base_t; |
---|
122 | public: |
---|
123 | typedef typename base_t::value_type value_type; |
---|
124 | typedef typename base_t::difference_type difference_type; |
---|
125 | typedef typename base_t::reference reference; |
---|
126 | |
---|
127 | integer_iterator_with_step(value_type first, value_type step, difference_type step_size) |
---|
128 | : m_first(first) |
---|
129 | , m_step(step) |
---|
130 | , m_step_size(step_size) |
---|
131 | { |
---|
132 | BOOST_ASSERT( step >= 0 ); |
---|
133 | BOOST_ASSERT( step_size != 0 ); |
---|
134 | } |
---|
135 | |
---|
136 | private: |
---|
137 | void increment() |
---|
138 | { |
---|
139 | ++m_step; |
---|
140 | } |
---|
141 | |
---|
142 | void decrement() |
---|
143 | { |
---|
144 | --m_step; |
---|
145 | } |
---|
146 | |
---|
147 | void advance(difference_type offset) |
---|
148 | { |
---|
149 | m_step += offset; |
---|
150 | } |
---|
151 | |
---|
152 | difference_type distance_to(const integer_iterator_with_step& other) const |
---|
153 | { |
---|
154 | return other.m_step - m_step; |
---|
155 | } |
---|
156 | |
---|
157 | bool equal(const integer_iterator_with_step& other) const |
---|
158 | { |
---|
159 | return m_step == other.m_step; |
---|
160 | } |
---|
161 | |
---|
162 | reference dereference() const |
---|
163 | { |
---|
164 | return m_first + (m_step * m_step_size); |
---|
165 | } |
---|
166 | |
---|
167 | friend class ::boost::iterator_core_access; |
---|
168 | value_type m_first; |
---|
169 | value_type m_step; |
---|
170 | difference_type m_step_size; |
---|
171 | }; |
---|
172 | |
---|
173 | } // namespace range_detail |
---|
174 | |
---|
175 | template<typename Integer> |
---|
176 | class integer_range |
---|
177 | : public iterator_range< range_detail::integer_iterator<Integer> > |
---|
178 | { |
---|
179 | typedef range_detail::integer_iterator<Integer> iterator_t; |
---|
180 | typedef iterator_range<iterator_t> base_t; |
---|
181 | public: |
---|
182 | integer_range(Integer first, Integer last) |
---|
183 | : base_t(iterator_t(first), iterator_t(last)) |
---|
184 | { |
---|
185 | } |
---|
186 | }; |
---|
187 | |
---|
188 | template<typename Integer> |
---|
189 | class strided_integer_range |
---|
190 | : public iterator_range< range_detail::integer_iterator_with_step<Integer> > |
---|
191 | { |
---|
192 | typedef range_detail::integer_iterator_with_step<Integer> iterator_t; |
---|
193 | typedef iterator_range<iterator_t> base_t; |
---|
194 | public: |
---|
195 | template<typename Iterator> |
---|
196 | strided_integer_range(Iterator first, Iterator last) |
---|
197 | : base_t(first, last) |
---|
198 | { |
---|
199 | } |
---|
200 | }; |
---|
201 | |
---|
202 | template<typename Integer> |
---|
203 | integer_range<Integer> |
---|
204 | irange(Integer first, Integer last) |
---|
205 | { |
---|
206 | BOOST_ASSERT( first <= last ); |
---|
207 | return integer_range<Integer>(first, last); |
---|
208 | } |
---|
209 | |
---|
210 | template<typename Integer, typename StepSize> |
---|
211 | strided_integer_range<Integer> |
---|
212 | irange(Integer first, Integer last, StepSize step_size) |
---|
213 | { |
---|
214 | BOOST_ASSERT( step_size != 0 ); |
---|
215 | BOOST_ASSERT( (step_size > 0) ? (last >= first) : (last <= first) ); |
---|
216 | |
---|
217 | typedef typename range_detail::integer_iterator_with_step<Integer> iterator_t; |
---|
218 | |
---|
219 | const std::ptrdiff_t last_step |
---|
220 | = (static_cast<std::ptrdiff_t>(last) - static_cast<std::ptrdiff_t>(first)) |
---|
221 | / (static_cast<std::ptrdiff_t>(step_size)); |
---|
222 | |
---|
223 | return strided_integer_range<Integer>( |
---|
224 | iterator_t(first, 0, step_size), |
---|
225 | iterator_t(first, last_step, step_size)); |
---|
226 | } |
---|
227 | |
---|
228 | } // namespace boost |
---|
229 | |
---|
230 | #endif // include guard |
---|