1 // Boost.Geometry Index
5 // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
7 // This file was modified by Oracle on 2019-2020.
8 // Modifications copyright (c) 2019-2020 Oracle and/or its affiliates.
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
15 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP
16 #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP
18 #include <type_traits>
20 #include <boost/container/vector.hpp>
22 #include <boost/geometry/core/static_assert.hpp>
24 #include <boost/geometry/index/detail/varray.hpp>
26 #include <boost/geometry/index/detail/rtree/node/concept.hpp>
27 #include <boost/geometry/index/detail/rtree/node/pairs.hpp>
28 #include <boost/geometry/index/detail/rtree/node/node_elements.hpp>
29 #include <boost/geometry/index/detail/rtree/node/scoped_deallocator.hpp>
31 //#include <boost/geometry/index/detail/rtree/node/weak_visitor.hpp>
32 //#include <boost/geometry/index/detail/rtree/node/weak_dynamic.hpp>
33 //#include <boost/geometry/index/detail/rtree/node/weak_static.hpp>
35 #include <boost/geometry/index/detail/rtree/node/variant_visitor.hpp>
36 #include <boost/geometry/index/detail/rtree/node/variant_dynamic.hpp>
37 #include <boost/geometry/index/detail/rtree/node/variant_static.hpp>
39 #include <boost/geometry/algorithms/expand.hpp>
41 #include <boost/geometry/index/detail/rtree/visitors/destroy.hpp>
42 #include <boost/geometry/index/detail/rtree/visitors/is_leaf.hpp>
44 #include <boost/geometry/index/detail/algorithms/bounds.hpp>
45 #include <boost/geometry/index/detail/is_bounding_geometry.hpp>
47 namespace boost { namespace geometry { namespace index {
49 namespace detail { namespace rtree {
53 template <typename Box, typename FwdIter, typename Translator, typename Strategy>
54 inline Box elements_box(FwdIter first, FwdIter last, Translator const& tr,
55 Strategy const& strategy)
59 // Only here to suppress 'uninitialized local variable used' warning
60 // until the suggestion below is not implemented
61 geometry::assign_inverse(result);
63 //BOOST_GEOMETRY_INDEX_ASSERT(first != last, "non-empty range required");
64 // NOTE: this is not elegant temporary solution,
65 // reference to box could be passed as parameter and bool returned
69 detail::bounds(element_indexable(*first, tr), result, strategy);
72 for ( ; first != last ; ++first )
73 detail::expand(result, element_indexable(*first, tr), strategy);
78 // Enlarge bounds of a leaf node WRT epsilon if needed.
79 // It's because Points and Segments are compared WRT machine epsilon.
80 // This ensures that leafs bounds correspond to the stored elements.
81 // NOTE: this is done only if the Indexable is not a Box
82 // in the future don't do it also for NSphere
83 template <typename Box, typename FwdIter, typename Translator, typename Strategy>
84 inline Box values_box(FwdIter first, FwdIter last, Translator const& tr,
85 Strategy const& strategy)
87 typedef typename std::iterator_traits<FwdIter>::value_type element_type;
88 BOOST_GEOMETRY_STATIC_ASSERT((is_leaf_element<element_type>::value),
89 "This function should be called only for elements of leaf nodes.",
92 Box result = elements_box<Box>(first, last, tr, strategy);
94 #ifdef BOOST_GEOMETRY_INDEX_EXPERIMENTAL_ENLARGE_BY_EPSILON
95 if (BOOST_GEOMETRY_CONDITION((
96 ! is_bounding_geometry
98 typename indexable_type<Translator>::type
101 geometry::detail::expand_by_epsilon(result);
108 // destroys subtree if the element is internal node's element
109 template <typename MembersHolder>
110 struct destroy_element
112 typedef typename MembersHolder::parameters_type parameters_type;
113 typedef typename MembersHolder::allocators_type allocators_type;
115 typedef typename MembersHolder::internal_node internal_node;
116 typedef typename MembersHolder::leaf leaf;
118 inline static void apply(typename internal_node::elements_type::value_type & element,
119 allocators_type & allocators)
121 detail::rtree::visitors::destroy<MembersHolder>::apply(element.second, allocators);
126 inline static void apply(typename leaf::elements_type::value_type &,
131 // destroys stored subtrees if internal node's elements are passed
132 template <typename MembersHolder>
133 struct destroy_elements
135 typedef typename MembersHolder::value_type value_type;
136 typedef typename MembersHolder::allocators_type allocators_type;
138 template <typename Range>
139 inline static void apply(Range & elements, allocators_type & allocators)
141 apply(boost::begin(elements), boost::end(elements), allocators);
144 template <typename It>
145 inline static void apply(It first, It last, allocators_type & allocators)
149 value_type, typename std::iterator_traits<It>::value_type
150 > is_range_of_values;
152 apply_dispatch(first, last, allocators, is_range_of_values());
156 template <typename It>
157 inline static void apply_dispatch(It first, It last, allocators_type & allocators,
158 std::false_type /*is_range_of_values*/)
160 for ( ; first != last ; ++first )
162 detail::rtree::visitors::destroy<MembersHolder>::apply(first->second, allocators);
168 template <typename It>
169 inline static void apply_dispatch(It /*first*/, It /*last*/, allocators_type & /*allocators*/,
170 std::true_type /*is_range_of_values*/)
174 // clears node, deletes all subtrees stored in node
176 template <typename MembersHolder>
179 typedef typename MembersHolder::parameters_type parameters_type;
180 typedef typename MembersHolder::allocators_type allocators_type;
182 typedef typename MembersHolder::node node;
183 typedef typename MembersHolder::internal_node internal_node;
184 typedef typename MembersHolder::leaf leaf;
186 inline static void apply(node & node, allocators_type & allocators)
188 rtree::visitors::is_leaf<MembersHolder> ilv;
189 rtree::apply_visitor(ilv, node);
192 apply(rtree::get<leaf>(node), allocators);
196 apply(rtree::get<internal_node>(node), allocators);
200 inline static void apply(internal_node & internal_node, allocators_type & allocators)
202 destroy_elements<MembersHolder>::apply(rtree::elements(internal_node), allocators);
203 rtree::elements(internal_node).clear();
206 inline static void apply(leaf & leaf, allocators_type &)
208 rtree::elements(leaf).clear();
213 template <typename Container, typename Iterator>
214 void move_from_back(Container & container, Iterator it)
216 BOOST_GEOMETRY_INDEX_ASSERT(!container.empty(), "cannot copy from empty container");
217 Iterator back_it = container.end();
221 *it = boost::move(*back_it); // MAY THROW (copy)
225 }} // namespace detail::rtree
227 }}} // namespace boost::geometry::index
229 #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_NODE_HPP