ever_circling_iterator.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_EVER_CIRCLING_ITERATOR_HPP
  11. #define BOOST_GEOMETRY_ITERATORS_EVER_CIRCLING_ITERATOR_HPP
  12. #include <boost/range.hpp>
  13. #include <boost/iterator/iterator_adaptor.hpp>
  14. #include <boost/iterator/iterator_categories.hpp>
  15. #include <boost/geometry/iterators/base.hpp>
  16. namespace boost { namespace geometry
  17. {
  18. /*!
  19. \brief Iterator which ever circles through a range
  20. \tparam Iterator iterator on which this class is based on
  21. \ingroup iterators
  22. \details If the iterator arrives at range.end() it restarts from the
  23. beginning. So it has to be stopped in another way.
  24. Don't call for(....; it++) because it will turn in an endless loop
  25. \note Name inspired on David Bowie's
  26. "Chant Of The Ever Circling Skeletal Family"
  27. */
  28. template <typename Iterator>
  29. struct ever_circling_iterator :
  30. public detail::iterators::iterator_base
  31. <
  32. ever_circling_iterator<Iterator>,
  33. Iterator
  34. >
  35. {
  36. friend class boost::iterator_core_access;
  37. explicit inline ever_circling_iterator(Iterator begin, Iterator end,
  38. bool skip_first = false)
  39. : m_begin(begin)
  40. , m_end(end)
  41. , m_skip_first(skip_first)
  42. {
  43. this->base_reference() = begin;
  44. }
  45. explicit inline ever_circling_iterator(Iterator begin, Iterator end, Iterator start,
  46. bool skip_first = false)
  47. : m_begin(begin)
  48. , m_end(end)
  49. , m_skip_first(skip_first)
  50. {
  51. this->base_reference() = start;
  52. }
  53. /// Navigate to a certain position, should be in [start .. end], if at end
  54. /// it will circle again.
  55. inline void moveto(Iterator it)
  56. {
  57. this->base_reference() = it;
  58. check_end();
  59. }
  60. private:
  61. inline void increment(bool possibly_skip = true)
  62. {
  63. (this->base_reference())++;
  64. check_end(possibly_skip);
  65. }
  66. inline void check_end(bool possibly_skip = true)
  67. {
  68. if (this->base() == this->m_end)
  69. {
  70. this->base_reference() = this->m_begin;
  71. if (m_skip_first && possibly_skip)
  72. {
  73. increment(false);
  74. }
  75. }
  76. }
  77. Iterator m_begin;
  78. Iterator m_end;
  79. bool m_skip_first;
  80. };
  81. template <typename Range>
  82. struct ever_circling_range_iterator
  83. : public boost::iterator_facade
  84. <
  85. ever_circling_range_iterator<Range>,
  86. typename boost::range_value<Range>::type const,
  87. boost::random_access_traversal_tag,
  88. typename boost::range_reference<Range const>::type,
  89. typename boost::range_difference<Range>::type
  90. >
  91. {
  92. private:
  93. typedef boost::iterator_facade
  94. <
  95. ever_circling_range_iterator<Range>,
  96. typename boost::range_value<Range>::type const,
  97. boost::random_access_traversal_tag,
  98. typename boost::range_reference<Range const>::type,
  99. typename boost::range_difference<Range>::type
  100. > base_type;
  101. public:
  102. /// Constructor including the range it is based on
  103. explicit inline ever_circling_range_iterator(Range& range)
  104. : m_range(&range)
  105. , m_iterator(boost::begin(range))
  106. , m_size(boost::size(range))
  107. , m_index(0)
  108. {}
  109. /// Default constructor
  110. explicit inline ever_circling_range_iterator()
  111. : m_range(NULL)
  112. , m_size(0)
  113. , m_index(0)
  114. {}
  115. typedef typename base_type::reference reference;
  116. typedef typename base_type::difference_type difference_type;
  117. private:
  118. friend class boost::iterator_core_access;
  119. inline reference dereference() const
  120. {
  121. return *m_iterator;
  122. }
  123. inline difference_type distance_to(ever_circling_range_iterator<Range> const& other) const
  124. {
  125. return other.m_index - this->m_index;
  126. }
  127. inline bool equal(ever_circling_range_iterator<Range> const& other) const
  128. {
  129. return this->m_range == other.m_range
  130. && this->m_index == other.m_index;
  131. }
  132. inline void increment()
  133. {
  134. ++m_index;
  135. if (m_index >= 0 && m_index < m_size)
  136. {
  137. ++m_iterator;
  138. }
  139. else
  140. {
  141. update_iterator();
  142. }
  143. }
  144. inline void decrement()
  145. {
  146. --m_index;
  147. if (m_index >= 0 && m_index < m_size)
  148. {
  149. --m_iterator;
  150. }
  151. else
  152. {
  153. update_iterator();
  154. }
  155. }
  156. inline void advance(difference_type n)
  157. {
  158. if (m_index >= 0 && m_index < m_size
  159. && m_index + n >= 0 && m_index + n < m_size)
  160. {
  161. m_index += n;
  162. m_iterator += n;
  163. }
  164. else
  165. {
  166. m_index += n;
  167. update_iterator();
  168. }
  169. }
  170. inline void update_iterator()
  171. {
  172. while (m_index < 0)
  173. {
  174. m_index += m_size;
  175. }
  176. m_index = m_index % m_size;
  177. this->m_iterator = boost::begin(*m_range) + m_index;
  178. }
  179. Range* m_range;
  180. typename boost::range_iterator<Range>::type m_iterator;
  181. difference_type m_size;
  182. difference_type m_index;
  183. };
  184. }} // namespace boost::geometry
  185. #endif // BOOST_GEOMETRY_ITERATORS_EVER_CIRCLING_ITERATOR_HPP