ever_circling_iterator.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // This file was modified by Oracle on 2020-2021.
  6. // Modifications copyright (c) 2020-2021 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_ITERATORS_EVER_CIRCLING_ITERATOR_HPP
  14. #define BOOST_GEOMETRY_ITERATORS_EVER_CIRCLING_ITERATOR_HPP
  15. #include <type_traits>
  16. #include <boost/iterator/iterator_adaptor.hpp>
  17. #include <boost/iterator/iterator_categories.hpp>
  18. #include <boost/range/begin.hpp>
  19. #include <boost/range/difference_type.hpp>
  20. #include <boost/range/reference.hpp>
  21. #include <boost/range/size.hpp>
  22. #include <boost/range/value_type.hpp>
  23. #include <boost/geometry/core/assert.hpp>
  24. #include <boost/geometry/iterators/detail/iterator_base.hpp>
  25. namespace boost { namespace geometry
  26. {
  27. /*!
  28. \brief Iterator which ever circles through a range
  29. \tparam Iterator iterator on which this class is based on
  30. \ingroup iterators
  31. \details If the iterator arrives at range.end() it restarts from the
  32. beginning. So it has to be stopped in another way.
  33. Don't call for(....; it++) because it will turn in an endless loop
  34. \note Name inspired on David Bowie's
  35. "Chant Of The Ever Circling Skeletal Family"
  36. */
  37. template <typename Iterator>
  38. struct ever_circling_iterator :
  39. public detail::iterators::iterator_base
  40. <
  41. ever_circling_iterator<Iterator>,
  42. Iterator
  43. >
  44. {
  45. ever_circling_iterator() = default;
  46. explicit inline ever_circling_iterator(Iterator begin, Iterator end,
  47. bool skip_first = false)
  48. : m_begin(begin)
  49. , m_end(end)
  50. , m_skip_first(skip_first)
  51. {
  52. this->base_reference() = begin;
  53. }
  54. explicit inline ever_circling_iterator(Iterator begin, Iterator end, Iterator start,
  55. bool skip_first = false)
  56. : m_begin(begin)
  57. , m_end(end)
  58. , m_skip_first(skip_first)
  59. {
  60. this->base_reference() = start;
  61. }
  62. template
  63. <
  64. typename OtherIterator,
  65. std::enable_if_t<std::is_convertible<OtherIterator, Iterator>::value, int> = 0
  66. >
  67. inline ever_circling_iterator(ever_circling_iterator<OtherIterator> const& other)
  68. : m_begin(other.m_begin)
  69. , m_end(other.m_end)
  70. , m_skip_first(other.m_skip_first)
  71. {}
  72. /// Navigate to a certain position, should be in [start .. end], if at end
  73. /// it will circle again.
  74. inline void moveto(Iterator it)
  75. {
  76. this->base_reference() = it;
  77. check_end();
  78. }
  79. private:
  80. template <typename OtherIterator> friend struct ever_circling_iterator;
  81. friend class boost::iterator_core_access;
  82. inline void increment(bool possibly_skip = true)
  83. {
  84. (this->base_reference())++;
  85. check_end(possibly_skip);
  86. }
  87. inline void check_end(bool possibly_skip = true)
  88. {
  89. if (this->base() == this->m_end)
  90. {
  91. this->base_reference() = this->m_begin;
  92. if (m_skip_first && possibly_skip)
  93. {
  94. increment(false);
  95. }
  96. }
  97. }
  98. Iterator m_begin;
  99. Iterator m_end;
  100. bool m_skip_first;
  101. };
  102. template <typename Range>
  103. struct ever_circling_range_iterator
  104. : public boost::iterator_facade
  105. <
  106. ever_circling_range_iterator<Range>,
  107. typename boost::range_value<Range>::type const,
  108. boost::random_access_traversal_tag,
  109. typename boost::range_reference<Range const>::type,
  110. typename boost::range_difference<Range>::type
  111. >
  112. {
  113. private:
  114. typedef boost::iterator_facade
  115. <
  116. ever_circling_range_iterator<Range>,
  117. typename boost::range_value<Range>::type const,
  118. boost::random_access_traversal_tag,
  119. typename boost::range_reference<Range const>::type,
  120. typename boost::range_difference<Range>::type
  121. > base_type;
  122. public:
  123. /// Constructor including the range it is based on
  124. explicit inline ever_circling_range_iterator(Range const& range)
  125. : m_begin(boost::begin(range))
  126. , m_iterator(boost::begin(range))
  127. , m_size(boost::size(range))
  128. , m_index(0)
  129. {}
  130. /// Default constructor
  131. explicit inline ever_circling_range_iterator()
  132. : m_size(0)
  133. , m_index(0)
  134. {}
  135. template
  136. <
  137. typename OtherRange,
  138. std::enable_if_t
  139. <
  140. std::is_convertible
  141. <
  142. typename boost::range_iterator<OtherRange const>::type,
  143. typename boost::range_iterator<Range const>::type
  144. >::value,
  145. int
  146. > = 0
  147. >
  148. inline ever_circling_range_iterator(ever_circling_range_iterator<OtherRange> const& other)
  149. : m_begin(other.m_begin)
  150. , m_iterator(other.m_iterator)
  151. , m_size(other.m_size)
  152. , m_index(other.m_index)
  153. {}
  154. typedef typename base_type::reference reference;
  155. typedef typename base_type::difference_type difference_type;
  156. private:
  157. template <typename OtherRange> friend struct ever_circling_range_iterator;
  158. friend class boost::iterator_core_access;
  159. inline reference dereference() const
  160. {
  161. return *m_iterator;
  162. }
  163. inline difference_type distance_to(ever_circling_range_iterator<Range> const& other) const
  164. {
  165. return other.m_index - this->m_index;
  166. }
  167. inline bool equal(ever_circling_range_iterator<Range> const& other) const
  168. {
  169. BOOST_GEOMETRY_ASSERT(m_begin == other.m_begin);
  170. return this->m_index == other.m_index;
  171. }
  172. inline void increment()
  173. {
  174. ++m_index;
  175. if (m_index >= 0 && m_index < m_size)
  176. {
  177. ++m_iterator;
  178. }
  179. else
  180. {
  181. update_iterator();
  182. }
  183. }
  184. inline void decrement()
  185. {
  186. --m_index;
  187. if (m_index >= 0 && m_index < m_size)
  188. {
  189. --m_iterator;
  190. }
  191. else
  192. {
  193. update_iterator();
  194. }
  195. }
  196. inline void advance(difference_type n)
  197. {
  198. if (m_index >= 0 && m_index < m_size
  199. && m_index + n >= 0 && m_index + n < m_size)
  200. {
  201. m_index += n;
  202. m_iterator += n;
  203. }
  204. else
  205. {
  206. m_index += n;
  207. update_iterator();
  208. }
  209. }
  210. inline void update_iterator()
  211. {
  212. while (m_index < 0)
  213. {
  214. m_index += m_size;
  215. }
  216. m_index = m_index % m_size;
  217. this->m_iterator = m_begin + m_index;
  218. }
  219. typename boost::range_iterator<Range const>::type m_begin;
  220. typename boost::range_iterator<Range const>::type m_iterator;
  221. difference_type m_size;
  222. difference_type m_index;
  223. };
  224. }} // namespace boost::geometry
  225. #endif // BOOST_GEOMETRY_ITERATORS_EVER_CIRCLING_ITERATOR_HPP