File indexing completed on 2025-01-18 09:35:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef BOOST_GEOMETRY_INDEX_RTREE_HPP
0018 #define BOOST_GEOMETRY_INDEX_RTREE_HPP
0019
0020
0021 #include <algorithm>
0022 #include <type_traits>
0023
0024
0025 #include <boost/container/new_allocator.hpp>
0026 #include <boost/tuple/tuple.hpp>
0027 #include <boost/core/invoke_swap.hpp>
0028
0029
0030 #include <boost/geometry/core/static_assert.hpp>
0031
0032 #include <boost/geometry/algorithms/detail/comparable_distance/interface.hpp>
0033 #include <boost/geometry/algorithms/detail/covered_by/interface.hpp>
0034 #include <boost/geometry/algorithms/detail/disjoint/interface.hpp>
0035 #include <boost/geometry/algorithms/detail/equals/interface.hpp>
0036 #include <boost/geometry/algorithms/detail/intersects/interface.hpp>
0037 #include <boost/geometry/algorithms/detail/overlaps/interface.hpp>
0038 #include <boost/geometry/algorithms/detail/touches/interface.hpp>
0039 #include <boost/geometry/algorithms/detail/within/interface.hpp>
0040 #include <boost/geometry/algorithms/centroid.hpp>
0041
0042 #include <boost/geometry/geometries/point.hpp>
0043 #include <boost/geometry/geometries/box.hpp>
0044
0045
0046 #include <boost/geometry/index/detail/config_begin.hpp>
0047
0048 #include <boost/geometry/index/detail/assert.hpp>
0049 #include <boost/geometry/index/detail/exception.hpp>
0050
0051 #include <boost/geometry/index/detail/rtree/options.hpp>
0052
0053 #include <boost/geometry/index/indexable.hpp>
0054 #include <boost/geometry/index/equal_to.hpp>
0055
0056 #include <boost/geometry/index/detail/translator.hpp>
0057
0058 #include <boost/geometry/index/predicates.hpp>
0059 #include <boost/geometry/index/distance_predicates.hpp>
0060 #include <boost/geometry/index/detail/rtree/adaptors.hpp>
0061
0062 #include <boost/geometry/index/detail/meta.hpp>
0063 #include <boost/geometry/index/detail/utilities.hpp>
0064 #include <boost/geometry/index/detail/rtree/node/node.hpp>
0065
0066 #include <boost/geometry/index/detail/algorithms/is_valid.hpp>
0067
0068 #include <boost/geometry/index/detail/rtree/visitors/insert.hpp>
0069 #include <boost/geometry/index/detail/rtree/visitors/iterator.hpp>
0070 #include <boost/geometry/index/detail/rtree/visitors/remove.hpp>
0071 #include <boost/geometry/index/detail/rtree/visitors/copy.hpp>
0072 #include <boost/geometry/index/detail/rtree/visitors/destroy.hpp>
0073 #include <boost/geometry/index/detail/rtree/visitors/spatial_query.hpp>
0074 #include <boost/geometry/index/detail/rtree/visitors/distance_query.hpp>
0075 #include <boost/geometry/index/detail/rtree/visitors/count.hpp>
0076 #include <boost/geometry/index/detail/rtree/visitors/children_box.hpp>
0077
0078 #include <boost/geometry/index/detail/rtree/linear/linear.hpp>
0079 #include <boost/geometry/index/detail/rtree/quadratic/quadratic.hpp>
0080 #include <boost/geometry/index/detail/rtree/rstar/rstar.hpp>
0081
0082
0083 #include <boost/geometry/index/detail/rtree/pack_create.hpp>
0084
0085 #include <boost/geometry/index/inserter.hpp>
0086
0087 #include <boost/geometry/index/detail/rtree/utilities/view.hpp>
0088
0089 #include <boost/geometry/index/detail/rtree/iterators.hpp>
0090 #include <boost/geometry/index/detail/rtree/query_iterators.hpp>
0091
0092 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
0093 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL_SERIALIZATION
0094 #define BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL_SERIALIZATION
0095 #endif
0096 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL_ITERATORS
0097 #define BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL_ITERATORS
0098 #endif
0099 #endif
0100
0101 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL_SERIALIZATION
0102 #include <boost/geometry/index/detail/serialization.hpp>
0103 #endif
0104
0105 #include <boost/geometry/util/range.hpp>
0106 #include <boost/geometry/util/type_traits.hpp>
0107
0108
0109
0110
0111
0112
0113
0114 namespace boost { namespace geometry { namespace index {
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165 template
0166 <
0167 typename Value,
0168 typename Parameters,
0169 typename IndexableGetter = index::indexable<Value>,
0170 typename EqualTo = index::equal_to<Value>,
0171 typename Allocator = boost::container::new_allocator<Value>
0172 >
0173 class rtree
0174 {
0175 public:
0176
0177 typedef Value value_type;
0178
0179 typedef Parameters parameters_type;
0180
0181 typedef IndexableGetter indexable_getter;
0182
0183 typedef EqualTo value_equal;
0184
0185 typedef Allocator allocator_type;
0186
0187
0188
0189 typedef typename index::detail::indexable_type<
0190 detail::translator<IndexableGetter, EqualTo>
0191 >::type indexable_type;
0192
0193
0194 typedef geometry::model::box<
0195 geometry::model::point<
0196 typename coordinate_type<indexable_type>::type,
0197 dimension<indexable_type>::value,
0198 typename coordinate_system<indexable_type>::type
0199 >
0200 >
0201 bounds_type;
0202
0203 private:
0204
0205 typedef bounds_type box_type;
0206
0207 struct members_holder
0208 : public detail::translator<IndexableGetter, EqualTo>
0209 , public Parameters
0210 , public detail::rtree::allocators
0211 <
0212 Allocator,
0213 Value,
0214 Parameters,
0215 bounds_type,
0216 typename detail::rtree::options_type<Parameters>::type::node_tag
0217 >
0218 {
0219 typedef Value value_type;
0220 typedef typename rtree::bounds_type bounds_type;
0221 typedef Parameters parameters_type;
0222
0223
0224
0225
0226 typedef bounds_type box_type;
0227 typedef detail::translator<IndexableGetter, EqualTo> translator_type;
0228 typedef typename detail::rtree::options_type<Parameters>::type options_type;
0229 typedef typename options_type::node_tag node_tag;
0230 typedef detail::rtree::allocators
0231 <
0232 Allocator, Value, Parameters, bounds_type, node_tag
0233 > allocators_type;
0234
0235 typedef typename detail::rtree::node
0236 <
0237 value_type, parameters_type, bounds_type, allocators_type, node_tag
0238 >::type node;
0239 typedef typename detail::rtree::internal_node
0240 <
0241 value_type, parameters_type, bounds_type, allocators_type, node_tag
0242 >::type internal_node;
0243 typedef typename detail::rtree::leaf
0244 <
0245 value_type, parameters_type, bounds_type, allocators_type, node_tag
0246 >::type leaf;
0247
0248
0249 typedef typename detail::rtree::visitor
0250 <
0251 value_type, parameters_type, bounds_type, allocators_type, node_tag, false
0252 >::type visitor;
0253 typedef typename detail::rtree::visitor
0254 <
0255 value_type, parameters_type, bounds_type, allocators_type, node_tag, true
0256 >::type visitor_const;
0257
0258 typedef typename allocators_type::node_pointer node_pointer;
0259
0260 typedef ::boost::container::allocator_traits<Allocator> allocator_traits_type;
0261 typedef typename allocators_type::size_type size_type;
0262
0263 private:
0264 members_holder(members_holder const&);
0265 members_holder & operator=(members_holder const&);
0266
0267 public:
0268 template <typename IndGet, typename ValEq, typename Alloc>
0269 members_holder(IndGet const& ind_get,
0270 ValEq const& val_eq,
0271 Parameters const& parameters,
0272 Alloc&& alloc)
0273 : translator_type(ind_get, val_eq)
0274 , Parameters(parameters)
0275 , allocators_type(std::forward<Alloc>(alloc))
0276 , values_count(0)
0277 , leafs_level(0)
0278 , root(0)
0279 {}
0280
0281 template <typename IndGet, typename ValEq>
0282 members_holder(IndGet const& ind_get,
0283 ValEq const& val_eq,
0284 Parameters const& parameters)
0285 : translator_type(ind_get, val_eq)
0286 , Parameters(parameters)
0287 , allocators_type()
0288 , values_count(0)
0289 , leafs_level(0)
0290 , root(0)
0291 {}
0292
0293 translator_type const& translator() const { return *this; }
0294
0295 IndexableGetter const& indexable_getter() const { return *this; }
0296 IndexableGetter & indexable_getter() { return *this; }
0297 EqualTo const& equal_to() const { return *this; }
0298 EqualTo & equal_to() { return *this; }
0299 Parameters const& parameters() const { return *this; }
0300 Parameters & parameters() { return *this; }
0301 allocators_type const& allocators() const { return *this; }
0302 allocators_type & allocators() { return *this; }
0303
0304 size_type values_count;
0305 size_type leafs_level;
0306 node_pointer root;
0307 };
0308
0309 typedef typename members_holder::translator_type translator_type;
0310 typedef typename members_holder::options_type options_type;
0311 typedef typename members_holder::allocators_type allocators_type;
0312 typedef typename members_holder::node node;
0313 typedef typename members_holder::internal_node internal_node;
0314 typedef typename members_holder::leaf leaf;
0315
0316 typedef typename members_holder::node_pointer node_pointer;
0317 typedef typename members_holder::allocator_traits_type allocator_traits_type;
0318
0319 friend class detail::rtree::utilities::view<rtree>;
0320 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL_SERIALIZATION
0321 friend class detail::rtree::private_view<rtree>;
0322 friend class detail::rtree::const_private_view<rtree>;
0323 #endif
0324
0325 public:
0326
0327
0328 typedef typename allocators_type::reference reference;
0329
0330 typedef typename allocators_type::const_reference const_reference;
0331
0332 typedef typename allocators_type::pointer pointer;
0333
0334 typedef typename allocators_type::const_pointer const_pointer;
0335
0336 typedef typename allocators_type::difference_type difference_type;
0337
0338 typedef typename allocators_type::size_type size_type;
0339
0340
0341 typedef index::detail::rtree::iterators::iterator
0342 <
0343 value_type, options_type, translator_type, box_type, allocators_type
0344 > const_iterator;
0345
0346
0347 typedef index::detail::rtree::iterators::query_iterator
0348 <
0349 value_type, allocators_type
0350 > const_query_iterator;
0351
0352 public:
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364 inline explicit rtree(parameters_type const& parameters = parameters_type(),
0365 indexable_getter const& getter = indexable_getter(),
0366 value_equal const& equal = value_equal())
0367 : m_members(getter, equal, parameters)
0368 {}
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381 inline rtree(parameters_type const& parameters,
0382 indexable_getter const& getter,
0383 value_equal const& equal,
0384 allocator_type const& allocator)
0385 : m_members(getter, equal, parameters, allocator)
0386 {}
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405 template<typename Iterator>
0406 inline rtree(Iterator first, Iterator last,
0407 parameters_type const& parameters = parameters_type(),
0408 indexable_getter const& getter = indexable_getter(),
0409 value_equal const& equal = value_equal(),
0410 allocator_type const& allocator = allocator_type())
0411 : m_members(getter, equal, parameters, allocator)
0412 {
0413 pack_construct(first, last, boost::container::new_allocator<void>());
0414 }
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432 template<typename Range>
0433 inline explicit rtree(Range const& rng,
0434 parameters_type const& parameters = parameters_type(),
0435 indexable_getter const& getter = indexable_getter(),
0436 value_equal const& equal = value_equal(),
0437 allocator_type const& allocator = allocator_type())
0438 : m_members(getter, equal, parameters, allocator)
0439 {
0440 pack_construct(::boost::begin(rng), ::boost::end(rng), boost::container::new_allocator<void>());
0441 }
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461 template<typename Iterator, typename PackAlloc>
0462 inline rtree(Iterator first, Iterator last,
0463 parameters_type const& parameters,
0464 indexable_getter const& getter,
0465 value_equal const& equal,
0466 allocator_type const& allocator,
0467 PackAlloc const& temp_allocator)
0468 : m_members(getter, equal, parameters, allocator)
0469 {
0470 pack_construct(first, last, temp_allocator);
0471 }
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490 template<typename Range, typename PackAlloc>
0491 inline explicit rtree(Range const& rng,
0492 parameters_type const& parameters,
0493 indexable_getter const& getter,
0494 value_equal const& equal,
0495 allocator_type const& allocator,
0496 PackAlloc const& temp_allocator)
0497 : m_members(getter, equal, parameters, allocator)
0498 {
0499 pack_construct(::boost::begin(rng), ::boost::end(rng), temp_allocator);
0500 }
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516 template<typename Iterator>
0517 inline rtree(Iterator first, Iterator last,
0518 allocator_type const& allocator)
0519 : m_members(indexable_getter(), value_equal(), parameters_type(), allocator)
0520 {
0521 pack_construct(first, last, boost::container::new_allocator<void>());
0522 }
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537 template<typename Range>
0538 inline explicit rtree(Range const& rng,
0539 allocator_type const& allocator)
0540 : m_members(indexable_getter(), value_equal(), parameters_type(), allocator)
0541 {
0542 pack_construct(::boost::begin(rng), ::boost::end(rng), boost::container::new_allocator<void>());
0543 }
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560 template<typename Iterator, typename PackAlloc>
0561 inline rtree(Iterator first, Iterator last,
0562 allocator_type const& allocator,
0563 PackAlloc const& temp_allocator)
0564 : m_members(indexable_getter(), value_equal(), parameters_type(), allocator)
0565 {
0566 pack_construct(first, last, temp_allocator);
0567 }
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583 template<typename Range, typename PackAlloc>
0584 inline explicit rtree(Range const& rng,
0585 allocator_type const& allocator,
0586 PackAlloc const& temp_allocator)
0587 : m_members(indexable_getter(), value_equal(), parameters_type(), allocator)
0588 {
0589 pack_construct(::boost::begin(rng), ::boost::end(rng), temp_allocator);
0590 }
0591
0592
0593
0594
0595
0596
0597
0598 inline ~rtree()
0599 {
0600 this->raw_destroy(*this);
0601 }
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615 inline rtree(rtree const& src)
0616 : m_members(src.m_members.indexable_getter(),
0617 src.m_members.equal_to(),
0618 src.m_members.parameters(),
0619 allocator_traits_type::select_on_container_copy_construction(src.get_allocator()))
0620 {
0621 this->raw_copy(src, *this, false);
0622 }
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637 inline rtree(rtree const& src, allocator_type const& allocator)
0638 : m_members(src.m_members.indexable_getter(),
0639 src.m_members.equal_to(),
0640 src.m_members.parameters(), allocator)
0641 {
0642 this->raw_copy(src, *this, false);
0643 }
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655 inline rtree(rtree&& src)
0656 : m_members(src.m_members.indexable_getter(),
0657 src.m_members.equal_to(),
0658 src.m_members.parameters(),
0659 std::move(src.m_members.allocators()))
0660 {
0661 boost::core::invoke_swap(m_members.values_count, src.m_members.values_count);
0662 boost::core::invoke_swap(m_members.leafs_level, src.m_members.leafs_level);
0663 boost::core::invoke_swap(m_members.root, src.m_members.root);
0664 }
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679 inline rtree(rtree&& src, allocator_type const& allocator)
0680 : m_members(src.m_members.indexable_getter(),
0681 src.m_members.equal_to(),
0682 src.m_members.parameters(),
0683 allocator)
0684 {
0685 if ( src.m_members.allocators() == allocator )
0686 {
0687 boost::core::invoke_swap(m_members.values_count, src.m_members.values_count);
0688 boost::core::invoke_swap(m_members.leafs_level, src.m_members.leafs_level);
0689 boost::core::invoke_swap(m_members.root, src.m_members.root);
0690 }
0691 else
0692 {
0693 this->raw_copy(src, *this, false);
0694 }
0695 }
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709 inline rtree & operator=(rtree const& src)
0710 {
0711 if ( &src != this )
0712 {
0713 allocators_type & this_allocs = m_members.allocators();
0714 allocators_type const& src_allocs = src.m_members.allocators();
0715
0716
0717
0718
0719
0720 typedef std::integral_constant<bool,
0721 allocator_traits_type::propagate_on_container_copy_assignment::value
0722 > propagate;
0723
0724 if ( propagate::value && !(this_allocs == src_allocs) )
0725 this->raw_destroy(*this);
0726 detail::assign_cond(this_allocs, src_allocs, propagate());
0727
0728
0729 this->raw_copy(src, *this, true);
0730 }
0731
0732 return *this;
0733 }
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747 inline rtree & operator=(rtree&& src)
0748 {
0749 if ( &src != this )
0750 {
0751 allocators_type & this_allocs = m_members.allocators();
0752 allocators_type & src_allocs = src.m_members.allocators();
0753
0754 if ( this_allocs == src_allocs )
0755 {
0756 this->raw_destroy(*this);
0757
0758 m_members.indexable_getter() = src.m_members.indexable_getter();
0759 m_members.equal_to() = src.m_members.equal_to();
0760 m_members.parameters() = src.m_members.parameters();
0761
0762 boost::core::invoke_swap(m_members.values_count, src.m_members.values_count);
0763 boost::core::invoke_swap(m_members.leafs_level, src.m_members.leafs_level);
0764 boost::core::invoke_swap(m_members.root, src.m_members.root);
0765
0766
0767
0768
0769
0770 typedef std::integral_constant<bool,
0771 allocator_traits_type::propagate_on_container_move_assignment::value
0772 > propagate;
0773 detail::move_cond(this_allocs, src_allocs, propagate());
0774 }
0775 else
0776 {
0777
0778
0779
0780 this->raw_copy(src, *this, true);
0781 }
0782 }
0783
0784 return *this;
0785 }
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797 void swap(rtree & other)
0798 {
0799 boost::core::invoke_swap(m_members.indexable_getter(), other.m_members.indexable_getter());
0800 boost::core::invoke_swap(m_members.equal_to(), other.m_members.equal_to());
0801 boost::core::invoke_swap(m_members.parameters(), other.m_members.parameters());
0802
0803
0804
0805
0806
0807 typedef std::integral_constant<bool,
0808 allocator_traits_type::propagate_on_container_swap::value
0809 > propagate;
0810 detail::swap_cond(m_members.allocators(), other.m_members.allocators(), propagate());
0811
0812 boost::core::invoke_swap(m_members.values_count, other.m_members.values_count);
0813 boost::core::invoke_swap(m_members.leafs_level, other.m_members.leafs_level);
0814 boost::core::invoke_swap(m_members.root, other.m_members.root);
0815 }
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832 inline void insert(value_type const& value)
0833 {
0834 if ( !m_members.root )
0835 this->raw_create();
0836
0837 this->raw_insert(value);
0838 }
0839
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856 template <typename Iterator>
0857 inline void insert(Iterator first, Iterator last)
0858 {
0859 if ( !m_members.root )
0860 this->raw_create();
0861
0862 for ( ; first != last ; ++first )
0863 this->raw_insert(*first);
0864 }
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881 template <typename ConvertibleOrRange>
0882 inline void insert(ConvertibleOrRange const& conv_or_rng)
0883 {
0884 if ( !m_members.root )
0885 this->raw_create();
0886
0887 typedef std::is_convertible<ConvertibleOrRange, value_type> is_conv_t;
0888 typedef range::detail::is_range<ConvertibleOrRange> is_range_t;
0889 BOOST_GEOMETRY_STATIC_ASSERT((is_conv_t::value || is_range_t::value),
0890 "The argument has to be convertible to Value type or be a Range.",
0891 ConvertibleOrRange);
0892
0893 this->insert_dispatch(conv_or_rng, is_conv_t());
0894 }
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916 inline size_type remove(value_type const& value)
0917 {
0918 if ( !m_members.root )
0919 return 0;
0920
0921 return this->raw_remove(value);
0922 }
0923
0924
0925
0926
0927
0928
0929
0930
0931
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946
0947 template <typename Iterator>
0948 inline size_type remove(Iterator first, Iterator last)
0949 {
0950 size_type result = 0;
0951
0952 if ( !m_members.root )
0953 return result;
0954
0955 for ( ; first != last ; ++first )
0956 result += this->raw_remove(*first);
0957 return result;
0958 }
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981 template <typename ConvertibleOrRange>
0982 inline size_type remove(ConvertibleOrRange const& conv_or_rng)
0983 {
0984 if ( !m_members.root )
0985 return 0;
0986
0987 typedef std::is_convertible<ConvertibleOrRange, value_type> is_conv_t;
0988 typedef range::detail::is_range<ConvertibleOrRange> is_range_t;
0989 BOOST_GEOMETRY_STATIC_ASSERT((is_conv_t::value || is_range_t::value),
0990 "The argument has to be convertible to Value type or be a Range.",
0991 ConvertibleOrRange);
0992
0993 return this->remove_dispatch(conv_or_rng, is_conv_t());
0994 }
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084 template <typename Predicates, typename OutIter>
1085 size_type query(Predicates const& predicates, OutIter out_it) const
1086 {
1087 return m_members.root
1088 ? query_dispatch(predicates, out_it)
1089 : 0;
1090 }
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134 template <typename Predicates>
1135 const_query_iterator qbegin(Predicates const& predicates) const
1136 {
1137 return const_query_iterator(qbegin_(predicates));
1138 }
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178 const_query_iterator qend() const
1179 {
1180 return const_query_iterator();
1181 }
1182
1183 private:
1184 template <typename Predicates>
1185 using query_iterator_t = std::conditional_t
1186 <
1187 detail::predicates_count_distance<Predicates>::value == 0,
1188 detail::rtree::iterators::spatial_query_iterator<members_holder, Predicates>,
1189 detail::rtree::iterators::distance_query_iterator<members_holder, Predicates>
1190 >;
1191
1192 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL_ITERATORS
1193 public:
1194 #else
1195 private:
1196 #endif
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250 template <typename Predicates>
1251 query_iterator_t<Predicates> qbegin_(Predicates const& predicates) const
1252 {
1253 BOOST_GEOMETRY_STATIC_ASSERT((detail::predicates_count_distance<Predicates>::value <= 1),
1254 "Only one distance predicate can be passed.",
1255 Predicates);
1256
1257 return m_members.root
1258 ? query_iterator_t<Predicates>(m_members, predicates)
1259 : query_iterator_t<Predicates>(predicates);
1260 }
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294 template <typename Predicates>
1295 query_iterator_t<Predicates> qend_(Predicates const& predicates) const
1296 {
1297 BOOST_GEOMETRY_STATIC_ASSERT((detail::predicates_count_distance<Predicates>::value <= 1),
1298 "Only one distance predicate can be passed.",
1299 Predicates);
1300
1301 return query_iterator_t<Predicates>(m_members.parameters(), m_members.translator(), predicates);
1302 }
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353 detail::rtree::iterators::end_query_iterator<value_type, allocators_type>
1354 qend_() const
1355 {
1356 return detail::rtree::iterators::end_query_iterator<value_type, allocators_type>();
1357 }
1358
1359 public:
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400 const_iterator begin() const
1401 {
1402 return m_members.root
1403 ? const_iterator(m_members.root)
1404 : const_iterator();
1405 }
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437 const_iterator end() const
1438 {
1439 return const_iterator();
1440 }
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450 inline size_type size() const
1451 {
1452 return m_members.values_count;
1453 }
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463 inline bool empty() const
1464 {
1465 return 0 == m_members.values_count;
1466 }
1467
1468
1469
1470
1471
1472
1473
1474 inline void clear()
1475 {
1476 this->raw_destroy(*this);
1477 }
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491 inline bounds_type bounds() const
1492 {
1493 bounds_type result;
1494
1495 geometry::assign_inverse(result);
1496
1497 if ( m_members.root )
1498 {
1499 detail::rtree::visitors::children_box
1500 <
1501 members_holder
1502 > box_v(result, m_members.parameters(), m_members.translator());
1503 detail::rtree::apply_visitor(box_v, *m_members.root);
1504 }
1505
1506 return result;
1507 }
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522 template <typename ValueOrIndexable>
1523 size_type count(ValueOrIndexable const& vori) const
1524 {
1525 if ( !m_members.root )
1526 return 0;
1527
1528
1529 typedef typename index::detail::convertible_type
1530 <
1531 ValueOrIndexable,
1532 value_type,
1533 indexable_type
1534 >::type value_or_indexable;
1535
1536 static const bool is_void = std::is_void<value_or_indexable>::value;
1537 BOOST_GEOMETRY_STATIC_ASSERT((! is_void),
1538 "The argument has to be convertible to Value or Indexable type.",
1539 ValueOrIndexable);
1540
1541
1542
1543 return this->template raw_count<value_or_indexable>(vori);
1544 }
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554 inline parameters_type parameters() const
1555 {
1556 return m_members.parameters();
1557 }
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567 indexable_getter indexable_get() const
1568 {
1569 return m_members.indexable_getter();
1570 }
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580 value_equal value_eq() const
1581 {
1582 return m_members.equal_to();
1583 }
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593 allocator_type get_allocator() const
1594 {
1595 return m_members.allocators().allocator();
1596 }
1597
1598 private:
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608 inline translator_type translator() const
1609 {
1610 return m_members.translator();
1611 }
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624 template <typename Visitor>
1625 inline void apply_visitor(Visitor & visitor) const
1626 {
1627 if ( m_members.root )
1628 detail::rtree::apply_visitor(visitor, *m_members.root);
1629 }
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641 inline size_type depth() const
1642 {
1643 return m_members.leafs_level;
1644 }
1645
1646 private:
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658 inline void raw_insert(value_type const& value)
1659 {
1660 BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist");
1661
1662 BOOST_GEOMETRY_INDEX_ASSERT(detail::is_valid(m_members.translator()(value)), "Indexable is invalid");
1663
1664 detail::rtree::visitors::insert<value_type, members_holder>
1665 insert_v(m_members.root, m_members.leafs_level, value,
1666 m_members.parameters(), m_members.translator(), m_members.allocators());
1667
1668 detail::rtree::apply_visitor(insert_v, *m_members.root);
1669
1670
1671
1672
1673
1674
1675
1676 ++m_members.values_count;
1677 }
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687 inline size_type raw_remove(value_type const& value)
1688 {
1689
1690 BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist");
1691
1692 detail::rtree::visitors::remove<members_holder>
1693 remove_v(m_members.root, m_members.leafs_level, value,
1694 m_members.parameters(), m_members.translator(), m_members.allocators());
1695
1696 detail::rtree::apply_visitor(remove_v, *m_members.root);
1697
1698
1699
1700 if ( remove_v.is_value_removed() )
1701 {
1702 BOOST_GEOMETRY_INDEX_ASSERT(0 < m_members.values_count, "unexpected state");
1703
1704 --m_members.values_count;
1705
1706 return 1;
1707 }
1708
1709 return 0;
1710 }
1711
1712
1713
1714
1715
1716
1717
1718 inline void raw_create()
1719 {
1720 BOOST_GEOMETRY_INDEX_ASSERT(0 == m_members.root, "the tree is already created");
1721
1722 m_members.root = detail::rtree::create_node<allocators_type, leaf>::apply(m_members.allocators());
1723 m_members.values_count = 0;
1724 m_members.leafs_level = 0;
1725 }
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735 inline void raw_destroy(rtree & t)
1736 {
1737 if ( t.m_members.root )
1738 {
1739 detail::rtree::visitors::destroy<members_holder>
1740 ::apply(t.m_members.root, t.m_members.allocators());
1741
1742 t.m_members.root = 0;
1743 }
1744 t.m_members.values_count = 0;
1745 t.m_members.leafs_level = 0;
1746 }
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759 inline void raw_copy(rtree const& src, rtree & dst, bool copy_tr_and_params) const
1760 {
1761 detail::rtree::visitors::copy<members_holder> copy_v(dst.m_members.allocators());
1762
1763 if ( src.m_members.root )
1764 detail::rtree::apply_visitor(copy_v, *src.m_members.root);
1765
1766 if ( copy_tr_and_params )
1767 {
1768 dst.m_members.indexable_getter() = src.m_members.indexable_getter();
1769 dst.m_members.equal_to() = src.m_members.equal_to();
1770 dst.m_members.parameters() = src.m_members.parameters();
1771 }
1772
1773
1774 if ( dst.m_members.root )
1775 {
1776 detail::rtree::visitors::destroy<members_holder>
1777 ::apply(dst.m_members.root, dst.m_members.allocators());
1778
1779 dst.m_members.root = 0;
1780 }
1781
1782 dst.m_members.root = copy_v.result;
1783 dst.m_members.values_count = src.m_members.values_count;
1784 dst.m_members.leafs_level = src.m_members.leafs_level;
1785 }
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795 template <typename ValueConvertible>
1796 inline void insert_dispatch(ValueConvertible const& val_conv,
1797 std::true_type )
1798 {
1799 this->raw_insert(val_conv);
1800 }
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810 template <typename Range>
1811 inline void insert_dispatch(Range const& rng,
1812 std::false_type )
1813 {
1814 typedef typename boost::range_const_iterator<Range>::type It;
1815 for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it )
1816 this->raw_insert(*it);
1817 }
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827 template <typename ValueConvertible>
1828 inline size_type remove_dispatch(ValueConvertible const& val_conv,
1829 std::true_type )
1830 {
1831 return this->raw_remove(val_conv);
1832 }
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842 template <typename Range>
1843 inline size_type remove_dispatch(Range const& rng,
1844 std::false_type )
1845 {
1846 size_type result = 0;
1847 typedef typename boost::range_const_iterator<Range>::type It;
1848 for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it )
1849 result += this->raw_remove(*it);
1850 return result;
1851 }
1852
1853
1854
1855
1856
1857
1858
1859 template
1860 <
1861 typename Predicates, typename OutIter,
1862 std::enable_if_t<(detail::predicates_count_distance<Predicates>::value == 0), int> = 0
1863 >
1864 size_type query_dispatch(Predicates const& predicates, OutIter out_it) const
1865 {
1866 detail::rtree::visitors::spatial_query<members_holder, Predicates, OutIter>
1867 query(m_members, predicates, out_it);
1868 return query.apply(m_members);
1869 }
1870
1871
1872
1873
1874
1875
1876
1877 template
1878 <
1879 typename Predicates, typename OutIter,
1880 std::enable_if_t<(detail::predicates_count_distance<Predicates>::value > 0), int> = 0
1881 >
1882 size_type query_dispatch(Predicates const& predicates, OutIter out_it) const
1883 {
1884 BOOST_GEOMETRY_STATIC_ASSERT((detail::predicates_count_distance<Predicates>::value == 1),
1885 "Only one distance predicate can be passed.",
1886 Predicates);
1887
1888 detail::rtree::visitors::distance_query<members_holder, Predicates>
1889 distance_v(m_members, predicates);
1890
1891 return distance_v.apply(m_members, out_it);
1892 }
1893
1894
1895
1896
1897
1898
1899
1900 template <typename ValueOrIndexable>
1901 size_type raw_count(ValueOrIndexable const& vori) const
1902 {
1903 BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist");
1904
1905 detail::rtree::visitors::count
1906 <
1907 ValueOrIndexable,
1908 members_holder
1909 > count_v(vori, m_members.parameters(), m_members.translator());
1910
1911 detail::rtree::apply_visitor(count_v, *m_members.root);
1912
1913 return count_v.found_count;
1914 }
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930 template<typename Iterator, typename PackAlloc>
1931 inline void pack_construct(Iterator first, Iterator last, PackAlloc const& temp_allocator)
1932 {
1933 typedef detail::rtree::pack<members_holder> pack;
1934 size_type vc = 0, ll = 0;
1935 m_members.root = pack::apply(first, last, vc, ll,
1936 m_members.parameters(), m_members.translator(),
1937 m_members.allocators(), temp_allocator);
1938 m_members.values_count = vc;
1939 m_members.leafs_level = ll;
1940 }
1941
1942 members_holder m_members;
1943 };
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
1956 inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
1957 Value const& v)
1958 {
1959 tree.insert(v);
1960 }
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973 template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
1974 typename Iterator>
1975 inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
1976 Iterator first, Iterator last)
1977 {
1978 tree.insert(first, last);
1979 }
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991 template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
1992 typename ConvertibleOrRange>
1993 inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
1994 ConvertibleOrRange const& conv_or_rng)
1995 {
1996 tree.insert(conv_or_rng);
1997 }
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2015 inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
2016 remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
2017 Value const& v)
2018 {
2019 return tree.remove(v);
2020 }
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040 template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
2041 typename Iterator>
2042 inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
2043 remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
2044 Iterator first, Iterator last)
2045 {
2046 return tree.remove(first, last);
2047 }
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066 template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
2067 typename ConvertibleOrRange>
2068 inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
2069 remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree,
2070 ConvertibleOrRange const& conv_or_rng)
2071 {
2072 return tree.remove(conv_or_rng);
2073 }
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
2149 typename Predicates, typename OutIter> inline
2150 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
2151 query(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree,
2152 Predicates const& predicates,
2153 OutIter out_it)
2154 {
2155 return tree.query(predicates, out_it);
2156 }
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
2187 typename Predicates> inline
2188 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_query_iterator
2189 qbegin(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree,
2190 Predicates const& predicates)
2191 {
2192 return tree.qbegin(predicates);
2193 }
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline
2219 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_query_iterator
2220 qend(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2221 {
2222 return tree.qend();
2223 }
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline
2252 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_iterator
2253 begin(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2254 {
2255 return tree.begin();
2256 }
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline
2285 typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_iterator
2286 end(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2287 {
2288 return tree.end();
2289 }
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2301 inline void clear(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree)
2302 {
2303 return tree.clear();
2304 }
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2318 inline size_t size(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2319 {
2320 return tree.size();
2321 }
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2335 inline bool empty(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2336 {
2337 return tree.bounds();
2338 }
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2352 inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::bounds_type
2353 bounds(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
2354 {
2355 return tree.bounds();
2356 }
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2369 inline void swap(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & l,
2370 rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & r)
2371 {
2372 return l.swap(r);
2373 }
2374
2375 }}}
2376
2377
2378 namespace boost {
2379
2380 template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
2381 struct range_mutable_iterator
2382 <
2383 boost::geometry::index::rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>
2384 >
2385 {
2386 typedef typename boost::geometry::index::rtree
2387 <
2388 Value, Parameters, IndexableGetter, EqualTo, Allocator
2389 >::const_iterator type;
2390 };
2391
2392 }
2393
2394 #include <boost/geometry/index/detail/config_end.hpp>
2395
2396 #endif