node.hpp 7.9 KB

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