1 // Boost.Geometry Index
3 // R-tree implementation
5 // Copyright (c) 2008 Federico J. Fernandez.
6 // Copyright (c) 2011-2017 Adam Wulkiewicz, Lodz, Poland.
8 // Use, modification and distribution is subject to the Boost Software License,
9 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_GEOMETRY_INDEX_RTREE_HPP
13 #define BOOST_GEOMETRY_INDEX_RTREE_HPP
19 #include <boost/tuple/tuple.hpp>
20 #include <boost/move/move.hpp>
23 #include <boost/geometry/algorithms/detail/comparable_distance/interface.hpp>
24 #include <boost/geometry/algorithms/detail/covered_by/interface.hpp>
25 #include <boost/geometry/algorithms/detail/disjoint/interface.hpp>
26 #include <boost/geometry/algorithms/detail/equals/interface.hpp>
27 #include <boost/geometry/algorithms/detail/intersects/interface.hpp>
28 #include <boost/geometry/algorithms/detail/overlaps/interface.hpp>
29 #include <boost/geometry/algorithms/detail/touches/interface.hpp>
30 #include <boost/geometry/algorithms/detail/within/interface.hpp>
31 #include <boost/geometry/algorithms/centroid.hpp>
33 #include <boost/geometry/geometries/point.hpp>
34 #include <boost/geometry/geometries/box.hpp>
36 // Boost.Geometry.Index
37 #include <boost/geometry/index/detail/config_begin.hpp>
39 #include <boost/geometry/index/detail/assert.hpp>
40 #include <boost/geometry/index/detail/exception.hpp>
42 #include <boost/geometry/index/detail/rtree/options.hpp>
44 #include <boost/geometry/index/indexable.hpp>
45 #include <boost/geometry/index/equal_to.hpp>
47 #include <boost/geometry/index/detail/translator.hpp>
49 #include <boost/geometry/index/predicates.hpp>
50 #include <boost/geometry/index/distance_predicates.hpp>
51 #include <boost/geometry/index/detail/rtree/adaptors.hpp>
53 #include <boost/geometry/index/detail/meta.hpp>
54 #include <boost/geometry/index/detail/utilities.hpp>
55 #include <boost/geometry/index/detail/rtree/node/node.hpp>
57 #include <boost/geometry/index/detail/algorithms/is_valid.hpp>
59 #include <boost/geometry/index/detail/rtree/visitors/insert.hpp>
60 #include <boost/geometry/index/detail/rtree/visitors/iterator.hpp>
61 #include <boost/geometry/index/detail/rtree/visitors/remove.hpp>
62 #include <boost/geometry/index/detail/rtree/visitors/copy.hpp>
63 #include <boost/geometry/index/detail/rtree/visitors/destroy.hpp>
64 #include <boost/geometry/index/detail/rtree/visitors/spatial_query.hpp>
65 #include <boost/geometry/index/detail/rtree/visitors/distance_query.hpp>
66 #include <boost/geometry/index/detail/rtree/visitors/count.hpp>
67 #include <boost/geometry/index/detail/rtree/visitors/children_box.hpp>
69 #include <boost/geometry/index/detail/rtree/linear/linear.hpp>
70 #include <boost/geometry/index/detail/rtree/quadratic/quadratic.hpp>
71 #include <boost/geometry/index/detail/rtree/rstar/rstar.hpp>
72 //#include <boost/geometry/extensions/index/detail/rtree/kmeans/kmeans.hpp>
74 #include <boost/geometry/index/detail/rtree/pack_create.hpp>
76 #include <boost/geometry/index/inserter.hpp>
78 #include <boost/geometry/index/detail/rtree/utilities/view.hpp>
80 #include <boost/geometry/index/detail/rtree/iterators.hpp>
81 #include <boost/geometry/index/detail/rtree/query_iterators.hpp>
83 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
85 #include <boost/geometry/index/detail/serialization.hpp>
88 // TODO change the name to bounding_tree
91 \defgroup rtree_functions R-tree free functions (boost::geometry::index::)
94 namespace boost { namespace geometry { namespace index {
97 \brief The R-tree spatial index.
99 This is self-balancing spatial index capable to store various types of Values
100 and balancing algorithms.
103 The user must pass a type defining the Parameters which will
104 be used in rtree creation process. This type is used e.g. to specify balancing
105 algorithm with specific parameters like min and max number of elements in node.
108 Predefined algorithms with compile-time parameters are:
109 \li <tt>boost::geometry::index::linear</tt>,
110 \li <tt>boost::geometry::index::quadratic</tt>,
111 \li <tt>boost::geometry::index::rstar</tt>.
114 Predefined algorithms with run-time parameters are:
115 \li \c boost::geometry::index::dynamic_linear,
116 \li \c boost::geometry::index::dynamic_quadratic,
117 \li \c boost::geometry::index::dynamic_rstar.
120 The object of IndexableGetter type translates from Value to Indexable each time
121 r-tree requires it. This means that this operation is done for each Value
122 access. Therefore the IndexableGetter should return the Indexable by
123 a reference type. The Indexable should not be calculated since it could harm
124 the performance. The default IndexableGetter can translate all types adapted
125 to Point, Box or Segment concepts (called Indexables). Furthermore, it can
126 handle <tt>std::pair<Indexable, T></tt>, <tt>boost::tuple<Indexable, ...></tt>
127 and <tt>std::tuple<Indexable, ...></tt> when possible. For example, for Value
128 of type <tt>std::pair<Box, int></tt>, the default IndexableGetter translates
129 from <tt>std::pair<Box, int> const&</tt> to <tt>Box const&</tt>.
132 The object of EqualTo type compares Values and returns <tt>true</tt> if they
133 are equal. It's similar to <tt>std::equal_to<></tt>. The default EqualTo
134 returns the result of <tt>boost::geometry::equals()</tt> for types adapted to
135 some Geometry concept defined in Boost.Geometry and the result of
136 <tt>operator==</tt> for other types. Components of Pairs and Tuples are
137 compared left-to-right.
139 \tparam Value The type of objects stored in the container.
140 \tparam Parameters Compile-time parameters.
141 \tparam IndexableGetter The function object extracting Indexable from Value.
142 \tparam EqualTo The function object comparing objects of type Value.
143 \tparam Allocator The allocator used to allocate/deallocate memory,
144 construct/destroy nodes and Values.
150 typename IndexableGetter = index::indexable<Value>,
151 typename EqualTo = index::equal_to<Value>,
152 typename Allocator = std::allocator<Value>
156 BOOST_COPYABLE_AND_MOVABLE(rtree)
159 /*! \brief The type of Value stored in the container. */
160 typedef Value value_type;
161 /*! \brief R-tree parameters type. */
162 typedef Parameters parameters_type;
163 /*! \brief The function object extracting Indexable from Value. */
164 typedef IndexableGetter indexable_getter;
165 /*! \brief The function object comparing objects of type Value. */
166 typedef EqualTo value_equal;
167 /*! \brief The type of allocator used by the container. */
168 typedef Allocator allocator_type;
170 // TODO: SHOULD THIS TYPE BE REMOVED?
171 /*! \brief The Indexable type to which Value is translated. */
172 typedef typename index::detail::indexable_type<
173 detail::translator<IndexableGetter, EqualTo>
174 >::type indexable_type;
176 /*! \brief The Box type used by the R-tree. */
177 typedef geometry::model::box<
178 geometry::model::point<
179 typename coordinate_type<indexable_type>::type,
180 dimension<indexable_type>::value,
181 typename coordinate_system<indexable_type>::type
188 typedef detail::translator<IndexableGetter, EqualTo> translator_type;
190 typedef bounds_type box_type;
191 typedef typename detail::rtree::options_type<Parameters>::type options_type;
192 typedef typename options_type::node_tag node_tag;
193 typedef detail::rtree::allocators<allocator_type, value_type, typename options_type::parameters_type, box_type, node_tag> allocators_type;
195 typedef typename detail::rtree::node<value_type, typename options_type::parameters_type, box_type, allocators_type, node_tag>::type node;
196 typedef typename detail::rtree::internal_node<value_type, typename options_type::parameters_type, box_type, allocators_type, node_tag>::type internal_node;
197 typedef typename detail::rtree::leaf<value_type, typename options_type::parameters_type, box_type, allocators_type, node_tag>::type leaf;
199 typedef typename allocators_type::node_pointer node_pointer;
200 typedef ::boost::container::allocator_traits<Allocator> allocator_traits_type;
201 typedef detail::rtree::subtree_destroyer<value_type, options_type, translator_type, box_type, allocators_type> subtree_destroyer;
203 friend class detail::rtree::utilities::view<rtree>;
204 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
205 friend class detail::rtree::private_view<rtree>;
206 friend class detail::rtree::const_private_view<rtree>;
211 /*! \brief Type of reference to Value. */
212 typedef typename allocators_type::reference reference;
213 /*! \brief Type of reference to const Value. */
214 typedef typename allocators_type::const_reference const_reference;
215 /*! \brief Type of pointer to Value. */
216 typedef typename allocators_type::pointer pointer;
217 /*! \brief Type of pointer to const Value. */
218 typedef typename allocators_type::const_pointer const_pointer;
219 /*! \brief Type of difference type. */
220 typedef typename allocators_type::difference_type difference_type;
221 /*! \brief Unsigned integral type used by the container. */
222 typedef typename allocators_type::size_type size_type;
224 /*! \brief Type of const iterator, category ForwardIterator. */
225 typedef index::detail::rtree::iterators::iterator
227 value_type, options_type, translator_type, box_type, allocators_type
230 /*! \brief Type of const query iterator, category ForwardIterator. */
231 typedef index::detail::rtree::iterators::query_iterator
233 value_type, allocators_type
234 > const_query_iterator;
239 \brief The constructor.
241 \param parameters The parameters object.
242 \param getter The function object extracting Indexable from Value.
243 \param equal The function object comparing Values.
246 If allocator default constructor throws.
248 inline explicit rtree(parameters_type const& parameters = parameters_type(),
249 indexable_getter const& getter = indexable_getter(),
250 value_equal const& equal = value_equal())
251 : m_members(getter, equal, parameters)
255 \brief The constructor.
257 \param parameters The parameters object.
258 \param getter The function object extracting Indexable from Value.
259 \param equal The function object comparing Values.
260 \param allocator The allocator object.
263 If allocator copy constructor throws.
265 inline rtree(parameters_type const& parameters,
266 indexable_getter const& getter,
267 value_equal const& equal,
268 allocator_type const& allocator)
269 : m_members(getter, equal, parameters, allocator)
273 \brief The constructor.
275 The tree is created using packing algorithm.
277 \param first The beginning of the range of Values.
278 \param last The end of the range of Values.
279 \param parameters The parameters object.
280 \param getter The function object extracting Indexable from Value.
281 \param equal The function object comparing Values.
282 \param allocator The allocator object.
285 \li If allocator copy constructor throws.
286 \li If Value copy constructor or copy assignment throws.
287 \li If allocation throws or returns invalid value.
289 template<typename Iterator>
290 inline rtree(Iterator first, Iterator last,
291 parameters_type const& parameters = parameters_type(),
292 indexable_getter const& getter = indexable_getter(),
293 value_equal const& equal = value_equal(),
294 allocator_type const& allocator = allocator_type())
295 : m_members(getter, equal, parameters, allocator)
297 typedef detail::rtree::pack<value_type, options_type, translator_type, box_type, allocators_type> pack;
298 size_type vc = 0, ll = 0;
299 m_members.root = pack::apply(first, last, vc, ll,
300 m_members.parameters(), m_members.translator(), m_members.allocators());
301 m_members.values_count = vc;
302 m_members.leafs_level = ll;
306 \brief The constructor.
308 The tree is created using packing algorithm.
310 \param rng The range of Values.
311 \param parameters The parameters object.
312 \param getter The function object extracting Indexable from Value.
313 \param equal The function object comparing Values.
314 \param allocator The allocator object.
317 \li If allocator copy constructor throws.
318 \li If Value copy constructor or copy assignment throws.
319 \li If allocation throws or returns invalid value.
321 template<typename Range>
322 inline explicit rtree(Range const& rng,
323 parameters_type const& parameters = parameters_type(),
324 indexable_getter const& getter = indexable_getter(),
325 value_equal const& equal = value_equal(),
326 allocator_type const& allocator = allocator_type())
327 : m_members(getter, equal, parameters, allocator)
329 typedef detail::rtree::pack<value_type, options_type, translator_type, box_type, allocators_type> pack;
330 size_type vc = 0, ll = 0;
331 m_members.root = pack::apply(::boost::begin(rng), ::boost::end(rng), vc, ll,
332 m_members.parameters(), m_members.translator(), m_members.allocators());
333 m_members.values_count = vc;
334 m_members.leafs_level = ll;
338 \brief The destructor.
345 this->raw_destroy(*this);
349 \brief The copy constructor.
351 It uses parameters, translator and allocator from the source tree.
353 \param src The rtree which content will be copied.
356 \li If allocator copy constructor throws.
357 \li If Value copy constructor throws.
358 \li If allocation throws or returns invalid value.
360 inline rtree(rtree const& src)
361 : m_members(src.m_members.indexable_getter(),
362 src.m_members.equal_to(),
363 src.m_members.parameters(),
364 allocator_traits_type::select_on_container_copy_construction(src.get_allocator()))
366 this->raw_copy(src, *this, false);
370 \brief The copy constructor.
372 It uses Parameters and translator from the source tree.
374 \param src The rtree which content will be copied.
375 \param allocator The allocator which will be used.
378 \li If allocator copy constructor throws.
379 \li If Value copy constructor throws.
380 \li If allocation throws or returns invalid value.
382 inline rtree(rtree const& src, allocator_type const& allocator)
383 : m_members(src.m_members.indexable_getter(),
384 src.m_members.equal_to(),
385 src.m_members.parameters(), allocator)
387 this->raw_copy(src, *this, false);
391 \brief The moving constructor.
393 It uses parameters, translator and allocator from the source tree.
395 \param src The rtree which content will be moved.
400 inline rtree(BOOST_RV_REF(rtree) src)
401 : m_members(src.m_members.indexable_getter(),
402 src.m_members.equal_to(),
403 src.m_members.parameters(),
404 boost::move(src.m_members.allocators()))
406 boost::swap(m_members.values_count, src.m_members.values_count);
407 boost::swap(m_members.leafs_level, src.m_members.leafs_level);
408 boost::swap(m_members.root, src.m_members.root);
412 \brief The moving constructor.
414 It uses parameters and translator from the source tree.
416 \param src The rtree which content will be moved.
417 \param allocator The allocator.
420 \li If allocator copy constructor throws.
421 \li If Value copy constructor throws (only if allocators aren't equal).
422 \li If allocation throws or returns invalid value (only if allocators aren't equal).
424 inline rtree(BOOST_RV_REF(rtree) src, allocator_type const& allocator)
425 : m_members(src.m_members.indexable_getter(),
426 src.m_members.equal_to(),
427 src.m_members.parameters(),
430 if ( src.m_members.allocators() == allocator )
432 boost::swap(m_members.values_count, src.m_members.values_count);
433 boost::swap(m_members.leafs_level, src.m_members.leafs_level);
434 boost::swap(m_members.root, src.m_members.root);
438 this->raw_copy(src, *this, false);
443 \brief The assignment operator.
445 It uses parameters and translator from the source tree.
447 \param src The rtree which content will be copied.
450 \li If Value copy constructor throws.
451 \li If allocation throws.
452 \li If allocation throws or returns invalid value.
454 inline rtree & operator=(BOOST_COPY_ASSIGN_REF(rtree) src)
458 allocators_type & this_allocs = m_members.allocators();
459 allocators_type const& src_allocs = src.m_members.allocators();
461 // NOTE: if propagate is true for std allocators on darwin 4.2.1, glibc++
462 // (allocators stored as base classes of members_holder)
463 // copying them changes values_count, in this case it doesn't cause errors since data must be copied
465 typedef boost::mpl::bool_<
466 allocator_traits_type::propagate_on_container_copy_assignment::value
469 if ( propagate::value && !(this_allocs == src_allocs) )
470 this->raw_destroy(*this);
471 detail::assign_cond(this_allocs, src_allocs, propagate());
473 // It uses m_allocators
474 this->raw_copy(src, *this, true);
481 \brief The moving assignment.
483 It uses parameters and translator from the source tree.
485 \param src The rtree which content will be moved.
488 Only if allocators aren't equal.
489 \li If Value copy constructor throws.
490 \li If allocation throws or returns invalid value.
492 inline rtree & operator=(BOOST_RV_REF(rtree) src)
496 allocators_type & this_allocs = m_members.allocators();
497 allocators_type & src_allocs = src.m_members.allocators();
499 if ( this_allocs == src_allocs )
501 this->raw_destroy(*this);
503 m_members.indexable_getter() = src.m_members.indexable_getter();
504 m_members.equal_to() = src.m_members.equal_to();
505 m_members.parameters() = src.m_members.parameters();
507 boost::swap(m_members.values_count, src.m_members.values_count);
508 boost::swap(m_members.leafs_level, src.m_members.leafs_level);
509 boost::swap(m_members.root, src.m_members.root);
511 // NOTE: if propagate is true for std allocators on darwin 4.2.1, glibc++
512 // (allocators stored as base classes of members_holder)
513 // moving them changes values_count
515 typedef boost::mpl::bool_<
516 allocator_traits_type::propagate_on_container_move_assignment::value
518 detail::move_cond(this_allocs, src_allocs, propagate());
522 // TODO - shouldn't here propagate_on_container_copy_assignment be checked like in operator=(const&)?
524 // It uses m_allocators
525 this->raw_copy(src, *this, true);
533 \brief Swaps contents of two rtrees.
535 Parameters, translator and allocators are swapped as well.
537 \param other The rtree which content will be swapped with this rtree content.
540 If allocators swap throws.
542 void swap(rtree & other)
544 boost::swap(m_members.indexable_getter(), other.m_members.indexable_getter());
545 boost::swap(m_members.equal_to(), other.m_members.equal_to());
546 boost::swap(m_members.parameters(), other.m_members.parameters());
548 // NOTE: if propagate is true for std allocators on darwin 4.2.1, glibc++
549 // (allocators stored as base classes of members_holder)
550 // swapping them changes values_count
552 typedef boost::mpl::bool_<
553 allocator_traits_type::propagate_on_container_swap::value
555 detail::swap_cond(m_members.allocators(), other.m_members.allocators(), propagate());
557 boost::swap(m_members.values_count, other.m_members.values_count);
558 boost::swap(m_members.leafs_level, other.m_members.leafs_level);
559 boost::swap(m_members.root, other.m_members.root);
563 \brief Insert a value to the index.
565 \param value The value which will be stored in the container.
568 \li If Value copy constructor or copy assignment throws.
569 \li If allocation throws or returns invalid value.
572 This operation only guarantees that there will be no memory leaks.
573 After an exception is thrown the R-tree may be left in an inconsistent state,
574 elements must not be inserted or removed. Other operations are allowed however
575 some of them may return invalid data.
577 inline void insert(value_type const& value)
579 if ( !m_members.root )
582 this->raw_insert(value);
586 \brief Insert a range of values to the index.
588 \param first The beginning of the range of values.
589 \param last The end of the range of values.
592 \li If Value copy constructor or copy assignment throws.
593 \li If allocation throws or returns invalid value.
596 This operation only guarantees that there will be no memory leaks.
597 After an exception is thrown the R-tree may be left in an inconsistent state,
598 elements must not be inserted or removed. Other operations are allowed however
599 some of them may return invalid data.
601 template <typename Iterator>
602 inline void insert(Iterator first, Iterator last)
604 if ( !m_members.root )
607 for ( ; first != last ; ++first )
608 this->raw_insert(*first);
612 \brief Insert a value created using convertible object or a range of values to the index.
614 \param conv_or_rng An object of type convertible to value_type or a range of values.
617 \li If Value copy constructor or copy assignment throws.
618 \li If allocation throws or returns invalid value.
621 This operation only guarantees that there will be no memory leaks.
622 After an exception is thrown the R-tree may be left in an inconsistent state,
623 elements must not be inserted or removed. Other operations are allowed however
624 some of them may return invalid data.
626 template <typename ConvertibleOrRange>
627 inline void insert(ConvertibleOrRange const& conv_or_rng)
629 if ( !m_members.root )
632 typedef boost::mpl::bool_
634 boost::is_convertible<ConvertibleOrRange, value_type>::value
637 this->insert_dispatch(conv_or_rng, is_conv_t());
641 \brief Remove a value from the container.
643 In contrast to the \c std::set or <tt>std::map erase()</tt> method
644 this method removes only one value from the container.
646 \param value The value which will be removed from the container.
648 \return 1 if the value was removed, 0 otherwise.
651 \li If Value copy constructor or copy assignment throws.
652 \li If allocation throws or returns invalid value.
655 This operation only guarantees that there will be no memory leaks.
656 After an exception is thrown the R-tree may be left in an inconsistent state,
657 elements must not be inserted or removed. Other operations are allowed however
658 some of them may return invalid data.
660 inline size_type remove(value_type const& value)
662 if ( !m_members.root )
665 return this->raw_remove(value);
669 \brief Remove a range of values from the container.
671 In contrast to the \c std::set or <tt>std::map erase()</tt> method
672 it doesn't take iterators pointing to values stored in this container. It removes values equal
673 to these passed as a range. Furthermore this method removes only one value for each one passed
674 in the range, not all equal values.
676 \param first The beginning of the range of values.
677 \param last The end of the range of values.
679 \return The number of removed values.
682 \li If Value copy constructor or copy assignment throws.
683 \li If allocation throws or returns invalid value.
686 This operation only guarantees that there will be no memory leaks.
687 After an exception is thrown the R-tree may be left in an inconsistent state,
688 elements must not be inserted or removed. Other operations are allowed however
689 some of them may return invalid data.
691 template <typename Iterator>
692 inline size_type remove(Iterator first, Iterator last)
694 size_type result = 0;
696 if ( !m_members.root )
699 for ( ; first != last ; ++first )
700 result += this->raw_remove(*first);
705 \brief Remove value corresponding to an object convertible to it or a range of values from the container.
707 In contrast to the \c std::set or <tt>std::map erase()</tt> method
708 it removes values equal to these passed as a range. Furthermore, this method removes only
709 one value for each one passed in the range, not all equal values.
711 \param conv_or_rng The object of type convertible to value_type or a range of values.
713 \return The number of removed values.
716 \li If Value copy constructor or copy assignment throws.
717 \li If allocation throws or returns invalid value.
720 This operation only guarantees that there will be no memory leaks.
721 After an exception is thrown the R-tree may be left in an inconsistent state,
722 elements must not be inserted or removed. Other operations are allowed however
723 some of them may return invalid data.
725 template <typename ConvertibleOrRange>
726 inline size_type remove(ConvertibleOrRange const& conv_or_rng)
728 if ( !m_members.root )
731 typedef boost::mpl::bool_
733 boost::is_convertible<ConvertibleOrRange, value_type>::value
736 return this->remove_dispatch(conv_or_rng, is_conv_t());
740 \brief Finds values meeting passed predicates e.g. nearest to some Point and/or intersecting some Box.
742 This query function performs spatial and k-nearest neighbor searches. It allows to pass a set of predicates.
743 Values will be returned only if all predicates are met.
745 <b>Spatial predicates</b>
747 Spatial predicates may be generated by one of the functions listed below:
748 \li \c boost::geometry::index::contains(),
749 \li \c boost::geometry::index::covered_by(),
750 \li \c boost::geometry::index::covers(),
751 \li \c boost::geometry::index::disjoint(),
752 \li \c boost::geometry::index::intersects(),
753 \li \c boost::geometry::index::overlaps(),
754 \li \c boost::geometry::index::within(),
756 It is possible to negate spatial predicates:
757 \li <tt>! boost::geometry::index::contains()</tt>,
758 \li <tt>! boost::geometry::index::covered_by()</tt>,
759 \li <tt>! boost::geometry::index::covers()</tt>,
760 \li <tt>! boost::geometry::index::disjoint()</tt>,
761 \li <tt>! boost::geometry::index::intersects()</tt>,
762 \li <tt>! boost::geometry::index::overlaps()</tt>,
763 \li <tt>! boost::geometry::index::within()</tt>
765 <b>Satisfies predicate</b>
767 This is a special kind of predicate which allows to pass a user-defined function or function object which checks
768 if Value should be returned by the query. It's generated by:
769 \li \c boost::geometry::index::satisfies().
771 <b>Nearest predicate</b>
773 If the nearest predicate is passed a k-nearest neighbor search will be performed. This query will result
774 in returning k values to the output iterator. Only one nearest predicate may be passed to the query.
775 It may be generated by:
776 \li \c boost::geometry::index::nearest().
778 <b>Connecting predicates</b>
780 Predicates may be passed together connected with \c operator&&().
784 // return elements intersecting box
785 tree.query(bgi::intersects(box), std::back_inserter(result));
786 // return elements intersecting poly but not within box
787 tree.query(bgi::intersects(poly) && !bgi::within(box), std::back_inserter(result));
788 // return elements overlapping box and meeting my_fun unary predicate
789 tree.query(bgi::overlaps(box) && bgi::satisfies(my_fun), std::back_inserter(result));
790 // return 5 elements nearest to pt and elements are intersecting box
791 tree.query(bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result));
793 // For each found value do_something (it is a type of function object)
794 tree.query(bgi::intersects(box),
795 boost::make_function_output_iterator(do_something()));
797 // For each value stored in the rtree do_something
798 // always_true is a type of function object always returning true
799 tree.query(bgi::satisfies(always_true()),
800 boost::make_function_output_iterator(do_something()));
802 // C++11 (lambda expression)
803 tree.query(bgi::intersects(box),
804 boost::make_function_output_iterator([](value_type const& val){
808 // C++14 (generic lambda expression)
809 tree.query(bgi::intersects(box),
810 boost::make_function_output_iterator([](auto const& val){
816 If Value copy constructor or copy assignment throws.
817 If predicates copy throws.
820 Only one \c nearest() predicate may be passed to the query. Passing more of them results in compile-time error.
822 \param predicates Predicates.
823 \param out_it The output iterator, e.g. generated by std::back_inserter().
825 \return The number of values found.
827 template <typename Predicates, typename OutIter>
828 size_type query(Predicates const& predicates, OutIter out_it) const
830 if ( !m_members.root )
833 static const unsigned distance_predicates_count = detail::predicates_count_distance<Predicates>::value;
834 static const bool is_distance_predicate = 0 < distance_predicates_count;
835 BOOST_MPL_ASSERT_MSG((distance_predicates_count <= 1), PASS_ONLY_ONE_DISTANCE_PREDICATE, (Predicates));
837 return query_dispatch(predicates, out_it, boost::mpl::bool_<is_distance_predicate>());
841 \brief Returns a query iterator pointing at the begin of the query range.
843 This method returns an iterator which may be used to perform iterative queries.
844 For the information about predicates which may be passed to this method see query().
848 for ( Rtree::const_query_iterator it = tree.qbegin(bgi::nearest(pt, 10000)) ;
849 it != tree.qend() ; ++it )
851 // do something with value
852 if ( has_enough_nearest_values() )
857 for ( auto it = tree.qbegin(bgi::nearest(pt, 3)) ; it != tree.qend() ; ++it )
859 // do something with value
862 // C++14 (generic lambda expression)
863 std::for_each(tree.qbegin(bgi::nearest(pt, 3)), tree.qend(), [](auto const& val){
864 // do something with value
868 \par Iterator category
872 If predicates copy throws.
873 If allocation throws.
876 The modification of the rtree may invalidate the iterators.
878 \param predicates Predicates.
880 \return The iterator pointing at the begin of the query range.
882 template <typename Predicates>
883 const_query_iterator qbegin(Predicates const& predicates) const
885 return const_query_iterator(qbegin_(predicates));
889 \brief Returns a query iterator pointing at the end of the query range.
891 This method returns an iterator which may be used to check if the query has ended.
895 for ( Rtree::const_query_iterator it = tree.qbegin(bgi::nearest(pt, 10000)) ;
896 it != tree.qend() ; ++it )
898 // do something with value
899 if ( has_enough_nearest_values() )
904 for ( auto it = tree.qbegin(bgi::nearest(pt, 3)) ; it != tree.qend() ; ++it )
906 // do something with value
909 // C++14 (generic lambda expression)
910 std::for_each(tree.qbegin(bgi::nearest(pt, 3)), tree.qend(), [](auto const& val){
911 // do something with value
915 \par Iterator category
922 The modification of the rtree may invalidate the iterators.
924 \return The iterator pointing at the end of the query range.
926 const_query_iterator qend() const
928 return const_query_iterator();
931 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
935 \brief Returns a query iterator pointing at the begin of the query range.
937 This method returns an iterator which may be used to perform iterative queries.
938 For the information about predicates which may be passed to this method see query().
940 The type of the returned iterator depends on the type of passed Predicates but the iterator of this type
941 may be assigned to the variable of const_query_iterator type. If you'd like to use the type of the iterator
942 returned by this method you may get the type e.g. by using C++11 decltype or Boost.Typeof library.
943 This iterator may be compared with iterators returned by both versions of qend() method.
947 // Store the result in the container using std::copy() - it requires both iterators of the same type
948 std::copy(tree.qbegin_(bgi::intersects(box)), tree.qend_(bgi::intersects(box)), std::back_inserter(result));
950 // Store the result in the container using std::copy() and type-erased iterators
951 Rtree::const_query_iterator first = tree.qbegin_(bgi::intersects(box));
952 Rtree::const_query_iterator last = tree.qend_();
953 std::copy(first, last, std::back_inserter(result));
956 typedef BOOST_TYPEOF(tree.qbegin(bgi::nearest(pt, 10000))) Iter;
957 for ( Iter it = tree.qbegin_(bgi::nearest(pt, 10000)) ; it != tree.qend_() ; ++it )
959 // do something with value
960 if ( has_enough_nearest_values() )
965 for ( auto it = tree.qbegin_(bgi::nearest(pt, 10000)) ; it != tree.qend_() ; ++it )
967 // do something with value
968 if ( has_enough_nearest_values() )
973 \par Iterator category
977 If predicates copy throws.
978 If allocation throws.
981 The modification of the rtree may invalidate the iterators.
983 \param predicates Predicates.
985 \return The iterator pointing at the begin of the query range.
987 template <typename Predicates>
988 typename boost::mpl::if_c<
989 detail::predicates_count_distance<Predicates>::value == 0,
990 detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>,
991 detail::rtree::iterators::distance_query_iterator<
992 value_type, options_type, translator_type, box_type, allocators_type, Predicates,
993 detail::predicates_find_distance<Predicates>::value
996 qbegin_(Predicates const& predicates) const
998 static const unsigned distance_predicates_count = detail::predicates_count_distance<Predicates>::value;
999 BOOST_MPL_ASSERT_MSG((distance_predicates_count <= 1), PASS_ONLY_ONE_DISTANCE_PREDICATE, (Predicates));
1001 typedef typename boost::mpl::if_c<
1002 detail::predicates_count_distance<Predicates>::value == 0,
1003 detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>,
1004 detail::rtree::iterators::distance_query_iterator<
1005 value_type, options_type, translator_type, box_type, allocators_type, Predicates,
1006 detail::predicates_find_distance<Predicates>::value
1008 >::type iterator_type;
1010 if ( !m_members.root )
1011 return iterator_type(m_members.translator(), predicates);
1013 return iterator_type(m_members.root, m_members.translator(), predicates);
1017 \brief Returns the query iterator pointing at the end of the query range.
1019 This method returns the iterator which may be used to perform iterative queries. For the information
1020 about the predicates which may be passed to this method see query().
1022 The type of the returned iterator depends on the type of passed Predicates but the iterator of this type
1023 may be assigned to the variable of const_query_iterator type. If you'd like to use the type of the iterator
1024 returned by this method you may get the type e.g. by using C++11 decltype or Boost.Typeof library.
1026 The type of the iterator returned by this method is the same as the one returned by qbegin() to which
1027 the same predicates were passed.
1031 // Store the result in the container using std::copy() - it requires both iterators of the same type
1032 std::copy(tree.qbegin_(bgi::intersects(box)), tree.qend_(bgi::intersects(box)), std::back_inserter(result));
1035 \par Iterator category
1039 If predicates copy throws.
1042 The modification of the rtree may invalidate the iterators.
1044 \param predicates Predicates.
1046 \return The iterator pointing at the end of the query range.
1048 template <typename Predicates>
1049 typename boost::mpl::if_c<
1050 detail::predicates_count_distance<Predicates>::value == 0,
1051 detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>,
1052 detail::rtree::iterators::distance_query_iterator<
1053 value_type, options_type, translator_type, box_type, allocators_type, Predicates,
1054 detail::predicates_find_distance<Predicates>::value
1057 qend_(Predicates const& predicates) const
1059 static const unsigned distance_predicates_count = detail::predicates_count_distance<Predicates>::value;
1060 BOOST_MPL_ASSERT_MSG((distance_predicates_count <= 1), PASS_ONLY_ONE_DISTANCE_PREDICATE, (Predicates));
1062 typedef typename boost::mpl::if_c<
1063 detail::predicates_count_distance<Predicates>::value == 0,
1064 detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>,
1065 detail::rtree::iterators::distance_query_iterator<
1066 value_type, options_type, translator_type, box_type, allocators_type, Predicates,
1067 detail::predicates_find_distance<Predicates>::value
1069 >::type iterator_type;
1071 return iterator_type(m_members.translator(), predicates);
1075 \brief Returns the query iterator pointing at the end of the query range.
1077 This method returns the iterator which may be compared with the iterator returned by qbegin() in order to
1078 check if the query has ended.
1080 The type of the returned iterator is different than the type returned by qbegin() but the iterator of this type
1081 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
1082 method, which most certainly will be faster than the type-erased iterator, you may get the type
1083 e.g. by using C++11 decltype or Boost.Typeof library.
1085 The type of the iterator returned by this method is different than the type returned by qbegin().
1089 // Store the result in the container using std::copy() and type-erased iterators
1090 Rtree::const_query_iterator first = tree.qbegin_(bgi::intersects(box));
1091 Rtree::const_query_iterator last = tree.qend_();
1092 std::copy(first, last, std::back_inserter(result));
1095 typedef BOOST_TYPEOF(tree.qbegin(bgi::nearest(pt, 10000))) Iter;
1096 for ( Iter it = tree.qbegin_(bgi::nearest(pt, 10000)) ; it != tree.qend_() ; ++it )
1098 // do something with value
1099 if ( has_enough_nearest_values() )
1104 for ( auto it = tree.qbegin_(bgi::nearest(pt, 10000)) ; it != tree.qend_() ; ++it )
1106 // do something with value
1107 if ( has_enough_nearest_values() )
1112 \par Iterator category
1119 The modification of the rtree may invalidate the iterators.
1121 \return The iterator pointing at the end of the query range.
1123 detail::rtree::iterators::end_query_iterator<value_type, allocators_type>
1126 return detail::rtree::iterators::end_query_iterator<value_type, allocators_type>();
1132 \brief Returns the iterator pointing at the begin of the rtree values range.
1134 This method returns the iterator which may be used to iterate over all values
1135 stored in the rtree.
1139 // Copy all values into the vector
1140 std::copy(tree.begin(), tree.end(), std::back_inserter(vec));
1142 for ( Rtree::const_iterator it = tree.begin() ; it != tree.end() ; ++it )
1144 // do something with value
1148 for ( auto it = tree.begin() ; it != tree.end() ; ++it )
1150 // do something with value
1153 // C++14 (generic lambda expression)
1154 std::for_each(tree.begin(), tree.end(), [](auto const& val){
1155 // do something with value
1159 \par Iterator category
1163 If allocation throws.
1166 The modification of the rtree may invalidate the iterators.
1168 \return The iterator pointing at the begin of the range.
1170 const_iterator begin() const
1172 if ( !m_members.root )
1173 return const_iterator();
1175 return const_iterator(m_members.root);
1179 \brief Returns the iterator pointing at the end of the rtree values range.
1181 This method returns the iterator which may be compared with the iterator returned by begin()
1182 in order to check if the iteration has ended.
1186 for ( Rtree::const_iterator it = tree.begin() ; it != tree.end() ; ++it )
1188 // do something with value
1191 // C++11 (lambda expression)
1192 std::for_each(tree.begin(), tree.end(), [](value_type const& val){
1193 // do something with value
1197 \par Iterator category
1204 The modification of the rtree may invalidate the iterators.
1206 \return The iterator pointing at the end of the range.
1208 const_iterator end() const
1210 return const_iterator();
1214 \brief Returns the number of stored values.
1216 \return The number of stored values.
1221 inline size_type size() const
1223 return m_members.values_count;
1227 \brief Query if the container is empty.
1229 \return true if the container is empty.
1234 inline bool empty() const
1236 return 0 == m_members.values_count;
1240 \brief Removes all values stored in the container.
1247 this->raw_destroy(*this);
1251 \brief Returns the box able to contain all values stored in the container.
1253 Returns the box able to contain all values stored in the container.
1254 If the container is empty the result of \c geometry::assign_inverse() is returned.
1256 \return The box able to contain all values stored in the container or an invalid box if
1257 there are no values in the container.
1262 inline bounds_type bounds() const
1265 // in order to suppress the uninitialized variable warnings
1266 geometry::assign_inverse(result);
1268 if ( m_members.root )
1270 detail::rtree::visitors::children_box<value_type, options_type, translator_type, box_type, allocators_type>
1271 box_v(result, m_members.translator());
1272 detail::rtree::apply_visitor(box_v, *m_members.root);
1279 \brief Count Values or Indexables stored in the container.
1281 For indexable_type it returns the number of values which indexables equals the parameter.
1282 For value_type it returns the number of values which equals the parameter.
1284 \param vori The value or indexable which will be counted.
1286 \return The number of values found.
1291 template <typename ValueOrIndexable>
1292 size_type count(ValueOrIndexable const& vori) const
1294 if ( !m_members.root )
1297 // the input should be convertible to Value or Indexable type
1299 enum { as_val = 0, as_ind, dont_know };
1300 typedef boost::mpl::int_
1302 boost::is_same<ValueOrIndexable, value_type>::value ?
1304 boost::is_same<ValueOrIndexable, indexable_type>::value ?
1306 boost::is_convertible<ValueOrIndexable, indexable_type>::value ?
1308 boost::is_convertible<ValueOrIndexable, value_type>::value ?
1313 BOOST_MPL_ASSERT_MSG((variant::value != dont_know),
1314 PASSED_OBJECT_NOT_CONVERTIBLE_TO_VALUE_NOR_INDEXABLE_TYPE,
1315 (ValueOrIndexable));
1317 typedef typename boost::mpl::if_c
1319 variant::value == as_val,
1322 >::type value_or_indexable;
1324 // NOTE: If an object of convertible but not the same type is passed
1325 // into the function, here a temporary will be created.
1326 return this->template raw_count<value_or_indexable>(vori);
1330 \brief Returns parameters.
1332 \return The parameters object.
1337 inline parameters_type parameters() const
1339 return m_members.parameters();
1343 \brief Returns function retrieving Indexable from Value.
1345 \return The indexable_getter object.
1350 indexable_getter indexable_get() const
1352 return m_members.indexable_getter();
1356 \brief Returns function comparing Values
1358 \return The value_equal function.
1363 value_equal value_eq() const
1365 return m_members.equal_to();
1369 \brief Returns allocator used by the rtree.
1371 \return The allocator.
1374 If allocator copy constructor throws.
1376 allocator_type get_allocator() const
1378 return m_members.allocators().allocator();
1384 \brief Returns the translator object.
1386 \return The translator object.
1391 inline translator_type translator() const
1393 return m_members.translator();
1397 \brief Apply a visitor to the nodes structure in order to perform some operator.
1399 This function is not a part of the 'official' interface. However it makes
1400 possible e.g. to pass a visitor drawing the tree structure.
1402 \param visitor The visitor object.
1405 If Visitor::operator() throws.
1407 template <typename Visitor>
1408 inline void apply_visitor(Visitor & visitor) const
1410 if ( m_members.root )
1411 detail::rtree::apply_visitor(visitor, *m_members.root);
1415 \brief Returns the depth of the R-tree.
1417 This function is not a part of the 'official' interface.
1419 \return The depth of the R-tree.
1424 inline size_type depth() const
1426 return m_members.leafs_level;
1432 \pre Root node must exist - m_root != 0.
1434 \brief Insert a value to the index.
1436 \param value The value which will be stored in the container.
1438 \par Exception-safety
1441 inline void raw_insert(value_type const& value)
1443 BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist");
1444 // CONSIDER: alternative - ignore invalid indexable or throw an exception
1445 BOOST_GEOMETRY_INDEX_ASSERT(detail::is_valid(m_members.translator()(value)), "Indexable is invalid");
1447 detail::rtree::visitors::insert<
1449 value_type, options_type, translator_type, box_type, allocators_type,
1450 typename options_type::insert_tag
1451 > insert_v(m_members.root, m_members.leafs_level, value,
1452 m_members.parameters(), m_members.translator(), m_members.allocators());
1454 detail::rtree::apply_visitor(insert_v, *m_members.root);
1457 // Think about this: If exception is thrown, may the root be removed?
1458 // Or it is just cleared?
1461 // If exception is thrown, m_values_count may be invalid
1462 ++m_members.values_count;
1466 \brief Remove the value from the container.
1468 \param value The value which will be removed from the container.
1470 \par Exception-safety
1473 inline size_type raw_remove(value_type const& value)
1475 // TODO: awulkiew - assert for correct value (indexable) ?
1476 BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist");
1478 detail::rtree::visitors::remove<
1479 value_type, options_type, translator_type, box_type, allocators_type
1480 > remove_v(m_members.root, m_members.leafs_level, value,
1481 m_members.parameters(), m_members.translator(), m_members.allocators());
1483 detail::rtree::apply_visitor(remove_v, *m_members.root);
1485 // If exception is thrown, m_values_count may be invalid
1487 if ( remove_v.is_value_removed() )
1489 BOOST_GEOMETRY_INDEX_ASSERT(0 < m_members.values_count, "unexpected state");
1491 --m_members.values_count;
1500 \brief Create an empty R-tree i.e. new empty root node and clear other attributes.
1502 \par Exception-safety
1505 inline void raw_create()
1507 BOOST_GEOMETRY_INDEX_ASSERT(0 == m_members.root, "the tree is already created");
1509 m_members.root = detail::rtree::create_node<allocators_type, leaf>::apply(m_members.allocators()); // MAY THROW (N: alloc)
1510 m_members.values_count = 0;
1511 m_members.leafs_level = 0;
1515 \brief Destroy the R-tree i.e. all nodes and clear attributes.
1517 \param t The container which is going to be destroyed.
1519 \par Exception-safety
1522 inline void raw_destroy(rtree & t)
1524 if ( t.m_members.root )
1526 detail::rtree::visitors::destroy<value_type, options_type, translator_type, box_type, allocators_type>
1527 del_v(t.m_members.root, t.m_members.allocators());
1528 detail::rtree::apply_visitor(del_v, *t.m_members.root);
1530 t.m_members.root = 0;
1532 t.m_members.values_count = 0;
1533 t.m_members.leafs_level = 0;
1537 \brief Copy the R-tree i.e. whole nodes structure, values and other attributes.
1538 It uses destination's allocators to create the new structure.
1540 \param src The source R-tree.
1541 \param dst The destination R-tree.
1542 \param copy_tr_and_params If true, translator and parameters will also be copied.
1544 \par Exception-safety
1547 inline void raw_copy(rtree const& src, rtree & dst, bool copy_tr_and_params) const
1549 detail::rtree::visitors::copy<value_type, options_type, translator_type, box_type, allocators_type>
1550 copy_v(dst.m_members.allocators());
1552 if ( src.m_members.root )
1553 detail::rtree::apply_visitor(copy_v, *src.m_members.root); // MAY THROW (V, E: alloc, copy, N: alloc)
1555 if ( copy_tr_and_params )
1557 dst.m_members.indexable_getter() = src.m_members.indexable_getter();
1558 dst.m_members.equal_to() = src.m_members.equal_to();
1559 dst.m_members.parameters() = src.m_members.parameters();
1562 // TODO use subtree_destroyer
1563 if ( dst.m_members.root )
1565 detail::rtree::visitors::destroy<value_type, options_type, translator_type, box_type, allocators_type>
1566 del_v(dst.m_members.root, dst.m_members.allocators());
1567 detail::rtree::apply_visitor(del_v, *dst.m_members.root);
1568 dst.m_members.root = 0;
1571 dst.m_members.root = copy_v.result;
1572 dst.m_members.values_count = src.m_members.values_count;
1573 dst.m_members.leafs_level = src.m_members.leafs_level;
1577 \brief Insert a value corresponding to convertible object into the index.
1579 \param val_conv The object convertible to value.
1581 \par Exception-safety
1584 template <typename ValueConvertible>
1585 inline void insert_dispatch(ValueConvertible const& val_conv,
1586 boost::mpl::bool_<true> const& /*is_convertible*/)
1588 this->raw_insert(val_conv);
1592 \brief Insert a range of values into the index.
1594 \param rng The range of values.
1596 \par Exception-safety
1599 template <typename Range>
1600 inline void insert_dispatch(Range const& rng,
1601 boost::mpl::bool_<false> const& /*is_convertible*/)
1603 BOOST_MPL_ASSERT_MSG((detail::is_range<Range>::value),
1604 PASSED_OBJECT_IS_NOT_CONVERTIBLE_TO_VALUE_NOR_A_RANGE,
1607 typedef typename boost::range_const_iterator<Range>::type It;
1608 for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it )
1609 this->raw_insert(*it);
1613 \brief Remove a value corresponding to convertible object from the index.
1615 \param val_conv The object convertible to value.
1617 \par Exception-safety
1620 template <typename ValueConvertible>
1621 inline size_type remove_dispatch(ValueConvertible const& val_conv,
1622 boost::mpl::bool_<true> const& /*is_convertible*/)
1624 return this->raw_remove(val_conv);
1628 \brief Remove a range of values from the index.
1630 \param rng The range of values which will be removed from the container.
1632 \par Exception-safety
1635 template <typename Range>
1636 inline size_type remove_dispatch(Range const& rng,
1637 boost::mpl::bool_<false> const& /*is_convertible*/)
1639 BOOST_MPL_ASSERT_MSG((detail::is_range<Range>::value),
1640 PASSED_OBJECT_IS_NOT_CONVERTIBLE_TO_VALUE_NOR_A_RANGE,
1643 size_type result = 0;
1644 typedef typename boost::range_const_iterator<Range>::type It;
1645 for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it )
1646 result += this->raw_remove(*it);
1651 \brief Return values meeting predicates.
1653 \par Exception-safety
1656 template <typename Predicates, typename OutIter>
1657 size_type query_dispatch(Predicates const& predicates, OutIter out_it, boost::mpl::bool_<false> const& /*is_distance_predicate*/) const
1659 detail::rtree::visitors::spatial_query<value_type, options_type, translator_type, box_type, allocators_type, Predicates, OutIter>
1660 find_v(m_members.translator(), predicates, out_it);
1662 detail::rtree::apply_visitor(find_v, *m_members.root);
1664 return find_v.found_count;
1668 \brief Perform nearest neighbour search.
1670 \par Exception-safety
1673 template <typename Predicates, typename OutIter>
1674 size_type query_dispatch(Predicates const& predicates, OutIter out_it, boost::mpl::bool_<true> const& /*is_distance_predicate*/) const
1676 BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist");
1678 static const unsigned distance_predicate_index = detail::predicates_find_distance<Predicates>::value;
1679 detail::rtree::visitors::distance_query<
1686 distance_predicate_index,
1688 > distance_v(m_members.parameters(), m_members.translator(), predicates, out_it);
1690 detail::rtree::apply_visitor(distance_v, *m_members.root);
1692 return distance_v.finish();
1696 \brief Count elements corresponding to value or indexable.
1698 \par Exception-safety
1701 template <typename ValueOrIndexable>
1702 size_type raw_count(ValueOrIndexable const& vori) const
1704 BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist");
1706 detail::rtree::visitors::count
1714 > count_v(vori, m_members.translator());
1716 detail::rtree::apply_visitor(count_v, *m_members.root);
1718 return count_v.found_count;
1721 struct members_holder
1722 : public translator_type
1724 , public allocators_type
1727 members_holder(members_holder const&);
1728 members_holder & operator=(members_holder const&);
1731 template <typename IndGet, typename ValEq, typename Alloc>
1732 members_holder(IndGet const& ind_get,
1733 ValEq const& val_eq,
1734 Parameters const& parameters,
1735 BOOST_FWD_REF(Alloc) alloc)
1736 : translator_type(ind_get, val_eq)
1737 , Parameters(parameters)
1738 , allocators_type(boost::forward<Alloc>(alloc))
1744 template <typename IndGet, typename ValEq>
1745 members_holder(IndGet const& ind_get,
1746 ValEq const& val_eq,
1747 Parameters const& parameters)
1748 : translator_type(ind_get, val_eq)
1749 , Parameters(parameters)
1756 translator_type const& translator() const { return *this; }
1758 IndexableGetter const& indexable_getter() const { return *this; }
1759 IndexableGetter & indexable_getter() { return *this; }
1760 EqualTo const& equal_to() const { return *this; }
1761 EqualTo & equal_to() { return *this; }
1762 Parameters const& parameters() const { return *this; }
1763 Parameters & parameters() { return *this; }
1764 allocators_type const& allocators() const { return *this; }
1765 allocators_type & allocators() { return *this; }
1767 size_type values_count;
1768 size_type leafs_level;
1772 members_holder m_members;
1776 \brief Insert a value to the index.
1778 It calls <tt>rtree::insert(value_type const&)</tt>.
1780 \ingroup rtree_functions
1782 \param tree The spatial index.
1783 \param v The value which will be stored in the index.
1785 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
1786 inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
1793 \brief Insert a range of values to the index.
1795 It calls <tt>rtree::insert(Iterator, Iterator)</tt>.
1797 \ingroup rtree_functions
1799 \param tree The spatial index.
1800 \param first The beginning of the range of values.
1801 \param last The end of the range of values.
1803 template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
1805 inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
1806 Iterator first, Iterator last)
1808 tree.insert(first, last);
1812 \brief Insert a value created using convertible object or a range of values to the index.
1814 It calls <tt>rtree::insert(ConvertibleOrRange const&)</tt>.
1816 \ingroup rtree_functions
1818 \param tree The spatial index.
1819 \param conv_or_rng The object of type convertible to value_type or a range of values.
1821 template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
1822 typename ConvertibleOrRange>
1823 inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
1824 ConvertibleOrRange const& conv_or_rng)
1826 tree.insert(conv_or_rng);
1830 \brief Remove a value from the container.
1832 Remove a value from the container. In contrast to the \c std::set or <tt>std::map erase()</tt> method
1833 this function removes only one value from the container.
1835 It calls <tt>rtree::remove(value_type const&)</tt>.
1837 \ingroup rtree_functions
1839 \param tree The spatial index.
1840 \param v The value which will be removed from the index.
1842 \return 1 if value was removed, 0 otherwise.
1844 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
1845 inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
1846 remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
1849 return tree.remove(v);
1853 \brief Remove a range of values from the container.
1855 Remove a range of values from the container. In contrast to the \c std::set or <tt>std::map erase()</tt> method
1856 it doesn't take iterators pointing to values stored in this container. It removes values equal
1857 to these passed as a range. Furthermore this function removes only one value for each one passed
1858 in the range, not all equal values.
1860 It calls <tt>rtree::remove(Iterator, Iterator)</tt>.
1862 \ingroup rtree_functions
1864 \param tree The spatial index.
1865 \param first The beginning of the range of values.
1866 \param last The end of the range of values.
1868 \return The number of removed values.
1870 template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
1872 inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
1873 remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
1874 Iterator first, Iterator last)
1876 return tree.remove(first, last);
1880 \brief Remove a value corresponding to an object convertible to it or a range of values from the container.
1882 Remove a value corresponding to an object convertible to it or a range of values from the container.
1883 In contrast to the \c std::set or <tt>std::map erase()</tt> method
1884 it removes values equal to these passed as a range. Furthermore this method removes only
1885 one value for each one passed in the range, not all equal values.
1887 It calls <tt>rtree::remove(ConvertibleOrRange const&)</tt>.
1889 \ingroup rtree_functions
1891 \param tree The spatial index.
1892 \param conv_or_rng The object of type convertible to value_type or the range of values.
1894 \return The number of removed values.
1896 template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
1897 typename ConvertibleOrRange>
1898 inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
1899 remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
1900 ConvertibleOrRange const& conv_or_rng)
1902 return tree.remove(conv_or_rng);
1906 \brief Finds values meeting passed predicates e.g. nearest to some Point and/or intersecting some Box.
1908 This query function performs spatial and k-nearest neighbor searches. It allows to pass a set of predicates.
1909 Values will be returned only if all predicates are met.
1911 <b>Spatial predicates</b>
1913 Spatial predicates may be generated by one of the functions listed below:
1914 \li \c boost::geometry::index::contains(),
1915 \li \c boost::geometry::index::covered_by(),
1916 \li \c boost::geometry::index::covers(),
1917 \li \c boost::geometry::index::disjoint(),
1918 \li \c boost::geometry::index::intersects(),
1919 \li \c boost::geometry::index::overlaps(),
1920 \li \c boost::geometry::index::within(),
1922 It is possible to negate spatial predicates:
1923 \li <tt>! boost::geometry::index::contains()</tt>,
1924 \li <tt>! boost::geometry::index::covered_by()</tt>,
1925 \li <tt>! boost::geometry::index::covers()</tt>,
1926 \li <tt>! boost::geometry::index::disjoint()</tt>,
1927 \li <tt>! boost::geometry::index::intersects()</tt>,
1928 \li <tt>! boost::geometry::index::overlaps()</tt>,
1929 \li <tt>! boost::geometry::index::within()</tt>
1931 <b>Satisfies predicate</b>
1933 This is a special kind of predicate which allows to pass a user-defined function or function object which checks
1934 if Value should be returned by the query. It's generated by:
1935 \li \c boost::geometry::index::satisfies().
1937 <b>Nearest predicate</b>
1939 If the nearest predicate is passed a k-nearest neighbor search will be performed. This query will result
1940 in returning k values to the output iterator. Only one nearest predicate may be passed to the query.
1941 It may be generated by:
1942 \li \c boost::geometry::index::nearest().
1944 <b>Connecting predicates</b>
1946 Predicates may be passed together connected with \c operator&&().
1950 // return elements intersecting box
1951 bgi::query(tree, bgi::intersects(box), std::back_inserter(result));
1952 // return elements intersecting poly but not within box
1953 bgi::query(tree, bgi::intersects(poly) && !bgi::within(box), std::back_inserter(result));
1954 // return elements overlapping box and meeting my_fun value predicate
1955 bgi::query(tree, bgi::overlaps(box) && bgi::satisfies(my_fun), std::back_inserter(result));
1956 // return 5 elements nearest to pt and elements are intersecting box
1957 bgi::query(tree, bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result));
1959 // For each found value do_something (it is a type of function object)
1960 tree.query(bgi::intersects(box),
1961 boost::make_function_output_iterator(do_something()));
1965 If Value copy constructor or copy assignment throws.
1968 Only one \c nearest() predicate may be passed to the query. Passing more of them results in compile-time error.
1970 \ingroup rtree_functions
1972 \param tree The rtree.
1973 \param predicates Predicates.
1974 \param out_it The output iterator, e.g. generated by std::back_inserter().
1976 \return The number of values found.
1978 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
1979 typename Predicates, typename OutIter> inline
1980 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
1981 query(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree,
1982 Predicates const& predicates,
1985 return tree.query(predicates, out_it);
1989 \brief Returns the query iterator pointing at the begin of the query range.
1991 This method returns the iterator which may be used to perform iterative queries. For the information
1992 about the predicates which may be passed to this method see query().
1996 std::for_each(bgi::qbegin(tree, bgi::nearest(pt, 3)), bgi::qend(tree), do_something());
1999 \par Iterator category
2003 If predicates copy throws.
2004 If allocation throws.
2007 The modification of the rtree may invalidate the iterators.
2009 \ingroup rtree_functions
2011 \param tree The rtree.
2012 \param predicates Predicates.
2014 \return The iterator pointing at the begin of the query range.
2016 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
2017 typename Predicates> inline
2018 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_query_iterator
2019 qbegin(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree,
2020 Predicates const& predicates)
2022 return tree.qbegin(predicates);
2026 \brief Returns the query iterator pointing at the end of the query range.
2028 This method returns the iterator which may be used to check if the query has ended.
2032 std::for_each(bgi::qbegin(tree, bgi::nearest(pt, 3)), bgi::qend(tree), do_something());
2035 \par Iterator category
2042 The modification of the rtree may invalidate the iterators.
2044 \ingroup rtree_functions
2046 \return The iterator pointing at the end of the query range.
2048 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline
2049 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_query_iterator
2050 qend(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2056 \brief Returns the iterator pointing at the begin of the rtree values range.
2058 This method returns the iterator which may be used to iterate over all values
2059 stored in the rtree.
2063 std::for_each(bgi::begin(tree), bgi::end(tree), do_something());
2065 std::for_each(boost::begin(tree), boost::end(tree), do_something());
2068 \par Iterator category
2072 If allocation throws.
2075 The modification of the rtree may invalidate the iterators.
2077 \ingroup rtree_functions
2079 \return The iterator pointing at the begin of the range.
2081 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline
2082 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_iterator
2083 begin(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2085 return tree.begin();
2089 \brief Returns the iterator pointing at the end of the rtree values range.
2091 This method returns the iterator which may be compared with the iterator returned by begin()
2092 in order to check if the iteration has ended.
2096 std::for_each(bgi::begin(tree), bgi::end(tree), do_something());
2098 std::for_each(boost::begin(tree), boost::end(tree), do_something());
2101 \par Iterator category
2108 The modification of the rtree may invalidate the iterators.
2110 \ingroup rtree_functions
2112 \return The iterator pointing at the end of the range.
2114 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline
2115 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_iterator
2116 end(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2122 \brief Remove all values from the index.
2124 It calls \c rtree::clear().
2126 \ingroup rtree_functions
2128 \param tree The spatial index.
2130 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2131 inline void clear(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree)
2133 return tree.clear();
2137 \brief Get the number of values stored in the index.
2139 It calls \c rtree::size().
2141 \ingroup rtree_functions
2143 \param tree The spatial index.
2145 \return The number of values stored in the index.
2147 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2148 inline size_t size(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2154 \brief Query if there are no values stored in the index.
2156 It calls \c rtree::empty().
2158 \ingroup rtree_functions
2160 \param tree The spatial index.
2162 \return true if there are no values in the index.
2164 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2165 inline bool empty(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2167 return tree.bounds();
2171 \brief Get the box containing all stored values or an invalid box if the index has no values.
2173 It calls \c rtree::envelope().
2175 \ingroup rtree_functions
2177 \param tree The spatial index.
2179 \return The box containing all stored values or an invalid box.
2181 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2182 inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::bounds_type
2183 bounds(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2185 return tree.bounds();
2189 \brief Exchanges the contents of the container with those of other.
2191 It calls \c rtree::swap().
2193 \ingroup rtree_functions
2195 \param l The first rtree.
2196 \param r The second rtree.
2198 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2199 inline void swap(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & l,
2200 rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & r)
2205 }}} // namespace boost::geometry::index
2207 // Boost.Range adaptation
2210 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2211 struct range_mutable_iterator
2213 boost::geometry::index::rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>
2216 typedef typename boost::geometry::index::rtree
2218 Value, Parameters, IndexableGetter, EqualTo, Allocator
2219 >::const_iterator type;
2222 } // namespace boost
2224 #include <boost/geometry/index/detail/config_end.hpp>
2226 #endif // BOOST_GEOMETRY_INDEX_RTREE_HPP