sub_range.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2013-2020.
  4. // Modifications copyright (c) 2013-2020, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_SUB_RANGE_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_SUB_RANGE_HPP
  11. #include <type_traits>
  12. #include <boost/geometry/algorithms/not_implemented.hpp>
  13. #include <boost/geometry/core/assert.hpp>
  14. #include <boost/geometry/core/exterior_ring.hpp>
  15. #include <boost/geometry/core/interior_rings.hpp>
  16. #include <boost/geometry/core/tag.hpp>
  17. #include <boost/geometry/core/tags.hpp>
  18. #include <boost/geometry/util/range.hpp>
  19. #include <boost/geometry/util/type_traits.hpp>
  20. namespace boost { namespace geometry {
  21. #ifndef DOXYGEN_NO_DETAIL
  22. #ifndef DOXYGEN_NO_DISPATCH
  23. namespace detail_dispatch {
  24. template
  25. <
  26. typename Geometry,
  27. typename Tag = typename geometry::tag<Geometry>::type,
  28. bool IsMulti = util::is_multi<Geometry>::value
  29. >
  30. struct sub_range : not_implemented<Tag>
  31. {};
  32. template <typename Geometry, typename Tag>
  33. struct sub_range<Geometry, Tag, false>
  34. {
  35. typedef Geometry & return_type;
  36. template <typename Id> static inline
  37. return_type apply(Geometry & geometry, Id const&)
  38. {
  39. return geometry;
  40. }
  41. };
  42. template <typename Geometry>
  43. struct sub_range<Geometry, polygon_tag, false>
  44. {
  45. typedef typename geometry::ring_return_type<Geometry>::type return_type;
  46. template <typename Id> static inline
  47. return_type apply(Geometry & geometry, Id const& id)
  48. {
  49. if ( id.ring_index < 0 )
  50. {
  51. return geometry::exterior_ring(geometry);
  52. }
  53. else
  54. {
  55. typedef typename boost::range_size
  56. <
  57. typename geometry::interior_type<Geometry>::type
  58. >::type size_type;
  59. size_type const ri = static_cast<size_type>(id.ring_index);
  60. return range::at(geometry::interior_rings(geometry), ri);
  61. }
  62. }
  63. };
  64. template <typename Geometry, typename Tag>
  65. struct sub_range<Geometry, Tag, true>
  66. {
  67. typedef typename boost::range_value<Geometry>::type value_type;
  68. typedef std::conditional_t
  69. <
  70. std::is_const<Geometry>::value,
  71. typename std::add_const<value_type>::type,
  72. value_type
  73. > sub_type;
  74. typedef detail_dispatch::sub_range<sub_type> sub_sub_range;
  75. // TODO: shouldn't it be return_type?
  76. typedef typename sub_sub_range::return_type return_type;
  77. template <typename Id> static inline
  78. return_type apply(Geometry & geometry, Id const& id)
  79. {
  80. BOOST_GEOMETRY_ASSERT(0 <= id.multi_index);
  81. typedef typename boost::range_size<Geometry>::type size_type;
  82. size_type const mi = static_cast<size_type>(id.multi_index);
  83. return sub_sub_range::apply(range::at(geometry, mi), id);
  84. }
  85. };
  86. } // namespace detail_dispatch
  87. #endif // DOXYGEN_NO_DISPATCH
  88. namespace detail {
  89. template <typename Geometry>
  90. struct sub_range_return_type
  91. {
  92. typedef typename detail_dispatch::sub_range<Geometry>::return_type type;
  93. };
  94. // This function also works for geometry::segment_identifier
  95. template <typename Geometry, typename Id> inline
  96. typename sub_range_return_type<Geometry>::type
  97. sub_range(Geometry & geometry, Id const& id)
  98. {
  99. return detail_dispatch::sub_range<Geometry>::apply(geometry, id);
  100. }
  101. template <typename Geometry, typename Id> inline
  102. typename sub_range_return_type<Geometry const>::type
  103. sub_range(Geometry const& geometry, Id const& id)
  104. {
  105. return detail_dispatch::sub_range<Geometry const>::apply(geometry, id);
  106. }
  107. } // namespace detail
  108. #endif // DOXYGEN_NO_DETAIL
  109. }} // namespace boost::geometry
  110. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_SUB_RANGE_HPP