closing_iterator.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
  11. #define BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
  12. #include <boost/range.hpp>
  13. #include <boost/iterator/iterator_facade.hpp>
  14. #include <boost/iterator/iterator_categories.hpp>
  15. namespace boost { namespace geometry
  16. {
  17. /*!
  18. \brief Iterator which iterates through a range, but adds first element at end of the range
  19. \tparam Range range on which this class is based on
  20. \ingroup iterators
  21. \note It's const iterator treating the Range as one containing non-mutable elements.
  22. For both "closing_iterator<Range> and "closing_iterator<Range const>
  23. const reference is always returned when dereferenced.
  24. \note This class is normally used from "closeable_view" if Close==true
  25. */
  26. template <typename Range>
  27. struct closing_iterator
  28. : public boost::iterator_facade
  29. <
  30. closing_iterator<Range>,
  31. typename boost::range_value<Range>::type const,
  32. boost::random_access_traversal_tag,
  33. typename boost::range_reference<Range const>::type,
  34. typename boost::range_difference<Range>::type
  35. >
  36. {
  37. private:
  38. typedef boost::iterator_facade
  39. <
  40. closing_iterator<Range>,
  41. typename boost::range_value<Range>::type const,
  42. boost::random_access_traversal_tag,
  43. typename boost::range_reference<Range const>::type,
  44. typename boost::range_difference<Range>::type
  45. > base_type;
  46. public:
  47. typedef typename base_type::reference reference;
  48. typedef typename base_type::difference_type difference_type;
  49. /// Constructor including the range it is based on
  50. explicit inline closing_iterator(Range& range)
  51. : m_range(&range)
  52. , m_iterator(boost::begin(range))
  53. , m_end(boost::end(range))
  54. , m_size(static_cast<difference_type>(boost::size(range)))
  55. , m_index(0)
  56. {}
  57. /// Constructor to indicate the end of a range
  58. explicit inline closing_iterator(Range& range, bool)
  59. : m_range(&range)
  60. , m_iterator(boost::end(range))
  61. , m_end(boost::end(range))
  62. , m_size(static_cast<difference_type>(boost::size(range)))
  63. , m_index((m_size == 0) ? 0 : m_size + 1)
  64. {}
  65. /// Default constructor
  66. explicit inline closing_iterator()
  67. : m_range(NULL)
  68. , m_size(0)
  69. , m_index(0)
  70. {}
  71. private:
  72. friend class boost::iterator_core_access;
  73. inline reference dereference() const
  74. {
  75. return *m_iterator;
  76. }
  77. inline difference_type distance_to(closing_iterator<Range> const& other) const
  78. {
  79. return other.m_index - this->m_index;
  80. }
  81. inline bool equal(closing_iterator<Range> const& other) const
  82. {
  83. return this->m_range == other.m_range
  84. && this->m_index == other.m_index;
  85. }
  86. inline void increment()
  87. {
  88. if (++m_index < m_size)
  89. {
  90. ++m_iterator;
  91. }
  92. else
  93. {
  94. update_iterator();
  95. }
  96. }
  97. inline void decrement()
  98. {
  99. if (m_index-- < m_size)
  100. {
  101. --m_iterator;
  102. }
  103. else
  104. {
  105. update_iterator();
  106. }
  107. }
  108. inline void advance(difference_type n)
  109. {
  110. if (m_index < m_size && m_index + n < m_size)
  111. {
  112. m_index += n;
  113. m_iterator += n;
  114. }
  115. else
  116. {
  117. m_index += n;
  118. update_iterator();
  119. }
  120. }
  121. inline void update_iterator()
  122. {
  123. this->m_iterator = m_index <= m_size
  124. ? boost::begin(*m_range) + (m_index % m_size)
  125. : boost::end(*m_range)
  126. ;
  127. }
  128. Range* m_range;
  129. typename boost::range_iterator<Range>::type m_iterator;
  130. typename boost::range_iterator<Range>::type m_end;
  131. difference_type m_size;
  132. difference_type m_index;
  133. };
  134. }} // namespace boost::geometry
  135. #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP