closing_iterator.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2020-2021, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  8. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
  13. #define BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
  14. #include <boost/iterator/iterator_facade.hpp>
  15. #include <boost/iterator/iterator_categories.hpp>
  16. #include <boost/range/begin.hpp>
  17. #include <boost/range/end.hpp>
  18. #include <boost/range/difference_type.hpp>
  19. #include <boost/range/reference.hpp>
  20. #include <boost/range/value_type.hpp>
  21. #include <boost/geometry/core/assert.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. /*!
  25. \brief Iterator which iterates through a range, but adds first element at end of the range
  26. \tparam Range range on which this class is based on
  27. \ingroup iterators
  28. \note It's const iterator treating the Range as one containing non-mutable elements.
  29. For both "closing_iterator<Range> and "closing_iterator<Range const>
  30. const reference is always returned when dereferenced.
  31. \note This class is normally used from "closeable_view" if Close==true
  32. */
  33. template <typename Range>
  34. struct closing_iterator
  35. : public boost::iterator_facade
  36. <
  37. closing_iterator<Range>,
  38. typename boost::range_value<Range>::type const,
  39. boost::random_access_traversal_tag,
  40. typename boost::range_reference<Range const>::type,
  41. typename boost::range_difference<Range>::type
  42. >
  43. {
  44. private:
  45. typedef boost::iterator_facade
  46. <
  47. closing_iterator<Range>,
  48. typename boost::range_value<Range>::type const,
  49. boost::random_access_traversal_tag,
  50. typename boost::range_reference<Range const>::type,
  51. typename boost::range_difference<Range>::type
  52. > base_type;
  53. public:
  54. typedef typename base_type::reference reference;
  55. typedef typename base_type::difference_type difference_type;
  56. /// Constructor including the range it is based on
  57. explicit inline closing_iterator(Range const& range)
  58. : m_iterator(boost::begin(range))
  59. , m_begin(boost::begin(range))
  60. , m_end(boost::end(range))
  61. , m_size(m_end - m_begin)
  62. , m_index(0)
  63. {}
  64. /// Constructor to indicate the end of a range
  65. explicit inline closing_iterator(Range const& range, bool)
  66. : m_iterator(boost::end(range))
  67. , m_begin(boost::begin(range))
  68. , m_end(boost::end(range))
  69. , m_size(m_end - m_begin)
  70. , m_index((m_size == 0) ? 0 : m_size + 1)
  71. {}
  72. /// Default constructor
  73. inline closing_iterator()
  74. : m_size(0)
  75. , m_index(0)
  76. {}
  77. template
  78. <
  79. typename OtherRange,
  80. std::enable_if_t
  81. <
  82. std::is_convertible
  83. <
  84. typename boost::range_iterator<OtherRange const>::type,
  85. typename boost::range_iterator<Range const>::type
  86. >::value,
  87. int
  88. > = 0
  89. >
  90. inline closing_iterator(closing_iterator<OtherRange> const& other)
  91. : m_iterator(other.m_iterator)
  92. , m_begin(other.m_begin)
  93. , m_end(other.m_end)
  94. , m_size(other.m_size)
  95. , m_index(other.m_index)
  96. {}
  97. private:
  98. template <typename OtherRange> friend struct closing_iterator;
  99. friend class boost::iterator_core_access;
  100. inline reference dereference() const
  101. {
  102. return *m_iterator;
  103. }
  104. inline difference_type distance_to(closing_iterator<Range> const& other) const
  105. {
  106. return other.m_index - this->m_index;
  107. }
  108. inline bool equal(closing_iterator<Range> const& other) const
  109. {
  110. BOOST_GEOMETRY_ASSERT(m_begin == other.m_begin && m_end == other.m_end);
  111. return this->m_index == other.m_index;
  112. }
  113. inline void increment()
  114. {
  115. if (++m_index < m_size)
  116. {
  117. ++m_iterator;
  118. }
  119. else
  120. {
  121. update_iterator();
  122. }
  123. }
  124. inline void decrement()
  125. {
  126. if (m_index-- < m_size)
  127. {
  128. --m_iterator;
  129. }
  130. else
  131. {
  132. update_iterator();
  133. }
  134. }
  135. inline void advance(difference_type n)
  136. {
  137. if (m_index < m_size && m_index + n < m_size)
  138. {
  139. m_index += n;
  140. m_iterator += n;
  141. }
  142. else
  143. {
  144. m_index += n;
  145. update_iterator();
  146. }
  147. }
  148. inline void update_iterator()
  149. {
  150. this->m_iterator = m_index <= m_size
  151. ? m_begin + (m_index % m_size)
  152. : m_end
  153. ;
  154. }
  155. typename boost::range_iterator<Range const>::type m_iterator;
  156. typename boost::range_iterator<Range const>::type m_begin;
  157. typename boost::range_iterator<Range const>::type m_end;
  158. difference_type m_size;
  159. difference_type m_index;
  160. };
  161. }} // namespace boost::geometry
  162. #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP