points_view.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.
  6. // Modifications copyright (c) 2020 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_VIEWS_DETAIL_POINTS_VIEW_HPP
  14. #define BOOST_GEOMETRY_VIEWS_DETAIL_POINTS_VIEW_HPP
  15. #include <boost/iterator/iterator_facade.hpp>
  16. #include <boost/iterator/iterator_categories.hpp>
  17. #include <boost/geometry/core/exception.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. namespace detail
  21. {
  22. // Adapts pointer, on points, to a Boost.Range
  23. template <typename Point, int MaxSize>
  24. class points_view
  25. {
  26. // Iterates over a series of points (indicated by pointer
  27. // to have it lightweight). Probably there is already an
  28. // equivalent of this within Boost. If so, TODO: use that one.
  29. // This used to be "box_iterator" and "segment_iterator".
  30. // ALTERNATIVE: use boost:array and its iterators
  31. struct points_iterator
  32. : public boost::iterator_facade
  33. <
  34. points_iterator,
  35. Point const,
  36. boost::random_access_traversal_tag
  37. >
  38. {
  39. // Constructor: Begin iterator
  40. inline points_iterator(Point const* p)
  41. : m_points(p)
  42. , m_index(0)
  43. {}
  44. // Constructor: End iterator
  45. inline points_iterator(Point const* p, bool)
  46. : m_points(p)
  47. , m_index(MaxSize)
  48. {}
  49. // Constructor: default (for Range Concept checking).
  50. inline points_iterator()
  51. : m_points(NULL)
  52. , m_index(MaxSize)
  53. {}
  54. typedef std::ptrdiff_t difference_type;
  55. private:
  56. friend class boost::iterator_core_access;
  57. inline Point const& dereference() const
  58. {
  59. if (m_index >= 0 && m_index < MaxSize)
  60. {
  61. return m_points[m_index];
  62. }
  63. // If it index larger (or smaller) return first point
  64. // (assuming initialized)
  65. return m_points[0];
  66. }
  67. inline bool equal(points_iterator const& other) const
  68. {
  69. return other.m_index == this->m_index;
  70. }
  71. inline void increment()
  72. {
  73. m_index++;
  74. }
  75. inline void decrement()
  76. {
  77. m_index--;
  78. }
  79. inline difference_type distance_to(points_iterator const& other) const
  80. {
  81. return other.m_index - this->m_index;
  82. }
  83. inline void advance(difference_type n)
  84. {
  85. m_index += n;
  86. }
  87. Point const* m_points;
  88. difference_type m_index;
  89. };
  90. public :
  91. typedef points_iterator const_iterator;
  92. typedef points_iterator iterator; // must be defined
  93. const_iterator begin() const { return const_iterator(m_points); }
  94. const_iterator end() const { return const_iterator(m_points, true); }
  95. // It may NOT be used non-const, so commented:
  96. //iterator begin() { return m_begin; }
  97. //iterator end() { return m_end; }
  98. protected :
  99. template <typename CopyPolicy>
  100. explicit points_view(CopyPolicy const& copy)
  101. {
  102. copy.apply(m_points);
  103. }
  104. private :
  105. // Copy points here - box might define them otherwise
  106. Point m_points[MaxSize];
  107. };
  108. }
  109. }} // namespace boost::geometry
  110. #endif // BOOST_GEOMETRY_VIEWS_DETAIL_POINTS_VIEW_HPP