1 | // Boost.Range library |
---|
2 | // |
---|
3 | // Copyright Neil Groves 2009. 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_DETAIL_JOIN_ITERATOR_HPP_INCLUDED |
---|
12 | #define BOOST_RANGE_DETAIL_JOIN_ITERATOR_HPP_INCLUDED |
---|
13 | |
---|
14 | #include <iterator> |
---|
15 | #include <boost/assert.hpp> |
---|
16 | #include <boost/iterator/iterator_traits.hpp> |
---|
17 | #include <boost/iterator/iterator_facade.hpp> |
---|
18 | #include <boost/range/begin.hpp> |
---|
19 | #include <boost/range/end.hpp> |
---|
20 | #include <boost/range/empty.hpp> |
---|
21 | #include <boost/range/detail/demote_iterator_traversal_tag.hpp> |
---|
22 | #include <boost/range/value_type.hpp> |
---|
23 | #include <boost/utility.hpp> |
---|
24 | |
---|
25 | namespace boost |
---|
26 | { |
---|
27 | namespace range_detail |
---|
28 | { |
---|
29 | |
---|
30 | template<typename Iterator1, typename Iterator2> |
---|
31 | struct join_iterator_link |
---|
32 | { |
---|
33 | public: |
---|
34 | join_iterator_link(Iterator1 last1, Iterator2 first2) |
---|
35 | : last1(last1) |
---|
36 | , first2(first2) |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | Iterator1 last1; |
---|
41 | Iterator2 first2; |
---|
42 | |
---|
43 | private: |
---|
44 | join_iterator_link() /* = delete */ ; |
---|
45 | }; |
---|
46 | |
---|
47 | class join_iterator_begin_tag {}; |
---|
48 | class join_iterator_end_tag {}; |
---|
49 | |
---|
50 | template<typename Iterator1 |
---|
51 | , typename Iterator2 |
---|
52 | , typename Reference |
---|
53 | > |
---|
54 | class join_iterator_union |
---|
55 | { |
---|
56 | public: |
---|
57 | typedef Iterator1 iterator1_t; |
---|
58 | typedef Iterator2 iterator2_t; |
---|
59 | |
---|
60 | join_iterator_union() {} |
---|
61 | join_iterator_union(unsigned int /*selected*/, const iterator1_t& it1, const iterator2_t& it2) : m_it1(it1), m_it2(it2) {} |
---|
62 | |
---|
63 | iterator1_t& it1() { return m_it1; } |
---|
64 | const iterator1_t& it1() const { return m_it1; } |
---|
65 | |
---|
66 | iterator2_t& it2() { return m_it2; } |
---|
67 | const iterator2_t& it2() const { return m_it2; } |
---|
68 | |
---|
69 | Reference dereference(unsigned int selected) const |
---|
70 | { |
---|
71 | return selected ? *m_it2 : *m_it1; |
---|
72 | } |
---|
73 | |
---|
74 | bool equal(const join_iterator_union& other, unsigned int selected) const |
---|
75 | { |
---|
76 | return selected |
---|
77 | ? m_it2 == other.m_it2 |
---|
78 | : m_it1 == other.m_it1; |
---|
79 | } |
---|
80 | |
---|
81 | private: |
---|
82 | iterator1_t m_it1; |
---|
83 | iterator2_t m_it2; |
---|
84 | }; |
---|
85 | |
---|
86 | template<class Iterator, class Reference> |
---|
87 | class join_iterator_union<Iterator, Iterator, Reference> |
---|
88 | { |
---|
89 | public: |
---|
90 | typedef Iterator iterator1_t; |
---|
91 | typedef Iterator iterator2_t; |
---|
92 | |
---|
93 | join_iterator_union() {} |
---|
94 | |
---|
95 | join_iterator_union(unsigned int selected, const iterator1_t& it1, const iterator2_t& it2) |
---|
96 | : m_it(selected ? it2 : it1) |
---|
97 | { |
---|
98 | } |
---|
99 | |
---|
100 | iterator1_t& it1() { return m_it; } |
---|
101 | const iterator1_t& it1() const { return m_it; } |
---|
102 | |
---|
103 | iterator2_t& it2() { return m_it; } |
---|
104 | const iterator2_t& it2() const { return m_it; } |
---|
105 | |
---|
106 | Reference dereference(unsigned int) const |
---|
107 | { |
---|
108 | return *m_it; |
---|
109 | } |
---|
110 | |
---|
111 | bool equal(const join_iterator_union& other, unsigned int selected) const |
---|
112 | { |
---|
113 | return m_it == other.m_it; |
---|
114 | } |
---|
115 | |
---|
116 | private: |
---|
117 | iterator1_t m_it; |
---|
118 | }; |
---|
119 | |
---|
120 | template<typename Iterator1 |
---|
121 | , typename Iterator2 |
---|
122 | , typename ValueType = typename iterator_value<Iterator1>::type |
---|
123 | , typename Reference = typename iterator_reference<Iterator1>::type |
---|
124 | , typename Traversal = typename demote_iterator_traversal_tag< |
---|
125 | typename iterator_traversal<Iterator1>::type |
---|
126 | , typename iterator_traversal<Iterator2>::type>::type |
---|
127 | > |
---|
128 | class join_iterator |
---|
129 | : public iterator_facade<join_iterator<Iterator1,Iterator2,ValueType,Reference,Traversal>, ValueType, Traversal, Reference> |
---|
130 | { |
---|
131 | typedef join_iterator_link<Iterator1, Iterator2> link_t; |
---|
132 | typedef join_iterator_union<Iterator1, Iterator2, Reference> iterator_union; |
---|
133 | public: |
---|
134 | typedef Iterator1 iterator1_t; |
---|
135 | typedef Iterator2 iterator2_t; |
---|
136 | |
---|
137 | join_iterator() |
---|
138 | : m_section(0u) |
---|
139 | , m_it(0u, iterator1_t(), iterator2_t()) |
---|
140 | , m_link(link_t(iterator1_t(), iterator2_t())) |
---|
141 | {} |
---|
142 | |
---|
143 | join_iterator(unsigned int section, Iterator1 current1, Iterator1 last1, Iterator2 first2, Iterator2 current2) |
---|
144 | : m_section(section) |
---|
145 | , m_it(section, current1, current2) |
---|
146 | , m_link(link_t(last1, first2)) |
---|
147 | { |
---|
148 | } |
---|
149 | |
---|
150 | template<typename Range1, typename Range2> |
---|
151 | join_iterator(Range1& r1, Range2& r2, join_iterator_begin_tag) |
---|
152 | : m_section(boost::empty(r1) ? 1u : 0u) |
---|
153 | , m_it(boost::empty(r1) ? 1u : 0u, boost::begin(r1), boost::begin(r2)) |
---|
154 | , m_link(link_t(boost::end(r1), boost::begin(r2))) |
---|
155 | { |
---|
156 | } |
---|
157 | |
---|
158 | template<typename Range1, typename Range2> |
---|
159 | join_iterator(const Range1& r1, const Range2& r2, join_iterator_begin_tag) |
---|
160 | : m_section(boost::empty(r1) ? 1u : 0u) |
---|
161 | , m_it(boost::empty(r1) ? 1u : 0u, boost::const_begin(r1), boost::const_begin(r2)) |
---|
162 | , m_link(link_t(boost::const_end(r1), boost::const_begin(r2))) |
---|
163 | { |
---|
164 | } |
---|
165 | |
---|
166 | template<typename Range1, typename Range2> |
---|
167 | join_iterator(Range1& r1, Range2& r2, join_iterator_end_tag) |
---|
168 | : m_section(1u) |
---|
169 | , m_it(1u, boost::end(r1), boost::end(r2)) |
---|
170 | , m_link(link_t(boost::end(r1), boost::begin(r2))) |
---|
171 | { |
---|
172 | } |
---|
173 | |
---|
174 | template<typename Range1, typename Range2> |
---|
175 | join_iterator(const Range1& r1, const Range2& r2, join_iterator_end_tag) |
---|
176 | : m_section(1u) |
---|
177 | , m_it(1u, boost::const_end(r1), boost::const_end(r2)) |
---|
178 | , m_link(link_t(boost::const_end(r1), boost::const_begin(r2))) |
---|
179 | { |
---|
180 | } |
---|
181 | |
---|
182 | private: |
---|
183 | void increment() |
---|
184 | { |
---|
185 | if (m_section) |
---|
186 | ++m_it.it2(); |
---|
187 | else |
---|
188 | { |
---|
189 | ++m_it.it1(); |
---|
190 | if (m_it.it1() == m_link.last1) |
---|
191 | { |
---|
192 | m_it.it2() = m_link.first2; |
---|
193 | m_section = 1u; |
---|
194 | } |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | void decrement() |
---|
199 | { |
---|
200 | if (m_section) |
---|
201 | { |
---|
202 | if (m_it.it2() == m_link.first2) |
---|
203 | { |
---|
204 | m_it.it1() = boost::prior(m_link.last1); |
---|
205 | m_section = 0u; |
---|
206 | } |
---|
207 | else |
---|
208 | --m_it.it2(); |
---|
209 | } |
---|
210 | else |
---|
211 | --m_it.it1(); |
---|
212 | } |
---|
213 | |
---|
214 | typename join_iterator::reference dereference() const |
---|
215 | { |
---|
216 | return m_it.dereference(m_section); |
---|
217 | } |
---|
218 | |
---|
219 | bool equal(const join_iterator& other) const |
---|
220 | { |
---|
221 | return m_section == other.m_section |
---|
222 | && m_it.equal(other.m_it, m_section); |
---|
223 | } |
---|
224 | |
---|
225 | void advance(typename join_iterator::difference_type offset) |
---|
226 | { |
---|
227 | if (m_section) |
---|
228 | advance_from_range2(offset); |
---|
229 | else |
---|
230 | advance_from_range1(offset); |
---|
231 | } |
---|
232 | |
---|
233 | typename join_iterator::difference_type distance_to(const join_iterator& other) const |
---|
234 | { |
---|
235 | typename join_iterator::difference_type result; |
---|
236 | if (m_section) |
---|
237 | { |
---|
238 | if (other.m_section) |
---|
239 | result = other.m_it.it2() - m_it.it2(); |
---|
240 | else |
---|
241 | { |
---|
242 | result = (m_link.first2 - m_it.it2()) |
---|
243 | + (other.m_it.it1() - m_link.last1); |
---|
244 | |
---|
245 | BOOST_ASSERT( result <= 0 ); |
---|
246 | } |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | if (other.m_section) |
---|
251 | { |
---|
252 | result = (m_link.last1 - m_it.it1()) |
---|
253 | + (other.m_it.it2() - m_link.first2); |
---|
254 | } |
---|
255 | else |
---|
256 | result = other.m_it.it1() - m_it.it1(); |
---|
257 | } |
---|
258 | return result; |
---|
259 | } |
---|
260 | |
---|
261 | void advance_from_range2(typename join_iterator::difference_type offset) |
---|
262 | { |
---|
263 | typedef typename join_iterator::difference_type difference_t; |
---|
264 | BOOST_ASSERT( m_section == 1u ); |
---|
265 | if (offset < 0) |
---|
266 | { |
---|
267 | difference_t r2_dist = m_link.first2 - m_it.it2(); |
---|
268 | BOOST_ASSERT( r2_dist <= 0 ); |
---|
269 | if (offset >= r2_dist) |
---|
270 | std::advance(m_it.it2(), offset); |
---|
271 | else |
---|
272 | { |
---|
273 | difference_t r1_dist = offset - r2_dist; |
---|
274 | BOOST_ASSERT( r1_dist <= 0 ); |
---|
275 | m_it.it1() = m_link.last1 + r1_dist; |
---|
276 | m_section = 0u; |
---|
277 | } |
---|
278 | } |
---|
279 | else |
---|
280 | std::advance(m_it.it2(), offset); |
---|
281 | } |
---|
282 | |
---|
283 | void advance_from_range1(typename join_iterator::difference_type offset) |
---|
284 | { |
---|
285 | typedef typename join_iterator::difference_type difference_t; |
---|
286 | BOOST_ASSERT( m_section == 0u ); |
---|
287 | if (offset > 0) |
---|
288 | { |
---|
289 | difference_t r1_dist = m_link.last1 - m_it.it1(); |
---|
290 | BOOST_ASSERT( r1_dist >= 0 ); |
---|
291 | if (offset < r1_dist) |
---|
292 | std::advance(m_it.it1(), offset); |
---|
293 | else |
---|
294 | { |
---|
295 | difference_t r2_dist = offset - r1_dist; |
---|
296 | BOOST_ASSERT( r2_dist >= 0 ); |
---|
297 | m_it.it2() = m_link.first2 + r2_dist; |
---|
298 | m_section = 1u; |
---|
299 | } |
---|
300 | } |
---|
301 | else |
---|
302 | std::advance(m_it.it1(), offset); |
---|
303 | } |
---|
304 | |
---|
305 | unsigned int m_section; |
---|
306 | iterator_union m_it; |
---|
307 | link_t m_link; |
---|
308 | |
---|
309 | friend class ::boost::iterator_core_access; |
---|
310 | }; |
---|
311 | |
---|
312 | } // namespace range_detail |
---|
313 | |
---|
314 | } // namespace boost |
---|
315 | |
---|
316 | #endif // include guard |
---|