element_iterator.hpp 14 KB

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