node.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/algorithms/expand.hpp>
  29. #include <boost/geometry/index/detail/rtree/visitors/destroy.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 MembersHolder>
  83. struct destroy_element
  84. {
  85. typedef typename MembersHolder::parameters_type parameters_type;
  86. typedef typename MembersHolder::allocators_type allocators_type;
  87. typedef typename MembersHolder::internal_node internal_node;
  88. typedef typename MembersHolder::leaf leaf;
  89. inline static void apply(typename internal_node::elements_type::value_type & element,
  90. allocators_type & allocators)
  91. {
  92. detail::rtree::visitors::destroy<MembersHolder>::apply(element.second, allocators);
  93. element.second = 0;
  94. }
  95. inline static void apply(typename leaf::elements_type::value_type &,
  96. allocators_type &)
  97. {}
  98. };
  99. // destroys stored subtrees if internal node's elements are passed
  100. template <typename MembersHolder>
  101. struct destroy_elements
  102. {
  103. typedef typename MembersHolder::value_type value_type;
  104. typedef typename MembersHolder::allocators_type allocators_type;
  105. template <typename Range>
  106. inline static void apply(Range & elements, allocators_type & allocators)
  107. {
  108. apply(boost::begin(elements), boost::end(elements), allocators);
  109. }
  110. template <typename It>
  111. inline static void apply(It first, It last, allocators_type & allocators)
  112. {
  113. typedef boost::mpl::bool_<
  114. boost::is_same<
  115. value_type, typename std::iterator_traits<It>::value_type
  116. >::value
  117. > is_range_of_values;
  118. apply_dispatch(first, last, allocators, is_range_of_values());
  119. }
  120. private:
  121. template <typename It>
  122. inline static void apply_dispatch(It first, It last, allocators_type & allocators,
  123. boost::mpl::bool_<false> const& /*is_range_of_values*/)
  124. {
  125. for ( ; first != last ; ++first )
  126. {
  127. detail::rtree::visitors::destroy<MembersHolder>::apply(first->second, allocators);
  128. first->second = 0;
  129. }
  130. }
  131. template <typename It>
  132. inline static void apply_dispatch(It /*first*/, It /*last*/, allocators_type & /*allocators*/,
  133. boost::mpl::bool_<true> const& /*is_range_of_values*/)
  134. {}
  135. };
  136. // clears node, deletes all subtrees stored in node
  137. /*
  138. template <typename MembersHolder>
  139. struct clear_node
  140. {
  141. typedef typename MembersHolder::parameters_type parameters_type;
  142. typedef typename MembersHolder::allocators_type allocators_type;
  143. typedef typename MembersHolder::node node;
  144. typedef typename MembersHolder::internal_node internal_node;
  145. typedef typename MembersHolder::leaf leaf;
  146. inline static void apply(node & node, allocators_type & allocators)
  147. {
  148. rtree::visitors::is_leaf<MembersHolder> ilv;
  149. rtree::apply_visitor(ilv, node);
  150. if ( ilv.result )
  151. {
  152. apply(rtree::get<leaf>(node), allocators);
  153. }
  154. else
  155. {
  156. apply(rtree::get<internal_node>(node), allocators);
  157. }
  158. }
  159. inline static void apply(internal_node & internal_node, allocators_type & allocators)
  160. {
  161. destroy_elements<MembersHolder>::apply(rtree::elements(internal_node), allocators);
  162. rtree::elements(internal_node).clear();
  163. }
  164. inline static void apply(leaf & leaf, allocators_type &)
  165. {
  166. rtree::elements(leaf).clear();
  167. }
  168. };
  169. */
  170. template <typename Container, typename Iterator>
  171. void move_from_back(Container & container, Iterator it)
  172. {
  173. BOOST_GEOMETRY_INDEX_ASSERT(!container.empty(), "cannot copy from empty container");
  174. Iterator back_it = container.end();
  175. --back_it;
  176. if ( it != back_it )
  177. {
  178. *it = boost::move(*back_it); // MAY THROW (copy)
  179. }
  180. }
  181. }} // namespace detail::rtree
  182. }}} // namespace boost::geometry::index
  183. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP