node.hpp 7.5 KB

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