join_iterator.hpp 11 KB

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