join_iterator.hpp 10 KB

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