1 // Boost.Geometry Index
5 // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
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)
11 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP
12 #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP
14 #include <boost/container/vector.hpp>
15 #include <boost/geometry/index/detail/varray.hpp>
17 #include <boost/geometry/index/detail/rtree/node/concept.hpp>
18 #include <boost/geometry/index/detail/rtree/node/pairs.hpp>
19 #include <boost/geometry/index/detail/rtree/node/node_elements.hpp>
20 #include <boost/geometry/index/detail/rtree/node/scoped_deallocator.hpp>
22 //#include <boost/geometry/index/detail/rtree/node/weak_visitor.hpp>
23 //#include <boost/geometry/index/detail/rtree/node/weak_dynamic.hpp>
24 //#include <boost/geometry/index/detail/rtree/node/weak_static.hpp>
26 #include <boost/geometry/index/detail/rtree/node/variant_visitor.hpp>
27 #include <boost/geometry/index/detail/rtree/node/variant_dynamic.hpp>
28 #include <boost/geometry/index/detail/rtree/node/variant_static.hpp>
30 #include <boost/geometry/index/detail/rtree/node/subtree_destroyer.hpp>
32 #include <boost/geometry/algorithms/expand.hpp>
34 #include <boost/geometry/index/detail/rtree/visitors/is_leaf.hpp>
36 #include <boost/geometry/index/detail/algorithms/bounds.hpp>
37 #include <boost/geometry/index/detail/is_bounding_geometry.hpp>
39 namespace boost { namespace geometry { namespace index {
41 namespace detail { namespace rtree {
45 template <typename Box, typename FwdIter, typename Translator>
46 inline Box elements_box(FwdIter first, FwdIter last, Translator const& tr)
50 // Only here to suppress 'uninitialized local variable used' warning
51 // until the suggestion below is not implemented
52 geometry::assign_inverse(result);
54 //BOOST_GEOMETRY_INDEX_ASSERT(first != last, "non-empty range required");
55 // NOTE: this is not elegant temporary solution,
56 // reference to box could be passed as parameter and bool returned
60 detail::bounds(element_indexable(*first, tr), result);
63 for ( ; first != last ; ++first )
64 geometry::expand(result, element_indexable(*first, tr));
69 // Enlarge bounds of a leaf node WRT epsilon if needed.
70 // It's because Points and Segments are compared WRT machine epsilon.
71 // This ensures that leafs bounds correspond to the stored elements.
72 // NOTE: this is done only if the Indexable is not a Box
73 // in the future don't do it also for NSphere
74 template <typename Box, typename FwdIter, typename Translator>
75 inline Box values_box(FwdIter first, FwdIter last, Translator const& tr)
77 typedef typename std::iterator_traits<FwdIter>::value_type element_type;
78 BOOST_MPL_ASSERT_MSG((is_leaf_element<element_type>::value),
79 SHOULD_BE_CALLED_ONLY_FOR_LEAF_ELEMENTS,
82 Box result = elements_box<Box>(first, last, tr);
84 #ifdef BOOST_GEOMETRY_INDEX_EXPERIMENTAL_ENLARGE_BY_EPSILON
85 if (BOOST_GEOMETRY_CONDITION((
86 ! is_bounding_geometry
88 typename indexable_type<Translator>::type
91 geometry::detail::expand_by_epsilon(result);
98 // destroys subtree if the element is internal node's element
99 template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
100 struct destroy_element
102 typedef typename Options::parameters_type parameters_type;
104 typedef typename rtree::internal_node<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
105 typedef typename rtree::leaf<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
107 typedef rtree::subtree_destroyer<Value, Options, Translator, Box, Allocators> subtree_destroyer;
109 inline static void apply(typename internal_node::elements_type::value_type & element, Allocators & allocators)
111 subtree_destroyer dummy(element.second, allocators);
115 inline static void apply(typename leaf::elements_type::value_type &, Allocators &) {}
118 // destroys stored subtrees if internal node's elements are passed
119 template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
120 struct destroy_elements
122 template <typename Range>
123 inline static void apply(Range & elements, Allocators & allocators)
125 apply(boost::begin(elements), boost::end(elements), allocators);
128 template <typename It>
129 inline static void apply(It first, It last, Allocators & allocators)
131 typedef boost::mpl::bool_<
133 Value, typename std::iterator_traits<It>::value_type
135 > is_range_of_values;
137 apply_dispatch(first, last, allocators, is_range_of_values());
141 template <typename It>
142 inline static void apply_dispatch(It first, It last, Allocators & allocators,
143 boost::mpl::bool_<false> const& /*is_range_of_values*/)
145 typedef rtree::subtree_destroyer<Value, Options, Translator, Box, Allocators> subtree_destroyer;
147 for ( ; first != last ; ++first )
149 subtree_destroyer dummy(first->second, allocators);
154 template <typename It>
155 inline static void apply_dispatch(It /*first*/, It /*last*/, Allocators & /*allocators*/,
156 boost::mpl::bool_<true> const& /*is_range_of_values*/)
160 // clears node, deletes all subtrees stored in node
161 template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
164 typedef typename Options::parameters_type parameters_type;
166 typedef typename rtree::node<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type node;
167 typedef typename rtree::internal_node<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
168 typedef typename rtree::leaf<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
170 inline static void apply(node & node, Allocators & allocators)
172 rtree::visitors::is_leaf<Value, Options, Box, Allocators> ilv;
173 rtree::apply_visitor(ilv, node);
176 apply(rtree::get<leaf>(node), allocators);
180 apply(rtree::get<internal_node>(node), allocators);
184 inline static void apply(internal_node & internal_node, Allocators & allocators)
186 destroy_elements<Value, Options, Translator, Box, Allocators>::apply(rtree::elements(internal_node), allocators);
187 rtree::elements(internal_node).clear();
190 inline static void apply(leaf & leaf, Allocators &)
192 rtree::elements(leaf).clear();
196 template <typename Container, typename Iterator>
197 void move_from_back(Container & container, Iterator it)
199 BOOST_GEOMETRY_INDEX_ASSERT(!container.empty(), "cannot copy from empty container");
200 Iterator back_it = container.end();
204 *it = boost::move(*back_it); // MAY THROW (copy)
208 }} // namespace detail::rtree
210 }}} // namespace boost::geometry::index
212 #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP