weak_dynamic.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Boost.Geometry Index
  2. //
  3. // R-tree nodes based on static conversion, storing dynamic-size containers
  4. //
  5. // Copyright (c) 2011-2023 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // This file was modified by Oracle on 2021-2023.
  8. // Modifications copyright (c) 2021-2023 Oracle and/or its affiliates.
  9. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. //
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_WEAK_DYNAMIC_HPP
  16. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_WEAK_DYNAMIC_HPP
  17. #include <boost/container/allocator_traits.hpp>
  18. #include <boost/container/vector.hpp>
  19. #include <boost/core/pointer_traits.hpp>
  20. #include <boost/core/invoke_swap.hpp>
  21. #include <boost/geometry/index/detail/rtree/options.hpp>
  22. #include <boost/geometry/index/detail/rtree/node/concept.hpp>
  23. #include <boost/geometry/index/detail/rtree/node/pairs.hpp>
  24. #include <boost/geometry/index/detail/rtree/node/scoped_deallocator.hpp>
  25. #include <boost/geometry/index/detail/rtree/node/weak_visitor.hpp>
  26. namespace boost { namespace geometry { namespace index {
  27. namespace detail { namespace rtree {
  28. // TODO: This should be defined in options.hpp
  29. // For now it's defined here to satisfy Boost header policy
  30. struct node_weak_dynamic_tag {};
  31. struct node_weak_static_tag {};
  32. template <typename Value, typename Parameters, typename Box, typename Allocators, typename Tag>
  33. struct weak_internal_node
  34. : public weak_node<Value, Parameters, Box, Allocators, Tag>
  35. {
  36. typedef rtree::ptr_pair<Box, typename Allocators::node_pointer> element_type;
  37. typedef typename boost::container::allocator_traits
  38. <
  39. typename Allocators::internal_node_allocator_type
  40. >::template rebind_alloc<element_type> allocator_type;
  41. typedef boost::container::vector<element_type, allocator_type> elements_type;
  42. template <typename Al>
  43. inline weak_internal_node(Al const& al)
  44. : elements(allocator_type(al))
  45. {}
  46. elements_type elements;
  47. };
  48. template <typename Value, typename Parameters, typename Box, typename Allocators, typename Tag>
  49. struct weak_leaf
  50. : public weak_node<Value, Parameters, Box, Allocators, Tag>
  51. {
  52. typedef typename boost::container::allocator_traits
  53. <
  54. typename Allocators::leaf_allocator_type
  55. >::template rebind_alloc<Value> allocator_type;
  56. typedef boost::container::vector<Value, allocator_type> elements_type;
  57. template <typename Al>
  58. inline weak_leaf(Al const& al)
  59. : elements(allocator_type(al))
  60. {}
  61. elements_type elements;
  62. };
  63. // nodes traits
  64. template <typename Value, typename Parameters, typename Box, typename Allocators>
  65. struct node<Value, Parameters, Box, Allocators, node_weak_dynamic_tag>
  66. {
  67. typedef weak_node<Value, Parameters, Box, Allocators, node_weak_dynamic_tag> type;
  68. };
  69. template <typename Value, typename Parameters, typename Box, typename Allocators>
  70. struct internal_node<Value, Parameters, Box, Allocators, node_weak_dynamic_tag>
  71. {
  72. typedef weak_internal_node<Value, Parameters, Box, Allocators, node_weak_dynamic_tag> type;
  73. };
  74. template <typename Value, typename Parameters, typename Box, typename Allocators>
  75. struct leaf<Value, Parameters, Box, Allocators, node_weak_dynamic_tag>
  76. {
  77. typedef weak_leaf<Value, Parameters, Box, Allocators, node_weak_dynamic_tag> type;
  78. };
  79. // visitor traits
  80. template <typename Value, typename Parameters, typename Box, typename Allocators, bool IsVisitableConst>
  81. struct visitor<Value, Parameters, Box, Allocators, node_weak_dynamic_tag, IsVisitableConst>
  82. {
  83. typedef weak_visitor<Value, Parameters, Box, Allocators, node_weak_dynamic_tag, IsVisitableConst> type;
  84. };
  85. // allocators
  86. template <typename Allocator, typename Value, typename Parameters, typename Box, typename Tag>
  87. struct internal_node_alloc
  88. {
  89. typedef typename internal_node
  90. <
  91. Value, Parameters, Box,
  92. allocators<Allocator, Value, Parameters, Box, Tag>,
  93. Tag
  94. >::type node_type;
  95. typedef typename boost::container::allocator_traits
  96. <
  97. Allocator
  98. >::template rebind_alloc<node_type> type;
  99. };
  100. template <typename Allocator, typename Value, typename Parameters, typename Box, typename Tag>
  101. struct leaf_alloc
  102. {
  103. typedef typename leaf
  104. <
  105. Value, Parameters, Box,
  106. allocators<Allocator, Value, Parameters, Box, Tag>,
  107. Tag
  108. >::type node_type;
  109. typedef typename ::boost::container::allocator_traits
  110. <
  111. Allocator
  112. >::template rebind_alloc<node_type> type;
  113. };
  114. template <typename Allocator, typename Value, typename Parameters, typename Box, typename Tag>
  115. struct node_alloc
  116. {
  117. typedef typename weak_node
  118. <
  119. Value, Parameters, Box,
  120. allocators<Allocator, Value, Parameters, Box, Tag>,
  121. Tag
  122. >::type node_type;
  123. typedef typename ::boost::container::allocator_traits
  124. <
  125. Allocator
  126. >::template rebind_alloc<node_type> type;
  127. };
  128. template <typename Allocator, typename Value, typename Parameters, typename Box>
  129. class allocators<Allocator, Value, Parameters, Box, node_weak_dynamic_tag>
  130. : public internal_node_alloc<Allocator, Value, Parameters, Box, node_weak_dynamic_tag>::type
  131. , public leaf_alloc<Allocator, Value, Parameters, Box, node_weak_dynamic_tag>::type
  132. {
  133. typedef detail::rtree::internal_node_alloc
  134. <
  135. Allocator, Value, Parameters, Box, node_weak_dynamic_tag
  136. > internal_node_alloc;
  137. typedef detail::rtree::leaf_alloc
  138. <
  139. Allocator, Value, Parameters, Box, node_weak_dynamic_tag
  140. > leaf_alloc;
  141. typedef detail::rtree::node_alloc
  142. <
  143. Allocator, Value, Parameters, Box, node_weak_dynamic_tag
  144. > node_alloc;
  145. public:
  146. typedef typename internal_node_alloc::type internal_node_allocator_type;
  147. typedef typename leaf_alloc::type leaf_allocator_type;
  148. typedef typename node_alloc::traits::pointer node_pointer;
  149. private:
  150. typedef typename boost::container::allocator_traits
  151. <
  152. leaf_allocator_type // leaf_allocator_type for consistency with weak_leaf
  153. >::template rebind_alloc<Value> value_allocator_type;
  154. typedef boost::container::allocator_traits<value_allocator_type> value_allocator_traits;
  155. public:
  156. typedef Allocator allocator_type;
  157. typedef Value value_type;
  158. typedef typename value_allocator_traits::reference reference;
  159. typedef typename value_allocator_traits::const_reference const_reference;
  160. typedef typename value_allocator_traits::size_type size_type;
  161. typedef typename value_allocator_traits::difference_type difference_type;
  162. typedef typename value_allocator_traits::pointer pointer;
  163. typedef typename value_allocator_traits::const_pointer const_pointer;
  164. inline allocators()
  165. : internal_node_allocator_type()
  166. , leaf_allocator_type()
  167. {}
  168. template <typename Alloc>
  169. inline explicit allocators(Alloc const& alloc)
  170. : internal_node_allocator_type(alloc)
  171. , leaf_allocator_type(alloc)
  172. {}
  173. inline allocators(allocators&& a)
  174. : internal_node_allocator_type(std::move(a.internal_node_allocator()))
  175. , leaf_allocator_type(std::move(a.leaf_allocator()))
  176. {}
  177. inline allocators & operator=(allocators&& a)
  178. {
  179. internal_node_allocator() = std::move(a.internal_node_allocator());
  180. leaf_allocator() = std::move(a.leaf_allocator());
  181. return *this;
  182. }
  183. inline allocators & operator=(allocators const& a)
  184. {
  185. internal_node_allocator() = a.internal_node_allocator();
  186. leaf_allocator() = a.leaf_allocator();
  187. return *this;
  188. }
  189. void swap(allocators & a)
  190. {
  191. boost::core::invoke_swap(internal_node_allocator(), a.internal_node_allocator());
  192. boost::core::invoke_swap(leaf_allocator(), a.leaf_allocator());
  193. }
  194. bool operator==(allocators const& a) const { return leaf_allocator() == a.leaf_allocator(); }
  195. template <typename Alloc>
  196. bool operator==(Alloc const& a) const { return leaf_allocator() == leaf_allocator_type(a); }
  197. Allocator allocator() const { return Allocator(leaf_allocator()); }
  198. internal_node_allocator_type & internal_node_allocator() { return *this; }
  199. internal_node_allocator_type const& internal_node_allocator() const { return *this; }
  200. leaf_allocator_type & leaf_allocator() { return *this; }
  201. leaf_allocator_type const& leaf_allocator() const { return *this; }
  202. };
  203. // create_node_impl
  204. template <typename BaseNodePtr, typename Node>
  205. struct create_weak_node
  206. {
  207. template <typename AllocNode>
  208. static inline BaseNodePtr apply(AllocNode & alloc_node)
  209. {
  210. typedef boost::container::allocator_traits<AllocNode> Al;
  211. typedef typename Al::pointer P;
  212. P p = Al::allocate(alloc_node, 1);
  213. if ( 0 == p )
  214. throw_runtime_error("boost::geometry::index::rtree node creation failed");
  215. scoped_deallocator<AllocNode> deallocator(p, alloc_node);
  216. Al::construct(alloc_node, boost::to_address(p), alloc_node);
  217. deallocator.release();
  218. return p;
  219. }
  220. };
  221. // destroy_node_impl
  222. template <typename Node>
  223. struct destroy_weak_node
  224. {
  225. template <typename AllocNode, typename BaseNodePtr>
  226. static inline void apply(AllocNode & alloc_node, BaseNodePtr n)
  227. {
  228. typedef boost::container::allocator_traits<AllocNode> Al;
  229. typedef typename Al::pointer P;
  230. P p(&static_cast<Node&>(rtree::get<Node>(*n)));
  231. Al::destroy(alloc_node, boost::addressof(*p));
  232. Al::deallocate(alloc_node, p, 1);
  233. }
  234. };
  235. // create_node
  236. template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
  237. struct create_node<
  238. Allocators,
  239. weak_internal_node<Value, Parameters, Box, Allocators, Tag>
  240. >
  241. {
  242. static inline typename Allocators::node_pointer
  243. apply(Allocators & allocators)
  244. {
  245. return create_weak_node<
  246. typename Allocators::node_pointer,
  247. weak_internal_node<Value, Parameters, Box, Allocators, Tag>
  248. >::apply(allocators.internal_node_allocator());
  249. }
  250. };
  251. template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
  252. struct create_node<
  253. Allocators,
  254. weak_leaf<Value, Parameters, Box, Allocators, Tag>
  255. >
  256. {
  257. static inline typename Allocators::node_pointer
  258. apply(Allocators & allocators)
  259. {
  260. return create_weak_node<
  261. typename Allocators::node_pointer,
  262. weak_leaf<Value, Parameters, Box, Allocators, Tag>
  263. >::apply(allocators.leaf_allocator());
  264. }
  265. };
  266. // destroy_node
  267. template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
  268. struct destroy_node<
  269. Allocators,
  270. weak_internal_node<Value, Parameters, Box, Allocators, Tag>
  271. >
  272. {
  273. static inline void apply(Allocators & allocators, typename Allocators::node_pointer n)
  274. {
  275. destroy_weak_node<
  276. weak_internal_node<Value, Parameters, Box, Allocators, Tag>
  277. >::apply(allocators.internal_node_allocator(), n);
  278. }
  279. };
  280. template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
  281. struct destroy_node<
  282. Allocators,
  283. weak_leaf<Value, Parameters, Box, Allocators, Tag>
  284. >
  285. {
  286. static inline void apply(Allocators & allocators, typename Allocators::node_pointer n)
  287. {
  288. destroy_weak_node<
  289. weak_leaf<Value, Parameters, Box, Allocators, Tag>
  290. >::apply(allocators.leaf_allocator(), n);
  291. }
  292. };
  293. }} // namespace detail::rtree
  294. }}} // namespace boost::geometry::index
  295. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_WEAK_DYNAMIC_HPP