annotate DEPENDENCIES/generic/include/boost/geometry/index/detail/rtree/pack_create.hpp @ 133:4acb5d8d80b6 tip

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