// Boost.Geometry Index // // R-tree nodes based on run-time polymorphism, storing std::vectors // // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland. // // Use, modification and distribution is subject to the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_DEFAULT_HPP #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_DEFAULT_HPP namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree { template struct dynamic_internal_node : public dynamic_node { typedef typename Allocators::leaf_allocator_type::template rebind< rtree::ptr_pair >::other elements_allocator_type; typedef boost::container::vector< rtree::ptr_pair, elements_allocator_type > elements_type; template inline dynamic_internal_node(Al const& al) : elements(al) {} void apply_visitor(dynamic_visitor & v) { v(*this); } void apply_visitor(dynamic_visitor & v) const { v(*this); } elements_type elements; }; template struct dynamic_leaf : public dynamic_node { typedef typename Allocators::leaf_allocator_type::template rebind< Value >::other elements_allocator_type; typedef boost::container::vector< Value, elements_allocator_type > elements_type; template inline dynamic_leaf(Al const& al) : elements(al) {} void apply_visitor(dynamic_visitor & v) { v(*this); } void apply_visitor(dynamic_visitor & v) const { v(*this); } elements_type elements; }; // nodes traits template struct node { typedef dynamic_node type; }; template struct internal_node { typedef dynamic_internal_node type; }; template struct leaf { typedef dynamic_leaf type; }; // visitor traits template struct visitor { typedef dynamic_visitor type; }; // element's indexable type template struct element_indexable_type { typedef typename indexable_type::type type; }; template struct element_indexable_type< rtree::ptr_pair, Translator > { typedef First type; }; // element's indexable getter template typename result_type::type element_indexable(Element const& el, Translator const& tr) { return tr(el); } template First const& element_indexable(rtree::ptr_pair const& el, Translator const& /*tr*/) { return el.first; } // nodes elements template struct elements_type { typedef typename Node::elements_type type; }; template inline typename elements_type::type & elements(Node & n) { return n.elements; } template inline typename elements_type::type const& elements(Node const& n) { return n.elements; } // elements derived type template struct container_from_elements_type { typedef boost::container::vector type; }; // allocators template class allocators : public Allocator::template rebind< typename internal_node, node_d_mem_dynamic_tag>::type >::other , public Allocator::template rebind< typename leaf, node_d_mem_dynamic_tag>::type >::other { typedef typename Allocator::template rebind< Value >::other value_allocator_type; public: typedef Allocator allocator_type; typedef Value value_type; typedef typename value_allocator_type::reference reference; typedef typename value_allocator_type::const_reference const_reference; typedef typename value_allocator_type::size_type size_type; typedef typename value_allocator_type::difference_type difference_type; typedef typename value_allocator_type::pointer pointer; typedef typename value_allocator_type::const_pointer const_pointer; typedef typename Allocator::template rebind< typename node::type >::other::pointer node_pointer; // typedef typename Allocator::template rebind< // typename internal_node::type // >::other::pointer internal_node_pointer; typedef typename Allocator::template rebind< typename internal_node::type >::other internal_node_allocator_type; typedef typename Allocator::template rebind< typename leaf::type >::other leaf_allocator_type; inline allocators() : internal_node_allocator_type() , leaf_allocator_type() {} template inline explicit allocators(Alloc const& alloc) : internal_node_allocator_type(alloc) , leaf_allocator_type(alloc) {} inline allocators(BOOST_FWD_REF(allocators) a) : internal_node_allocator_type(boost::move(a.internal_node_allocator())) , leaf_allocator_type(boost::move(a.leaf_allocator())) {} inline allocators & operator=(BOOST_FWD_REF(allocators) a) { internal_node_allocator() = ::boost::move(a.internal_node_allocator()); leaf_allocator() = ::boost::move(a.leaf_allocator()); return *this; } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES inline allocators & operator=(allocators const& a) { internal_node_allocator() = a.internal_node_allocator(); leaf_allocator() = a.leaf_allocator(); return *this; } #endif void swap(allocators & a) { boost::swap(internal_node_allocator(), a.internal_node_allocator()); boost::swap(leaf_allocator(), a.leaf_allocator()); } bool operator==(allocators const& a) const { return leaf_allocator() == a.leaf_allocator(); } template bool operator==(Alloc const& a) const { return leaf_allocator() == leaf_allocator_type(a); } Allocator allocator() const { return Allocator(leaf_allocator()); } internal_node_allocator_type & internal_node_allocator() { return *this; } internal_node_allocator_type const& internal_node_allocator() const { return *this; } leaf_allocator_type & leaf_allocator() { return *this; } leaf_allocator_type const& leaf_allocator() const { return *this; } }; // create_node_impl template struct create_dynamic_node { template static inline BaseNodePtr apply(AllocNode & alloc_node) { typedef boost::container::allocator_traits Al; typedef typename Al::pointer P; P p = Al::allocate(alloc_node, 1); if ( 0 == p ) throw_runtime_error("boost::geometry::index::rtree node creation failed"); auto_deallocator deallocator(alloc_node, p); Al::construct(alloc_node, boost::addressof(*p), alloc_node); deallocator.release(); return p; } }; // destroy_node_impl template struct destroy_dynamic_node { template static inline void apply(AllocNode & alloc_node, BaseNodePtr n) { typedef boost::container::allocator_traits Al; typedef typename Al::pointer P; P p(&static_cast(rtree::get(*n))); Al::destroy(alloc_node, boost::addressof(*p)); Al::deallocate(alloc_node, p, 1); } }; // create_node template struct create_node< Allocators, dynamic_internal_node > { static inline typename Allocators::node_pointer apply(Allocators & allocators) { return create_dynamic_node< typename Allocators::node_pointer, dynamic_internal_node >::apply(allocators.internal_node_allocator()); } }; template struct create_node< Allocators, dynamic_leaf > { static inline typename Allocators::node_pointer apply(Allocators & allocators) { return create_dynamic_node< typename Allocators::node_pointer, dynamic_leaf >::apply(allocators.leaf_allocator()); } }; // destroy_node template struct destroy_node< Allocators, dynamic_internal_node > { static inline void apply(Allocators & allocators, typename Allocators::node_pointer n) { destroy_dynamic_node< dynamic_internal_node >::apply(allocators.internal_node_allocator(), n); } }; template struct destroy_node< Allocators, dynamic_leaf > { static inline void apply(Allocators & allocators, typename Allocators::node_pointer n) { destroy_dynamic_node< dynamic_leaf >::apply(allocators.leaf_allocator(), n); } }; }} // namespace detail::rtree }}} // namespace boost::geometry::index #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_DEFAULT_HPP