concatenate_iterator.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_ITERATORS_CONCATENATE_ITERATOR_HPP
  7. #define BOOST_GEOMETRY_ITERATORS_CONCATENATE_ITERATOR_HPP
  8. #include <boost/mpl/assert.hpp>
  9. #include <boost/type_traits/is_convertible.hpp>
  10. #include <boost/iterator/iterator_facade.hpp>
  11. #include <boost/iterator/iterator_categories.hpp>
  12. namespace boost { namespace geometry
  13. {
  14. template
  15. <
  16. typename Iterator1,
  17. typename Iterator2,
  18. typename Value,
  19. typename Reference = Value&
  20. >
  21. class concatenate_iterator
  22. : public boost::iterator_facade
  23. <
  24. concatenate_iterator<Iterator1, Iterator2, Value, Reference>,
  25. Value,
  26. boost::bidirectional_traversal_tag,
  27. Reference
  28. >
  29. {
  30. private:
  31. Iterator1 m_it1, m_end1;
  32. Iterator2 m_begin2, m_it2;
  33. public:
  34. typedef Iterator1 first_iterator_type;
  35. typedef Iterator2 second_iterator_type;
  36. // default constructor
  37. concatenate_iterator() {}
  38. // for begin
  39. concatenate_iterator(Iterator1 it1, Iterator1 end1,
  40. Iterator2 begin2, Iterator2 it2)
  41. : m_it1(it1), m_end1(end1), m_begin2(begin2), m_it2(it2)
  42. {}
  43. // for end
  44. concatenate_iterator(Iterator1 end1, Iterator2 begin2, Iterator2 end2)
  45. : m_it1(end1), m_end1(end1), m_begin2(begin2), m_it2(end2)
  46. {}
  47. template
  48. <
  49. typename OtherIt1,
  50. typename OtherIt2,
  51. typename OtherValue,
  52. typename OtherReference
  53. >
  54. concatenate_iterator(concatenate_iterator
  55. <
  56. OtherIt1,
  57. OtherIt2,
  58. OtherValue,
  59. OtherReference
  60. > const& other)
  61. : m_it1(other.m_it1)
  62. , m_end1(other.m_end1)
  63. , m_begin2(other.m_begin2)
  64. , m_it2(other.m_it2)
  65. {
  66. static const bool are_conv
  67. = boost::is_convertible<OtherIt1, Iterator1>::value
  68. && boost::is_convertible<OtherIt2, Iterator2>::value;
  69. BOOST_MPL_ASSERT_MSG((are_conv),
  70. NOT_CONVERTIBLE,
  71. (types<OtherIt1, OtherIt2>));
  72. }
  73. private:
  74. friend class boost::iterator_core_access;
  75. template <typename It1, typename It2, typename V, typename R>
  76. friend class concatenate_iterator;
  77. inline Reference dereference() const
  78. {
  79. if ( m_it1 == m_end1 )
  80. {
  81. return *m_it2;
  82. }
  83. return *m_it1;
  84. }
  85. template
  86. <
  87. typename OtherIt1,
  88. typename OtherIt2,
  89. typename OtherValue,
  90. typename OtherReference
  91. >
  92. inline bool equal(concatenate_iterator
  93. <
  94. OtherIt1,
  95. OtherIt2,
  96. OtherValue,
  97. OtherReference
  98. > const& other) const
  99. {
  100. return m_it1 == other.m_it1 && m_it2 == other.m_it2;
  101. }
  102. inline void increment()
  103. {
  104. if ( m_it1 == m_end1 )
  105. {
  106. ++m_it2;
  107. }
  108. else
  109. {
  110. ++m_it1;
  111. }
  112. }
  113. inline void decrement()
  114. {
  115. if ( m_it2 == m_begin2 )
  116. {
  117. --m_it1;
  118. }
  119. else
  120. {
  121. --m_it2;
  122. }
  123. }
  124. };
  125. }} // namespace boost::geometry
  126. #endif // BOOST_GEOMETRY_ITERATORS_CONCATENATE_ITERATOR_HPP