concatenate_iterator.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014-2021, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_GEOMETRY_ITERATORS_CONCATENATE_ITERATOR_HPP
  8. #define BOOST_GEOMETRY_ITERATORS_CONCATENATE_ITERATOR_HPP
  9. #include <type_traits>
  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() = default;
  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. std::enable_if_t
  54. <
  55. std::is_convertible<OtherIt1, Iterator1>::value
  56. && std::is_convertible<OtherIt2, Iterator2>::value,
  57. int
  58. > = 0
  59. >
  60. concatenate_iterator(concatenate_iterator
  61. <
  62. OtherIt1,
  63. OtherIt2,
  64. OtherValue,
  65. OtherReference
  66. > const& other)
  67. : m_it1(other.m_it1)
  68. , m_end1(other.m_end1)
  69. , m_begin2(other.m_begin2)
  70. , m_it2(other.m_it2)
  71. {}
  72. concatenate_iterator(concatenate_iterator const& other) = default;
  73. concatenate_iterator& operator=(concatenate_iterator const& other) = default;
  74. private:
  75. friend class boost::iterator_core_access;
  76. template <typename It1, typename It2, typename V, typename R>
  77. friend class concatenate_iterator;
  78. inline Reference dereference() const
  79. {
  80. if ( m_it1 == m_end1 )
  81. {
  82. return *m_it2;
  83. }
  84. return *m_it1;
  85. }
  86. template
  87. <
  88. typename OtherIt1,
  89. typename OtherIt2,
  90. typename OtherValue,
  91. typename OtherReference
  92. >
  93. inline bool equal(concatenate_iterator
  94. <
  95. OtherIt1,
  96. OtherIt2,
  97. OtherValue,
  98. OtherReference
  99. > const& other) const
  100. {
  101. return m_it1 == other.m_it1 && m_it2 == other.m_it2;
  102. }
  103. inline void increment()
  104. {
  105. if ( m_it1 == m_end1 )
  106. {
  107. ++m_it2;
  108. }
  109. else
  110. {
  111. ++m_it1;
  112. }
  113. }
  114. inline void decrement()
  115. {
  116. if ( m_it2 == m_begin2 )
  117. {
  118. --m_it1;
  119. }
  120. else
  121. {
  122. --m_it2;
  123. }
  124. }
  125. };
  126. }} // namespace boost::geometry
  127. #endif // BOOST_GEOMETRY_ITERATORS_CONCATENATE_ITERATOR_HPP