random_access_view.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Boost.Geometry
  2. // Copyright (c) 2022-2023, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_GEOMETRY_VIEWS_DETAIL_RANDOM_ACCESS_VIEW_HPP
  8. #define BOOST_GEOMETRY_VIEWS_DETAIL_RANDOM_ACCESS_VIEW_HPP
  9. #include <vector>
  10. #include <boost/range/begin.hpp>
  11. #include <boost/range/end.hpp>
  12. #include <boost/range/iterator.hpp>
  13. #include <boost/range/size.hpp>
  14. #include <boost/geometry/algorithms/detail/visit.hpp>
  15. #include <boost/geometry/core/geometry_types.hpp>
  16. #include <boost/geometry/core/tag.hpp>
  17. #include <boost/geometry/core/tags.hpp>
  18. #include <boost/geometry/util/sequence.hpp>
  19. #include <boost/geometry/util/type_traits.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. #ifndef DOXYGEN_NO_DETAIL
  23. namespace detail
  24. {
  25. template <typename Range>
  26. struct is_random_access_range
  27. : std::is_convertible
  28. <
  29. typename boost::iterator_traversal
  30. <
  31. typename boost::range_iterator<Range>::type
  32. >::type,
  33. boost::random_access_traversal_tag
  34. >
  35. {};
  36. template <typename Geometry>
  37. struct is_geometry_collection_recursive
  38. : util::bool_constant
  39. <
  40. util::is_geometry_collection
  41. <
  42. typename util::sequence_find_if
  43. <
  44. typename traits::geometry_types<std::remove_const_t<Geometry>>::type,
  45. util::is_geometry_collection
  46. >::type
  47. >::value
  48. >
  49. {};
  50. template
  51. <
  52. typename GeometryCollection,
  53. bool IsRandomAccess = is_random_access_range<GeometryCollection>::value,
  54. bool IsRecursive = is_geometry_collection_recursive<GeometryCollection>::value
  55. >
  56. class random_access_view
  57. : public std::vector<typename boost::range_iterator<GeometryCollection>::type>
  58. {
  59. // NOTE: An alternative would be to implement iterator holding base iterators
  60. // to geometry collections of lower levels to process after the current level
  61. // of bfs traversal is finished.
  62. using base_t = std::vector<typename boost::range_iterator<GeometryCollection>::type>;
  63. public:
  64. random_access_view(GeometryCollection & geometry)
  65. {
  66. this->reserve(boost::size(geometry));
  67. detail::visit_breadth_first_impl<true>::apply([&](auto&&, auto iter)
  68. {
  69. this->push_back(iter);
  70. return true;
  71. }, geometry);
  72. }
  73. };
  74. template <typename GeometryCollection>
  75. class random_access_view<GeometryCollection, true, false>
  76. {
  77. public:
  78. using iterator = typename boost::range_iterator<GeometryCollection>::type;
  79. using const_iterator = typename boost::range_const_iterator<GeometryCollection>::type;
  80. random_access_view(GeometryCollection & geometry)
  81. : m_begin(boost::begin(geometry))
  82. , m_end(boost::end(geometry))
  83. {}
  84. iterator begin() { return m_begin; }
  85. iterator end() { return m_end; }
  86. const_iterator begin() const { return m_begin; }
  87. const_iterator end() const { return m_end; }
  88. private:
  89. iterator m_begin, m_end;
  90. };
  91. template <typename GeometryCollection>
  92. struct random_access_view_iter_visit
  93. {
  94. template <typename Function, typename Iterator>
  95. static void apply(Function && function, Iterator iterator)
  96. {
  97. geometry::traits::iter_visit
  98. <
  99. std::remove_const_t<GeometryCollection>
  100. >::apply(std::forward<Function>(function), *iterator);
  101. }
  102. };
  103. template <typename ...Ts>
  104. struct remove_geometry_collections_pack
  105. {
  106. using type = util::type_sequence<>;
  107. };
  108. template <typename T, typename ...Ts>
  109. struct remove_geometry_collections_pack<T, Ts...>
  110. {
  111. using next_sequence = typename remove_geometry_collections_pack<Ts...>::type;
  112. using type = std::conditional_t
  113. <
  114. util::is_geometry_collection<T>::value,
  115. next_sequence,
  116. typename util::sequence_merge<util::type_sequence<T>, next_sequence>::type
  117. >;
  118. };
  119. template <typename Types>
  120. struct remove_geometry_collections;
  121. template <typename ...Ts>
  122. struct remove_geometry_collections<util::type_sequence<Ts...>>
  123. : remove_geometry_collections_pack<Ts...>
  124. {};
  125. } // namespace detail
  126. #endif // DOXYGEN_NO_DETAIL
  127. #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  128. namespace traits
  129. {
  130. template<typename GeometryCollection, bool IsRandomAccess, bool IsRecursive>
  131. struct tag<geometry::detail::random_access_view<GeometryCollection, IsRandomAccess, IsRecursive>>
  132. {
  133. using type = geometry_collection_tag;
  134. };
  135. template <typename GeometryCollection>
  136. struct iter_visit<geometry::detail::random_access_view<GeometryCollection, false, false>>
  137. : geometry::detail::random_access_view_iter_visit<GeometryCollection>
  138. {};
  139. template <typename GeometryCollection>
  140. struct iter_visit<geometry::detail::random_access_view<GeometryCollection, true, true>>
  141. : geometry::detail::random_access_view_iter_visit<GeometryCollection>
  142. {};
  143. template <typename GeometryCollection>
  144. struct iter_visit<geometry::detail::random_access_view<GeometryCollection, false, true>>
  145. : geometry::detail::random_access_view_iter_visit<GeometryCollection>
  146. {};
  147. template <typename GeometryCollection>
  148. struct iter_visit<geometry::detail::random_access_view<GeometryCollection, true, false>>
  149. {
  150. template <typename Function, typename Iterator>
  151. static void apply(Function && function, Iterator iterator)
  152. {
  153. geometry::traits::iter_visit
  154. <
  155. std::remove_const_t<GeometryCollection>
  156. >::apply(std::forward<Function>(function), iterator);
  157. }
  158. };
  159. template <typename GeometryCollection, bool IsRandomAccess>
  160. struct geometry_types<geometry::detail::random_access_view<GeometryCollection, IsRandomAccess, false>>
  161. : traits::geometry_types
  162. <
  163. std::remove_const_t<GeometryCollection>
  164. >
  165. {};
  166. template <typename GeometryCollection, bool IsRandomAccess>
  167. struct geometry_types<geometry::detail::random_access_view<GeometryCollection, IsRandomAccess, true>>
  168. : geometry::detail::remove_geometry_collections
  169. <
  170. typename traits::geometry_types
  171. <
  172. std::remove_const_t<GeometryCollection>
  173. >::type
  174. >
  175. {};
  176. } // namespace traits
  177. #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  178. }} // namespace boost::geometry
  179. #endif // BOOST_GEOMETRY_VIEWS_DETAIL_RANDOM_ACCESS_VIEW_HPP