Chris@16
|
1 /*-----------------------------------------------------------------------------+
|
Chris@16
|
2 Copyright (c) 2009-2009: Joachim Faulhaber
|
Chris@16
|
3 +------------------------------------------------------------------------------+
|
Chris@16
|
4 Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
5 (See accompanying file LICENCE.txt or copy at
|
Chris@16
|
6 http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 +-----------------------------------------------------------------------------*/
|
Chris@16
|
8 #ifndef BOOST_ICL_DETAIL_ELEMENT_ITERATOR_HPP_JOFA_091104
|
Chris@16
|
9 #define BOOST_ICL_DETAIL_ELEMENT_ITERATOR_HPP_JOFA_091104
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/mpl/if.hpp>
|
Chris@16
|
12 #include <boost/iterator/iterator_facade.hpp>
|
Chris@16
|
13 #include <boost/config/warning_disable.hpp>
|
Chris@16
|
14 #include <boost/icl/type_traits/succ_pred.hpp>
|
Chris@16
|
15 #include <boost/icl/detail/mapped_reference.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost{namespace icl
|
Chris@16
|
18 {
|
Chris@16
|
19
|
Chris@16
|
20 //------------------------------------------------------------------------------
|
Chris@16
|
21 template<class Type>
|
Chris@16
|
22 struct is_std_pair
|
Chris@16
|
23 {
|
Chris@16
|
24 typedef is_std_pair<Type> type;
|
Chris@16
|
25 BOOST_STATIC_CONSTANT(bool, value = false);
|
Chris@16
|
26 };
|
Chris@16
|
27
|
Chris@16
|
28 template<class FirstT, class SecondT>
|
Chris@16
|
29 struct is_std_pair<std::pair<FirstT, SecondT> >
|
Chris@16
|
30 {
|
Chris@16
|
31 typedef is_std_pair<std::pair<FirstT, SecondT> > type;
|
Chris@16
|
32 BOOST_STATIC_CONSTANT(bool, value = true);
|
Chris@16
|
33 };
|
Chris@16
|
34
|
Chris@16
|
35
|
Chris@16
|
36 //------------------------------------------------------------------------------
|
Chris@16
|
37 template<class Type>
|
Chris@16
|
38 struct first_element
|
Chris@16
|
39 {
|
Chris@16
|
40 typedef Type type;
|
Chris@16
|
41 };
|
Chris@16
|
42
|
Chris@16
|
43 template<class FirstT, class SecondT>
|
Chris@16
|
44 struct first_element<std::pair<FirstT, SecondT> >
|
Chris@16
|
45 {
|
Chris@16
|
46 typedef FirstT type;
|
Chris@16
|
47 };
|
Chris@16
|
48
|
Chris@16
|
49 //------------------------------------------------------------------------------
|
Chris@16
|
50 template <class SegmentIteratorT> class element_iterator;
|
Chris@16
|
51
|
Chris@16
|
52 template<class IteratorT>
|
Chris@16
|
53 struct is_reverse
|
Chris@16
|
54 {
|
Chris@16
|
55 typedef is_reverse type;
|
Chris@16
|
56 BOOST_STATIC_CONSTANT(bool, value = false);
|
Chris@16
|
57 };
|
Chris@16
|
58
|
Chris@16
|
59 template<class BaseIteratorT>
|
Chris@16
|
60 struct is_reverse<std::reverse_iterator<BaseIteratorT> >
|
Chris@16
|
61 {
|
Chris@16
|
62 typedef is_reverse<std::reverse_iterator<BaseIteratorT> > type;
|
Chris@16
|
63 BOOST_STATIC_CONSTANT(bool, value = true);
|
Chris@16
|
64 };
|
Chris@16
|
65
|
Chris@16
|
66 template<class BaseIteratorT>
|
Chris@16
|
67 struct is_reverse<icl::element_iterator<BaseIteratorT> >
|
Chris@16
|
68 {
|
Chris@16
|
69 typedef is_reverse<icl::element_iterator<BaseIteratorT> > type;
|
Chris@16
|
70 BOOST_STATIC_CONSTANT(bool, value = is_reverse<BaseIteratorT>::value);
|
Chris@16
|
71 };
|
Chris@16
|
72
|
Chris@16
|
73 //------------------------------------------------------------------------------
|
Chris@16
|
74 template<class SegmentT>
|
Chris@16
|
75 struct elemental;
|
Chris@16
|
76
|
Chris@16
|
77 #ifdef ICL_USE_INTERVAL_TEMPLATE_TEMPLATE
|
Chris@16
|
78
|
Chris@16
|
79 template<class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval>
|
Chris@16
|
80 struct elemental<ICL_INTERVAL_TYPE(Interval,DomainT,Compare) >
|
Chris@16
|
81 {
|
Chris@16
|
82 typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) segment_type;
|
Chris@16
|
83 typedef segment_type interval_type;
|
Chris@16
|
84 typedef DomainT type;
|
Chris@16
|
85 typedef DomainT domain_type;
|
Chris@16
|
86 typedef DomainT codomain_type;
|
Chris@16
|
87 typedef DomainT transit_type;
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 template< class DomainT, class CodomainT,
|
Chris@16
|
91 ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval >
|
Chris@16
|
92 struct elemental<std::pair<ICL_INTERVAL_TYPE(Interval,DomainT,Compare)const, CodomainT> >
|
Chris@16
|
93 {
|
Chris@16
|
94 typedef std::pair<ICL_INTERVAL_TYPE(Interval,DomainT,Compare), CodomainT> segment_type;
|
Chris@16
|
95 typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
|
Chris@16
|
96 typedef std::pair<DomainT, CodomainT> type;
|
Chris@16
|
97 typedef DomainT domain_type;
|
Chris@16
|
98 typedef CodomainT codomain_type;
|
Chris@16
|
99 typedef mapped_reference<DomainT, CodomainT> transit_type;
|
Chris@16
|
100 };
|
Chris@16
|
101
|
Chris@16
|
102 #else //ICL_USE_INTERVAL_TEMPLATE_TYPE
|
Chris@16
|
103
|
Chris@16
|
104 template<ICL_INTERVAL(ICL_COMPARE) Interval>
|
Chris@16
|
105 struct elemental
|
Chris@16
|
106 {
|
Chris@16
|
107 typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) segment_type;
|
Chris@16
|
108 typedef segment_type interval_type;
|
Chris@16
|
109 typedef typename interval_traits<interval_type>::domain_type domain_type;
|
Chris@16
|
110 typedef domain_type type;
|
Chris@16
|
111 typedef domain_type codomain_type;
|
Chris@16
|
112 typedef domain_type transit_type;
|
Chris@16
|
113 };
|
Chris@16
|
114
|
Chris@16
|
115 template< class CodomainT, ICL_INTERVAL(ICL_COMPARE) Interval >
|
Chris@16
|
116 struct elemental<std::pair<ICL_INTERVAL_TYPE(Interval,DomainT,Compare)const, CodomainT> >
|
Chris@16
|
117 {
|
Chris@16
|
118 typedef std::pair<ICL_INTERVAL_TYPE(Interval,DomainT,Compare), CodomainT> segment_type;
|
Chris@16
|
119 typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
|
Chris@16
|
120 typedef typename interval_traits<interval_type>::domain_type domain_type;
|
Chris@16
|
121 typedef CodomainT codomain_type;
|
Chris@16
|
122 typedef std::pair<domain_type, codomain_type> type;
|
Chris@16
|
123 typedef mapped_reference<domain_type, codomain_type> transit_type;
|
Chris@16
|
124 };
|
Chris@16
|
125
|
Chris@16
|
126 #endif //ICL_USE_INTERVAL_TEMPLATE_TEMPLATE
|
Chris@16
|
127
|
Chris@16
|
128
|
Chris@16
|
129 //------------------------------------------------------------------------------
|
Chris@16
|
130 //- struct segment_adapter
|
Chris@16
|
131 //------------------------------------------------------------------------------
|
Chris@16
|
132 template<class SegmentIteratorT, class SegmentT>
|
Chris@16
|
133 struct segment_adapter;
|
Chris@16
|
134
|
Chris@16
|
135 #ifdef ICL_USE_INTERVAL_TEMPLATE_TEMPLATE
|
Chris@16
|
136
|
Chris@16
|
137 template<class SegmentIteratorT, class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval>
|
Chris@16
|
138 struct segment_adapter<SegmentIteratorT, ICL_INTERVAL_TYPE(Interval,DomainT,Compare) >
|
Chris@16
|
139 {
|
Chris@16
|
140 typedef segment_adapter type;
|
Chris@16
|
141 typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) segment_type;
|
Chris@16
|
142 typedef segment_type interval_type;
|
Chris@16
|
143 typedef typename interval_type::difference_type domain_difference_type;
|
Chris@16
|
144 typedef DomainT domain_type;
|
Chris@16
|
145 typedef DomainT codomain_type;
|
Chris@16
|
146 typedef domain_type element_type;
|
Chris@16
|
147 typedef domain_type& transit_type;
|
Chris@16
|
148
|
Chris@16
|
149 static domain_type first (const SegmentIteratorT& leaper){ return leaper->first(); }
|
Chris@16
|
150 static domain_type last (const SegmentIteratorT& leaper){ return leaper->last(); }
|
Chris@16
|
151 static domain_difference_type length(const SegmentIteratorT& leaper){ return leaper->length();}
|
Chris@16
|
152
|
Chris@16
|
153 static transit_type transient_element(domain_type& inter_pos, const SegmentIteratorT& leaper,
|
Chris@16
|
154 const domain_difference_type& sneaker)
|
Chris@16
|
155 {
|
Chris@16
|
156 inter_pos = is_reverse<SegmentIteratorT>::value ? leaper->last() - sneaker
|
Chris@16
|
157 : leaper->first() + sneaker;
|
Chris@16
|
158 return inter_pos;
|
Chris@16
|
159 }
|
Chris@16
|
160 };
|
Chris@16
|
161
|
Chris@16
|
162 template < class SegmentIteratorT, class DomainT, class CodomainT,
|
Chris@16
|
163 ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval >
|
Chris@16
|
164 struct segment_adapter<SegmentIteratorT, std::pair<ICL_INTERVAL_TYPE(Interval,DomainT,Compare)const, CodomainT> >
|
Chris@16
|
165 {
|
Chris@16
|
166 typedef segment_adapter type;
|
Chris@16
|
167 typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
|
Chris@16
|
168 typedef DomainT domain_type;
|
Chris@16
|
169 typedef std::pair<DomainT, CodomainT> element_type;
|
Chris@16
|
170 typedef CodomainT codomain_type;
|
Chris@16
|
171 typedef mapped_reference<DomainT, CodomainT> transit_type;
|
Chris@16
|
172 typedef typename difference_type_of<interval_traits<interval_type> >::type
|
Chris@16
|
173 domain_difference_type;
|
Chris@16
|
174
|
Chris@16
|
175 static domain_type first (const SegmentIteratorT& leaper){ return leaper->first.first(); }
|
Chris@16
|
176 static domain_type last (const SegmentIteratorT& leaper){ return leaper->first.last(); }
|
Chris@16
|
177 static domain_difference_type length(const SegmentIteratorT& leaper){ return leaper->first.length();}
|
Chris@16
|
178
|
Chris@16
|
179 static transit_type transient_element(domain_type& inter_pos, const SegmentIteratorT& leaper,
|
Chris@16
|
180 const domain_difference_type& sneaker)
|
Chris@16
|
181 {
|
Chris@16
|
182 inter_pos = is_reverse<SegmentIteratorT>::value ? leaper->first.last() - sneaker
|
Chris@16
|
183 : leaper->first.first() + sneaker;
|
Chris@16
|
184 return transit_type(inter_pos, leaper->second);
|
Chris@16
|
185 }
|
Chris@16
|
186 };
|
Chris@16
|
187
|
Chris@16
|
188 #else // ICL_USE_INTERVAL_TEMPLATE_TYPE
|
Chris@16
|
189
|
Chris@16
|
190 template<class SegmentIteratorT, ICL_INTERVAL(ICL_COMPARE) Interval>
|
Chris@16
|
191 struct segment_adapter
|
Chris@16
|
192 {
|
Chris@16
|
193 typedef segment_adapter type;
|
Chris@16
|
194 typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) segment_type;
|
Chris@16
|
195 typedef segment_type interval_type;
|
Chris@16
|
196 typedef typename interval_traits<interval_type>::domain_type domain_type;
|
Chris@16
|
197 typedef domain_type codomain_type;
|
Chris@16
|
198 typedef domain_type element_type;
|
Chris@16
|
199 typedef domain_type& transit_type;
|
Chris@16
|
200 typedef typename difference_type_of<interval_traits<interval_type> >::type
|
Chris@16
|
201 domain_difference_type;
|
Chris@16
|
202
|
Chris@16
|
203 static domain_type first (const SegmentIteratorT& leaper){ return leaper->first(); }
|
Chris@16
|
204 static domain_type last (const SegmentIteratorT& leaper){ return leaper->last(); }
|
Chris@16
|
205 static domain_difference_type length(const SegmentIteratorT& leaper){ return icl::length(*leaper);}
|
Chris@16
|
206
|
Chris@16
|
207 static transit_type transient_element(domain_type& inter_pos, const SegmentIteratorT& leaper,
|
Chris@16
|
208 const domain_difference_type& sneaker)
|
Chris@16
|
209 {
|
Chris@16
|
210 inter_pos = is_reverse<SegmentIteratorT>::value ? icl::last(*leaper) - sneaker
|
Chris@16
|
211 : icl::first(*leaper) + sneaker;
|
Chris@16
|
212 return inter_pos;
|
Chris@16
|
213 }
|
Chris@16
|
214 };
|
Chris@16
|
215
|
Chris@16
|
216 template < class SegmentIteratorT, class CodomainT, ICL_INTERVAL(ICL_COMPARE) Interval >
|
Chris@16
|
217 struct segment_adapter<SegmentIteratorT, std::pair<ICL_INTERVAL_TYPE(Interval,DomainT,Compare)const, CodomainT> >
|
Chris@16
|
218 {
|
Chris@16
|
219 typedef segment_adapter type;
|
Chris@16
|
220 typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
|
Chris@16
|
221 typedef typename interval_traits<interval_type>::domain_type domain_type;
|
Chris@16
|
222 typedef CodomainT codomain_type;
|
Chris@16
|
223 typedef std::pair<domain_type, codomain_type> element_type;
|
Chris@16
|
224 typedef mapped_reference<domain_type, CodomainT> transit_type;
|
Chris@16
|
225 typedef typename difference_type_of<interval_traits<interval_type> >::type
|
Chris@16
|
226 domain_difference_type;
|
Chris@16
|
227
|
Chris@16
|
228 static domain_type first (const SegmentIteratorT& leaper){ return leaper->first.first(); }
|
Chris@16
|
229 static domain_type last (const SegmentIteratorT& leaper){ return leaper->first.last(); }
|
Chris@16
|
230 static domain_difference_type length(const SegmentIteratorT& leaper){ return icl::length(leaper->first);}
|
Chris@16
|
231
|
Chris@16
|
232 static transit_type transient_element(domain_type& inter_pos, const SegmentIteratorT& leaper,
|
Chris@16
|
233 const domain_difference_type& sneaker)
|
Chris@16
|
234 {
|
Chris@16
|
235 inter_pos = is_reverse<SegmentIteratorT>::value ? icl::last(leaper->first) - sneaker
|
Chris@16
|
236 : icl::first(leaper->first) + sneaker;
|
Chris@16
|
237 return transit_type(inter_pos, leaper->second);
|
Chris@16
|
238 }
|
Chris@16
|
239 };
|
Chris@16
|
240
|
Chris@16
|
241 #endif // ICL_USE_INTERVAL_TEMPLATE_TEMPLATE
|
Chris@16
|
242
|
Chris@16
|
243 template <class SegmentIteratorT>
|
Chris@16
|
244 class element_iterator
|
Chris@16
|
245 : public boost::iterator_facade<
|
Chris@16
|
246 element_iterator<SegmentIteratorT>
|
Chris@16
|
247 , typename elemental<typename SegmentIteratorT::value_type>::transit_type
|
Chris@16
|
248 , boost::bidirectional_traversal_tag
|
Chris@16
|
249 , typename elemental<typename SegmentIteratorT::value_type>::transit_type
|
Chris@16
|
250 >
|
Chris@16
|
251 {
|
Chris@16
|
252 public:
|
Chris@16
|
253 typedef element_iterator type;
|
Chris@16
|
254 typedef SegmentIteratorT segment_iterator;
|
Chris@16
|
255 typedef typename SegmentIteratorT::value_type segment_type;
|
Chris@16
|
256 typedef typename first_element<segment_type>::type interval_type;
|
Chris@16
|
257 typedef typename elemental<segment_type>::type element_type;
|
Chris@16
|
258 typedef typename elemental<segment_type>::domain_type domain_type;
|
Chris@16
|
259 typedef typename elemental<segment_type>::codomain_type codomain_type;
|
Chris@16
|
260 typedef typename elemental<segment_type>::transit_type transit_type;
|
Chris@16
|
261 typedef transit_type value_type;
|
Chris@16
|
262 typedef typename difference_type_of<interval_traits<interval_type> >::type
|
Chris@16
|
263 domain_difference_type;
|
Chris@16
|
264
|
Chris@16
|
265 private:
|
Chris@16
|
266 typedef typename segment_adapter<segment_iterator,segment_type>::type adapt;
|
Chris@16
|
267
|
Chris@16
|
268 struct enabler{};
|
Chris@16
|
269
|
Chris@16
|
270 public:
|
Chris@16
|
271 element_iterator()
|
Chris@16
|
272 : _saltator(identity_element<segment_iterator>::value())
|
Chris@16
|
273 , _reptator(identity_element<domain_difference_type>::value()){}
|
Chris@16
|
274
|
Chris@16
|
275 explicit element_iterator(segment_iterator jumper)
|
Chris@16
|
276 : _saltator(jumper), _reptator(identity_element<domain_difference_type>::value()) {}
|
Chris@16
|
277
|
Chris@16
|
278 template <class SaltatorT>
|
Chris@16
|
279 element_iterator
|
Chris@16
|
280 ( element_iterator<SaltatorT> const& other
|
Chris@16
|
281 , typename enable_if<boost::is_convertible<SaltatorT*,SegmentIteratorT*>, enabler>::type = enabler())
|
Chris@16
|
282 : _saltator(other._saltator), _reptator(other._reptator) {}
|
Chris@16
|
283
|
Chris@16
|
284 private:
|
Chris@16
|
285 friend class boost::iterator_core_access;
|
Chris@16
|
286 template <class> friend class element_iterator;
|
Chris@16
|
287
|
Chris@16
|
288 template <class SaltatorT>
|
Chris@16
|
289 bool equal(element_iterator<SaltatorT> const& other) const
|
Chris@16
|
290 {
|
Chris@16
|
291 return this->_saltator == other._saltator
|
Chris@16
|
292 && this->_reptator == other._reptator;
|
Chris@16
|
293 }
|
Chris@16
|
294
|
Chris@16
|
295 void increment()
|
Chris@16
|
296 {
|
Chris@16
|
297 if(_reptator < icl::pred(adapt::length(_saltator)))
|
Chris@16
|
298 ++_reptator;
|
Chris@16
|
299 else
|
Chris@16
|
300 {
|
Chris@16
|
301 ++_saltator;
|
Chris@16
|
302 _reptator = identity_element<domain_difference_type>::value();
|
Chris@16
|
303 }
|
Chris@16
|
304 }
|
Chris@16
|
305
|
Chris@16
|
306 void decrement()
|
Chris@16
|
307 {
|
Chris@16
|
308 if(identity_element<domain_difference_type>::value() < _reptator)
|
Chris@16
|
309 --_reptator;
|
Chris@16
|
310 else
|
Chris@16
|
311 {
|
Chris@16
|
312 --_saltator;
|
Chris@16
|
313 _reptator = adapt::length(_saltator);
|
Chris@16
|
314 --_reptator;
|
Chris@16
|
315 }
|
Chris@16
|
316 }
|
Chris@16
|
317
|
Chris@16
|
318 value_type dereference()const
|
Chris@16
|
319 {
|
Chris@16
|
320 return adapt::transient_element(_inter_pos, _saltator, _reptator);
|
Chris@16
|
321 }
|
Chris@16
|
322
|
Chris@16
|
323 private:
|
Chris@16
|
324 segment_iterator _saltator; // satltare: to jump : the fast moving iterator
|
Chris@16
|
325 mutable domain_difference_type _reptator; // reptare: to sneak : the slow moving iterator 0 based
|
Chris@16
|
326 mutable domain_type _inter_pos; // inter position : Position within the current segment
|
Chris@16
|
327 // _saltator->first.first() <= _inter_pos <= _saltator->first.last()
|
Chris@16
|
328 };
|
Chris@16
|
329
|
Chris@16
|
330 }} // namespace icl boost
|
Chris@16
|
331
|
Chris@16
|
332 #endif // BOOST_ICL_DETAIL_ELEMENT_ITERATOR_HPP_JOFA_091104
|
Chris@16
|
333
|
Chris@16
|
334
|
Chris@16
|
335
|