9
3

remove.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Boost.Geometry Index
  2. //
  3. // R-tree removing visitor implementation
  4. //
  5. // Copyright (c) 2011-2017 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_VISITORS_REMOVE_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_REMOVE_HPP
  16. #include <boost/geometry/index/detail/rtree/visitors/is_leaf.hpp>
  17. #include <boost/geometry/algorithms/detail/covered_by/interface.hpp>
  18. namespace boost { namespace geometry { namespace index {
  19. namespace detail { namespace rtree { namespace visitors {
  20. // Default remove algorithm
  21. template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
  22. class remove
  23. : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, false>::type
  24. {
  25. typedef typename Options::parameters_type parameters_type;
  26. typedef typename rtree::node<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type node;
  27. typedef typename rtree::internal_node<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
  28. typedef typename rtree::leaf<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
  29. typedef rtree::subtree_destroyer<Value, Options, Translator, Box, Allocators> subtree_destroyer;
  30. typedef typename Allocators::node_pointer node_pointer;
  31. typedef typename Allocators::size_type size_type;
  32. typedef typename rtree::elements_type<internal_node>::type::size_type internal_size_type;
  33. //typedef typename Allocators::internal_node_pointer internal_node_pointer;
  34. typedef internal_node * internal_node_pointer;
  35. public:
  36. inline remove(node_pointer & root,
  37. size_type & leafs_level,
  38. Value const& value,
  39. parameters_type const& parameters,
  40. Translator const& translator,
  41. Allocators & allocators)
  42. : m_value(value)
  43. , m_parameters(parameters)
  44. , m_translator(translator)
  45. , m_allocators(allocators)
  46. , m_root_node(root)
  47. , m_leafs_level(leafs_level)
  48. , m_is_value_removed(false)
  49. , m_parent(0)
  50. , m_current_child_index(0)
  51. , m_current_level(0)
  52. , m_is_underflow(false)
  53. {
  54. // TODO
  55. // assert - check if Value/Box is correct
  56. }
  57. inline void operator()(internal_node & n)
  58. {
  59. typedef typename rtree::elements_type<internal_node>::type children_type;
  60. children_type & children = rtree::elements(n);
  61. // traverse children which boxes intersects value's box
  62. internal_size_type child_node_index = 0;
  63. for ( ; child_node_index < children.size() ; ++child_node_index )
  64. {
  65. if ( index::detail::covered_by_bounds(m_translator(m_value),
  66. children[child_node_index].first,
  67. index::detail::get_strategy(m_parameters)) )
  68. {
  69. // next traversing step
  70. traverse_apply_visitor(n, child_node_index); // MAY THROW
  71. if ( m_is_value_removed )
  72. break;
  73. }
  74. }
  75. // value was found and removed
  76. if ( m_is_value_removed )
  77. {
  78. typedef typename rtree::elements_type<internal_node>::type elements_type;
  79. typedef typename elements_type::iterator element_iterator;
  80. elements_type & elements = rtree::elements(n);
  81. // underflow occured - child node should be removed
  82. if ( m_is_underflow )
  83. {
  84. element_iterator underfl_el_it = elements.begin() + child_node_index;
  85. size_type relative_level = m_leafs_level - m_current_level;
  86. // move node to the container - store node's relative level as well and return new underflow state
  87. // NOTE: if the min elements number is 1, then after an underflow
  88. // here the child elements count is 0, so it's not required to store this node,
  89. // it could just be destroyed
  90. m_is_underflow = store_underflowed_node(elements, underfl_el_it, relative_level); // MAY THROW (E: alloc, copy)
  91. }
  92. // n is not root - adjust aabb
  93. if ( 0 != m_parent )
  94. {
  95. // underflow state should be ok here
  96. // note that there may be less than min_elems elements in root
  97. // so this condition should be checked only here
  98. BOOST_GEOMETRY_INDEX_ASSERT((elements.size() < m_parameters.get_min_elements()) == m_is_underflow, "unexpected state");
  99. rtree::elements(*m_parent)[m_current_child_index].first
  100. = rtree::elements_box<Box>(elements.begin(), elements.end(), m_translator,
  101. index::detail::get_strategy(m_parameters));
  102. }
  103. // n is root node
  104. else
  105. {
  106. BOOST_GEOMETRY_INDEX_ASSERT(&n == &rtree::get<internal_node>(*m_root_node), "node must be the root");
  107. // reinsert elements from removed nodes (underflows)
  108. reinsert_removed_nodes_elements(); // MAY THROW (V, E: alloc, copy, N: alloc)
  109. // shorten the tree
  110. // NOTE: if the min elements number is 1, then after underflow
  111. // here the number of elements may be equal to 0
  112. // this can occur only for the last removed element
  113. if ( rtree::elements(n).size() <= 1 )
  114. {
  115. node_pointer root_to_destroy = m_root_node;
  116. if ( rtree::elements(n).size() == 0 )
  117. m_root_node = 0;
  118. else
  119. m_root_node = rtree::elements(n)[0].second;
  120. --m_leafs_level;
  121. rtree::destroy_node<Allocators, internal_node>::apply(m_allocators, root_to_destroy);
  122. }
  123. }
  124. }
  125. }
  126. inline void operator()(leaf & n)
  127. {
  128. typedef typename rtree::elements_type<leaf>::type elements_type;
  129. elements_type & elements = rtree::elements(n);
  130. // find value and remove it
  131. for ( typename elements_type::iterator it = elements.begin() ; it != elements.end() ; ++it )
  132. {
  133. if ( m_translator.equals(*it, m_value, index::detail::get_strategy(m_parameters)) )
  134. {
  135. rtree::move_from_back(elements, it); // MAY THROW (V: copy)
  136. elements.pop_back();
  137. m_is_value_removed = true;
  138. break;
  139. }
  140. }
  141. // if value was removed
  142. if ( m_is_value_removed )
  143. {
  144. BOOST_GEOMETRY_INDEX_ASSERT(0 < m_parameters.get_min_elements(), "min number of elements is too small");
  145. // calc underflow
  146. m_is_underflow = elements.size() < m_parameters.get_min_elements();
  147. // n is not root - adjust aabb
  148. if ( 0 != m_parent )
  149. {
  150. rtree::elements(*m_parent)[m_current_child_index].first
  151. = rtree::values_box<Box>(elements.begin(), elements.end(), m_translator,
  152. index::detail::get_strategy(m_parameters));
  153. }
  154. }
  155. }
  156. bool is_value_removed() const
  157. {
  158. return m_is_value_removed;
  159. }
  160. private:
  161. typedef std::vector< std::pair<size_type, node_pointer> > UnderflowNodes;
  162. void traverse_apply_visitor(internal_node &n, internal_size_type choosen_node_index)
  163. {
  164. // save previous traverse inputs and set new ones
  165. internal_node_pointer parent_bckup = m_parent;
  166. internal_size_type current_child_index_bckup = m_current_child_index;
  167. size_type current_level_bckup = m_current_level;
  168. m_parent = &n;
  169. m_current_child_index = choosen_node_index;
  170. ++m_current_level;
  171. // next traversing step
  172. rtree::apply_visitor(*this, *rtree::elements(n)[choosen_node_index].second); // MAY THROW (V, E: alloc, copy, N: alloc)
  173. // restore previous traverse inputs
  174. m_parent = parent_bckup;
  175. m_current_child_index = current_child_index_bckup;
  176. m_current_level = current_level_bckup;
  177. }
  178. bool store_underflowed_node(
  179. typename rtree::elements_type<internal_node>::type & elements,
  180. typename rtree::elements_type<internal_node>::type::iterator underfl_el_it,
  181. size_type relative_level)
  182. {
  183. // move node to the container - store node's relative level as well
  184. m_underflowed_nodes.push_back(std::make_pair(relative_level, underfl_el_it->second)); // MAY THROW (E: alloc, copy)
  185. BOOST_TRY
  186. {
  187. // NOTE: those are elements of the internal node which means that copy/move shouldn't throw
  188. // Though it's safer in case if the pointer type could throw in copy ctor.
  189. // In the future this try-catch block could be removed.
  190. rtree::move_from_back(elements, underfl_el_it); // MAY THROW (E: copy)
  191. elements.pop_back();
  192. }
  193. BOOST_CATCH(...)
  194. {
  195. m_underflowed_nodes.pop_back();
  196. BOOST_RETHROW // RETHROW
  197. }
  198. BOOST_CATCH_END
  199. // calc underflow
  200. return elements.size() < m_parameters.get_min_elements();
  201. }
  202. static inline bool is_leaf(node const& n)
  203. {
  204. visitors::is_leaf<Value, Options, Box, Allocators> ilv;
  205. rtree::apply_visitor(ilv, n);
  206. return ilv.result;
  207. }
  208. void reinsert_removed_nodes_elements()
  209. {
  210. typename UnderflowNodes::reverse_iterator it = m_underflowed_nodes.rbegin();
  211. BOOST_TRY
  212. {
  213. // reinsert elements from removed nodes
  214. // begin with levels closer to the root
  215. for ( ; it != m_underflowed_nodes.rend() ; ++it )
  216. {
  217. // it->first is an index of a level of a node, not children
  218. // counted from the leafs level
  219. bool const node_is_leaf = it->first == 1;
  220. BOOST_GEOMETRY_INDEX_ASSERT(node_is_leaf == is_leaf(*it->second), "unexpected condition");
  221. if ( node_is_leaf )
  222. {
  223. reinsert_node_elements(rtree::get<leaf>(*it->second), it->first); // MAY THROW (V, E: alloc, copy, N: alloc)
  224. rtree::destroy_node<Allocators, leaf>::apply(m_allocators, it->second);
  225. }
  226. else
  227. {
  228. reinsert_node_elements(rtree::get<internal_node>(*it->second), it->first); // MAY THROW (V, E: alloc, copy, N: alloc)
  229. rtree::destroy_node<Allocators, internal_node>::apply(m_allocators, it->second);
  230. }
  231. }
  232. //m_underflowed_nodes.clear();
  233. }
  234. BOOST_CATCH(...)
  235. {
  236. // destroy current and remaining nodes
  237. for ( ; it != m_underflowed_nodes.rend() ; ++it )
  238. {
  239. subtree_destroyer dummy(it->second, m_allocators);
  240. }
  241. //m_underflowed_nodes.clear();
  242. BOOST_RETHROW // RETHROW
  243. }
  244. BOOST_CATCH_END
  245. }
  246. template <typename Node>
  247. void reinsert_node_elements(Node &n, size_type node_relative_level)
  248. {
  249. typedef typename rtree::elements_type<Node>::type elements_type;
  250. elements_type & elements = rtree::elements(n);
  251. typename elements_type::iterator it = elements.begin();
  252. BOOST_TRY
  253. {
  254. for ( ; it != elements.end() ; ++it )
  255. {
  256. visitors::insert<
  257. typename elements_type::value_type,
  258. Value, Options, Translator, Box, Allocators,
  259. typename Options::insert_tag
  260. > insert_v(
  261. m_root_node, m_leafs_level, *it,
  262. m_parameters, m_translator, m_allocators,
  263. node_relative_level - 1);
  264. rtree::apply_visitor(insert_v, *m_root_node); // MAY THROW (V, E: alloc, copy, N: alloc)
  265. }
  266. }
  267. BOOST_CATCH(...)
  268. {
  269. ++it;
  270. rtree::destroy_elements<Value, Options, Translator, Box, Allocators>
  271. ::apply(it, elements.end(), m_allocators);
  272. elements.clear();
  273. BOOST_RETHROW // RETHROW
  274. }
  275. BOOST_CATCH_END
  276. }
  277. Value const& m_value;
  278. parameters_type const& m_parameters;
  279. Translator const& m_translator;
  280. Allocators & m_allocators;
  281. node_pointer & m_root_node;
  282. size_type & m_leafs_level;
  283. bool m_is_value_removed;
  284. UnderflowNodes m_underflowed_nodes;
  285. // traversing input parameters
  286. internal_node_pointer m_parent;
  287. internal_size_type m_current_child_index;
  288. size_type m_current_level;
  289. // traversing output parameters
  290. bool m_is_underflow;
  291. };
  292. }}} // namespace detail::rtree::visitors
  293. }}} // namespace boost::geometry::index
  294. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_REMOVE_HPP