sub_range.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. namespace boost { namespace geometry {
  20. #ifndef DOXYGEN_NO_DETAIL
  21. #ifndef DOXYGEN_NO_DISPATCH
  22. namespace detail_dispatch {
  23. template <typename Geometry,
  24. typename Tag = typename geometry::tag<Geometry>::type,
  25. bool IsMulti = std::is_base_of<multi_tag, Tag>::value>
  26. struct sub_range : not_implemented<Tag>
  27. {};
  28. template <typename Geometry, typename Tag>
  29. struct sub_range<Geometry, Tag, false>
  30. {
  31. typedef Geometry & return_type;
  32. template <typename Id> static inline
  33. return_type apply(Geometry & geometry, Id const&)
  34. {
  35. return geometry;
  36. }
  37. };
  38. template <typename Geometry>
  39. struct sub_range<Geometry, polygon_tag, false>
  40. {
  41. typedef typename geometry::ring_return_type<Geometry>::type return_type;
  42. template <typename Id> static inline
  43. return_type apply(Geometry & geometry, Id const& id)
  44. {
  45. if ( id.ring_index < 0 )
  46. {
  47. return geometry::exterior_ring(geometry);
  48. }
  49. else
  50. {
  51. typedef typename boost::range_size
  52. <
  53. typename geometry::interior_type<Geometry>::type
  54. >::type size_type;
  55. size_type const ri = static_cast<size_type>(id.ring_index);
  56. return range::at(geometry::interior_rings(geometry), ri);
  57. }
  58. }
  59. };
  60. template <typename Geometry, typename Tag>
  61. struct sub_range<Geometry, Tag, true>
  62. {
  63. typedef typename boost::range_value<Geometry>::type value_type;
  64. typedef std::conditional_t
  65. <
  66. std::is_const<Geometry>::value,
  67. typename std::add_const<value_type>::type,
  68. value_type
  69. > sub_type;
  70. typedef detail_dispatch::sub_range<sub_type> sub_sub_range;
  71. // TODO: shouldn't it be return_type?
  72. typedef typename sub_sub_range::return_type return_type;
  73. template <typename Id> static inline
  74. return_type apply(Geometry & geometry, Id const& id)
  75. {
  76. BOOST_GEOMETRY_ASSERT(0 <= id.multi_index);
  77. typedef typename boost::range_size<Geometry>::type size_type;
  78. size_type const mi = static_cast<size_type>(id.multi_index);
  79. return sub_sub_range::apply(range::at(geometry, mi), id);
  80. }
  81. };
  82. } // namespace detail_dispatch
  83. #endif // DOXYGEN_NO_DISPATCH
  84. namespace detail {
  85. template <typename Geometry>
  86. struct sub_range_return_type
  87. {
  88. typedef typename detail_dispatch::sub_range<Geometry>::return_type type;
  89. };
  90. // This function also works for geometry::segment_identifier
  91. template <typename Geometry, typename Id> inline
  92. typename sub_range_return_type<Geometry>::type
  93. sub_range(Geometry & geometry, Id const& id)
  94. {
  95. return detail_dispatch::sub_range<Geometry>::apply(geometry, id);
  96. }
  97. template <typename Geometry, typename Id> inline
  98. typename sub_range_return_type<Geometry const>::type
  99. sub_range(Geometry const& geometry, Id const& id)
  100. {
  101. return detail_dispatch::sub_range<Geometry const>::apply(geometry, id);
  102. }
  103. } // namespace detail
  104. #endif // DOXYGEN_NO_DETAIL
  105. }} // namespace boost::geometry
  106. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_SUB_RANGE_HPP