node.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Boost.Geometry Index
  2. //
  3. // R-tree nodes
  4. //
  5. // Copyright (c) 2011-2023 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // This file was modified by Oracle on 2019-2020.
  8. // Modifications copyright (c) 2019-2020 Oracle and/or its affiliates.
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. //
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP
  16. #include <type_traits>
  17. #include <boost/container/vector.hpp>
  18. #include <boost/geometry/core/static_assert.hpp>
  19. #include <boost/geometry/algorithms/expand.hpp>
  20. #include <boost/geometry/index/detail/varray.hpp>
  21. #include <boost/geometry/index/detail/rtree/node/concept.hpp>
  22. #include <boost/geometry/index/detail/rtree/node/pairs.hpp>
  23. #include <boost/geometry/index/detail/rtree/node/node_elements.hpp>
  24. #include <boost/geometry/index/detail/rtree/node/scoped_deallocator.hpp>
  25. //#include <boost/geometry/index/detail/rtree/node/weak_visitor.hpp>
  26. //#include <boost/geometry/index/detail/rtree/node/weak_dynamic.hpp>
  27. //#include <boost/geometry/index/detail/rtree/node/weak_static.hpp>
  28. #include <boost/geometry/index/detail/rtree/node/variant_visitor.hpp>
  29. #include <boost/geometry/index/detail/rtree/node/variant_dynamic.hpp>
  30. #include <boost/geometry/index/detail/rtree/node/variant_static.hpp>
  31. #include <boost/geometry/index/detail/rtree/visitors/destroy.hpp>
  32. #include <boost/geometry/index/detail/rtree/visitors/is_leaf.hpp>
  33. #include <boost/geometry/index/detail/algorithms/bounds.hpp>
  34. #include <boost/geometry/index/detail/is_bounding_geometry.hpp>
  35. #include <boost/geometry/util/constexpr.hpp>
  36. namespace boost { namespace geometry { namespace index {
  37. namespace detail { namespace rtree {
  38. // elements box
  39. template <typename Box, typename FwdIter, typename Translator, typename Strategy>
  40. inline Box elements_box(FwdIter first, FwdIter last, Translator const& tr,
  41. Strategy const& strategy)
  42. {
  43. Box result;
  44. // Only here to suppress 'uninitialized local variable used' warning
  45. // until the suggestion below is not implemented
  46. geometry::assign_inverse(result);
  47. //BOOST_GEOMETRY_INDEX_ASSERT(first != last, "non-empty range required");
  48. // NOTE: this is not elegant temporary solution,
  49. // reference to box could be passed as parameter and bool returned
  50. if ( first == last )
  51. return result;
  52. detail::bounds(element_indexable(*first, tr), result, strategy);
  53. ++first;
  54. for ( ; first != last ; ++first )
  55. detail::expand(result, element_indexable(*first, tr), strategy);
  56. return result;
  57. }
  58. // Enlarge bounds of a leaf node WRT epsilon if needed.
  59. // It's because Points and Segments are compared WRT machine epsilon.
  60. // This ensures that leafs bounds correspond to the stored elements.
  61. // NOTE: this is done only if the Indexable is not a Box
  62. // in the future don't do it also for NSphere
  63. template <typename Box, typename FwdIter, typename Translator, typename Strategy>
  64. inline Box values_box(FwdIter first, FwdIter last, Translator const& tr,
  65. Strategy const& strategy)
  66. {
  67. typedef typename std::iterator_traits<FwdIter>::value_type element_type;
  68. BOOST_GEOMETRY_STATIC_ASSERT((is_leaf_element<element_type>::value),
  69. "This function should be called only for elements of leaf nodes.",
  70. element_type);
  71. Box result = elements_box<Box>(first, last, tr, strategy);
  72. #ifdef BOOST_GEOMETRY_INDEX_EXPERIMENTAL_ENLARGE_BY_EPSILON
  73. if BOOST_GEOMETRY_CONSTEXPR (! index::detail::is_bounding_geometry
  74. <
  75. typename indexable_type<Translator>::type
  76. >::value)
  77. {
  78. geometry::detail::expand_by_epsilon(result);
  79. }
  80. #endif
  81. return result;
  82. }
  83. // destroys subtree if the element is internal node's element
  84. template <typename MembersHolder>
  85. struct destroy_element
  86. {
  87. typedef typename MembersHolder::parameters_type parameters_type;
  88. typedef typename MembersHolder::allocators_type allocators_type;
  89. typedef typename MembersHolder::internal_node internal_node;
  90. typedef typename MembersHolder::leaf leaf;
  91. inline static void apply(typename internal_node::elements_type::value_type & element,
  92. allocators_type & allocators)
  93. {
  94. detail::rtree::visitors::destroy<MembersHolder>::apply(element.second, allocators);
  95. element.second = 0;
  96. }
  97. inline static void apply(typename leaf::elements_type::value_type &,
  98. allocators_type &)
  99. {}
  100. };
  101. // destroys stored subtrees if internal node's elements are passed
  102. template <typename MembersHolder>
  103. struct destroy_elements
  104. {
  105. typedef typename MembersHolder::value_type value_type;
  106. typedef typename MembersHolder::allocators_type allocators_type;
  107. template <typename Range>
  108. inline static void apply(Range & elements, allocators_type & allocators)
  109. {
  110. apply(boost::begin(elements), boost::end(elements), allocators);
  111. }
  112. template <typename It>
  113. inline static void apply(It first, It last, allocators_type & allocators)
  114. {
  115. typedef std::is_same
  116. <
  117. value_type, typename std::iterator_traits<It>::value_type
  118. > is_range_of_values;
  119. apply_dispatch(first, last, allocators, is_range_of_values());
  120. }
  121. private:
  122. template <typename It>
  123. inline static void apply_dispatch(It first, It last, allocators_type & allocators,
  124. std::false_type /*is_range_of_values*/)
  125. {
  126. for ( ; first != last ; ++first )
  127. {
  128. detail::rtree::visitors::destroy<MembersHolder>::apply(first->second, allocators);
  129. first->second = 0;
  130. }
  131. }
  132. template <typename It>
  133. inline static void apply_dispatch(It /*first*/, It /*last*/, allocators_type & /*allocators*/,
  134. std::true_type /*is_range_of_values*/)
  135. {}
  136. };
  137. // clears node, deletes all subtrees stored in node
  138. /*
  139. template <typename MembersHolder>
  140. struct clear_node
  141. {
  142. typedef typename MembersHolder::parameters_type parameters_type;
  143. typedef typename MembersHolder::allocators_type allocators_type;
  144. typedef typename MembersHolder::node node;
  145. typedef typename MembersHolder::internal_node internal_node;
  146. typedef typename MembersHolder::leaf leaf;
  147. inline static void apply(node & node, allocators_type & allocators)
  148. {
  149. rtree::visitors::is_leaf<MembersHolder> ilv;
  150. rtree::apply_visitor(ilv, node);
  151. if ( ilv.result )
  152. {
  153. apply(rtree::get<leaf>(node), allocators);
  154. }
  155. else
  156. {
  157. apply(rtree::get<internal_node>(node), allocators);
  158. }
  159. }
  160. inline static void apply(internal_node & internal_node, allocators_type & allocators)
  161. {
  162. destroy_elements<MembersHolder>::apply(rtree::elements(internal_node), allocators);
  163. rtree::elements(internal_node).clear();
  164. }
  165. inline static void apply(leaf & leaf, allocators_type &)
  166. {
  167. rtree::elements(leaf).clear();
  168. }
  169. };
  170. */
  171. template <typename Container, typename Iterator>
  172. void move_from_back(Container & container, Iterator it)
  173. {
  174. BOOST_GEOMETRY_INDEX_ASSERT(!container.empty(), "cannot copy from empty container");
  175. Iterator back_it = container.end();
  176. --back_it;
  177. if ( it != back_it )
  178. {
  179. *it = std::move(*back_it); // MAY THROW (copy)
  180. }
  181. }
  182. }} // namespace detail::rtree
  183. }}} // namespace boost::geometry::index
  184. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP