]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry Index |
2 | // | |
3 | // R-tree implementation | |
4 | // | |
5 | // Copyright (c) 2008 Federico J. Fernandez. | |
92f5a8d4 TL |
6 | // Copyright (c) 2011-2019 Adam Wulkiewicz, Lodz, Poland. |
7 | // | |
8 | // This file was modified by Oracle on 2019. | |
9 | // Modifications copyright (c) 2019 Oracle and/or its affiliates. | |
10 | // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle | |
7c673cae FG |
11 | // |
12 | // Use, modification and distribution is subject to the Boost Software License, | |
13 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
14 | // http://www.boost.org/LICENSE_1_0.txt) | |
15 | ||
16 | #ifndef BOOST_GEOMETRY_INDEX_RTREE_HPP | |
17 | #define BOOST_GEOMETRY_INDEX_RTREE_HPP | |
18 | ||
19 | // STD | |
20 | #include <algorithm> | |
21 | ||
22 | // Boost | |
11fdf7f2 | 23 | #include <boost/container/new_allocator.hpp> |
7c673cae | 24 | #include <boost/move/move.hpp> |
11fdf7f2 | 25 | #include <boost/tuple/tuple.hpp> |
7c673cae FG |
26 | |
27 | // Boost.Geometry | |
28 | #include <boost/geometry/algorithms/detail/comparable_distance/interface.hpp> | |
b32b8144 FG |
29 | #include <boost/geometry/algorithms/detail/covered_by/interface.hpp> |
30 | #include <boost/geometry/algorithms/detail/disjoint/interface.hpp> | |
31 | #include <boost/geometry/algorithms/detail/equals/interface.hpp> | |
32 | #include <boost/geometry/algorithms/detail/intersects/interface.hpp> | |
33 | #include <boost/geometry/algorithms/detail/overlaps/interface.hpp> | |
34 | #include <boost/geometry/algorithms/detail/touches/interface.hpp> | |
35 | #include <boost/geometry/algorithms/detail/within/interface.hpp> | |
7c673cae | 36 | #include <boost/geometry/algorithms/centroid.hpp> |
7c673cae FG |
37 | |
38 | #include <boost/geometry/geometries/point.hpp> | |
39 | #include <boost/geometry/geometries/box.hpp> | |
40 | ||
7c673cae FG |
41 | // Boost.Geometry.Index |
42 | #include <boost/geometry/index/detail/config_begin.hpp> | |
43 | ||
44 | #include <boost/geometry/index/detail/assert.hpp> | |
45 | #include <boost/geometry/index/detail/exception.hpp> | |
46 | ||
47 | #include <boost/geometry/index/detail/rtree/options.hpp> | |
48 | ||
49 | #include <boost/geometry/index/indexable.hpp> | |
50 | #include <boost/geometry/index/equal_to.hpp> | |
51 | ||
52 | #include <boost/geometry/index/detail/translator.hpp> | |
53 | ||
54 | #include <boost/geometry/index/predicates.hpp> | |
55 | #include <boost/geometry/index/distance_predicates.hpp> | |
56 | #include <boost/geometry/index/detail/rtree/adaptors.hpp> | |
57 | ||
58 | #include <boost/geometry/index/detail/meta.hpp> | |
59 | #include <boost/geometry/index/detail/utilities.hpp> | |
60 | #include <boost/geometry/index/detail/rtree/node/node.hpp> | |
61 | ||
62 | #include <boost/geometry/index/detail/algorithms/is_valid.hpp> | |
63 | ||
64 | #include <boost/geometry/index/detail/rtree/visitors/insert.hpp> | |
65 | #include <boost/geometry/index/detail/rtree/visitors/iterator.hpp> | |
66 | #include <boost/geometry/index/detail/rtree/visitors/remove.hpp> | |
67 | #include <boost/geometry/index/detail/rtree/visitors/copy.hpp> | |
68 | #include <boost/geometry/index/detail/rtree/visitors/destroy.hpp> | |
69 | #include <boost/geometry/index/detail/rtree/visitors/spatial_query.hpp> | |
70 | #include <boost/geometry/index/detail/rtree/visitors/distance_query.hpp> | |
71 | #include <boost/geometry/index/detail/rtree/visitors/count.hpp> | |
72 | #include <boost/geometry/index/detail/rtree/visitors/children_box.hpp> | |
73 | ||
74 | #include <boost/geometry/index/detail/rtree/linear/linear.hpp> | |
75 | #include <boost/geometry/index/detail/rtree/quadratic/quadratic.hpp> | |
76 | #include <boost/geometry/index/detail/rtree/rstar/rstar.hpp> | |
77 | //#include <boost/geometry/extensions/index/detail/rtree/kmeans/kmeans.hpp> | |
78 | ||
79 | #include <boost/geometry/index/detail/rtree/pack_create.hpp> | |
80 | ||
81 | #include <boost/geometry/index/inserter.hpp> | |
82 | ||
83 | #include <boost/geometry/index/detail/rtree/utilities/view.hpp> | |
84 | ||
85 | #include <boost/geometry/index/detail/rtree/iterators.hpp> | |
86 | #include <boost/geometry/index/detail/rtree/query_iterators.hpp> | |
87 | ||
88 | #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL | |
89 | // serialization | |
90 | #include <boost/geometry/index/detail/serialization.hpp> | |
91 | #endif | |
92 | ||
93 | // TODO change the name to bounding_tree | |
94 | ||
95 | /*! | |
96 | \defgroup rtree_functions R-tree free functions (boost::geometry::index::) | |
97 | */ | |
98 | ||
99 | namespace boost { namespace geometry { namespace index { | |
100 | ||
101 | /*! | |
102 | \brief The R-tree spatial index. | |
103 | ||
104 | This is self-balancing spatial index capable to store various types of Values | |
105 | and balancing algorithms. | |
106 | ||
107 | \par Parameters | |
108 | The user must pass a type defining the Parameters which will | |
109 | be used in rtree creation process. This type is used e.g. to specify balancing | |
110 | algorithm with specific parameters like min and max number of elements in node. | |
111 | ||
112 | \par | |
113 | Predefined algorithms with compile-time parameters are: | |
114 | \li <tt>boost::geometry::index::linear</tt>, | |
115 | \li <tt>boost::geometry::index::quadratic</tt>, | |
116 | \li <tt>boost::geometry::index::rstar</tt>. | |
117 | ||
118 | \par | |
119 | Predefined algorithms with run-time parameters are: | |
120 | \li \c boost::geometry::index::dynamic_linear, | |
121 | \li \c boost::geometry::index::dynamic_quadratic, | |
122 | \li \c boost::geometry::index::dynamic_rstar. | |
123 | ||
124 | \par IndexableGetter | |
125 | The object of IndexableGetter type translates from Value to Indexable each time | |
126 | r-tree requires it. This means that this operation is done for each Value | |
127 | access. Therefore the IndexableGetter should return the Indexable by | |
128 | a reference type. The Indexable should not be calculated since it could harm | |
129 | the performance. The default IndexableGetter can translate all types adapted | |
130 | to Point, Box or Segment concepts (called Indexables). Furthermore, it can | |
131 | handle <tt>std::pair<Indexable, T></tt>, <tt>boost::tuple<Indexable, ...></tt> | |
132 | and <tt>std::tuple<Indexable, ...></tt> when possible. For example, for Value | |
133 | of type <tt>std::pair<Box, int></tt>, the default IndexableGetter translates | |
134 | from <tt>std::pair<Box, int> const&</tt> to <tt>Box const&</tt>. | |
135 | ||
136 | \par EqualTo | |
137 | The object of EqualTo type compares Values and returns <tt>true</tt> if they | |
138 | are equal. It's similar to <tt>std::equal_to<></tt>. The default EqualTo | |
139 | returns the result of <tt>boost::geometry::equals()</tt> for types adapted to | |
140 | some Geometry concept defined in Boost.Geometry and the result of | |
141 | <tt>operator==</tt> for other types. Components of Pairs and Tuples are | |
142 | compared left-to-right. | |
143 | ||
144 | \tparam Value The type of objects stored in the container. | |
145 | \tparam Parameters Compile-time parameters. | |
146 | \tparam IndexableGetter The function object extracting Indexable from Value. | |
147 | \tparam EqualTo The function object comparing objects of type Value. | |
148 | \tparam Allocator The allocator used to allocate/deallocate memory, | |
149 | construct/destroy nodes and Values. | |
150 | */ | |
b32b8144 FG |
151 | template |
152 | < | |
7c673cae FG |
153 | typename Value, |
154 | typename Parameters, | |
155 | typename IndexableGetter = index::indexable<Value>, | |
156 | typename EqualTo = index::equal_to<Value>, | |
11fdf7f2 | 157 | typename Allocator = boost::container::new_allocator<Value> |
7c673cae FG |
158 | > |
159 | class rtree | |
160 | { | |
161 | BOOST_COPYABLE_AND_MOVABLE(rtree) | |
162 | ||
163 | public: | |
164 | /*! \brief The type of Value stored in the container. */ | |
165 | typedef Value value_type; | |
166 | /*! \brief R-tree parameters type. */ | |
167 | typedef Parameters parameters_type; | |
168 | /*! \brief The function object extracting Indexable from Value. */ | |
169 | typedef IndexableGetter indexable_getter; | |
170 | /*! \brief The function object comparing objects of type Value. */ | |
171 | typedef EqualTo value_equal; | |
172 | /*! \brief The type of allocator used by the container. */ | |
173 | typedef Allocator allocator_type; | |
174 | ||
175 | // TODO: SHOULD THIS TYPE BE REMOVED? | |
176 | /*! \brief The Indexable type to which Value is translated. */ | |
177 | typedef typename index::detail::indexable_type< | |
178 | detail::translator<IndexableGetter, EqualTo> | |
179 | >::type indexable_type; | |
180 | ||
181 | /*! \brief The Box type used by the R-tree. */ | |
182 | typedef geometry::model::box< | |
183 | geometry::model::point< | |
184 | typename coordinate_type<indexable_type>::type, | |
185 | dimension<indexable_type>::value, | |
186 | typename coordinate_system<indexable_type>::type | |
187 | > | |
188 | > | |
189 | bounds_type; | |
190 | ||
191 | private: | |
192 | ||
193 | typedef detail::translator<IndexableGetter, EqualTo> translator_type; | |
194 | ||
195 | typedef bounds_type box_type; | |
196 | typedef typename detail::rtree::options_type<Parameters>::type options_type; | |
197 | typedef typename options_type::node_tag node_tag; | |
92f5a8d4 TL |
198 | typedef detail::rtree::allocators |
199 | < | |
200 | allocator_type, | |
201 | value_type, | |
202 | typename options_type::parameters_type, | |
203 | box_type, | |
204 | node_tag | |
205 | > allocators_type; | |
7c673cae | 206 | |
92f5a8d4 TL |
207 | typedef typename detail::rtree::node |
208 | < | |
209 | value_type, | |
210 | typename options_type::parameters_type, | |
211 | box_type, | |
212 | allocators_type, | |
213 | node_tag | |
214 | >::type node; | |
215 | typedef typename detail::rtree::internal_node | |
216 | < | |
217 | value_type, | |
218 | typename options_type::parameters_type, | |
219 | box_type, | |
220 | allocators_type, | |
221 | node_tag | |
222 | >::type internal_node; | |
223 | typedef typename detail::rtree::leaf | |
224 | < | |
225 | value_type, | |
226 | typename options_type::parameters_type, | |
227 | box_type, | |
228 | allocators_type, | |
229 | node_tag | |
230 | >::type leaf; | |
7c673cae FG |
231 | |
232 | typedef typename allocators_type::node_pointer node_pointer; | |
233 | typedef ::boost::container::allocator_traits<Allocator> allocator_traits_type; | |
92f5a8d4 TL |
234 | typedef detail::rtree::subtree_destroyer |
235 | < | |
236 | value_type, options_type, translator_type, box_type, allocators_type | |
237 | > subtree_destroyer; | |
7c673cae FG |
238 | |
239 | friend class detail::rtree::utilities::view<rtree>; | |
240 | #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL | |
241 | friend class detail::rtree::private_view<rtree>; | |
242 | friend class detail::rtree::const_private_view<rtree>; | |
243 | #endif | |
244 | ||
245 | public: | |
246 | ||
247 | /*! \brief Type of reference to Value. */ | |
248 | typedef typename allocators_type::reference reference; | |
249 | /*! \brief Type of reference to const Value. */ | |
250 | typedef typename allocators_type::const_reference const_reference; | |
251 | /*! \brief Type of pointer to Value. */ | |
252 | typedef typename allocators_type::pointer pointer; | |
253 | /*! \brief Type of pointer to const Value. */ | |
254 | typedef typename allocators_type::const_pointer const_pointer; | |
255 | /*! \brief Type of difference type. */ | |
256 | typedef typename allocators_type::difference_type difference_type; | |
257 | /*! \brief Unsigned integral type used by the container. */ | |
258 | typedef typename allocators_type::size_type size_type; | |
259 | ||
260 | /*! \brief Type of const iterator, category ForwardIterator. */ | |
261 | typedef index::detail::rtree::iterators::iterator | |
262 | < | |
263 | value_type, options_type, translator_type, box_type, allocators_type | |
264 | > const_iterator; | |
265 | ||
266 | /*! \brief Type of const query iterator, category ForwardIterator. */ | |
267 | typedef index::detail::rtree::iterators::query_iterator | |
268 | < | |
269 | value_type, allocators_type | |
270 | > const_query_iterator; | |
271 | ||
272 | public: | |
273 | ||
274 | /*! | |
275 | \brief The constructor. | |
276 | ||
277 | \param parameters The parameters object. | |
278 | \param getter The function object extracting Indexable from Value. | |
279 | \param equal The function object comparing Values. | |
280 | ||
281 | \par Throws | |
282 | If allocator default constructor throws. | |
283 | */ | |
284 | inline explicit rtree(parameters_type const& parameters = parameters_type(), | |
285 | indexable_getter const& getter = indexable_getter(), | |
286 | value_equal const& equal = value_equal()) | |
287 | : m_members(getter, equal, parameters) | |
288 | {} | |
289 | ||
290 | /*! | |
291 | \brief The constructor. | |
292 | ||
293 | \param parameters The parameters object. | |
294 | \param getter The function object extracting Indexable from Value. | |
295 | \param equal The function object comparing Values. | |
296 | \param allocator The allocator object. | |
297 | ||
298 | \par Throws | |
299 | If allocator copy constructor throws. | |
300 | */ | |
301 | inline rtree(parameters_type const& parameters, | |
302 | indexable_getter const& getter, | |
303 | value_equal const& equal, | |
304 | allocator_type const& allocator) | |
305 | : m_members(getter, equal, parameters, allocator) | |
306 | {} | |
307 | ||
308 | /*! | |
309 | \brief The constructor. | |
310 | ||
311 | The tree is created using packing algorithm. | |
312 | ||
313 | \param first The beginning of the range of Values. | |
314 | \param last The end of the range of Values. | |
315 | \param parameters The parameters object. | |
316 | \param getter The function object extracting Indexable from Value. | |
317 | \param equal The function object comparing Values. | |
318 | \param allocator The allocator object. | |
319 | ||
320 | \par Throws | |
321 | \li If allocator copy constructor throws. | |
322 | \li If Value copy constructor or copy assignment throws. | |
323 | \li If allocation throws or returns invalid value. | |
324 | */ | |
325 | template<typename Iterator> | |
326 | inline rtree(Iterator first, Iterator last, | |
327 | parameters_type const& parameters = parameters_type(), | |
328 | indexable_getter const& getter = indexable_getter(), | |
329 | value_equal const& equal = value_equal(), | |
330 | allocator_type const& allocator = allocator_type()) | |
331 | : m_members(getter, equal, parameters, allocator) | |
332 | { | |
333 | typedef detail::rtree::pack<value_type, options_type, translator_type, box_type, allocators_type> pack; | |
334 | size_type vc = 0, ll = 0; | |
335 | m_members.root = pack::apply(first, last, vc, ll, | |
336 | m_members.parameters(), m_members.translator(), m_members.allocators()); | |
337 | m_members.values_count = vc; | |
338 | m_members.leafs_level = ll; | |
339 | } | |
340 | ||
341 | /*! | |
342 | \brief The constructor. | |
343 | ||
344 | The tree is created using packing algorithm. | |
345 | ||
346 | \param rng The range of Values. | |
347 | \param parameters The parameters object. | |
348 | \param getter The function object extracting Indexable from Value. | |
349 | \param equal The function object comparing Values. | |
350 | \param allocator The allocator object. | |
351 | ||
352 | \par Throws | |
353 | \li If allocator copy constructor throws. | |
354 | \li If Value copy constructor or copy assignment throws. | |
355 | \li If allocation throws or returns invalid value. | |
356 | */ | |
357 | template<typename Range> | |
358 | inline explicit rtree(Range const& rng, | |
359 | parameters_type const& parameters = parameters_type(), | |
360 | indexable_getter const& getter = indexable_getter(), | |
361 | value_equal const& equal = value_equal(), | |
362 | allocator_type const& allocator = allocator_type()) | |
363 | : m_members(getter, equal, parameters, allocator) | |
364 | { | |
365 | typedef detail::rtree::pack<value_type, options_type, translator_type, box_type, allocators_type> pack; | |
366 | size_type vc = 0, ll = 0; | |
367 | m_members.root = pack::apply(::boost::begin(rng), ::boost::end(rng), vc, ll, | |
368 | m_members.parameters(), m_members.translator(), m_members.allocators()); | |
369 | m_members.values_count = vc; | |
370 | m_members.leafs_level = ll; | |
371 | } | |
372 | ||
373 | /*! | |
374 | \brief The destructor. | |
375 | ||
376 | \par Throws | |
377 | Nothing. | |
378 | */ | |
379 | inline ~rtree() | |
380 | { | |
381 | this->raw_destroy(*this); | |
382 | } | |
383 | ||
384 | /*! | |
385 | \brief The copy constructor. | |
386 | ||
387 | It uses parameters, translator and allocator from the source tree. | |
388 | ||
389 | \param src The rtree which content will be copied. | |
390 | ||
391 | \par Throws | |
392 | \li If allocator copy constructor throws. | |
393 | \li If Value copy constructor throws. | |
394 | \li If allocation throws or returns invalid value. | |
395 | */ | |
396 | inline rtree(rtree const& src) | |
397 | : m_members(src.m_members.indexable_getter(), | |
398 | src.m_members.equal_to(), | |
399 | src.m_members.parameters(), | |
400 | allocator_traits_type::select_on_container_copy_construction(src.get_allocator())) | |
401 | { | |
402 | this->raw_copy(src, *this, false); | |
403 | } | |
404 | ||
405 | /*! | |
406 | \brief The copy constructor. | |
407 | ||
408 | It uses Parameters and translator from the source tree. | |
409 | ||
410 | \param src The rtree which content will be copied. | |
411 | \param allocator The allocator which will be used. | |
412 | ||
413 | \par Throws | |
414 | \li If allocator copy constructor throws. | |
415 | \li If Value copy constructor throws. | |
416 | \li If allocation throws or returns invalid value. | |
417 | */ | |
418 | inline rtree(rtree const& src, allocator_type const& allocator) | |
419 | : m_members(src.m_members.indexable_getter(), | |
420 | src.m_members.equal_to(), | |
421 | src.m_members.parameters(), allocator) | |
422 | { | |
423 | this->raw_copy(src, *this, false); | |
424 | } | |
425 | ||
426 | /*! | |
427 | \brief The moving constructor. | |
428 | ||
429 | It uses parameters, translator and allocator from the source tree. | |
430 | ||
431 | \param src The rtree which content will be moved. | |
432 | ||
433 | \par Throws | |
434 | Nothing. | |
435 | */ | |
436 | inline rtree(BOOST_RV_REF(rtree) src) | |
437 | : m_members(src.m_members.indexable_getter(), | |
438 | src.m_members.equal_to(), | |
439 | src.m_members.parameters(), | |
440 | boost::move(src.m_members.allocators())) | |
441 | { | |
442 | boost::swap(m_members.values_count, src.m_members.values_count); | |
443 | boost::swap(m_members.leafs_level, src.m_members.leafs_level); | |
444 | boost::swap(m_members.root, src.m_members.root); | |
445 | } | |
446 | ||
447 | /*! | |
448 | \brief The moving constructor. | |
449 | ||
450 | It uses parameters and translator from the source tree. | |
451 | ||
452 | \param src The rtree which content will be moved. | |
453 | \param allocator The allocator. | |
454 | ||
455 | \par Throws | |
456 | \li If allocator copy constructor throws. | |
457 | \li If Value copy constructor throws (only if allocators aren't equal). | |
458 | \li If allocation throws or returns invalid value (only if allocators aren't equal). | |
459 | */ | |
460 | inline rtree(BOOST_RV_REF(rtree) src, allocator_type const& allocator) | |
461 | : m_members(src.m_members.indexable_getter(), | |
462 | src.m_members.equal_to(), | |
463 | src.m_members.parameters(), | |
464 | allocator) | |
465 | { | |
466 | if ( src.m_members.allocators() == allocator ) | |
467 | { | |
468 | boost::swap(m_members.values_count, src.m_members.values_count); | |
469 | boost::swap(m_members.leafs_level, src.m_members.leafs_level); | |
470 | boost::swap(m_members.root, src.m_members.root); | |
471 | } | |
472 | else | |
473 | { | |
474 | this->raw_copy(src, *this, false); | |
475 | } | |
476 | } | |
477 | ||
478 | /*! | |
479 | \brief The assignment operator. | |
480 | ||
481 | It uses parameters and translator from the source tree. | |
482 | ||
483 | \param src The rtree which content will be copied. | |
484 | ||
485 | \par Throws | |
486 | \li If Value copy constructor throws. | |
487 | \li If allocation throws. | |
488 | \li If allocation throws or returns invalid value. | |
489 | */ | |
490 | inline rtree & operator=(BOOST_COPY_ASSIGN_REF(rtree) src) | |
491 | { | |
492 | if ( &src != this ) | |
493 | { | |
494 | allocators_type & this_allocs = m_members.allocators(); | |
495 | allocators_type const& src_allocs = src.m_members.allocators(); | |
496 | ||
497 | // NOTE: if propagate is true for std allocators on darwin 4.2.1, glibc++ | |
498 | // (allocators stored as base classes of members_holder) | |
499 | // copying them changes values_count, in this case it doesn't cause errors since data must be copied | |
500 | ||
501 | typedef boost::mpl::bool_< | |
502 | allocator_traits_type::propagate_on_container_copy_assignment::value | |
503 | > propagate; | |
504 | ||
505 | if ( propagate::value && !(this_allocs == src_allocs) ) | |
506 | this->raw_destroy(*this); | |
507 | detail::assign_cond(this_allocs, src_allocs, propagate()); | |
508 | ||
509 | // It uses m_allocators | |
510 | this->raw_copy(src, *this, true); | |
511 | } | |
512 | ||
513 | return *this; | |
514 | } | |
515 | ||
516 | /*! | |
517 | \brief The moving assignment. | |
518 | ||
519 | It uses parameters and translator from the source tree. | |
520 | ||
521 | \param src The rtree which content will be moved. | |
522 | ||
523 | \par Throws | |
524 | Only if allocators aren't equal. | |
525 | \li If Value copy constructor throws. | |
526 | \li If allocation throws or returns invalid value. | |
527 | */ | |
528 | inline rtree & operator=(BOOST_RV_REF(rtree) src) | |
529 | { | |
530 | if ( &src != this ) | |
531 | { | |
532 | allocators_type & this_allocs = m_members.allocators(); | |
533 | allocators_type & src_allocs = src.m_members.allocators(); | |
534 | ||
535 | if ( this_allocs == src_allocs ) | |
536 | { | |
537 | this->raw_destroy(*this); | |
538 | ||
539 | m_members.indexable_getter() = src.m_members.indexable_getter(); | |
540 | m_members.equal_to() = src.m_members.equal_to(); | |
541 | m_members.parameters() = src.m_members.parameters(); | |
542 | ||
543 | boost::swap(m_members.values_count, src.m_members.values_count); | |
544 | boost::swap(m_members.leafs_level, src.m_members.leafs_level); | |
545 | boost::swap(m_members.root, src.m_members.root); | |
546 | ||
547 | // NOTE: if propagate is true for std allocators on darwin 4.2.1, glibc++ | |
548 | // (allocators stored as base classes of members_holder) | |
549 | // moving them changes values_count | |
550 | ||
551 | typedef boost::mpl::bool_< | |
552 | allocator_traits_type::propagate_on_container_move_assignment::value | |
553 | > propagate; | |
554 | detail::move_cond(this_allocs, src_allocs, propagate()); | |
555 | } | |
556 | else | |
557 | { | |
558 | // TODO - shouldn't here propagate_on_container_copy_assignment be checked like in operator=(const&)? | |
559 | ||
560 | // It uses m_allocators | |
561 | this->raw_copy(src, *this, true); | |
562 | } | |
563 | } | |
564 | ||
565 | return *this; | |
566 | } | |
567 | ||
568 | /*! | |
569 | \brief Swaps contents of two rtrees. | |
570 | ||
571 | Parameters, translator and allocators are swapped as well. | |
572 | ||
573 | \param other The rtree which content will be swapped with this rtree content. | |
574 | ||
575 | \par Throws | |
576 | If allocators swap throws. | |
577 | */ | |
578 | void swap(rtree & other) | |
579 | { | |
580 | boost::swap(m_members.indexable_getter(), other.m_members.indexable_getter()); | |
581 | boost::swap(m_members.equal_to(), other.m_members.equal_to()); | |
582 | boost::swap(m_members.parameters(), other.m_members.parameters()); | |
583 | ||
584 | // NOTE: if propagate is true for std allocators on darwin 4.2.1, glibc++ | |
585 | // (allocators stored as base classes of members_holder) | |
586 | // swapping them changes values_count | |
587 | ||
588 | typedef boost::mpl::bool_< | |
589 | allocator_traits_type::propagate_on_container_swap::value | |
590 | > propagate; | |
591 | detail::swap_cond(m_members.allocators(), other.m_members.allocators(), propagate()); | |
592 | ||
593 | boost::swap(m_members.values_count, other.m_members.values_count); | |
594 | boost::swap(m_members.leafs_level, other.m_members.leafs_level); | |
595 | boost::swap(m_members.root, other.m_members.root); | |
596 | } | |
597 | ||
598 | /*! | |
599 | \brief Insert a value to the index. | |
600 | ||
601 | \param value The value which will be stored in the container. | |
602 | ||
603 | \par Throws | |
604 | \li If Value copy constructor or copy assignment throws. | |
605 | \li If allocation throws or returns invalid value. | |
606 | ||
607 | \warning | |
608 | This operation only guarantees that there will be no memory leaks. | |
609 | After an exception is thrown the R-tree may be left in an inconsistent state, | |
610 | elements must not be inserted or removed. Other operations are allowed however | |
611 | some of them may return invalid data. | |
612 | */ | |
613 | inline void insert(value_type const& value) | |
614 | { | |
615 | if ( !m_members.root ) | |
616 | this->raw_create(); | |
617 | ||
618 | this->raw_insert(value); | |
619 | } | |
620 | ||
621 | /*! | |
622 | \brief Insert a range of values to the index. | |
623 | ||
624 | \param first The beginning of the range of values. | |
625 | \param last The end of the range of values. | |
626 | ||
627 | \par Throws | |
628 | \li If Value copy constructor or copy assignment throws. | |
629 | \li If allocation throws or returns invalid value. | |
630 | ||
631 | \warning | |
632 | This operation only guarantees that there will be no memory leaks. | |
633 | After an exception is thrown the R-tree may be left in an inconsistent state, | |
634 | elements must not be inserted or removed. Other operations are allowed however | |
635 | some of them may return invalid data. | |
636 | */ | |
637 | template <typename Iterator> | |
638 | inline void insert(Iterator first, Iterator last) | |
639 | { | |
640 | if ( !m_members.root ) | |
641 | this->raw_create(); | |
642 | ||
643 | for ( ; first != last ; ++first ) | |
644 | this->raw_insert(*first); | |
645 | } | |
646 | ||
647 | /*! | |
648 | \brief Insert a value created using convertible object or a range of values to the index. | |
649 | ||
650 | \param conv_or_rng An object of type convertible to value_type or a range of values. | |
651 | ||
652 | \par Throws | |
653 | \li If Value copy constructor or copy assignment throws. | |
654 | \li If allocation throws or returns invalid value. | |
655 | ||
656 | \warning | |
657 | This operation only guarantees that there will be no memory leaks. | |
658 | After an exception is thrown the R-tree may be left in an inconsistent state, | |
659 | elements must not be inserted or removed. Other operations are allowed however | |
660 | some of them may return invalid data. | |
661 | */ | |
662 | template <typename ConvertibleOrRange> | |
663 | inline void insert(ConvertibleOrRange const& conv_or_rng) | |
664 | { | |
665 | if ( !m_members.root ) | |
666 | this->raw_create(); | |
667 | ||
668 | typedef boost::mpl::bool_ | |
669 | < | |
670 | boost::is_convertible<ConvertibleOrRange, value_type>::value | |
671 | > is_conv_t; | |
672 | ||
673 | this->insert_dispatch(conv_or_rng, is_conv_t()); | |
674 | } | |
675 | ||
676 | /*! | |
677 | \brief Remove a value from the container. | |
678 | ||
679 | In contrast to the \c std::set or <tt>std::map erase()</tt> method | |
680 | this method removes only one value from the container. | |
681 | ||
682 | \param value The value which will be removed from the container. | |
683 | ||
684 | \return 1 if the value was removed, 0 otherwise. | |
685 | ||
686 | \par Throws | |
687 | \li If Value copy constructor or copy assignment throws. | |
688 | \li If allocation throws or returns invalid value. | |
689 | ||
690 | \warning | |
691 | This operation only guarantees that there will be no memory leaks. | |
692 | After an exception is thrown the R-tree may be left in an inconsistent state, | |
693 | elements must not be inserted or removed. Other operations are allowed however | |
694 | some of them may return invalid data. | |
695 | */ | |
696 | inline size_type remove(value_type const& value) | |
697 | { | |
698 | if ( !m_members.root ) | |
699 | return 0; | |
700 | ||
701 | return this->raw_remove(value); | |
702 | } | |
703 | ||
704 | /*! | |
705 | \brief Remove a range of values from the container. | |
706 | ||
707 | In contrast to the \c std::set or <tt>std::map erase()</tt> method | |
708 | it doesn't take iterators pointing to values stored in this container. It removes values equal | |
709 | to these passed as a range. Furthermore this method removes only one value for each one passed | |
710 | in the range, not all equal values. | |
711 | ||
712 | \param first The beginning of the range of values. | |
713 | \param last The end of the range of values. | |
714 | ||
715 | \return The number of removed values. | |
716 | ||
717 | \par Throws | |
718 | \li If Value copy constructor or copy assignment throws. | |
719 | \li If allocation throws or returns invalid value. | |
720 | ||
721 | \warning | |
722 | This operation only guarantees that there will be no memory leaks. | |
723 | After an exception is thrown the R-tree may be left in an inconsistent state, | |
724 | elements must not be inserted or removed. Other operations are allowed however | |
725 | some of them may return invalid data. | |
726 | */ | |
727 | template <typename Iterator> | |
728 | inline size_type remove(Iterator first, Iterator last) | |
729 | { | |
730 | size_type result = 0; | |
731 | ||
732 | if ( !m_members.root ) | |
733 | return result; | |
734 | ||
735 | for ( ; first != last ; ++first ) | |
736 | result += this->raw_remove(*first); | |
737 | return result; | |
738 | } | |
739 | ||
740 | /*! | |
741 | \brief Remove value corresponding to an object convertible to it or a range of values from the container. | |
742 | ||
743 | In contrast to the \c std::set or <tt>std::map erase()</tt> method | |
744 | it removes values equal to these passed as a range. Furthermore, this method removes only | |
745 | one value for each one passed in the range, not all equal values. | |
746 | ||
747 | \param conv_or_rng The object of type convertible to value_type or a range of values. | |
748 | ||
749 | \return The number of removed values. | |
750 | ||
751 | \par Throws | |
752 | \li If Value copy constructor or copy assignment throws. | |
753 | \li If allocation throws or returns invalid value. | |
754 | ||
755 | \warning | |
756 | This operation only guarantees that there will be no memory leaks. | |
757 | After an exception is thrown the R-tree may be left in an inconsistent state, | |
758 | elements must not be inserted or removed. Other operations are allowed however | |
759 | some of them may return invalid data. | |
760 | */ | |
761 | template <typename ConvertibleOrRange> | |
762 | inline size_type remove(ConvertibleOrRange const& conv_or_rng) | |
763 | { | |
764 | if ( !m_members.root ) | |
765 | return 0; | |
766 | ||
767 | typedef boost::mpl::bool_ | |
768 | < | |
769 | boost::is_convertible<ConvertibleOrRange, value_type>::value | |
770 | > is_conv_t; | |
771 | ||
772 | return this->remove_dispatch(conv_or_rng, is_conv_t()); | |
773 | } | |
774 | ||
775 | /*! | |
776 | \brief Finds values meeting passed predicates e.g. nearest to some Point and/or intersecting some Box. | |
777 | ||
778 | This query function performs spatial and k-nearest neighbor searches. It allows to pass a set of predicates. | |
779 | Values will be returned only if all predicates are met. | |
780 | ||
781 | <b>Spatial predicates</b> | |
782 | ||
783 | Spatial predicates may be generated by one of the functions listed below: | |
784 | \li \c boost::geometry::index::contains(), | |
785 | \li \c boost::geometry::index::covered_by(), | |
786 | \li \c boost::geometry::index::covers(), | |
787 | \li \c boost::geometry::index::disjoint(), | |
788 | \li \c boost::geometry::index::intersects(), | |
789 | \li \c boost::geometry::index::overlaps(), | |
790 | \li \c boost::geometry::index::within(), | |
791 | ||
792 | It is possible to negate spatial predicates: | |
793 | \li <tt>! boost::geometry::index::contains()</tt>, | |
794 | \li <tt>! boost::geometry::index::covered_by()</tt>, | |
795 | \li <tt>! boost::geometry::index::covers()</tt>, | |
796 | \li <tt>! boost::geometry::index::disjoint()</tt>, | |
797 | \li <tt>! boost::geometry::index::intersects()</tt>, | |
798 | \li <tt>! boost::geometry::index::overlaps()</tt>, | |
799 | \li <tt>! boost::geometry::index::within()</tt> | |
800 | ||
801 | <b>Satisfies predicate</b> | |
802 | ||
803 | This is a special kind of predicate which allows to pass a user-defined function or function object which checks | |
804 | if Value should be returned by the query. It's generated by: | |
805 | \li \c boost::geometry::index::satisfies(). | |
806 | ||
807 | <b>Nearest predicate</b> | |
808 | ||
809 | If the nearest predicate is passed a k-nearest neighbor search will be performed. This query will result | |
810 | in returning k values to the output iterator. Only one nearest predicate may be passed to the query. | |
811 | It may be generated by: | |
812 | \li \c boost::geometry::index::nearest(). | |
813 | ||
814 | <b>Connecting predicates</b> | |
815 | ||
816 | Predicates may be passed together connected with \c operator&&(). | |
817 | ||
818 | \par Example | |
819 | \verbatim | |
820 | // return elements intersecting box | |
821 | tree.query(bgi::intersects(box), std::back_inserter(result)); | |
822 | // return elements intersecting poly but not within box | |
823 | tree.query(bgi::intersects(poly) && !bgi::within(box), std::back_inserter(result)); | |
824 | // return elements overlapping box and meeting my_fun unary predicate | |
825 | tree.query(bgi::overlaps(box) && bgi::satisfies(my_fun), std::back_inserter(result)); | |
826 | // return 5 elements nearest to pt and elements are intersecting box | |
827 | tree.query(bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result)); | |
828 | ||
829 | // For each found value do_something (it is a type of function object) | |
830 | tree.query(bgi::intersects(box), | |
831 | boost::make_function_output_iterator(do_something())); | |
832 | ||
833 | // For each value stored in the rtree do_something | |
834 | // always_true is a type of function object always returning true | |
835 | tree.query(bgi::satisfies(always_true()), | |
836 | boost::make_function_output_iterator(do_something())); | |
837 | ||
838 | // C++11 (lambda expression) | |
839 | tree.query(bgi::intersects(box), | |
840 | boost::make_function_output_iterator([](value_type const& val){ | |
841 | // do something | |
842 | })); | |
843 | ||
844 | // C++14 (generic lambda expression) | |
845 | tree.query(bgi::intersects(box), | |
846 | boost::make_function_output_iterator([](auto const& val){ | |
847 | // do something | |
848 | })); | |
849 | \endverbatim | |
850 | ||
851 | \par Throws | |
852 | If Value copy constructor or copy assignment throws. | |
853 | If predicates copy throws. | |
854 | ||
855 | \warning | |
b32b8144 | 856 | Only one \c nearest() predicate may be passed to the query. Passing more of them results in compile-time error. |
7c673cae FG |
857 | |
858 | \param predicates Predicates. | |
859 | \param out_it The output iterator, e.g. generated by std::back_inserter(). | |
860 | ||
861 | \return The number of values found. | |
862 | */ | |
863 | template <typename Predicates, typename OutIter> | |
864 | size_type query(Predicates const& predicates, OutIter out_it) const | |
865 | { | |
866 | if ( !m_members.root ) | |
867 | return 0; | |
868 | ||
869 | static const unsigned distance_predicates_count = detail::predicates_count_distance<Predicates>::value; | |
870 | static const bool is_distance_predicate = 0 < distance_predicates_count; | |
871 | BOOST_MPL_ASSERT_MSG((distance_predicates_count <= 1), PASS_ONLY_ONE_DISTANCE_PREDICATE, (Predicates)); | |
872 | ||
873 | return query_dispatch(predicates, out_it, boost::mpl::bool_<is_distance_predicate>()); | |
874 | } | |
875 | ||
876 | /*! | |
877 | \brief Returns a query iterator pointing at the begin of the query range. | |
878 | ||
879 | This method returns an iterator which may be used to perform iterative queries. | |
880 | For the information about predicates which may be passed to this method see query(). | |
881 | ||
882 | \par Example | |
883 | \verbatim | |
884 | for ( Rtree::const_query_iterator it = tree.qbegin(bgi::nearest(pt, 10000)) ; | |
885 | it != tree.qend() ; ++it ) | |
886 | { | |
887 | // do something with value | |
888 | if ( has_enough_nearest_values() ) | |
889 | break; | |
890 | } | |
891 | ||
892 | // C++11 (auto) | |
893 | for ( auto it = tree.qbegin(bgi::nearest(pt, 3)) ; it != tree.qend() ; ++it ) | |
894 | { | |
895 | // do something with value | |
896 | } | |
897 | ||
898 | // C++14 (generic lambda expression) | |
899 | std::for_each(tree.qbegin(bgi::nearest(pt, 3)), tree.qend(), [](auto const& val){ | |
900 | // do something with value | |
901 | }); | |
902 | \endverbatim | |
903 | ||
904 | \par Iterator category | |
905 | ForwardIterator | |
906 | ||
907 | \par Throws | |
908 | If predicates copy throws. | |
909 | If allocation throws. | |
910 | ||
911 | \warning | |
912 | The modification of the rtree may invalidate the iterators. | |
913 | ||
914 | \param predicates Predicates. | |
915 | ||
916 | \return The iterator pointing at the begin of the query range. | |
917 | */ | |
918 | template <typename Predicates> | |
919 | const_query_iterator qbegin(Predicates const& predicates) const | |
920 | { | |
921 | return const_query_iterator(qbegin_(predicates)); | |
922 | } | |
923 | ||
924 | /*! | |
925 | \brief Returns a query iterator pointing at the end of the query range. | |
926 | ||
927 | This method returns an iterator which may be used to check if the query has ended. | |
928 | ||
929 | \par Example | |
930 | \verbatim | |
931 | for ( Rtree::const_query_iterator it = tree.qbegin(bgi::nearest(pt, 10000)) ; | |
932 | it != tree.qend() ; ++it ) | |
933 | { | |
934 | // do something with value | |
935 | if ( has_enough_nearest_values() ) | |
936 | break; | |
937 | } | |
938 | ||
939 | // C++11 (auto) | |
940 | for ( auto it = tree.qbegin(bgi::nearest(pt, 3)) ; it != tree.qend() ; ++it ) | |
941 | { | |
942 | // do something with value | |
943 | } | |
944 | ||
945 | // C++14 (generic lambda expression) | |
946 | std::for_each(tree.qbegin(bgi::nearest(pt, 3)), tree.qend(), [](auto const& val){ | |
947 | // do something with value | |
948 | }); | |
949 | \endverbatim | |
950 | ||
951 | \par Iterator category | |
952 | ForwardIterator | |
953 | ||
954 | \par Throws | |
955 | Nothing | |
956 | ||
957 | \warning | |
958 | The modification of the rtree may invalidate the iterators. | |
959 | ||
960 | \return The iterator pointing at the end of the query range. | |
961 | */ | |
962 | const_query_iterator qend() const | |
963 | { | |
964 | return const_query_iterator(); | |
965 | } | |
966 | ||
967 | #ifndef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL | |
968 | private: | |
969 | #endif | |
970 | /*! | |
971 | \brief Returns a query iterator pointing at the begin of the query range. | |
972 | ||
973 | This method returns an iterator which may be used to perform iterative queries. | |
974 | For the information about predicates which may be passed to this method see query(). | |
975 | ||
976 | The type of the returned iterator depends on the type of passed Predicates but the iterator of this type | |
977 | may be assigned to the variable of const_query_iterator type. If you'd like to use the type of the iterator | |
978 | returned by this method you may get the type e.g. by using C++11 decltype or Boost.Typeof library. | |
979 | This iterator may be compared with iterators returned by both versions of qend() method. | |
980 | ||
981 | \par Example | |
982 | \verbatim | |
983 | // Store the result in the container using std::copy() - it requires both iterators of the same type | |
984 | std::copy(tree.qbegin_(bgi::intersects(box)), tree.qend_(bgi::intersects(box)), std::back_inserter(result)); | |
985 | ||
986 | // Store the result in the container using std::copy() and type-erased iterators | |
987 | Rtree::const_query_iterator first = tree.qbegin_(bgi::intersects(box)); | |
988 | Rtree::const_query_iterator last = tree.qend_(); | |
989 | std::copy(first, last, std::back_inserter(result)); | |
990 | ||
991 | // Boost.Typeof | |
992 | typedef BOOST_TYPEOF(tree.qbegin(bgi::nearest(pt, 10000))) Iter; | |
993 | for ( Iter it = tree.qbegin_(bgi::nearest(pt, 10000)) ; it != tree.qend_() ; ++it ) | |
994 | { | |
995 | // do something with value | |
996 | if ( has_enough_nearest_values() ) | |
997 | break; | |
998 | } | |
999 | ||
1000 | // C++11 (auto) | |
1001 | for ( auto it = tree.qbegin_(bgi::nearest(pt, 10000)) ; it != tree.qend_() ; ++it ) | |
1002 | { | |
1003 | // do something with value | |
1004 | if ( has_enough_nearest_values() ) | |
1005 | break; | |
1006 | } | |
1007 | \endverbatim | |
1008 | ||
1009 | \par Iterator category | |
1010 | ForwardIterator | |
1011 | ||
1012 | \par Throws | |
1013 | If predicates copy throws. | |
1014 | If allocation throws. | |
1015 | ||
1016 | \warning | |
1017 | The modification of the rtree may invalidate the iterators. | |
1018 | ||
1019 | \param predicates Predicates. | |
1020 | ||
1021 | \return The iterator pointing at the begin of the query range. | |
1022 | */ | |
1023 | template <typename Predicates> | |
1024 | typename boost::mpl::if_c< | |
1025 | detail::predicates_count_distance<Predicates>::value == 0, | |
1026 | detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>, | |
1027 | detail::rtree::iterators::distance_query_iterator< | |
1028 | value_type, options_type, translator_type, box_type, allocators_type, Predicates, | |
1029 | detail::predicates_find_distance<Predicates>::value | |
1030 | > | |
1031 | >::type | |
1032 | qbegin_(Predicates const& predicates) const | |
1033 | { | |
1034 | static const unsigned distance_predicates_count = detail::predicates_count_distance<Predicates>::value; | |
1035 | BOOST_MPL_ASSERT_MSG((distance_predicates_count <= 1), PASS_ONLY_ONE_DISTANCE_PREDICATE, (Predicates)); | |
1036 | ||
1037 | typedef typename boost::mpl::if_c< | |
1038 | detail::predicates_count_distance<Predicates>::value == 0, | |
1039 | detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>, | |
1040 | detail::rtree::iterators::distance_query_iterator< | |
1041 | value_type, options_type, translator_type, box_type, allocators_type, Predicates, | |
1042 | detail::predicates_find_distance<Predicates>::value | |
1043 | > | |
1044 | >::type iterator_type; | |
1045 | ||
1046 | if ( !m_members.root ) | |
92f5a8d4 | 1047 | return iterator_type(m_members.parameters(), m_members.translator(), predicates); |
7c673cae | 1048 | |
92f5a8d4 | 1049 | return iterator_type(m_members.root, m_members.parameters(), m_members.translator(), predicates); |
7c673cae FG |
1050 | } |
1051 | ||
1052 | /*! | |
1053 | \brief Returns the query iterator pointing at the end of the query range. | |
1054 | ||
1055 | This method returns the iterator which may be used to perform iterative queries. For the information | |
1056 | about the predicates which may be passed to this method see query(). | |
1057 | ||
1058 | The type of the returned iterator depends on the type of passed Predicates but the iterator of this type | |
1059 | may be assigned to the variable of const_query_iterator type. If you'd like to use the type of the iterator | |
1060 | returned by this method you may get the type e.g. by using C++11 decltype or Boost.Typeof library. | |
1061 | ||
1062 | The type of the iterator returned by this method is the same as the one returned by qbegin() to which | |
1063 | the same predicates were passed. | |
1064 | ||
1065 | \par Example | |
1066 | \verbatim | |
1067 | // Store the result in the container using std::copy() - it requires both iterators of the same type | |
1068 | std::copy(tree.qbegin_(bgi::intersects(box)), tree.qend_(bgi::intersects(box)), std::back_inserter(result)); | |
1069 | \endverbatim | |
1070 | ||
1071 | \par Iterator category | |
1072 | ForwardIterator | |
1073 | ||
1074 | \par Throws | |
1075 | If predicates copy throws. | |
1076 | ||
1077 | \warning | |
1078 | The modification of the rtree may invalidate the iterators. | |
1079 | ||
1080 | \param predicates Predicates. | |
1081 | ||
1082 | \return The iterator pointing at the end of the query range. | |
1083 | */ | |
1084 | template <typename Predicates> | |
1085 | typename boost::mpl::if_c< | |
1086 | detail::predicates_count_distance<Predicates>::value == 0, | |
1087 | detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>, | |
1088 | detail::rtree::iterators::distance_query_iterator< | |
1089 | value_type, options_type, translator_type, box_type, allocators_type, Predicates, | |
1090 | detail::predicates_find_distance<Predicates>::value | |
1091 | > | |
1092 | >::type | |
1093 | qend_(Predicates const& predicates) const | |
1094 | { | |
1095 | static const unsigned distance_predicates_count = detail::predicates_count_distance<Predicates>::value; | |
1096 | BOOST_MPL_ASSERT_MSG((distance_predicates_count <= 1), PASS_ONLY_ONE_DISTANCE_PREDICATE, (Predicates)); | |
1097 | ||
1098 | typedef typename boost::mpl::if_c< | |
1099 | detail::predicates_count_distance<Predicates>::value == 0, | |
1100 | detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>, | |
1101 | detail::rtree::iterators::distance_query_iterator< | |
1102 | value_type, options_type, translator_type, box_type, allocators_type, Predicates, | |
1103 | detail::predicates_find_distance<Predicates>::value | |
1104 | > | |
1105 | >::type iterator_type; | |
1106 | ||
92f5a8d4 | 1107 | return iterator_type(m_members.parameters(), m_members.translator(), predicates); |
7c673cae FG |
1108 | } |
1109 | ||
1110 | /*! | |
1111 | \brief Returns the query iterator pointing at the end of the query range. | |
1112 | ||
1113 | This method returns the iterator which may be compared with the iterator returned by qbegin() in order to | |
1114 | check if the query has ended. | |
1115 | ||
1116 | The type of the returned iterator is different than the type returned by qbegin() but the iterator of this type | |
1117 | may be assigned to the variable of const_query_iterator type. If you'd like to use the type of the iterator returned by this | |
1118 | method, which most certainly will be faster than the type-erased iterator, you may get the type | |
1119 | e.g. by using C++11 decltype or Boost.Typeof library. | |
1120 | ||
b32b8144 | 1121 | The type of the iterator returned by this method is different than the type returned by qbegin(). |
7c673cae FG |
1122 | |
1123 | \par Example | |
1124 | \verbatim | |
1125 | // Store the result in the container using std::copy() and type-erased iterators | |
1126 | Rtree::const_query_iterator first = tree.qbegin_(bgi::intersects(box)); | |
1127 | Rtree::const_query_iterator last = tree.qend_(); | |
1128 | std::copy(first, last, std::back_inserter(result)); | |
1129 | ||
1130 | // Boost.Typeof | |
1131 | typedef BOOST_TYPEOF(tree.qbegin(bgi::nearest(pt, 10000))) Iter; | |
1132 | for ( Iter it = tree.qbegin_(bgi::nearest(pt, 10000)) ; it != tree.qend_() ; ++it ) | |
1133 | { | |
1134 | // do something with value | |
1135 | if ( has_enough_nearest_values() ) | |
1136 | break; | |
1137 | } | |
1138 | ||
1139 | // C++11 (auto) | |
1140 | for ( auto it = tree.qbegin_(bgi::nearest(pt, 10000)) ; it != tree.qend_() ; ++it ) | |
1141 | { | |
1142 | // do something with value | |
1143 | if ( has_enough_nearest_values() ) | |
1144 | break; | |
1145 | } | |
1146 | \endverbatim | |
1147 | ||
1148 | \par Iterator category | |
1149 | ForwardIterator | |
1150 | ||
1151 | \par Throws | |
1152 | Nothing | |
1153 | ||
1154 | \warning | |
1155 | The modification of the rtree may invalidate the iterators. | |
1156 | ||
1157 | \return The iterator pointing at the end of the query range. | |
1158 | */ | |
1159 | detail::rtree::iterators::end_query_iterator<value_type, allocators_type> | |
1160 | qend_() const | |
1161 | { | |
1162 | return detail::rtree::iterators::end_query_iterator<value_type, allocators_type>(); | |
1163 | } | |
1164 | ||
1165 | public: | |
1166 | ||
1167 | /*! | |
1168 | \brief Returns the iterator pointing at the begin of the rtree values range. | |
1169 | ||
1170 | This method returns the iterator which may be used to iterate over all values | |
1171 | stored in the rtree. | |
1172 | ||
1173 | \par Example | |
1174 | \verbatim | |
1175 | // Copy all values into the vector | |
1176 | std::copy(tree.begin(), tree.end(), std::back_inserter(vec)); | |
1177 | ||
1178 | for ( Rtree::const_iterator it = tree.begin() ; it != tree.end() ; ++it ) | |
1179 | { | |
1180 | // do something with value | |
1181 | } | |
1182 | ||
1183 | // C++11 (auto) | |
1184 | for ( auto it = tree.begin() ; it != tree.end() ; ++it ) | |
1185 | { | |
1186 | // do something with value | |
1187 | } | |
1188 | ||
1189 | // C++14 (generic lambda expression) | |
1190 | std::for_each(tree.begin(), tree.end(), [](auto const& val){ | |
1191 | // do something with value | |
1192 | }) | |
1193 | \endverbatim | |
1194 | ||
1195 | \par Iterator category | |
1196 | ForwardIterator | |
1197 | ||
1198 | \par Throws | |
1199 | If allocation throws. | |
1200 | ||
1201 | \warning | |
1202 | The modification of the rtree may invalidate the iterators. | |
1203 | ||
1204 | \return The iterator pointing at the begin of the range. | |
1205 | */ | |
1206 | const_iterator begin() const | |
1207 | { | |
1208 | if ( !m_members.root ) | |
1209 | return const_iterator(); | |
1210 | ||
1211 | return const_iterator(m_members.root); | |
1212 | } | |
1213 | ||
1214 | /*! | |
1215 | \brief Returns the iterator pointing at the end of the rtree values range. | |
1216 | ||
1217 | This method returns the iterator which may be compared with the iterator returned by begin() | |
1218 | in order to check if the iteration has ended. | |
1219 | ||
1220 | \par Example | |
1221 | \verbatim | |
1222 | for ( Rtree::const_iterator it = tree.begin() ; it != tree.end() ; ++it ) | |
1223 | { | |
1224 | // do something with value | |
1225 | } | |
1226 | ||
1227 | // C++11 (lambda expression) | |
1228 | std::for_each(tree.begin(), tree.end(), [](value_type const& val){ | |
1229 | // do something with value | |
1230 | }) | |
1231 | \endverbatim | |
1232 | ||
1233 | \par Iterator category | |
1234 | ForwardIterator | |
1235 | ||
1236 | \par Throws | |
1237 | Nothing. | |
1238 | ||
1239 | \warning | |
1240 | The modification of the rtree may invalidate the iterators. | |
1241 | ||
1242 | \return The iterator pointing at the end of the range. | |
1243 | */ | |
1244 | const_iterator end() const | |
1245 | { | |
1246 | return const_iterator(); | |
1247 | } | |
1248 | ||
1249 | /*! | |
1250 | \brief Returns the number of stored values. | |
1251 | ||
1252 | \return The number of stored values. | |
1253 | ||
1254 | \par Throws | |
1255 | Nothing. | |
1256 | */ | |
1257 | inline size_type size() const | |
1258 | { | |
1259 | return m_members.values_count; | |
1260 | } | |
1261 | ||
1262 | /*! | |
1263 | \brief Query if the container is empty. | |
1264 | ||
1265 | \return true if the container is empty. | |
1266 | ||
1267 | \par Throws | |
1268 | Nothing. | |
1269 | */ | |
1270 | inline bool empty() const | |
1271 | { | |
1272 | return 0 == m_members.values_count; | |
1273 | } | |
1274 | ||
1275 | /*! | |
1276 | \brief Removes all values stored in the container. | |
1277 | ||
1278 | \par Throws | |
1279 | Nothing. | |
1280 | */ | |
1281 | inline void clear() | |
1282 | { | |
1283 | this->raw_destroy(*this); | |
1284 | } | |
1285 | ||
1286 | /*! | |
1287 | \brief Returns the box able to contain all values stored in the container. | |
1288 | ||
1289 | Returns the box able to contain all values stored in the container. | |
1290 | If the container is empty the result of \c geometry::assign_inverse() is returned. | |
1291 | ||
1292 | \return The box able to contain all values stored in the container or an invalid box if | |
1293 | there are no values in the container. | |
1294 | ||
1295 | \par Throws | |
1296 | Nothing. | |
1297 | */ | |
1298 | inline bounds_type bounds() const | |
1299 | { | |
1300 | bounds_type result; | |
1301 | // in order to suppress the uninitialized variable warnings | |
1302 | geometry::assign_inverse(result); | |
1303 | ||
1304 | if ( m_members.root ) | |
1305 | { | |
92f5a8d4 TL |
1306 | detail::rtree::visitors::children_box |
1307 | < | |
1308 | value_type, options_type, translator_type, box_type, allocators_type | |
1309 | > box_v(result, m_members.parameters(), m_members.translator()); | |
7c673cae FG |
1310 | detail::rtree::apply_visitor(box_v, *m_members.root); |
1311 | } | |
1312 | ||
1313 | return result; | |
1314 | } | |
1315 | ||
1316 | /*! | |
1317 | \brief Count Values or Indexables stored in the container. | |
1318 | ||
1319 | For indexable_type it returns the number of values which indexables equals the parameter. | |
1320 | For value_type it returns the number of values which equals the parameter. | |
1321 | ||
1322 | \param vori The value or indexable which will be counted. | |
1323 | ||
1324 | \return The number of values found. | |
1325 | ||
1326 | \par Throws | |
1327 | Nothing. | |
1328 | */ | |
1329 | template <typename ValueOrIndexable> | |
1330 | size_type count(ValueOrIndexable const& vori) const | |
1331 | { | |
1332 | if ( !m_members.root ) | |
1333 | return 0; | |
1334 | ||
1335 | // the input should be convertible to Value or Indexable type | |
92f5a8d4 | 1336 | typedef typename index::detail::convertible_type |
7c673cae | 1337 | < |
92f5a8d4 | 1338 | ValueOrIndexable, |
7c673cae FG |
1339 | value_type, |
1340 | indexable_type | |
1341 | >::type value_or_indexable; | |
1342 | ||
92f5a8d4 TL |
1343 | static const bool is_void = boost::is_same<value_or_indexable, void>::value; |
1344 | BOOST_MPL_ASSERT_MSG((! is_void), | |
1345 | PASSED_OBJECT_NOT_CONVERTIBLE_TO_VALUE_NOR_INDEXABLE_TYPE, | |
1346 | (ValueOrIndexable)); | |
1347 | ||
7c673cae FG |
1348 | // NOTE: If an object of convertible but not the same type is passed |
1349 | // into the function, here a temporary will be created. | |
1350 | return this->template raw_count<value_or_indexable>(vori); | |
1351 | } | |
1352 | ||
1353 | /*! | |
1354 | \brief Returns parameters. | |
1355 | ||
1356 | \return The parameters object. | |
1357 | ||
1358 | \par Throws | |
1359 | Nothing. | |
1360 | */ | |
1361 | inline parameters_type parameters() const | |
1362 | { | |
1363 | return m_members.parameters(); | |
1364 | } | |
1365 | ||
1366 | /*! | |
1367 | \brief Returns function retrieving Indexable from Value. | |
1368 | ||
1369 | \return The indexable_getter object. | |
1370 | ||
1371 | \par Throws | |
1372 | Nothing. | |
1373 | */ | |
1374 | indexable_getter indexable_get() const | |
1375 | { | |
1376 | return m_members.indexable_getter(); | |
1377 | } | |
1378 | ||
1379 | /*! | |
1380 | \brief Returns function comparing Values | |
1381 | ||
1382 | \return The value_equal function. | |
1383 | ||
1384 | \par Throws | |
1385 | Nothing. | |
1386 | */ | |
1387 | value_equal value_eq() const | |
1388 | { | |
1389 | return m_members.equal_to(); | |
1390 | } | |
1391 | ||
1392 | /*! | |
1393 | \brief Returns allocator used by the rtree. | |
1394 | ||
1395 | \return The allocator. | |
1396 | ||
1397 | \par Throws | |
1398 | If allocator copy constructor throws. | |
1399 | */ | |
1400 | allocator_type get_allocator() const | |
1401 | { | |
1402 | return m_members.allocators().allocator(); | |
1403 | } | |
1404 | ||
1405 | private: | |
1406 | ||
1407 | /*! | |
1408 | \brief Returns the translator object. | |
1409 | ||
1410 | \return The translator object. | |
1411 | ||
1412 | \par Throws | |
1413 | Nothing. | |
1414 | */ | |
1415 | inline translator_type translator() const | |
1416 | { | |
1417 | return m_members.translator(); | |
1418 | } | |
1419 | ||
1420 | /*! | |
1421 | \brief Apply a visitor to the nodes structure in order to perform some operator. | |
1422 | ||
1423 | This function is not a part of the 'official' interface. However it makes | |
1424 | possible e.g. to pass a visitor drawing the tree structure. | |
1425 | ||
1426 | \param visitor The visitor object. | |
1427 | ||
1428 | \par Throws | |
1429 | If Visitor::operator() throws. | |
1430 | */ | |
1431 | template <typename Visitor> | |
1432 | inline void apply_visitor(Visitor & visitor) const | |
1433 | { | |
1434 | if ( m_members.root ) | |
1435 | detail::rtree::apply_visitor(visitor, *m_members.root); | |
1436 | } | |
1437 | ||
1438 | /*! | |
1439 | \brief Returns the depth of the R-tree. | |
1440 | ||
1441 | This function is not a part of the 'official' interface. | |
1442 | ||
1443 | \return The depth of the R-tree. | |
1444 | ||
1445 | \par Throws | |
1446 | Nothing. | |
1447 | */ | |
1448 | inline size_type depth() const | |
1449 | { | |
1450 | return m_members.leafs_level; | |
1451 | } | |
1452 | ||
1453 | private: | |
1454 | ||
1455 | /*! | |
1456 | \pre Root node must exist - m_root != 0. | |
1457 | ||
1458 | \brief Insert a value to the index. | |
1459 | ||
1460 | \param value The value which will be stored in the container. | |
1461 | ||
1462 | \par Exception-safety | |
1463 | basic | |
1464 | */ | |
1465 | inline void raw_insert(value_type const& value) | |
1466 | { | |
1467 | BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist"); | |
1468 | // CONSIDER: alternative - ignore invalid indexable or throw an exception | |
1469 | BOOST_GEOMETRY_INDEX_ASSERT(detail::is_valid(m_members.translator()(value)), "Indexable is invalid"); | |
1470 | ||
1471 | detail::rtree::visitors::insert< | |
1472 | value_type, | |
1473 | value_type, options_type, translator_type, box_type, allocators_type, | |
1474 | typename options_type::insert_tag | |
1475 | > insert_v(m_members.root, m_members.leafs_level, value, | |
1476 | m_members.parameters(), m_members.translator(), m_members.allocators()); | |
1477 | ||
1478 | detail::rtree::apply_visitor(insert_v, *m_members.root); | |
1479 | ||
1480 | // TODO | |
1481 | // Think about this: If exception is thrown, may the root be removed? | |
1482 | // Or it is just cleared? | |
1483 | ||
1484 | // TODO | |
1485 | // If exception is thrown, m_values_count may be invalid | |
1486 | ++m_members.values_count; | |
1487 | } | |
1488 | ||
1489 | /*! | |
1490 | \brief Remove the value from the container. | |
1491 | ||
1492 | \param value The value which will be removed from the container. | |
1493 | ||
1494 | \par Exception-safety | |
1495 | basic | |
1496 | */ | |
1497 | inline size_type raw_remove(value_type const& value) | |
1498 | { | |
1499 | // TODO: awulkiew - assert for correct value (indexable) ? | |
1500 | BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist"); | |
1501 | ||
1502 | detail::rtree::visitors::remove< | |
1503 | value_type, options_type, translator_type, box_type, allocators_type | |
1504 | > remove_v(m_members.root, m_members.leafs_level, value, | |
1505 | m_members.parameters(), m_members.translator(), m_members.allocators()); | |
1506 | ||
1507 | detail::rtree::apply_visitor(remove_v, *m_members.root); | |
1508 | ||
1509 | // If exception is thrown, m_values_count may be invalid | |
1510 | ||
1511 | if ( remove_v.is_value_removed() ) | |
1512 | { | |
1513 | BOOST_GEOMETRY_INDEX_ASSERT(0 < m_members.values_count, "unexpected state"); | |
1514 | ||
1515 | --m_members.values_count; | |
1516 | ||
1517 | return 1; | |
1518 | } | |
1519 | ||
1520 | return 0; | |
1521 | } | |
1522 | ||
1523 | /*! | |
1524 | \brief Create an empty R-tree i.e. new empty root node and clear other attributes. | |
1525 | ||
1526 | \par Exception-safety | |
1527 | strong | |
1528 | */ | |
1529 | inline void raw_create() | |
1530 | { | |
1531 | BOOST_GEOMETRY_INDEX_ASSERT(0 == m_members.root, "the tree is already created"); | |
1532 | ||
1533 | m_members.root = detail::rtree::create_node<allocators_type, leaf>::apply(m_members.allocators()); // MAY THROW (N: alloc) | |
1534 | m_members.values_count = 0; | |
1535 | m_members.leafs_level = 0; | |
1536 | } | |
1537 | ||
1538 | /*! | |
1539 | \brief Destroy the R-tree i.e. all nodes and clear attributes. | |
1540 | ||
1541 | \param t The container which is going to be destroyed. | |
1542 | ||
1543 | \par Exception-safety | |
1544 | nothrow | |
1545 | */ | |
1546 | inline void raw_destroy(rtree & t) | |
1547 | { | |
1548 | if ( t.m_members.root ) | |
1549 | { | |
1550 | detail::rtree::visitors::destroy<value_type, options_type, translator_type, box_type, allocators_type> | |
1551 | del_v(t.m_members.root, t.m_members.allocators()); | |
1552 | detail::rtree::apply_visitor(del_v, *t.m_members.root); | |
1553 | ||
1554 | t.m_members.root = 0; | |
1555 | } | |
1556 | t.m_members.values_count = 0; | |
1557 | t.m_members.leafs_level = 0; | |
1558 | } | |
1559 | ||
1560 | /*! | |
1561 | \brief Copy the R-tree i.e. whole nodes structure, values and other attributes. | |
1562 | It uses destination's allocators to create the new structure. | |
1563 | ||
1564 | \param src The source R-tree. | |
1565 | \param dst The destination R-tree. | |
1566 | \param copy_tr_and_params If true, translator and parameters will also be copied. | |
1567 | ||
1568 | \par Exception-safety | |
1569 | strong | |
1570 | */ | |
1571 | inline void raw_copy(rtree const& src, rtree & dst, bool copy_tr_and_params) const | |
1572 | { | |
1573 | detail::rtree::visitors::copy<value_type, options_type, translator_type, box_type, allocators_type> | |
1574 | copy_v(dst.m_members.allocators()); | |
1575 | ||
1576 | if ( src.m_members.root ) | |
1577 | detail::rtree::apply_visitor(copy_v, *src.m_members.root); // MAY THROW (V, E: alloc, copy, N: alloc) | |
1578 | ||
1579 | if ( copy_tr_and_params ) | |
1580 | { | |
1581 | dst.m_members.indexable_getter() = src.m_members.indexable_getter(); | |
1582 | dst.m_members.equal_to() = src.m_members.equal_to(); | |
1583 | dst.m_members.parameters() = src.m_members.parameters(); | |
1584 | } | |
1585 | ||
1586 | // TODO use subtree_destroyer | |
1587 | if ( dst.m_members.root ) | |
1588 | { | |
1589 | detail::rtree::visitors::destroy<value_type, options_type, translator_type, box_type, allocators_type> | |
1590 | del_v(dst.m_members.root, dst.m_members.allocators()); | |
1591 | detail::rtree::apply_visitor(del_v, *dst.m_members.root); | |
1592 | dst.m_members.root = 0; | |
1593 | } | |
1594 | ||
1595 | dst.m_members.root = copy_v.result; | |
1596 | dst.m_members.values_count = src.m_members.values_count; | |
1597 | dst.m_members.leafs_level = src.m_members.leafs_level; | |
1598 | } | |
1599 | ||
1600 | /*! | |
1601 | \brief Insert a value corresponding to convertible object into the index. | |
1602 | ||
1603 | \param val_conv The object convertible to value. | |
1604 | ||
1605 | \par Exception-safety | |
1606 | basic | |
1607 | */ | |
1608 | template <typename ValueConvertible> | |
1609 | inline void insert_dispatch(ValueConvertible const& val_conv, | |
1610 | boost::mpl::bool_<true> const& /*is_convertible*/) | |
1611 | { | |
1612 | this->raw_insert(val_conv); | |
1613 | } | |
1614 | ||
1615 | /*! | |
1616 | \brief Insert a range of values into the index. | |
1617 | ||
1618 | \param rng The range of values. | |
1619 | ||
1620 | \par Exception-safety | |
1621 | basic | |
1622 | */ | |
1623 | template <typename Range> | |
1624 | inline void insert_dispatch(Range const& rng, | |
1625 | boost::mpl::bool_<false> const& /*is_convertible*/) | |
1626 | { | |
1627 | BOOST_MPL_ASSERT_MSG((detail::is_range<Range>::value), | |
1628 | PASSED_OBJECT_IS_NOT_CONVERTIBLE_TO_VALUE_NOR_A_RANGE, | |
1629 | (Range)); | |
1630 | ||
1631 | typedef typename boost::range_const_iterator<Range>::type It; | |
1632 | for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it ) | |
1633 | this->raw_insert(*it); | |
1634 | } | |
1635 | ||
1636 | /*! | |
1637 | \brief Remove a value corresponding to convertible object from the index. | |
1638 | ||
1639 | \param val_conv The object convertible to value. | |
1640 | ||
1641 | \par Exception-safety | |
1642 | basic | |
1643 | */ | |
1644 | template <typename ValueConvertible> | |
1645 | inline size_type remove_dispatch(ValueConvertible const& val_conv, | |
1646 | boost::mpl::bool_<true> const& /*is_convertible*/) | |
1647 | { | |
1648 | return this->raw_remove(val_conv); | |
1649 | } | |
1650 | ||
1651 | /*! | |
1652 | \brief Remove a range of values from the index. | |
1653 | ||
1654 | \param rng The range of values which will be removed from the container. | |
1655 | ||
1656 | \par Exception-safety | |
1657 | basic | |
1658 | */ | |
1659 | template <typename Range> | |
1660 | inline size_type remove_dispatch(Range const& rng, | |
1661 | boost::mpl::bool_<false> const& /*is_convertible*/) | |
1662 | { | |
1663 | BOOST_MPL_ASSERT_MSG((detail::is_range<Range>::value), | |
1664 | PASSED_OBJECT_IS_NOT_CONVERTIBLE_TO_VALUE_NOR_A_RANGE, | |
1665 | (Range)); | |
1666 | ||
1667 | size_type result = 0; | |
1668 | typedef typename boost::range_const_iterator<Range>::type It; | |
1669 | for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it ) | |
1670 | result += this->raw_remove(*it); | |
1671 | return result; | |
1672 | } | |
1673 | ||
1674 | /*! | |
1675 | \brief Return values meeting predicates. | |
1676 | ||
1677 | \par Exception-safety | |
1678 | strong | |
1679 | */ | |
1680 | template <typename Predicates, typename OutIter> | |
1681 | size_type query_dispatch(Predicates const& predicates, OutIter out_it, boost::mpl::bool_<false> const& /*is_distance_predicate*/) const | |
1682 | { | |
92f5a8d4 TL |
1683 | detail::rtree::visitors::spatial_query |
1684 | < | |
1685 | value_type, options_type, translator_type, box_type, allocators_type, Predicates, OutIter | |
1686 | >find_v(m_members.parameters(), m_members.translator(), predicates, out_it); | |
7c673cae FG |
1687 | |
1688 | detail::rtree::apply_visitor(find_v, *m_members.root); | |
1689 | ||
1690 | return find_v.found_count; | |
1691 | } | |
1692 | ||
1693 | /*! | |
1694 | \brief Perform nearest neighbour search. | |
1695 | ||
1696 | \par Exception-safety | |
1697 | strong | |
1698 | */ | |
1699 | template <typename Predicates, typename OutIter> | |
1700 | size_type query_dispatch(Predicates const& predicates, OutIter out_it, boost::mpl::bool_<true> const& /*is_distance_predicate*/) const | |
1701 | { | |
1702 | BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist"); | |
1703 | ||
1704 | static const unsigned distance_predicate_index = detail::predicates_find_distance<Predicates>::value; | |
1705 | detail::rtree::visitors::distance_query< | |
1706 | value_type, | |
1707 | options_type, | |
1708 | translator_type, | |
1709 | box_type, | |
1710 | allocators_type, | |
1711 | Predicates, | |
1712 | distance_predicate_index, | |
1713 | OutIter | |
1714 | > distance_v(m_members.parameters(), m_members.translator(), predicates, out_it); | |
1715 | ||
1716 | detail::rtree::apply_visitor(distance_v, *m_members.root); | |
1717 | ||
1718 | return distance_v.finish(); | |
1719 | } | |
1720 | ||
1721 | /*! | |
1722 | \brief Count elements corresponding to value or indexable. | |
1723 | ||
1724 | \par Exception-safety | |
1725 | strong | |
1726 | */ | |
1727 | template <typename ValueOrIndexable> | |
1728 | size_type raw_count(ValueOrIndexable const& vori) const | |
1729 | { | |
1730 | BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist"); | |
1731 | ||
1732 | detail::rtree::visitors::count | |
1733 | < | |
1734 | ValueOrIndexable, | |
1735 | value_type, | |
1736 | options_type, | |
1737 | translator_type, | |
1738 | box_type, | |
1739 | allocators_type | |
92f5a8d4 | 1740 | > count_v(vori, m_members.parameters(), m_members.translator()); |
7c673cae FG |
1741 | |
1742 | detail::rtree::apply_visitor(count_v, *m_members.root); | |
1743 | ||
1744 | return count_v.found_count; | |
1745 | } | |
1746 | ||
1747 | struct members_holder | |
1748 | : public translator_type | |
1749 | , public Parameters | |
1750 | , public allocators_type | |
1751 | { | |
1752 | private: | |
1753 | members_holder(members_holder const&); | |
1754 | members_holder & operator=(members_holder const&); | |
1755 | ||
1756 | public: | |
1757 | template <typename IndGet, typename ValEq, typename Alloc> | |
1758 | members_holder(IndGet const& ind_get, | |
1759 | ValEq const& val_eq, | |
1760 | Parameters const& parameters, | |
1761 | BOOST_FWD_REF(Alloc) alloc) | |
1762 | : translator_type(ind_get, val_eq) | |
1763 | , Parameters(parameters) | |
1764 | , allocators_type(boost::forward<Alloc>(alloc)) | |
1765 | , values_count(0) | |
1766 | , leafs_level(0) | |
1767 | , root(0) | |
1768 | {} | |
1769 | ||
1770 | template <typename IndGet, typename ValEq> | |
1771 | members_holder(IndGet const& ind_get, | |
1772 | ValEq const& val_eq, | |
1773 | Parameters const& parameters) | |
1774 | : translator_type(ind_get, val_eq) | |
1775 | , Parameters(parameters) | |
1776 | , allocators_type() | |
1777 | , values_count(0) | |
1778 | , leafs_level(0) | |
1779 | , root(0) | |
1780 | {} | |
1781 | ||
1782 | translator_type const& translator() const { return *this; } | |
1783 | ||
1784 | IndexableGetter const& indexable_getter() const { return *this; } | |
1785 | IndexableGetter & indexable_getter() { return *this; } | |
1786 | EqualTo const& equal_to() const { return *this; } | |
1787 | EqualTo & equal_to() { return *this; } | |
1788 | Parameters const& parameters() const { return *this; } | |
1789 | Parameters & parameters() { return *this; } | |
1790 | allocators_type const& allocators() const { return *this; } | |
1791 | allocators_type & allocators() { return *this; } | |
1792 | ||
1793 | size_type values_count; | |
1794 | size_type leafs_level; | |
1795 | node_pointer root; | |
1796 | }; | |
1797 | ||
1798 | members_holder m_members; | |
1799 | }; | |
1800 | ||
1801 | /*! | |
1802 | \brief Insert a value to the index. | |
1803 | ||
1804 | It calls <tt>rtree::insert(value_type const&)</tt>. | |
1805 | ||
1806 | \ingroup rtree_functions | |
1807 | ||
1808 | \param tree The spatial index. | |
1809 | \param v The value which will be stored in the index. | |
1810 | */ | |
1811 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> | |
1812 | inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, | |
1813 | Value const& v) | |
1814 | { | |
1815 | tree.insert(v); | |
1816 | } | |
1817 | ||
1818 | /*! | |
1819 | \brief Insert a range of values to the index. | |
1820 | ||
1821 | It calls <tt>rtree::insert(Iterator, Iterator)</tt>. | |
1822 | ||
1823 | \ingroup rtree_functions | |
1824 | ||
1825 | \param tree The spatial index. | |
1826 | \param first The beginning of the range of values. | |
1827 | \param last The end of the range of values. | |
1828 | */ | |
1829 | template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, | |
1830 | typename Iterator> | |
1831 | inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, | |
1832 | Iterator first, Iterator last) | |
1833 | { | |
1834 | tree.insert(first, last); | |
1835 | } | |
1836 | ||
1837 | /*! | |
1838 | \brief Insert a value created using convertible object or a range of values to the index. | |
1839 | ||
1840 | It calls <tt>rtree::insert(ConvertibleOrRange const&)</tt>. | |
1841 | ||
1842 | \ingroup rtree_functions | |
1843 | ||
1844 | \param tree The spatial index. | |
1845 | \param conv_or_rng The object of type convertible to value_type or a range of values. | |
1846 | */ | |
1847 | template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, | |
1848 | typename ConvertibleOrRange> | |
1849 | inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, | |
1850 | ConvertibleOrRange const& conv_or_rng) | |
1851 | { | |
1852 | tree.insert(conv_or_rng); | |
1853 | } | |
1854 | ||
1855 | /*! | |
1856 | \brief Remove a value from the container. | |
1857 | ||
1858 | Remove a value from the container. In contrast to the \c std::set or <tt>std::map erase()</tt> method | |
1859 | this function removes only one value from the container. | |
1860 | ||
1861 | It calls <tt>rtree::remove(value_type const&)</tt>. | |
1862 | ||
1863 | \ingroup rtree_functions | |
1864 | ||
1865 | \param tree The spatial index. | |
1866 | \param v The value which will be removed from the index. | |
1867 | ||
1868 | \return 1 if value was removed, 0 otherwise. | |
1869 | */ | |
1870 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> | |
1871 | inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type | |
1872 | remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, | |
1873 | Value const& v) | |
1874 | { | |
1875 | return tree.remove(v); | |
1876 | } | |
1877 | ||
1878 | /*! | |
1879 | \brief Remove a range of values from the container. | |
1880 | ||
1881 | Remove a range of values from the container. In contrast to the \c std::set or <tt>std::map erase()</tt> method | |
1882 | it doesn't take iterators pointing to values stored in this container. It removes values equal | |
1883 | to these passed as a range. Furthermore this function removes only one value for each one passed | |
1884 | in the range, not all equal values. | |
1885 | ||
1886 | It calls <tt>rtree::remove(Iterator, Iterator)</tt>. | |
1887 | ||
1888 | \ingroup rtree_functions | |
1889 | ||
1890 | \param tree The spatial index. | |
1891 | \param first The beginning of the range of values. | |
1892 | \param last The end of the range of values. | |
1893 | ||
1894 | \return The number of removed values. | |
1895 | */ | |
1896 | template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, | |
1897 | typename Iterator> | |
1898 | inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type | |
1899 | remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, | |
1900 | Iterator first, Iterator last) | |
1901 | { | |
1902 | return tree.remove(first, last); | |
1903 | } | |
1904 | ||
1905 | /*! | |
1906 | \brief Remove a value corresponding to an object convertible to it or a range of values from the container. | |
1907 | ||
1908 | Remove a value corresponding to an object convertible to it or a range of values from the container. | |
1909 | In contrast to the \c std::set or <tt>std::map erase()</tt> method | |
1910 | it removes values equal to these passed as a range. Furthermore this method removes only | |
1911 | one value for each one passed in the range, not all equal values. | |
1912 | ||
1913 | It calls <tt>rtree::remove(ConvertibleOrRange const&)</tt>. | |
1914 | ||
1915 | \ingroup rtree_functions | |
1916 | ||
1917 | \param tree The spatial index. | |
1918 | \param conv_or_rng The object of type convertible to value_type or the range of values. | |
1919 | ||
1920 | \return The number of removed values. | |
1921 | */ | |
1922 | template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, | |
1923 | typename ConvertibleOrRange> | |
1924 | inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type | |
1925 | remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, | |
1926 | ConvertibleOrRange const& conv_or_rng) | |
1927 | { | |
1928 | return tree.remove(conv_or_rng); | |
1929 | } | |
1930 | ||
1931 | /*! | |
1932 | \brief Finds values meeting passed predicates e.g. nearest to some Point and/or intersecting some Box. | |
1933 | ||
1934 | This query function performs spatial and k-nearest neighbor searches. It allows to pass a set of predicates. | |
1935 | Values will be returned only if all predicates are met. | |
1936 | ||
1937 | <b>Spatial predicates</b> | |
1938 | ||
1939 | Spatial predicates may be generated by one of the functions listed below: | |
1940 | \li \c boost::geometry::index::contains(), | |
1941 | \li \c boost::geometry::index::covered_by(), | |
1942 | \li \c boost::geometry::index::covers(), | |
1943 | \li \c boost::geometry::index::disjoint(), | |
1944 | \li \c boost::geometry::index::intersects(), | |
1945 | \li \c boost::geometry::index::overlaps(), | |
1946 | \li \c boost::geometry::index::within(), | |
1947 | ||
1948 | It is possible to negate spatial predicates: | |
1949 | \li <tt>! boost::geometry::index::contains()</tt>, | |
1950 | \li <tt>! boost::geometry::index::covered_by()</tt>, | |
1951 | \li <tt>! boost::geometry::index::covers()</tt>, | |
1952 | \li <tt>! boost::geometry::index::disjoint()</tt>, | |
1953 | \li <tt>! boost::geometry::index::intersects()</tt>, | |
1954 | \li <tt>! boost::geometry::index::overlaps()</tt>, | |
1955 | \li <tt>! boost::geometry::index::within()</tt> | |
1956 | ||
1957 | <b>Satisfies predicate</b> | |
1958 | ||
1959 | This is a special kind of predicate which allows to pass a user-defined function or function object which checks | |
1960 | if Value should be returned by the query. It's generated by: | |
1961 | \li \c boost::geometry::index::satisfies(). | |
1962 | ||
1963 | <b>Nearest predicate</b> | |
1964 | ||
1965 | If the nearest predicate is passed a k-nearest neighbor search will be performed. This query will result | |
1966 | in returning k values to the output iterator. Only one nearest predicate may be passed to the query. | |
1967 | It may be generated by: | |
1968 | \li \c boost::geometry::index::nearest(). | |
1969 | ||
1970 | <b>Connecting predicates</b> | |
1971 | ||
1972 | Predicates may be passed together connected with \c operator&&(). | |
1973 | ||
1974 | \par Example | |
1975 | \verbatim | |
1976 | // return elements intersecting box | |
1977 | bgi::query(tree, bgi::intersects(box), std::back_inserter(result)); | |
1978 | // return elements intersecting poly but not within box | |
1979 | bgi::query(tree, bgi::intersects(poly) && !bgi::within(box), std::back_inserter(result)); | |
1980 | // return elements overlapping box and meeting my_fun value predicate | |
1981 | bgi::query(tree, bgi::overlaps(box) && bgi::satisfies(my_fun), std::back_inserter(result)); | |
1982 | // return 5 elements nearest to pt and elements are intersecting box | |
1983 | bgi::query(tree, bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result)); | |
1984 | ||
1985 | // For each found value do_something (it is a type of function object) | |
1986 | tree.query(bgi::intersects(box), | |
1987 | boost::make_function_output_iterator(do_something())); | |
1988 | \endverbatim | |
1989 | ||
1990 | \par Throws | |
1991 | If Value copy constructor or copy assignment throws. | |
1992 | ||
1993 | \warning | |
b32b8144 | 1994 | Only one \c nearest() predicate may be passed to the query. Passing more of them results in compile-time error. |
7c673cae FG |
1995 | |
1996 | \ingroup rtree_functions | |
1997 | ||
1998 | \param tree The rtree. | |
1999 | \param predicates Predicates. | |
2000 | \param out_it The output iterator, e.g. generated by std::back_inserter(). | |
2001 | ||
2002 | \return The number of values found. | |
2003 | */ | |
2004 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, | |
2005 | typename Predicates, typename OutIter> inline | |
2006 | typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type | |
2007 | query(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree, | |
2008 | Predicates const& predicates, | |
2009 | OutIter out_it) | |
2010 | { | |
2011 | return tree.query(predicates, out_it); | |
2012 | } | |
2013 | ||
2014 | /*! | |
2015 | \brief Returns the query iterator pointing at the begin of the query range. | |
2016 | ||
2017 | This method returns the iterator which may be used to perform iterative queries. For the information | |
2018 | about the predicates which may be passed to this method see query(). | |
2019 | ||
2020 | \par Example | |
2021 | \verbatim | |
2022 | std::for_each(bgi::qbegin(tree, bgi::nearest(pt, 3)), bgi::qend(tree), do_something()); | |
2023 | \endverbatim | |
2024 | ||
2025 | \par Iterator category | |
2026 | ForwardIterator | |
2027 | ||
2028 | \par Throws | |
2029 | If predicates copy throws. | |
2030 | If allocation throws. | |
2031 | ||
2032 | \warning | |
2033 | The modification of the rtree may invalidate the iterators. | |
2034 | ||
2035 | \ingroup rtree_functions | |
2036 | ||
2037 | \param tree The rtree. | |
2038 | \param predicates Predicates. | |
2039 | ||
2040 | \return The iterator pointing at the begin of the query range. | |
2041 | */ | |
2042 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, | |
2043 | typename Predicates> inline | |
2044 | typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_query_iterator | |
2045 | qbegin(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree, | |
2046 | Predicates const& predicates) | |
2047 | { | |
2048 | return tree.qbegin(predicates); | |
2049 | } | |
2050 | ||
2051 | /*! | |
2052 | \brief Returns the query iterator pointing at the end of the query range. | |
2053 | ||
2054 | This method returns the iterator which may be used to check if the query has ended. | |
2055 | ||
2056 | \par Example | |
2057 | \verbatim | |
2058 | std::for_each(bgi::qbegin(tree, bgi::nearest(pt, 3)), bgi::qend(tree), do_something()); | |
2059 | \endverbatim | |
2060 | ||
2061 | \par Iterator category | |
2062 | ForwardIterator | |
2063 | ||
2064 | \par Throws | |
2065 | Nothing | |
2066 | ||
2067 | \warning | |
2068 | The modification of the rtree may invalidate the iterators. | |
2069 | ||
2070 | \ingroup rtree_functions | |
2071 | ||
2072 | \return The iterator pointing at the end of the query range. | |
2073 | */ | |
2074 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline | |
2075 | typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_query_iterator | |
2076 | qend(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree) | |
2077 | { | |
2078 | return tree.qend(); | |
2079 | } | |
2080 | ||
2081 | /*! | |
2082 | \brief Returns the iterator pointing at the begin of the rtree values range. | |
2083 | ||
2084 | This method returns the iterator which may be used to iterate over all values | |
2085 | stored in the rtree. | |
2086 | ||
2087 | \par Example | |
2088 | \verbatim | |
2089 | std::for_each(bgi::begin(tree), bgi::end(tree), do_something()); | |
2090 | // the same as | |
2091 | std::for_each(boost::begin(tree), boost::end(tree), do_something()); | |
2092 | \endverbatim | |
2093 | ||
2094 | \par Iterator category | |
2095 | ForwardIterator | |
2096 | ||
2097 | \par Throws | |
2098 | If allocation throws. | |
2099 | ||
2100 | \warning | |
2101 | The modification of the rtree may invalidate the iterators. | |
2102 | ||
2103 | \ingroup rtree_functions | |
2104 | ||
2105 | \return The iterator pointing at the begin of the range. | |
2106 | */ | |
2107 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline | |
2108 | typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_iterator | |
2109 | begin(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree) | |
2110 | { | |
2111 | return tree.begin(); | |
2112 | } | |
2113 | ||
2114 | /*! | |
2115 | \brief Returns the iterator pointing at the end of the rtree values range. | |
2116 | ||
2117 | This method returns the iterator which may be compared with the iterator returned by begin() | |
2118 | in order to check if the iteration has ended. | |
2119 | ||
2120 | \par Example | |
2121 | \verbatim | |
2122 | std::for_each(bgi::begin(tree), bgi::end(tree), do_something()); | |
2123 | // the same as | |
2124 | std::for_each(boost::begin(tree), boost::end(tree), do_something()); | |
2125 | \endverbatim | |
2126 | ||
2127 | \par Iterator category | |
2128 | ForwardIterator | |
2129 | ||
2130 | \par Throws | |
2131 | Nothing. | |
2132 | ||
2133 | \warning | |
2134 | The modification of the rtree may invalidate the iterators. | |
2135 | ||
2136 | \ingroup rtree_functions | |
2137 | ||
2138 | \return The iterator pointing at the end of the range. | |
2139 | */ | |
2140 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline | |
2141 | typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_iterator | |
2142 | end(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree) | |
2143 | { | |
2144 | return tree.end(); | |
2145 | } | |
2146 | ||
2147 | /*! | |
2148 | \brief Remove all values from the index. | |
2149 | ||
2150 | It calls \c rtree::clear(). | |
2151 | ||
2152 | \ingroup rtree_functions | |
2153 | ||
2154 | \param tree The spatial index. | |
2155 | */ | |
2156 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> | |
2157 | inline void clear(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree) | |
2158 | { | |
2159 | return tree.clear(); | |
2160 | } | |
2161 | ||
2162 | /*! | |
2163 | \brief Get the number of values stored in the index. | |
2164 | ||
2165 | It calls \c rtree::size(). | |
2166 | ||
2167 | \ingroup rtree_functions | |
2168 | ||
2169 | \param tree The spatial index. | |
2170 | ||
2171 | \return The number of values stored in the index. | |
2172 | */ | |
2173 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> | |
2174 | inline size_t size(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree) | |
2175 | { | |
2176 | return tree.size(); | |
2177 | } | |
2178 | ||
2179 | /*! | |
2180 | \brief Query if there are no values stored in the index. | |
2181 | ||
2182 | It calls \c rtree::empty(). | |
2183 | ||
2184 | \ingroup rtree_functions | |
2185 | ||
2186 | \param tree The spatial index. | |
2187 | ||
2188 | \return true if there are no values in the index. | |
2189 | */ | |
2190 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> | |
2191 | inline bool empty(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree) | |
2192 | { | |
2193 | return tree.bounds(); | |
2194 | } | |
2195 | ||
2196 | /*! | |
2197 | \brief Get the box containing all stored values or an invalid box if the index has no values. | |
2198 | ||
2199 | It calls \c rtree::envelope(). | |
2200 | ||
2201 | \ingroup rtree_functions | |
2202 | ||
2203 | \param tree The spatial index. | |
2204 | ||
2205 | \return The box containing all stored values or an invalid box. | |
2206 | */ | |
2207 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> | |
2208 | inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::bounds_type | |
2209 | bounds(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree) | |
2210 | { | |
2211 | return tree.bounds(); | |
2212 | } | |
2213 | ||
2214 | /*! | |
2215 | \brief Exchanges the contents of the container with those of other. | |
2216 | ||
2217 | It calls \c rtree::swap(). | |
2218 | ||
2219 | \ingroup rtree_functions | |
2220 | ||
2221 | \param l The first rtree. | |
2222 | \param r The second rtree. | |
2223 | */ | |
2224 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> | |
2225 | inline void swap(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & l, | |
2226 | rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & r) | |
2227 | { | |
2228 | return l.swap(r); | |
2229 | } | |
2230 | ||
2231 | }}} // namespace boost::geometry::index | |
2232 | ||
2233 | // Boost.Range adaptation | |
2234 | namespace boost { | |
2235 | ||
2236 | template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> | |
2237 | struct range_mutable_iterator | |
2238 | < | |
2239 | boost::geometry::index::rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> | |
2240 | > | |
2241 | { | |
2242 | typedef typename boost::geometry::index::rtree | |
2243 | < | |
2244 | Value, Parameters, IndexableGetter, EqualTo, Allocator | |
2245 | >::const_iterator type; | |
2246 | }; | |
2247 | ||
2248 | } // namespace boost | |
2249 | ||
7c673cae FG |
2250 | #include <boost/geometry/index/detail/config_end.hpp> |
2251 | ||
2252 | #endif // BOOST_GEOMETRY_INDEX_RTREE_HPP |