variant_dynamic.hpp 9.0 KB

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