variant_dynamic.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Boost.Geometry Index
  2. //
  3. // R-tree nodes based on Boost.Variant, 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_VARIANT_DYNAMIC_HPP
  16. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_VARIANT_DYNAMIC_HPP
  17. #include <utility>
  18. #include <boost/container/allocator_traits.hpp>
  19. #include <boost/container/vector.hpp>
  20. #include <boost/core/invoke_swap.hpp>
  21. #include <boost/core/pointer_traits.hpp>
  22. #include <boost/variant/static_visitor.hpp>
  23. #include <boost/variant/variant.hpp>
  24. #include <boost/geometry/index/detail/rtree/options.hpp>
  25. #include <boost/geometry/index/detail/rtree/node/concept.hpp>
  26. #include <boost/geometry/index/detail/rtree/node/pairs.hpp>
  27. #include <boost/geometry/index/detail/rtree/node/scoped_deallocator.hpp>
  28. namespace boost { namespace geometry { namespace index {
  29. namespace detail { namespace rtree {
  30. // nodes default types
  31. template <typename Value, typename Parameters, typename Box, typename Allocators, typename Tag>
  32. struct variant_internal_node
  33. {
  34. typedef rtree::ptr_pair<Box, typename Allocators::node_pointer> element_type;
  35. typedef typename boost::container::allocator_traits
  36. <
  37. typename Allocators::node_allocator_type
  38. >::template rebind_alloc<element_type> allocator_type;
  39. typedef boost::container::vector<element_type, allocator_type> elements_type;
  40. template <typename Al>
  41. inline variant_internal_node(Al const& al)
  42. : elements(allocator_type(al))
  43. {}
  44. elements_type elements;
  45. };
  46. template <typename Value, typename Parameters, typename Box, typename Allocators, typename Tag>
  47. struct variant_leaf
  48. {
  49. typedef typename boost::container::allocator_traits
  50. <
  51. typename Allocators::node_allocator_type
  52. >::template rebind_alloc<Value> allocator_type;
  53. typedef boost::container::vector<Value, allocator_type> elements_type;
  54. template <typename Al>
  55. inline variant_leaf(Al const& al)
  56. : elements(allocator_type(al))
  57. {}
  58. elements_type elements;
  59. };
  60. // nodes traits
  61. template <typename Value, typename Parameters, typename Box, typename Allocators>
  62. struct node<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>
  63. {
  64. typedef boost::variant<
  65. variant_leaf<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>,
  66. variant_internal_node<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>
  67. > type;
  68. };
  69. template <typename Value, typename Parameters, typename Box, typename Allocators>
  70. struct internal_node<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>
  71. {
  72. typedef variant_internal_node<Value, Parameters, Box, Allocators, node_variant_dynamic_tag> type;
  73. };
  74. template <typename Value, typename Parameters, typename Box, typename Allocators>
  75. struct leaf<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>
  76. {
  77. typedef variant_leaf<Value, Parameters, Box, Allocators, node_variant_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_variant_dynamic_tag, IsVisitableConst>
  82. {
  83. typedef static_visitor<> type;
  84. };
  85. // allocators
  86. template <typename Allocator, typename Value, typename Parameters, typename Box, typename Tag>
  87. struct node_alloc
  88. {
  89. typedef typename 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. typedef boost::container::allocator_traits<type> traits;
  100. };
  101. template <typename Allocator, typename Value, typename Parameters, typename Box>
  102. class allocators<Allocator, Value, Parameters, Box, node_variant_dynamic_tag>
  103. : public detail::rtree::node_alloc
  104. <
  105. Allocator, Value, Parameters, Box, node_variant_dynamic_tag
  106. >::type
  107. {
  108. typedef detail::rtree::node_alloc
  109. <
  110. Allocator, Value, Parameters, Box, node_variant_dynamic_tag
  111. > node_alloc;
  112. public:
  113. typedef typename node_alloc::type node_allocator_type;
  114. typedef typename node_alloc::traits::pointer node_pointer;
  115. private:
  116. typedef typename boost::container::allocator_traits
  117. <
  118. node_allocator_type // node_allocator_type for consistency with variant_leaf
  119. >::template rebind_alloc<Value> value_allocator_type;
  120. typedef boost::container::allocator_traits<value_allocator_type> value_allocator_traits;
  121. public:
  122. typedef Allocator allocator_type;
  123. typedef Value value_type;
  124. typedef typename value_allocator_traits::reference reference;
  125. typedef typename value_allocator_traits::const_reference const_reference;
  126. typedef typename value_allocator_traits::size_type size_type;
  127. typedef typename value_allocator_traits::difference_type difference_type;
  128. typedef typename value_allocator_traits::pointer pointer;
  129. typedef typename value_allocator_traits::const_pointer const_pointer;
  130. inline allocators()
  131. : node_allocator_type()
  132. {}
  133. template <typename Alloc>
  134. inline explicit allocators(Alloc const& alloc)
  135. : node_allocator_type(alloc)
  136. {}
  137. inline allocators(allocators&& a)
  138. : node_allocator_type(std::move(a.node_allocator()))
  139. {}
  140. inline allocators & operator=(allocators&& a)
  141. {
  142. node_allocator() = std::move(a.node_allocator());
  143. return *this;
  144. }
  145. inline allocators & operator=(allocators const& a)
  146. {
  147. node_allocator() = a.node_allocator();
  148. return *this;
  149. }
  150. void swap(allocators & a)
  151. {
  152. boost::core::invoke_swap(node_allocator(), a.node_allocator());
  153. }
  154. bool operator==(allocators const& a) const { return node_allocator() == a.node_allocator(); }
  155. template <typename Alloc>
  156. bool operator==(Alloc const& a) const { return node_allocator() == node_allocator_type(a); }
  157. Allocator allocator() const { return Allocator(node_allocator()); }
  158. node_allocator_type & node_allocator() { return *this; }
  159. node_allocator_type const& node_allocator() const { return *this; }
  160. };
  161. // create_node_variant
  162. template <typename VariantPtr, typename Node>
  163. struct create_variant_node
  164. {
  165. template <typename AllocNode>
  166. static inline VariantPtr apply(AllocNode & alloc_node)
  167. {
  168. typedef boost::container::allocator_traits<AllocNode> Al;
  169. typedef typename Al::pointer P;
  170. P p = Al::allocate(alloc_node, 1);
  171. if ( 0 == p )
  172. throw_runtime_error("boost::geometry::index::rtree node creation failed");
  173. scoped_deallocator<AllocNode> deallocator(p, alloc_node);
  174. Al::construct(alloc_node, boost::to_address(p), Node(alloc_node)); // implicit cast to Variant
  175. deallocator.release();
  176. return p;
  177. }
  178. };
  179. // destroy_node_variant
  180. template <typename Node>
  181. struct destroy_variant_node
  182. {
  183. template <typename AllocNode, typename VariantPtr>
  184. static inline void apply(AllocNode & alloc_node, VariantPtr n)
  185. {
  186. typedef boost::container::allocator_traits<AllocNode> Al;
  187. Al::destroy(alloc_node, boost::addressof(*n));
  188. Al::deallocate(alloc_node, n, 1);
  189. }
  190. };
  191. // create_node
  192. template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
  193. struct create_node<
  194. Allocators,
  195. variant_internal_node<Value, Parameters, Box, Allocators, Tag>
  196. >
  197. {
  198. static inline typename Allocators::node_pointer
  199. apply(Allocators & allocators)
  200. {
  201. return create_variant_node<
  202. typename Allocators::node_pointer,
  203. variant_internal_node<Value, Parameters, Box, Allocators, Tag>
  204. >::apply(allocators.node_allocator());
  205. }
  206. };
  207. template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
  208. struct create_node<
  209. Allocators,
  210. variant_leaf<Value, Parameters, Box, Allocators, Tag>
  211. >
  212. {
  213. static inline typename Allocators::node_pointer
  214. apply(Allocators & allocators)
  215. {
  216. return create_variant_node<
  217. typename Allocators::node_pointer,
  218. variant_leaf<Value, Parameters, Box, Allocators, Tag>
  219. >::apply(allocators.node_allocator());
  220. }
  221. };
  222. // destroy_node
  223. template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
  224. struct destroy_node<
  225. Allocators,
  226. variant_internal_node<Value, Parameters, Box, Allocators, Tag>
  227. >
  228. {
  229. static inline void apply(Allocators & allocators, typename Allocators::node_pointer n)
  230. {
  231. destroy_variant_node<
  232. variant_internal_node<Value, Parameters, Box, Allocators, Tag>
  233. >::apply(allocators.node_allocator(), n);
  234. }
  235. };
  236. template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
  237. struct destroy_node<
  238. Allocators,
  239. variant_leaf<Value, Parameters, Box, Allocators, Tag>
  240. >
  241. {
  242. static inline void apply(Allocators & allocators, typename Allocators::node_pointer n)
  243. {
  244. destroy_variant_node<
  245. variant_leaf<Value, Parameters, Box, Allocators, Tag>
  246. >::apply(allocators.node_allocator(), n);
  247. }
  248. };
  249. }} // namespace detail::rtree
  250. }}} // namespace boost::geometry::index
  251. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_VARIANT_DYNAMIC_HPP