]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/geometry/include/boost/geometry/index/detail/rtree/pack_create.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / geometry / include / boost / geometry / index / detail / rtree / pack_create.hpp
CommitLineData
7c673cae
FG
1// Boost.Geometry Index
2//
3// R-tree initial packing
4//
5// Copyright (c) 2011-2015 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
11#ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP
12#define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP
13
14#include <boost/geometry/algorithms/expand.hpp>
15#include <boost/geometry/index/detail/algorithms/bounds.hpp>
16
17#include <boost/geometry/algorithms/detail/expand_by_epsilon.hpp>
18
19namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree {
20
21namespace pack_utils {
22
23template <std::size_t Dimension>
24struct biggest_edge
25{
26 BOOST_STATIC_ASSERT(0 < Dimension);
27 template <typename Box>
28 static inline void apply(Box const& box, typename coordinate_type<Box>::type & length, std::size_t & dim_index)
29 {
30 biggest_edge<Dimension-1>::apply(box, length, dim_index);
31 typename coordinate_type<Box>::type curr
32 = geometry::get<max_corner, Dimension-1>(box) - geometry::get<min_corner, Dimension-1>(box);
33 if ( length < curr )
34 {
35 dim_index = Dimension - 1;
36 length = curr;
37 }
38 }
39};
40
41template <>
42struct biggest_edge<1>
43{
44 template <typename Box>
45 static inline void apply(Box const& box, typename coordinate_type<Box>::type & length, std::size_t & dim_index)
46 {
47 dim_index = 0;
48 length = geometry::get<max_corner, 0>(box) - geometry::get<min_corner, 0>(box);
49 }
50};
51
52template <std::size_t I>
53struct point_entries_comparer
54{
55 template <typename PointEntry>
56 bool operator()(PointEntry const& e1, PointEntry const& e2) const
57 {
58 return geometry::get<I>(e1.first) < geometry::get<I>(e2.first);
59 }
60};
61
62template <std::size_t I, std::size_t Dimension>
63struct nth_element_and_half_boxes
64{
65 template <typename EIt, typename Box>
66 static inline void apply(EIt first, EIt median, EIt last, Box const& box, Box & left, Box & right, std::size_t dim_index)
67 {
68 if ( I == dim_index )
69 {
70 std::nth_element(first, median, last, point_entries_comparer<I>());
71
72 geometry::convert(box, left);
73 geometry::convert(box, right);
74 typename coordinate_type<Box>::type edge_len
75 = geometry::get<max_corner, I>(box) - geometry::get<min_corner, I>(box);
76 typename coordinate_type<Box>::type median
77 = geometry::get<min_corner, I>(box) + edge_len / 2;
78 geometry::set<max_corner, I>(left, median);
79 geometry::set<min_corner, I>(right, median);
80 }
81 else
82 nth_element_and_half_boxes<I+1, Dimension>::apply(first, median, last, box, left, right, dim_index);
83 }
84};
85
86template <std::size_t Dimension>
87struct nth_element_and_half_boxes<Dimension, Dimension>
88{
89 template <typename EIt, typename Box>
90 static inline void apply(EIt , EIt , EIt , Box const& , Box & , Box & , std::size_t ) {}
91};
92
93} // namespace pack_utils
94
95// STR leafs number are calculated as rcount/max
96// and the number of splitting planes for each dimension as (count/max)^(1/dimension)
97// <-> for dimension==2 -> sqrt(count/max)
98//
99// The main flaw of this algorithm is that the resulting tree will have bad structure for:
100// 1. non-uniformly distributed elements
101// Statistic check could be performed, e.g. based on variance of lengths of elements edges for each dimension
102// 2. elements distributed mainly along one axis
103// Calculate bounding box of all elements and then number of dividing planes for a dimension
104// from the length of BB edge for this dimension (more or less assuming that elements are uniformly-distributed squares)
105//
106// Another thing is that the last node may have less elements than Max or even Min.
107// The number of splitting planes must be chosen more carefully than count/max
108//
109// This algorithm is something between STR and TGS
110// it is more similar to the top-down recursive kd-tree creation algorithm
111// using object median split and split axis of greatest BB edge
112// BB is only used as a hint (assuming objects are distributed uniformly)
113//
114// Implemented algorithm guarantees that the number of elements in nodes will be between Min and Max
115// and that nodes are packed as tightly as possible
116// e.g. for 177 values Max = 5 and Min = 2 it will construct the following tree:
117// ROOT 177
118// L1 125 52
119// L2 25 25 25 25 25 25 17 10
120// L3 5x5 5x5 5x5 5x5 5x5 5x5 3x5+2 2x5
121
122template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
123class pack
124{
125 typedef typename rtree::node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type node;
126 typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
127 typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
128
129 typedef typename Allocators::node_pointer node_pointer;
130 typedef rtree::subtree_destroyer<Value, Options, Translator, Box, Allocators> subtree_destroyer;
131 typedef typename Allocators::size_type size_type;
132
133 typedef typename geometry::point_type<Box>::type point_type;
134 typedef typename geometry::coordinate_type<point_type>::type coordinate_type;
135 typedef typename detail::default_content_result<Box>::type content_type;
136 typedef typename Options::parameters_type parameters_type;
137 static const std::size_t dimension = geometry::dimension<point_type>::value;
138
139 typedef typename rtree::container_from_elements_type<
140 typename rtree::elements_type<leaf>::type,
141 std::size_t
142 >::type values_counts_container;
143
144 typedef typename rtree::elements_type<internal_node>::type internal_elements;
145 typedef typename internal_elements::value_type internal_element;
146
147public:
148 // Arbitrary iterators
149 template <typename InIt> inline static
150 node_pointer apply(InIt first, InIt last, size_type & values_count, size_type & leafs_level,
151 parameters_type const& parameters, Translator const& translator, Allocators & allocators)
152 {
153 typedef typename std::iterator_traits<InIt>::difference_type diff_type;
154
155 diff_type diff = std::distance(first, last);
156 if ( diff <= 0 )
157 return node_pointer(0);
158
159 typedef std::pair<point_type, InIt> entry_type;
160 std::vector<entry_type> entries;
161
162 values_count = static_cast<size_type>(diff);
163 entries.reserve(values_count);
164
165 expandable_box<Box> hint_box;
166 for ( ; first != last ; ++first )
167 {
168 // NOTE: support for iterators not returning true references adapted
169 // to Geometry concept and default translator returning true reference
170 // An alternative would be to dereference the iterator and translate
171 // in one expression each time the indexable was needed.
172 typename std::iterator_traits<InIt>::reference in_ref = *first;
173 typename Translator::result_type indexable = translator(in_ref);
174
175 // NOTE: added for consistency with insert()
176 // CONSIDER: alternative - ignore invalid indexable or throw an exception
177 BOOST_GEOMETRY_INDEX_ASSERT(detail::is_valid(indexable), "Indexable is invalid");
178
179 hint_box.expand(indexable);
180
181 point_type pt;
182 geometry::centroid(indexable, pt);
183 entries.push_back(std::make_pair(pt, first));
184 }
185
186 subtree_elements_counts subtree_counts = calculate_subtree_elements_counts(values_count, parameters, leafs_level);
187 internal_element el = per_level(entries.begin(), entries.end(), hint_box.get(), values_count, subtree_counts,
188 parameters, translator, allocators);
189
190 return el.second;
191 }
192
193private:
194 template <typename BoxType>
195 class expandable_box
196 {
197 public:
198 expandable_box()
199 : m_initialized(false)
200 {}
201
202 template <typename Indexable>
203 explicit expandable_box(Indexable const& indexable)
204 : m_initialized(true)
205 {
206 detail::bounds(indexable, m_box);
207 }
208
209 template <typename Indexable>
210 void expand(Indexable const& indexable)
211 {
212 if ( !m_initialized )
213 {
214 // it's guaranteed that the Box will be initialized
215 // only for Points, Boxes and Segments but that's ok
216 // since only those Geometries can be stored
217 detail::bounds(indexable, m_box);
218 m_initialized = true;
219 }
220 else
221 {
222 geometry::expand(m_box, indexable);
223 }
224 }
225
226 void expand_by_epsilon()
227 {
228 geometry::detail::expand_by_epsilon(m_box);
229 }
230
231 BoxType const& get() const
232 {
233 BOOST_GEOMETRY_INDEX_ASSERT(m_initialized, "uninitialized envelope accessed");
234 return m_box;
235 }
236
237 private:
238 bool m_initialized;
239 BoxType m_box;
240 };
241
242 struct subtree_elements_counts
243 {
244 subtree_elements_counts(std::size_t ma, std::size_t mi) : maxc(ma), minc(mi) {}
245 std::size_t maxc;
246 std::size_t minc;
247 };
248
249 template <typename EIt> inline static
250 internal_element per_level(EIt first, EIt last, Box const& hint_box, std::size_t values_count, subtree_elements_counts const& subtree_counts,
251 parameters_type const& parameters, Translator const& translator, Allocators & allocators)
252 {
253 BOOST_GEOMETRY_INDEX_ASSERT(0 < std::distance(first, last) && static_cast<std::size_t>(std::distance(first, last)) == values_count,
254 "unexpected parameters");
255
256 if ( subtree_counts.maxc <= 1 )
257 {
258 // ROOT or LEAF
259 BOOST_GEOMETRY_INDEX_ASSERT(values_count <= parameters.get_max_elements(),
260 "too big number of elements");
261 // if !root check m_parameters.get_min_elements() <= count
262
263 // create new leaf node
264 node_pointer n = rtree::create_node<Allocators, leaf>::apply(allocators); // MAY THROW (A)
265 subtree_destroyer auto_remover(n, allocators);
266 leaf & l = rtree::get<leaf>(*n);
267
268 // reserve space for values
269 rtree::elements(l).reserve(values_count); // MAY THROW (A)
270
271 // calculate values box and copy values
272 // initialize the box explicitly to avoid GCC-4.4 uninitialized variable warnings with O2
273 expandable_box<Box> elements_box(translator(*(first->second)));
274 rtree::elements(l).push_back(*(first->second)); // MAY THROW (A?,C)
275 for ( ++first ; first != last ; ++first )
276 {
277 // NOTE: push_back() must be called at the end in order to support move_iterator.
278 // The iterator is dereferenced 2x (no temporary reference) to support
279 // non-true reference types and move_iterator without boost::forward<>.
280 elements_box.expand(translator(*(first->second)));
281 rtree::elements(l).push_back(*(first->second)); // MAY THROW (A?,C)
282 }
283
284#ifdef BOOST_GEOMETRY_INDEX_EXPERIMENTAL_ENLARGE_BY_EPSILON
285 // Enlarge bounds of a leaf node.
286 // It's because Points and Segments are compared WRT machine epsilon
287 // This ensures that leafs bounds correspond to the stored elements
288 // NOTE: this is done only if the Indexable is a different kind of Geometry
289 // than the bounds (only Box for now). Spatial predicates are checked
290 // the same way for Geometry of the same kind.
291 if ( BOOST_GEOMETRY_CONDITION((
292 ! index::detail::is_bounding_geometry
293 <
294 typename indexable_type<Translator>::type
295 >::value )) )
296 {
297 elements_box.expand_by_epsilon();
298 }
299#endif
300
301 auto_remover.release();
302 return internal_element(elements_box.get(), n);
303 }
304
305 // calculate next max and min subtree counts
306 subtree_elements_counts next_subtree_counts = subtree_counts;
307 next_subtree_counts.maxc /= parameters.get_max_elements();
308 next_subtree_counts.minc /= parameters.get_max_elements();
309
310 // create new internal node
311 node_pointer n = rtree::create_node<Allocators, internal_node>::apply(allocators); // MAY THROW (A)
312 subtree_destroyer auto_remover(n, allocators);
313 internal_node & in = rtree::get<internal_node>(*n);
314
315 // reserve space for values
316 std::size_t nodes_count = calculate_nodes_count(values_count, subtree_counts);
317 rtree::elements(in).reserve(nodes_count); // MAY THROW (A)
318 // calculate values box and copy values
319 expandable_box<Box> elements_box;
320
321 per_level_packets(first, last, hint_box, values_count, subtree_counts, next_subtree_counts,
322 rtree::elements(in), elements_box,
323 parameters, translator, allocators);
324
325 auto_remover.release();
326 return internal_element(elements_box.get(), n);
327 }
328
329 template <typename EIt, typename ExpandableBox> inline static
330 void per_level_packets(EIt first, EIt last, Box const& hint_box,
331 std::size_t values_count,
332 subtree_elements_counts const& subtree_counts,
333 subtree_elements_counts const& next_subtree_counts,
334 internal_elements & elements, ExpandableBox & elements_box,
335 parameters_type const& parameters, Translator const& translator, Allocators & allocators)
336 {
337 BOOST_GEOMETRY_INDEX_ASSERT(0 < std::distance(first, last) && static_cast<std::size_t>(std::distance(first, last)) == values_count,
338 "unexpected parameters");
339
340 BOOST_GEOMETRY_INDEX_ASSERT(subtree_counts.minc <= values_count,
341 "too small number of elements");
342
343 // only one packet
344 if ( values_count <= subtree_counts.maxc )
345 {
346 // the end, move to the next level
347 internal_element el = per_level(first, last, hint_box, values_count, next_subtree_counts,
348 parameters, translator, allocators);
349
350 // in case if push_back() do throw here
351 // and even if this is not probable (previously reserved memory, nonthrowing pairs copy)
352 // this case is also tested by exceptions test.
353 subtree_destroyer auto_remover(el.second, allocators);
354 // this container should have memory allocated, reserve() called outside
355 elements.push_back(el); // MAY THROW (A?,C) - however in normal conditions shouldn't
356 auto_remover.release();
357
358 elements_box.expand(el.first);
359 return;
360 }
361
362 std::size_t median_count = calculate_median_count(values_count, subtree_counts);
363 EIt median = first + median_count;
364
365 coordinate_type greatest_length;
366 std::size_t greatest_dim_index = 0;
367 pack_utils::biggest_edge<dimension>::apply(hint_box, greatest_length, greatest_dim_index);
368 Box left, right;
369 pack_utils::nth_element_and_half_boxes<0, dimension>
370 ::apply(first, median, last, hint_box, left, right, greatest_dim_index);
371
372 per_level_packets(first, median, left,
373 median_count, subtree_counts, next_subtree_counts,
374 elements, elements_box,
375 parameters, translator, allocators);
376 per_level_packets(median, last, right,
377 values_count - median_count, subtree_counts, next_subtree_counts,
378 elements, elements_box,
379 parameters, translator, allocators);
380 }
381
382 inline static
383 subtree_elements_counts calculate_subtree_elements_counts(std::size_t elements_count, parameters_type const& parameters, size_type & leafs_level)
384 {
385 boost::ignore_unused_variable_warning(parameters);
386
387 subtree_elements_counts res(1, 1);
388 leafs_level = 0;
389
390 std::size_t smax = parameters.get_max_elements();
391 for ( ; smax < elements_count ; smax *= parameters.get_max_elements(), ++leafs_level )
392 res.maxc = smax;
393
394 res.minc = parameters.get_min_elements() * (res.maxc / parameters.get_max_elements());
395
396 return res;
397 }
398
399 inline static
400 std::size_t calculate_nodes_count(std::size_t count,
401 subtree_elements_counts const& subtree_counts)
402 {
403 std::size_t n = count / subtree_counts.maxc;
404 std::size_t r = count % subtree_counts.maxc;
405
406 if ( 0 < r && r < subtree_counts.minc )
407 {
408 std::size_t count_minus_min = count - subtree_counts.minc;
409 n = count_minus_min / subtree_counts.maxc;
410 r = count_minus_min % subtree_counts.maxc;
411 ++n;
412 }
413
414 if ( 0 < r )
415 ++n;
416
417 return n;
418 }
419
420 inline static
421 std::size_t calculate_median_count(std::size_t count,
422 subtree_elements_counts const& subtree_counts)
423 {
424 // e.g. for max = 5, min = 2, count = 52, subtree_max = 25, subtree_min = 10
425
426 std::size_t n = count / subtree_counts.maxc; // e.g. 52 / 25 = 2
427 std::size_t r = count % subtree_counts.maxc; // e.g. 52 % 25 = 2
428 std::size_t median_count = (n / 2) * subtree_counts.maxc; // e.g. 2 / 2 * 25 = 25
429
430 if ( 0 != r ) // e.g. 0 != 2
431 {
432 if ( subtree_counts.minc <= r ) // e.g. 10 <= 2 == false
433 {
434 //BOOST_GEOMETRY_INDEX_ASSERT(0 < n, "unexpected value");
435 median_count = ((n+1)/2) * subtree_counts.maxc; // if calculated ((2+1)/2) * 25 which would be ok, but not in all cases
436 }
437 else // r < subtree_counts.second // e.g. 2 < 10 == true
438 {
439 std::size_t count_minus_min = count - subtree_counts.minc; // e.g. 52 - 10 = 42
440 n = count_minus_min / subtree_counts.maxc; // e.g. 42 / 25 = 1
441 r = count_minus_min % subtree_counts.maxc; // e.g. 42 % 25 = 17
442 if ( r == 0 ) // e.g. false
443 {
444 // n can't be equal to 0 because then there wouldn't be any element in the other node
445 //BOOST_GEOMETRY_INDEX_ASSERT(0 < n, "unexpected value");
446 median_count = ((n+1)/2) * subtree_counts.maxc; // if calculated ((1+1)/2) * 25 which would be ok, but not in all cases
447 }
448 else
449 {
450 if ( n == 0 ) // e.g. false
451 median_count = r; // if calculated -> 17 which is wrong!
452 else
453 median_count = ((n+2)/2) * subtree_counts.maxc; // e.g. ((1+2)/2) * 25 = 25
454 }
455 }
456 }
457
458 return median_count;
459 }
460};
461
462}}}}} // namespace boost::geometry::index::detail::rtree
463
464#endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP