rtree.hpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. // Boost.Geometry Index
  2. //
  3. // R-tree implementation
  4. //
  5. // Copyright (c) 2008 Federico J. Fernandez.
  6. // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
  7. //
  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)
  11. #ifndef BOOST_GEOMETRY_INDEX_RTREE_HPP
  12. #define BOOST_GEOMETRY_INDEX_RTREE_HPP
  13. #include <algorithm>
  14. #include <boost/tuple/tuple.hpp>
  15. #include <boost/move/move.hpp>
  16. #include <boost/geometry/geometry.hpp>
  17. #include <boost/geometry/index/detail/config_begin.hpp>
  18. #include <boost/geometry/index/detail/assert.hpp>
  19. #include <boost/geometry/index/detail/exception.hpp>
  20. #include <boost/geometry/index/detail/rtree/options.hpp>
  21. #include <boost/geometry/index/indexable.hpp>
  22. #include <boost/geometry/index/equal_to.hpp>
  23. #include <boost/geometry/index/detail/translator.hpp>
  24. #include <boost/geometry/index/predicates.hpp>
  25. #include <boost/geometry/index/distance_predicates.hpp>
  26. #include <boost/geometry/index/detail/rtree/adaptors.hpp>
  27. #include <boost/geometry/index/detail/meta.hpp>
  28. #include <boost/geometry/index/detail/utilities.hpp>
  29. #include <boost/geometry/index/detail/rtree/node/node.hpp>
  30. #include <boost/geometry/index/detail/algorithms/is_valid.hpp>
  31. #include <boost/geometry/index/detail/rtree/visitors/insert.hpp>
  32. #include <boost/geometry/index/detail/rtree/visitors/remove.hpp>
  33. #include <boost/geometry/index/detail/rtree/visitors/copy.hpp>
  34. #include <boost/geometry/index/detail/rtree/visitors/destroy.hpp>
  35. #include <boost/geometry/index/detail/rtree/visitors/spatial_query.hpp>
  36. #include <boost/geometry/index/detail/rtree/visitors/distance_query.hpp>
  37. #include <boost/geometry/index/detail/rtree/visitors/count.hpp>
  38. #include <boost/geometry/index/detail/rtree/visitors/children_box.hpp>
  39. #include <boost/geometry/index/detail/rtree/linear/linear.hpp>
  40. #include <boost/geometry/index/detail/rtree/quadratic/quadratic.hpp>
  41. #include <boost/geometry/index/detail/rtree/rstar/rstar.hpp>
  42. //#include <boost/geometry/extensions/index/detail/rtree/kmeans/kmeans.hpp>
  43. #include <boost/geometry/index/detail/rtree/pack_create.hpp>
  44. #include <boost/geometry/index/inserter.hpp>
  45. #include <boost/geometry/index/detail/rtree/utilities/view.hpp>
  46. #include <boost/geometry/index/detail/rtree/query_iterators.hpp>
  47. #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  48. // serialization
  49. #include <boost/geometry/index/detail/serialization.hpp>
  50. #endif
  51. // TODO change the name to bounding_tree
  52. /*!
  53. \defgroup rtree_functions R-tree free functions (boost::geometry::index::)
  54. */
  55. namespace boost { namespace geometry { namespace index {
  56. /*!
  57. \brief The R-tree spatial index.
  58. This is self-balancing spatial index capable to store various types of Values and balancing algorithms.
  59. \par Parameters
  60. The user must pass a type defining the Parameters which will
  61. be used in rtree creation process. This type is used e.g. to specify balancing algorithm
  62. with specific parameters like min and max number of elements in node.
  63. \par
  64. Predefined algorithms with compile-time parameters are:
  65. \li <tt>boost::geometry::index::linear</tt>,
  66. \li <tt>boost::geometry::index::quadratic</tt>,
  67. \li <tt>boost::geometry::index::rstar</tt>.
  68. \par
  69. Predefined algorithms with run-time parameters are:
  70. \li \c boost::geometry::index::dynamic_linear,
  71. \li \c boost::geometry::index::dynamic_quadratic,
  72. \li \c boost::geometry::index::dynamic_rstar.
  73. \par IndexableGetter
  74. The object of IndexableGetter type translates from Value to Indexable each time r-tree requires it. Which means that this
  75. operation is done for each Value access. Therefore the IndexableGetter should return the Indexable by
  76. const reference instead of a value. Default one can translate all types adapted to Point
  77. or Box concepts (called Indexables). It also handles <tt>std::pair<Indexable, T></tt> and
  78. <tt>boost::tuple<Indexable, ...></tt>. For example, if <tt>std::pair<Box, int></tt> is stored in the
  79. container, the default IndexableGetter translates from <tt>std::pair<Box, int> const&</tt> to <tt>Box const&</tt>.
  80. \par EqualTo
  81. The object of EqualTo type compares Values and returns <tt>true</tt> if they're equal. It's similar to <tt>std::equal_to<></tt>.
  82. The default EqualTo returns the result of <tt>boost::geometry::equals()</tt> for types adapted to some Geometry concept
  83. defined in Boost.Geometry and the result of operator= for other types. Components of Pairs and Tuples are compared left-to-right.
  84. \tparam Value The type of objects stored in the container.
  85. \tparam Parameters Compile-time parameters.
  86. \tparam IndexableGetter The function object extracting Indexable from Value.
  87. \tparam EqualTo The function object comparing objects of type Value.
  88. \tparam Allocator The allocator used to allocate/deallocate memory, construct/destroy nodes and Values.
  89. */
  90. template <
  91. typename Value,
  92. typename Parameters,
  93. typename IndexableGetter = index::indexable<Value>,
  94. typename EqualTo = index::equal_to<Value>,
  95. typename Allocator = std::allocator<Value>
  96. >
  97. class rtree
  98. {
  99. BOOST_COPYABLE_AND_MOVABLE(rtree)
  100. public:
  101. /*! \brief The type of Value stored in the container. */
  102. typedef Value value_type;
  103. /*! \brief R-tree parameters type. */
  104. typedef Parameters parameters_type;
  105. /*! \brief The function object extracting Indexable from Value. */
  106. typedef IndexableGetter indexable_getter;
  107. /*! \brief The function object comparing objects of type Value. */
  108. typedef EqualTo value_equal;
  109. /*! \brief The type of allocator used by the container. */
  110. typedef Allocator allocator_type;
  111. // TODO: SHOULD THIS TYPE BE REMOVED?
  112. /*! \brief The Indexable type to which Value is translated. */
  113. typedef typename index::detail::indexable_type<
  114. detail::translator<IndexableGetter, EqualTo>
  115. >::type indexable_type;
  116. /*! \brief The Box type used by the R-tree. */
  117. typedef geometry::model::box<
  118. geometry::model::point<
  119. typename coordinate_type<indexable_type>::type,
  120. dimension<indexable_type>::value,
  121. typename coordinate_system<indexable_type>::type
  122. >
  123. >
  124. bounds_type;
  125. private:
  126. typedef detail::translator<IndexableGetter, EqualTo> translator_type;
  127. typedef bounds_type box_type;
  128. typedef typename detail::rtree::options_type<Parameters>::type options_type;
  129. typedef typename options_type::node_tag node_tag;
  130. typedef detail::rtree::allocators<allocator_type, value_type, typename options_type::parameters_type, box_type, node_tag> allocators_type;
  131. typedef typename detail::rtree::node<value_type, typename options_type::parameters_type, box_type, allocators_type, node_tag>::type node;
  132. typedef typename detail::rtree::internal_node<value_type, typename options_type::parameters_type, box_type, allocators_type, node_tag>::type internal_node;
  133. typedef typename detail::rtree::leaf<value_type, typename options_type::parameters_type, box_type, allocators_type, node_tag>::type leaf;
  134. typedef typename allocators_type::node_pointer node_pointer;
  135. typedef ::boost::container::allocator_traits<Allocator> allocator_traits_type;
  136. typedef detail::rtree::node_auto_ptr<value_type, options_type, translator_type, box_type, allocators_type> node_auto_ptr;
  137. friend class detail::rtree::utilities::view<rtree>;
  138. #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  139. friend class detail::rtree::private_view<rtree>;
  140. friend class detail::rtree::const_private_view<rtree>;
  141. #endif
  142. public:
  143. /*! \brief Type of reference to Value. */
  144. typedef typename allocators_type::reference reference;
  145. /*! \brief Type of reference to const Value. */
  146. typedef typename allocators_type::const_reference const_reference;
  147. /*! \brief Type of pointer to Value. */
  148. typedef typename allocators_type::pointer pointer;
  149. /*! \brief Type of pointer to const Value. */
  150. typedef typename allocators_type::const_pointer const_pointer;
  151. /*! \brief Type of difference type. */
  152. typedef typename allocators_type::difference_type difference_type;
  153. /*! \brief Unsigned integral type used by the container. */
  154. typedef typename allocators_type::size_type size_type;
  155. /*! \brief Type of const query iterator. */
  156. typedef index::detail::rtree::iterators::query_iterator<value_type, allocators_type> const_query_iterator;
  157. public:
  158. /*!
  159. \brief The constructor.
  160. \param parameters The parameters object.
  161. \param getter The function object extracting Indexable from Value.
  162. \param equal The function object comparing Values.
  163. \par Throws
  164. If allocator default constructor throws.
  165. */
  166. inline explicit rtree(parameters_type const& parameters = parameters_type(),
  167. indexable_getter const& getter = indexable_getter(),
  168. value_equal const& equal = value_equal())
  169. : m_members(getter, equal, parameters)
  170. {}
  171. /*!
  172. \brief The constructor.
  173. \param parameters The parameters object.
  174. \param getter The function object extracting Indexable from Value.
  175. \param equal The function object comparing Values.
  176. \param allocator The allocator object.
  177. \par Throws
  178. If allocator copy constructor throws.
  179. */
  180. inline rtree(parameters_type const& parameters,
  181. indexable_getter const& getter,
  182. value_equal const& equal,
  183. allocator_type const& allocator)
  184. : m_members(getter, equal, parameters, allocator)
  185. {}
  186. /*!
  187. \brief The constructor.
  188. The tree is created using packing algorithm.
  189. \param first The beginning of the range of Values.
  190. \param last The end of the range of Values.
  191. \param parameters The parameters object.
  192. \param getter The function object extracting Indexable from Value.
  193. \param equal The function object comparing Values.
  194. \param allocator The allocator object.
  195. \par Throws
  196. \li If allocator copy constructor throws.
  197. \li If Value copy constructor or copy assignment throws.
  198. \li If allocation throws or returns invalid value.
  199. */
  200. template<typename Iterator>
  201. inline rtree(Iterator first, Iterator last,
  202. parameters_type const& parameters = parameters_type(),
  203. indexable_getter const& getter = indexable_getter(),
  204. value_equal const& equal = value_equal(),
  205. allocator_type const& allocator = allocator_type())
  206. : m_members(getter, equal, parameters, allocator)
  207. {
  208. typedef detail::rtree::pack<value_type, options_type, translator_type, box_type, allocators_type> pack;
  209. size_type vc = 0, ll = 0;
  210. m_members.root = pack::apply(first, last, vc, ll,
  211. m_members.parameters(), m_members.translator(), m_members.allocators());
  212. m_members.values_count = vc;
  213. m_members.leafs_level = ll;
  214. }
  215. /*!
  216. \brief The constructor.
  217. The tree is created using packing algorithm.
  218. \param rng The range of Values.
  219. \param parameters The parameters object.
  220. \param getter The function object extracting Indexable from Value.
  221. \param equal The function object comparing Values.
  222. \param allocator The allocator object.
  223. \par Throws
  224. \li If allocator copy constructor throws.
  225. \li If Value copy constructor or copy assignment throws.
  226. \li If allocation throws or returns invalid value.
  227. */
  228. template<typename Range>
  229. inline explicit rtree(Range const& rng,
  230. parameters_type const& parameters = parameters_type(),
  231. indexable_getter const& getter = indexable_getter(),
  232. value_equal const& equal = value_equal(),
  233. allocator_type const& allocator = allocator_type())
  234. : m_members(getter, equal, parameters, allocator)
  235. {
  236. typedef detail::rtree::pack<value_type, options_type, translator_type, box_type, allocators_type> pack;
  237. size_type vc = 0, ll = 0;
  238. m_members.root = pack::apply(::boost::begin(rng), ::boost::end(rng), vc, ll,
  239. m_members.parameters(), m_members.translator(), m_members.allocators());
  240. m_members.values_count = vc;
  241. m_members.leafs_level = ll;
  242. }
  243. /*!
  244. \brief The destructor.
  245. \par Throws
  246. Nothing.
  247. */
  248. inline ~rtree()
  249. {
  250. this->raw_destroy(*this);
  251. }
  252. /*!
  253. \brief The copy constructor.
  254. It uses parameters, translator and allocator from the source tree.
  255. \param src The rtree which content will be copied.
  256. \par Throws
  257. \li If allocator copy constructor throws.
  258. \li If Value copy constructor throws.
  259. \li If allocation throws or returns invalid value.
  260. */
  261. inline rtree(rtree const& src)
  262. : m_members(src.m_members.indexable_getter(),
  263. src.m_members.equal_to(),
  264. src.m_members.parameters(),
  265. allocator_traits_type::select_on_container_copy_construction(src.get_allocator()))
  266. {
  267. this->raw_copy(src, *this, false);
  268. }
  269. /*!
  270. \brief The copy constructor.
  271. It uses Parameters and translator from the source tree.
  272. \param src The rtree which content will be copied.
  273. \param allocator The allocator which will be used.
  274. \par Throws
  275. \li If allocator copy constructor throws.
  276. \li If Value copy constructor throws.
  277. \li If allocation throws or returns invalid value.
  278. */
  279. inline rtree(rtree const& src, allocator_type const& allocator)
  280. : m_members(src.m_members.indexable_getter(),
  281. src.m_members.equal_to(),
  282. src.m_members.parameters(), allocator)
  283. {
  284. this->raw_copy(src, *this, false);
  285. }
  286. /*!
  287. \brief The moving constructor.
  288. It uses parameters, translator and allocator from the source tree.
  289. \param src The rtree which content will be moved.
  290. \par Throws
  291. Nothing.
  292. */
  293. inline rtree(BOOST_RV_REF(rtree) src)
  294. : m_members(src.m_members.indexable_getter(),
  295. src.m_members.equal_to(),
  296. src.m_members.parameters(),
  297. boost::move(src.m_members.allocators()))
  298. {
  299. boost::swap(m_members.values_count, src.m_members.values_count);
  300. boost::swap(m_members.leafs_level, src.m_members.leafs_level);
  301. boost::swap(m_members.root, src.m_members.root);
  302. }
  303. /*!
  304. \brief The moving constructor.
  305. It uses parameters and translator from the source tree.
  306. \param src The rtree which content will be moved.
  307. \param allocator The allocator.
  308. \par Throws
  309. \li If allocator copy constructor throws.
  310. \li If Value copy constructor throws (only if allocators aren't equal).
  311. \li If allocation throws or returns invalid value (only if allocators aren't equal).
  312. */
  313. inline rtree(BOOST_RV_REF(rtree) src, allocator_type const& allocator)
  314. : m_members(src.m_members.indexable_getter(),
  315. src.m_members.equal_to(),
  316. src.m_members.parameters(),
  317. allocator)
  318. {
  319. if ( src.m_members.allocators() == allocator )
  320. {
  321. boost::swap(m_members.values_count, src.m_members.values_count);
  322. boost::swap(m_members.leafs_level, src.m_members.leafs_level);
  323. boost::swap(m_members.root, src.m_members.root);
  324. }
  325. else
  326. {
  327. this->raw_copy(src, *this, false);
  328. }
  329. }
  330. /*!
  331. \brief The assignment operator.
  332. It uses parameters and translator from the source tree.
  333. \param src The rtree which content will be copied.
  334. \par Throws
  335. \li If Value copy constructor throws.
  336. \li If allocation throws.
  337. \li If allocation throws or returns invalid value.
  338. */
  339. inline rtree & operator=(BOOST_COPY_ASSIGN_REF(rtree) src)
  340. {
  341. if ( &src != this )
  342. {
  343. allocators_type & this_allocs = m_members.allocators();
  344. allocators_type const& src_allocs = src.m_members.allocators();
  345. // NOTE: if propagate is true for std allocators on darwin 4.2.1, glibc++
  346. // (allocators stored as base classes of members_holder)
  347. // copying them changes values_count, in this case it doesn't cause errors since data must be copied
  348. typedef boost::mpl::bool_<
  349. allocator_traits_type::propagate_on_container_copy_assignment::value
  350. > propagate;
  351. if ( propagate::value && !(this_allocs == src_allocs) )
  352. this->raw_destroy(*this);
  353. detail::assign_cond(this_allocs, src_allocs, propagate());
  354. // It uses m_allocators
  355. this->raw_copy(src, *this, true);
  356. }
  357. return *this;
  358. }
  359. /*!
  360. \brief The moving assignment.
  361. It uses parameters and translator from the source tree.
  362. \param src The rtree which content will be moved.
  363. \par Throws
  364. Only if allocators aren't equal.
  365. \li If Value copy constructor throws.
  366. \li If allocation throws or returns invalid value.
  367. */
  368. inline rtree & operator=(BOOST_RV_REF(rtree) src)
  369. {
  370. if ( &src != this )
  371. {
  372. allocators_type & this_allocs = m_members.allocators();
  373. allocators_type & src_allocs = src.m_members.allocators();
  374. if ( this_allocs == src_allocs )
  375. {
  376. this->raw_destroy(*this);
  377. m_members.indexable_getter() = src.m_members.indexable_getter();
  378. m_members.equal_to() = src.m_members.equal_to();
  379. m_members.parameters() = src.m_members.parameters();
  380. boost::swap(m_members.values_count, src.m_members.values_count);
  381. boost::swap(m_members.leafs_level, src.m_members.leafs_level);
  382. boost::swap(m_members.root, src.m_members.root);
  383. // NOTE: if propagate is true for std allocators on darwin 4.2.1, glibc++
  384. // (allocators stored as base classes of members_holder)
  385. // moving them changes values_count
  386. typedef boost::mpl::bool_<
  387. allocator_traits_type::propagate_on_container_move_assignment::value
  388. > propagate;
  389. detail::move_cond(this_allocs, src_allocs, propagate());
  390. }
  391. else
  392. {
  393. // TODO - shouldn't here propagate_on_container_copy_assignment be checked like in operator=(const&)?
  394. // It uses m_allocators
  395. this->raw_copy(src, *this, true);
  396. }
  397. }
  398. return *this;
  399. }
  400. /*!
  401. \brief Swaps contents of two rtrees.
  402. Parameters, translator and allocators are swapped as well.
  403. \param other The rtree which content will be swapped with this rtree content.
  404. \par Throws
  405. If allocators swap throws.
  406. */
  407. void swap(rtree & other)
  408. {
  409. boost::swap(m_members.indexable_getter(), other.m_members.indexable_getter());
  410. boost::swap(m_members.equal_to(), other.m_members.equal_to());
  411. boost::swap(m_members.parameters(), other.m_members.parameters());
  412. // NOTE: if propagate is true for std allocators on darwin 4.2.1, glibc++
  413. // (allocators stored as base classes of members_holder)
  414. // swapping them changes values_count
  415. typedef boost::mpl::bool_<
  416. allocator_traits_type::propagate_on_container_swap::value
  417. > propagate;
  418. detail::swap_cond(m_members.allocators(), other.m_members.allocators(), propagate());
  419. boost::swap(m_members.values_count, other.m_members.values_count);
  420. boost::swap(m_members.leafs_level, other.m_members.leafs_level);
  421. boost::swap(m_members.root, other.m_members.root);
  422. }
  423. /*!
  424. \brief Insert a value to the index.
  425. \param value The value which will be stored in the container.
  426. \par Throws
  427. \li If Value copy constructor or copy assignment throws.
  428. \li If allocation throws or returns invalid value.
  429. \warning
  430. This operation only guarantees that there will be no memory leaks.
  431. After an exception is thrown the R-tree may be left in an inconsistent state,
  432. elements must not be inserted or removed. Other operations are allowed however
  433. some of them may return invalid data.
  434. */
  435. inline void insert(value_type const& value)
  436. {
  437. if ( !m_members.root )
  438. this->raw_create();
  439. this->raw_insert(value);
  440. }
  441. /*!
  442. \brief Insert a range of values to the index.
  443. \param first The beginning of the range of values.
  444. \param last The end of the range of values.
  445. \par Throws
  446. \li If Value copy constructor or copy assignment throws.
  447. \li If allocation throws or returns invalid value.
  448. \warning
  449. This operation only guarantees that there will be no memory leaks.
  450. After an exception is thrown the R-tree may be left in an inconsistent state,
  451. elements must not be inserted or removed. Other operations are allowed however
  452. some of them may return invalid data.
  453. */
  454. template <typename Iterator>
  455. inline void insert(Iterator first, Iterator last)
  456. {
  457. if ( !m_members.root )
  458. this->raw_create();
  459. for ( ; first != last ; ++first )
  460. this->raw_insert(*first);
  461. }
  462. /*!
  463. \brief Insert a range of values to the index.
  464. \param rng The range of values.
  465. \par Throws
  466. \li If Value copy constructor or copy assignment throws.
  467. \li If allocation throws or returns invalid value.
  468. \warning
  469. This operation only guarantees that there will be no memory leaks.
  470. After an exception is thrown the R-tree may be left in an inconsistent state,
  471. elements must not be inserted or removed. Other operations are allowed however
  472. some of them may return invalid data.
  473. */
  474. template <typename Range>
  475. inline void insert(Range const& rng)
  476. {
  477. BOOST_MPL_ASSERT_MSG((detail::is_range<Range>::value), PASSED_OBJECT_IS_NOT_A_RANGE, (Range));
  478. if ( !m_members.root )
  479. this->raw_create();
  480. typedef typename boost::range_const_iterator<Range>::type It;
  481. for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it )
  482. this->raw_insert(*it);
  483. }
  484. /*!
  485. \brief Remove a value from the container.
  486. In contrast to the \c std::set or <tt>std::map erase()</tt> method
  487. this method removes only one value from the container.
  488. \param value The value which will be removed from the container.
  489. \return 1 if the value was removed, 0 otherwise.
  490. \par Throws
  491. \li If Value copy constructor or copy assignment throws.
  492. \li If allocation throws or returns invalid value.
  493. \warning
  494. This operation only guarantees that there will be no memory leaks.
  495. After an exception is thrown the R-tree may be left in an inconsistent state,
  496. elements must not be inserted or removed. Other operations are allowed however
  497. some of them may return invalid data.
  498. */
  499. inline size_type remove(value_type const& value)
  500. {
  501. return this->raw_remove(value);
  502. }
  503. /*!
  504. \brief Remove a range of values from the container.
  505. In contrast to the \c std::set or <tt>std::map erase()</tt> method
  506. it doesn't take iterators pointing to values stored in this container. It removes values equal
  507. to these passed as a range. Furthermore this method removes only one value for each one passed
  508. in the range, not all equal values.
  509. \param first The beginning of the range of values.
  510. \param last The end of the range of values.
  511. \return The number of removed values.
  512. \par Throws
  513. \li If Value copy constructor or copy assignment throws.
  514. \li If allocation throws or returns invalid value.
  515. \warning
  516. This operation only guarantees that there will be no memory leaks.
  517. After an exception is thrown the R-tree may be left in an inconsistent state,
  518. elements must not be inserted or removed. Other operations are allowed however
  519. some of them may return invalid data.
  520. */
  521. template <typename Iterator>
  522. inline size_type remove(Iterator first, Iterator last)
  523. {
  524. size_type result = 0;
  525. for ( ; first != last ; ++first )
  526. result += this->raw_remove(*first);
  527. return result;
  528. }
  529. /*!
  530. \brief Remove a range of values from the container.
  531. In contrast to the \c std::set or <tt>std::map erase()</tt> method
  532. it removes values equal to these passed as a range. Furthermore, this method removes only
  533. one value for each one passed in the range, not all equal values.
  534. \param rng The range of values.
  535. \return The number of removed values.
  536. \par Throws
  537. \li If Value copy constructor or copy assignment throws.
  538. \li If allocation throws or returns invalid value.
  539. \warning
  540. This operation only guarantees that there will be no memory leaks.
  541. After an exception is thrown the R-tree may be left in an inconsistent state,
  542. elements must not be inserted or removed. Other operations are allowed however
  543. some of them may return invalid data.
  544. */
  545. template <typename Range>
  546. inline size_type remove(Range const& rng)
  547. {
  548. BOOST_MPL_ASSERT_MSG((detail::is_range<Range>::value), PASSED_OBJECT_IS_NOT_A_RANGE, (Range));
  549. size_type result = 0;
  550. typedef typename boost::range_const_iterator<Range>::type It;
  551. for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it )
  552. result += this->raw_remove(*it);
  553. return result;
  554. }
  555. /*!
  556. \brief Finds values meeting passed predicates e.g. nearest to some Point and/or intersecting some Box.
  557. This query function performs spatial and k-nearest neighbor searches. It allows to pass a set of predicates.
  558. Values will be returned only if all predicates are met.
  559. <b>Spatial predicates</b>
  560. Spatial predicates may be generated by one of the functions listed below:
  561. \li \c boost::geometry::index::contains(),
  562. \li \c boost::geometry::index::covered_by(),
  563. \li \c boost::geometry::index::covers(),
  564. \li \c boost::geometry::index::disjoint(),
  565. \li \c boost::geometry::index::intersects(),
  566. \li \c boost::geometry::index::overlaps(),
  567. \li \c boost::geometry::index::within(),
  568. It is possible to negate spatial predicates:
  569. \li <tt>! boost::geometry::index::contains()</tt>,
  570. \li <tt>! boost::geometry::index::covered_by()</tt>,
  571. \li <tt>! boost::geometry::index::covers()</tt>,
  572. \li <tt>! boost::geometry::index::disjoint()</tt>,
  573. \li <tt>! boost::geometry::index::intersects()</tt>,
  574. \li <tt>! boost::geometry::index::overlaps()</tt>,
  575. \li <tt>! boost::geometry::index::within()</tt>
  576. <b>Satisfies predicate</b>
  577. This is a special kind of predicate which allows to pass a user-defined function or function object which checks
  578. if Value should be returned by the query. It's generated by:
  579. \li \c boost::geometry::index::satisfies().
  580. <b>Nearest predicate</b>
  581. If the nearest predicate is passed a k-nearest neighbor search will be performed. This query will result
  582. in returning k values to the output iterator. Only one nearest predicate may be passed to the query.
  583. It may be generated by:
  584. \li \c boost::geometry::index::nearest().
  585. <b>Connecting predicates</b>
  586. Predicates may be passed together connected with \c operator&&().
  587. \par Example
  588. \verbatim
  589. // return elements intersecting box
  590. tree.query(bgi::intersects(box), std::back_inserter(result));
  591. // return elements intersecting poly but not within box
  592. tree.query(bgi::intersects(poly) && !bgi::within(box), std::back_inserter(result));
  593. // return elements overlapping box and meeting my_fun unary predicate
  594. tree.query(bgi::overlaps(box) && bgi::satisfies(my_fun), std::back_inserter(result));
  595. // return 5 elements nearest to pt and elements are intersecting box
  596. tree.query(bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result));
  597. \endverbatim
  598. \par Throws
  599. If Value copy constructor or copy assignment throws.
  600. If predicates copy throws.
  601. \warning
  602. Only one \c nearest() perdicate may be passed to the query. Passing more of them results in compile-time error.
  603. \param predicates Predicates.
  604. \param out_it The output iterator, e.g. generated by std::back_inserter().
  605. \return The number of values found.
  606. */
  607. template <typename Predicates, typename OutIter>
  608. size_type query(Predicates const& predicates, OutIter out_it) const
  609. {
  610. if ( !m_members.root )
  611. return 0;
  612. static const unsigned distance_predicates_count = detail::predicates_count_distance<Predicates>::value;
  613. static const bool is_distance_predicate = 0 < distance_predicates_count;
  614. BOOST_MPL_ASSERT_MSG((distance_predicates_count <= 1), PASS_ONLY_ONE_DISTANCE_PREDICATE, (Predicates));
  615. return query_dispatch(predicates, out_it, boost::mpl::bool_<is_distance_predicate>());
  616. }
  617. /*!
  618. \brief Returns the query iterator pointing at the begin of the query range.
  619. This method returns the iterator which may be used to perform iterative queries. For the information
  620. about the predicates which may be passed to this method see query().
  621. \par Example
  622. \verbatim
  623. for ( Rtree::const_query_iterator it = tree.qbegin(bgi::nearest(pt, 10000)) ;
  624. it != tree.qend() ; ++it )
  625. {
  626. // do something with value
  627. if ( has_enough_nearest_values() )
  628. break;
  629. }
  630. \endverbatim
  631. \par Throws
  632. If predicates copy throws.
  633. If allocation throws.
  634. \param predicates Predicates.
  635. \return The iterator pointing at the begin of the query range.
  636. */
  637. template <typename Predicates>
  638. const_query_iterator qbegin(Predicates const& predicates) const
  639. {
  640. return const_query_iterator(qbegin_(predicates));
  641. }
  642. /*!
  643. \brief Returns the query iterator pointing at the end of the query range.
  644. This method returns the iterator which may be used to check if the query has ended.
  645. \par Example
  646. \verbatim
  647. for ( Rtree::const_query_iterator it = tree.qbegin(bgi::nearest(pt, 10000)) ;
  648. it != tree.qend() ; ++it )
  649. {
  650. // do something with value
  651. if ( has_enough_nearest_values() )
  652. break;
  653. }
  654. \endverbatim
  655. \par Throws
  656. Nothing
  657. \return The iterator pointing at the end of the query range.
  658. */
  659. const_query_iterator qend() const
  660. {
  661. return const_query_iterator();
  662. }
  663. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  664. private:
  665. #endif
  666. /*!
  667. \brief Returns the query iterator pointing at the begin of the query range.
  668. This method returns the iterator which may be used to perform iterative queries. For the information
  669. about the predicates which may be passed to this method see query().
  670. The type of the returned iterator depends on the type of passed Predicates but the iterator of this type
  671. may be assigned to the variable of const_query_iterator type. If you'd like to use the type of the iterator
  672. returned by this method you may get the type e.g. by using C++11 decltype or Boost.Typeof library.
  673. This iterator may be compared with iterators returned by both versions of qend() method.
  674. \par Example
  675. \verbatim
  676. // Store the result in the container using std::copy() - it requires both iterators of the same type
  677. std::copy(tree.qbegin(bgi::intersects(box)), tree.qend(bgi::intersects(box)), std::back_inserter(result));
  678. // Store the result in the container using std::copy() and type-erased iterators
  679. Rtree::const_query_iterator first = tree.qbegin(bgi::intersects(box));
  680. Rtree::const_query_iterator last = tree.qend();
  681. std::copy(first, last, std::back_inserter(result));
  682. // Boost.Typeof
  683. typedef BOOST_TYPEOF(tree.qbegin(bgi::nearest(pt, 10000))) Iter;
  684. for ( Iter it = tree.qbegin(bgi::nearest(pt, 10000)) ; it != tree.qend() ; ++it )
  685. {
  686. // do something with value
  687. if ( has_enough_nearest_values() )
  688. break;
  689. }
  690. \endverbatim
  691. \par Throws
  692. If predicates copy throws.
  693. If allocation throws.
  694. \param predicates Predicates.
  695. \return The iterator pointing at the begin of the query range.
  696. */
  697. template <typename Predicates>
  698. typename boost::mpl::if_c<
  699. detail::predicates_count_distance<Predicates>::value == 0,
  700. detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>,
  701. detail::rtree::iterators::distance_query_iterator<
  702. value_type, options_type, translator_type, box_type, allocators_type, Predicates,
  703. detail::predicates_find_distance<Predicates>::value
  704. >
  705. >::type
  706. qbegin_(Predicates const& predicates) const
  707. {
  708. static const unsigned distance_predicates_count = detail::predicates_count_distance<Predicates>::value;
  709. BOOST_MPL_ASSERT_MSG((distance_predicates_count <= 1), PASS_ONLY_ONE_DISTANCE_PREDICATE, (Predicates));
  710. typedef typename boost::mpl::if_c<
  711. detail::predicates_count_distance<Predicates>::value == 0,
  712. detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>,
  713. detail::rtree::iterators::distance_query_iterator<
  714. value_type, options_type, translator_type, box_type, allocators_type, Predicates,
  715. detail::predicates_find_distance<Predicates>::value
  716. >
  717. >::type iterator_type;
  718. if ( !m_members.root )
  719. return iterator_type(m_members.translator(), predicates);
  720. return iterator_type(m_members.root, m_members.translator(), predicates);
  721. }
  722. /*!
  723. \brief Returns the query iterator pointing at the end of the query range.
  724. This method returns the iterator which may be used to perform iterative queries. For the information
  725. about the predicates which may be passed to this method see query().
  726. The type of the returned iterator depends on the type of passed Predicates but the iterator of this type
  727. may be assigned to the variable of const_query_iterator type. If you'd like to use the type of the iterator
  728. returned by this method you may get the type e.g. by using C++11 decltype or Boost.Typeof library.
  729. The type of the iterator returned by this method is the same as the one returned by qbegin() to which
  730. the same predicates were passed.
  731. \par Example
  732. \verbatim
  733. // Store the result in the container using std::copy() - it requires both iterators of the same type
  734. std::copy(tree.qbegin(bgi::intersects(box)), tree.qend(bgi::intersects(box)), std::back_inserter(result));
  735. \endverbatim
  736. \par Throws
  737. If predicates copy throws.
  738. \param predicates Predicates.
  739. \return The iterator pointing at the end of the query range.
  740. */
  741. template <typename Predicates>
  742. typename boost::mpl::if_c<
  743. detail::predicates_count_distance<Predicates>::value == 0,
  744. detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>,
  745. detail::rtree::iterators::distance_query_iterator<
  746. value_type, options_type, translator_type, box_type, allocators_type, Predicates,
  747. detail::predicates_find_distance<Predicates>::value
  748. >
  749. >::type
  750. qend_(Predicates const& predicates) const
  751. {
  752. static const unsigned distance_predicates_count = detail::predicates_count_distance<Predicates>::value;
  753. BOOST_MPL_ASSERT_MSG((distance_predicates_count <= 1), PASS_ONLY_ONE_DISTANCE_PREDICATE, (Predicates));
  754. typedef typename boost::mpl::if_c<
  755. detail::predicates_count_distance<Predicates>::value == 0,
  756. detail::rtree::iterators::spatial_query_iterator<value_type, options_type, translator_type, box_type, allocators_type, Predicates>,
  757. detail::rtree::iterators::distance_query_iterator<
  758. value_type, options_type, translator_type, box_type, allocators_type, Predicates,
  759. detail::predicates_find_distance<Predicates>::value
  760. >
  761. >::type iterator_type;
  762. return iterator_type(m_members.translator(), predicates);
  763. }
  764. /*!
  765. \brief Returns the query iterator pointing at the end of the query range.
  766. This method returns the iterator which may be compared with the iterator returned by qbegin() in order to
  767. check if the query has ended.
  768. The type of the returned iterator is different than the type returned by qbegin() but the iterator of this type
  769. 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
  770. method, which most certainly will be faster than the type-erased iterator, you may get the type
  771. e.g. by using C++11 decltype or Boost.Typeof library.
  772. The type of the iterator returned by this method is dfferent than the type returned by qbegin().
  773. \par Example
  774. \verbatim
  775. // Store the result in the container using std::copy() and type-erased iterators
  776. Rtree::const_query_iterator first = tree.qbegin(bgi::intersects(box));
  777. Rtree::const_query_iterator last = tree.qend();
  778. std::copy(first, last, std::back_inserter(result));
  779. // Boost.Typeof
  780. typedef BOOST_TYPEOF(tree.qbegin(bgi::nearest(pt, 10000))) Iter;
  781. for ( Iter it = tree.qbegin(bgi::nearest(pt, 10000)) ; it != tree.qend() ; ++it )
  782. {
  783. // do something with value
  784. if ( has_enough_nearest_values() )
  785. break;
  786. }
  787. \endverbatim
  788. \par Throws
  789. Nothing
  790. \return The iterator pointing at the end of the query range.
  791. */
  792. detail::rtree::iterators::end_query_iterator<value_type, allocators_type>
  793. qend_() const
  794. {
  795. return detail::rtree::iterators::end_query_iterator<value_type, allocators_type>();
  796. }
  797. public:
  798. /*!
  799. \brief Returns the number of stored values.
  800. \return The number of stored values.
  801. \par Throws
  802. Nothing.
  803. */
  804. inline size_type size() const
  805. {
  806. return m_members.values_count;
  807. }
  808. /*!
  809. \brief Query if the container is empty.
  810. \return true if the container is empty.
  811. \par Throws
  812. Nothing.
  813. */
  814. inline bool empty() const
  815. {
  816. return 0 == m_members.values_count;
  817. }
  818. /*!
  819. \brief Removes all values stored in the container.
  820. \par Throws
  821. Nothing.
  822. */
  823. inline void clear()
  824. {
  825. this->raw_destroy(*this);
  826. }
  827. /*!
  828. \brief Returns the box able to contain all values stored in the container.
  829. Returns the box able to contain all values stored in the container.
  830. If the container is empty the result of \c geometry::assign_inverse() is returned.
  831. \return The box able to contain all values stored in the container or an invalid box if
  832. there are no values in the container.
  833. \par Throws
  834. Nothing.
  835. */
  836. inline bounds_type bounds() const
  837. {
  838. bounds_type result;
  839. if ( !m_members.root )
  840. {
  841. geometry::assign_inverse(result);
  842. return result;
  843. }
  844. detail::rtree::visitors::children_box<value_type, options_type, translator_type, box_type, allocators_type>
  845. box_v(result, m_members.translator());
  846. detail::rtree::apply_visitor(box_v, *m_members.root);
  847. return result;
  848. }
  849. /*!
  850. \brief Count Values or Indexables stored in the container.
  851. For indexable_type it returns the number of values which indexables equals the parameter.
  852. For value_type it returns the number of values which equals the parameter.
  853. \param vori The value or indexable which will be counted.
  854. \return The number of values found.
  855. \par Throws
  856. Nothing.
  857. */
  858. template <typename ValueOrIndexable>
  859. size_type count(ValueOrIndexable const& vori) const
  860. {
  861. if ( !m_members.root )
  862. return 0;
  863. detail::rtree::visitors::count<ValueOrIndexable, value_type, options_type, translator_type, box_type, allocators_type>
  864. count_v(vori, m_members.translator());
  865. detail::rtree::apply_visitor(count_v, *m_members.root);
  866. return count_v.found_count;
  867. }
  868. /*!
  869. \brief Returns parameters.
  870. \return The parameters object.
  871. \par Throws
  872. Nothing.
  873. */
  874. inline parameters_type parameters() const
  875. {
  876. return m_members.parameters();
  877. }
  878. /*!
  879. \brief Returns function retrieving Indexable from Value.
  880. \return The indexable_getter object.
  881. \par Throws
  882. Nothing.
  883. */
  884. indexable_getter indexable_get() const
  885. {
  886. return m_members.indexable_getter();
  887. }
  888. /*!
  889. \brief Returns function comparing Values
  890. \return The value_equal function.
  891. \par Throws
  892. Nothing.
  893. */
  894. value_equal value_eq() const
  895. {
  896. return m_members.equal_to();
  897. }
  898. /*!
  899. \brief Returns allocator used by the rtree.
  900. \return The allocator.
  901. \par Throws
  902. If allocator copy constructor throws.
  903. */
  904. allocator_type get_allocator() const
  905. {
  906. return m_members.allocators().allocator();
  907. }
  908. private:
  909. /*!
  910. \brief Returns the translator object.
  911. \return The translator object.
  912. \par Throws
  913. Nothing.
  914. */
  915. inline translator_type translator() const
  916. {
  917. return m_members.translator();
  918. }
  919. /*!
  920. \brief Apply a visitor to the nodes structure in order to perform some operator.
  921. This function is not a part of the 'official' interface. However it makes
  922. possible e.g. to pass a visitor drawing the tree structure.
  923. \param visitor The visitor object.
  924. \par Throws
  925. If Visitor::operator() throws.
  926. */
  927. template <typename Visitor>
  928. inline void apply_visitor(Visitor & visitor) const
  929. {
  930. if ( m_members.root )
  931. detail::rtree::apply_visitor(visitor, *m_members.root);
  932. }
  933. /*!
  934. \brief Returns the depth of the R-tree.
  935. This function is not a part of the 'official' interface.
  936. \return The depth of the R-tree.
  937. \par Throws
  938. Nothing.
  939. */
  940. inline size_type depth() const
  941. {
  942. return m_members.leafs_level;
  943. }
  944. private:
  945. /*!
  946. \pre Root node must exist - m_root != 0.
  947. \brief Insert a value to the index.
  948. \param value The value which will be stored in the container.
  949. \par Exception-safety
  950. basic
  951. */
  952. inline void raw_insert(value_type const& value)
  953. {
  954. BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist");
  955. BOOST_GEOMETRY_INDEX_ASSERT(detail::is_valid(m_members.translator()(value)), "Indexable is invalid");
  956. detail::rtree::visitors::insert<
  957. value_type,
  958. value_type, options_type, translator_type, box_type, allocators_type,
  959. typename options_type::insert_tag
  960. > insert_v(m_members.root, m_members.leafs_level, value,
  961. m_members.parameters(), m_members.translator(), m_members.allocators());
  962. detail::rtree::apply_visitor(insert_v, *m_members.root);
  963. // TODO
  964. // Think about this: If exception is thrown, may the root be removed?
  965. // Or it is just cleared?
  966. // TODO
  967. // If exception is thrown, m_values_count may be invalid
  968. ++m_members.values_count;
  969. }
  970. /*!
  971. \brief Remove the value from the container.
  972. \param value The value which will be removed from the container.
  973. \par Exception-safety
  974. basic
  975. */
  976. inline size_type raw_remove(value_type const& value)
  977. {
  978. // TODO: awulkiew - assert for correct value (indexable) ?
  979. BOOST_GEOMETRY_INDEX_ASSERT(m_members.root, "The root must exist");
  980. detail::rtree::visitors::remove<
  981. value_type, options_type, translator_type, box_type, allocators_type
  982. > remove_v(m_members.root, m_members.leafs_level, value,
  983. m_members.parameters(), m_members.translator(), m_members.allocators());
  984. detail::rtree::apply_visitor(remove_v, *m_members.root);
  985. // If exception is thrown, m_values_count may be invalid
  986. if ( remove_v.is_value_removed() )
  987. {
  988. BOOST_GEOMETRY_INDEX_ASSERT(0 < m_members.values_count, "unexpected state");
  989. --m_members.values_count;
  990. return 1;
  991. }
  992. return 0;
  993. }
  994. /*!
  995. \brief Create an empty R-tree i.e. new empty root node and clear other attributes.
  996. \par Exception-safety
  997. strong
  998. */
  999. inline void raw_create()
  1000. {
  1001. BOOST_GEOMETRY_INDEX_ASSERT(0 == m_members.root, "the tree is already created");
  1002. m_members.root = detail::rtree::create_node<allocators_type, leaf>::apply(m_members.allocators()); // MAY THROW (N: alloc)
  1003. m_members.values_count = 0;
  1004. m_members.leafs_level = 0;
  1005. }
  1006. /*!
  1007. \brief Destroy the R-tree i.e. all nodes and clear attributes.
  1008. \param t The container which is going to be destroyed.
  1009. \par Exception-safety
  1010. nothrow
  1011. */
  1012. inline void raw_destroy(rtree & t)
  1013. {
  1014. if ( t.m_members.root )
  1015. {
  1016. detail::rtree::visitors::destroy<value_type, options_type, translator_type, box_type, allocators_type>
  1017. del_v(t.m_members.root, t.m_members.allocators());
  1018. detail::rtree::apply_visitor(del_v, *t.m_members.root);
  1019. t.m_members.root = 0;
  1020. }
  1021. t.m_members.values_count = 0;
  1022. t.m_members.leafs_level = 0;
  1023. }
  1024. /*!
  1025. \brief Copy the R-tree i.e. whole nodes structure, values and other attributes.
  1026. It uses destination's allocators to create the new structure.
  1027. \param src The source R-tree.
  1028. \param dst The destination R-tree.
  1029. \param copy_tr_and_params If true, translator and parameters will also be copied.
  1030. \par Exception-safety
  1031. strong
  1032. */
  1033. inline void raw_copy(rtree const& src, rtree & dst, bool copy_tr_and_params) const
  1034. {
  1035. detail::rtree::visitors::copy<value_type, options_type, translator_type, box_type, allocators_type>
  1036. copy_v(dst.m_members.allocators());
  1037. if ( src.m_members.root )
  1038. detail::rtree::apply_visitor(copy_v, *src.m_members.root); // MAY THROW (V, E: alloc, copy, N: alloc)
  1039. if ( copy_tr_and_params )
  1040. {
  1041. dst.m_members.indexable_getter() = src.m_members.indexable_getter();
  1042. dst.m_members.equal_to() = src.m_members.equal_to();
  1043. dst.m_members.parameters() = src.m_members.parameters();
  1044. }
  1045. // TODO use node_auto_ptr
  1046. if ( dst.m_members.root )
  1047. {
  1048. detail::rtree::visitors::destroy<value_type, options_type, translator_type, box_type, allocators_type>
  1049. del_v(dst.m_members.root, dst.m_members.allocators());
  1050. detail::rtree::apply_visitor(del_v, *dst.m_members.root);
  1051. dst.m_members.root = 0;
  1052. }
  1053. dst.m_members.root = copy_v.result;
  1054. dst.m_members.values_count = src.m_members.values_count;
  1055. dst.m_members.leafs_level = src.m_members.leafs_level;
  1056. }
  1057. /*!
  1058. \brief Return values meeting predicates.
  1059. \par Exception-safety
  1060. strong
  1061. */
  1062. template <typename Predicates, typename OutIter>
  1063. size_type query_dispatch(Predicates const& predicates, OutIter out_it, boost::mpl::bool_<false> const& /*is_distance_predicate*/) const
  1064. {
  1065. detail::rtree::visitors::spatial_query<value_type, options_type, translator_type, box_type, allocators_type, Predicates, OutIter>
  1066. find_v(m_members.translator(), predicates, out_it);
  1067. detail::rtree::apply_visitor(find_v, *m_members.root);
  1068. return find_v.found_count;
  1069. }
  1070. /*!
  1071. \brief Perform nearest neighbour search.
  1072. \par Exception-safety
  1073. strong
  1074. */
  1075. template <typename Predicates, typename OutIter>
  1076. size_type query_dispatch(Predicates const& predicates, OutIter out_it, boost::mpl::bool_<true> const& /*is_distance_predicate*/) const
  1077. {
  1078. static const unsigned distance_predicate_index = detail::predicates_find_distance<Predicates>::value;
  1079. detail::rtree::visitors::distance_query<
  1080. value_type,
  1081. options_type,
  1082. translator_type,
  1083. box_type,
  1084. allocators_type,
  1085. Predicates,
  1086. distance_predicate_index,
  1087. OutIter
  1088. > distance_v(m_members.parameters(), m_members.translator(), predicates, out_it);
  1089. detail::rtree::apply_visitor(distance_v, *m_members.root);
  1090. return distance_v.finish();
  1091. }
  1092. struct members_holder
  1093. : public translator_type
  1094. , public Parameters
  1095. , public allocators_type
  1096. {
  1097. private:
  1098. members_holder(members_holder const&);
  1099. members_holder & operator=(members_holder const&);
  1100. public:
  1101. template <typename IndGet, typename ValEq, typename Alloc>
  1102. members_holder(IndGet const& ind_get,
  1103. ValEq const& val_eq,
  1104. Parameters const& parameters,
  1105. BOOST_FWD_REF(Alloc) alloc)
  1106. : translator_type(ind_get, val_eq)
  1107. , Parameters(parameters)
  1108. , allocators_type(boost::forward<Alloc>(alloc))
  1109. , values_count(0)
  1110. , leafs_level(0)
  1111. , root(0)
  1112. {}
  1113. template <typename IndGet, typename ValEq>
  1114. members_holder(IndGet const& ind_get,
  1115. ValEq const& val_eq,
  1116. Parameters const& parameters)
  1117. : translator_type(ind_get, val_eq)
  1118. , Parameters(parameters)
  1119. , allocators_type()
  1120. , values_count(0)
  1121. , leafs_level(0)
  1122. , root(0)
  1123. {}
  1124. translator_type const& translator() const { return *this; }
  1125. IndexableGetter const& indexable_getter() const { return *this; }
  1126. IndexableGetter & indexable_getter() { return *this; }
  1127. EqualTo const& equal_to() const { return *this; }
  1128. EqualTo & equal_to() { return *this; }
  1129. Parameters const& parameters() const { return *this; }
  1130. Parameters & parameters() { return *this; }
  1131. allocators_type const& allocators() const { return *this; }
  1132. allocators_type & allocators() { return *this; }
  1133. size_type values_count;
  1134. size_type leafs_level;
  1135. node_pointer root;
  1136. };
  1137. members_holder m_members;
  1138. };
  1139. /*!
  1140. \brief Insert a value to the index.
  1141. It calls <tt>rtree::insert(value_type const&)</tt>.
  1142. \ingroup rtree_functions
  1143. \param tree The spatial index.
  1144. \param v The value which will be stored in the index.
  1145. */
  1146. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
  1147. inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, Value const& v)
  1148. {
  1149. tree.insert(v);
  1150. }
  1151. /*!
  1152. \brief Insert a range of values to the index.
  1153. It calls <tt>rtree::insert(Iterator, Iterator)</tt>.
  1154. \ingroup rtree_functions
  1155. \param tree The spatial index.
  1156. \param first The beginning of the range of values.
  1157. \param last The end of the range of values.
  1158. */
  1159. template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, typename Iterator>
  1160. inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, Iterator first, Iterator last)
  1161. {
  1162. tree.insert(first, last);
  1163. }
  1164. /*!
  1165. \brief Insert a range of values to the index.
  1166. It calls <tt>rtree::insert(Range const&)</tt>.
  1167. \ingroup rtree_functions
  1168. \param tree The spatial index.
  1169. \param rng The range of values.
  1170. */
  1171. template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, typename Range>
  1172. inline void insert(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, Range const& rng)
  1173. {
  1174. tree.insert(rng);
  1175. }
  1176. /*!
  1177. \brief Remove a value from the container.
  1178. Remove a value from the container. In contrast to the \c std::set or <tt>std::map erase()</tt> method
  1179. this function removes only one value from the container.
  1180. It calls <tt>rtree::remove(value_type const&)</tt>.
  1181. \ingroup rtree_functions
  1182. \param tree The spatial index.
  1183. \param v The value which will be removed from the index.
  1184. \return 1 if value was removed, 0 otherwise.
  1185. */
  1186. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
  1187. inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
  1188. remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, Value const& v)
  1189. {
  1190. return tree.remove(v);
  1191. }
  1192. /*!
  1193. \brief Remove a range of values from the container.
  1194. Remove a range of values from the container. In contrast to the \c std::set or <tt>std::map erase()</tt> method
  1195. it doesn't take iterators pointing to values stored in this container. It removes values equal
  1196. to these passed as a range. Furthermore this function removes only one value for each one passed
  1197. in the range, not all equal values.
  1198. It calls <tt>rtree::remove(Iterator, Iterator)</tt>.
  1199. \ingroup rtree_functions
  1200. \param tree The spatial index.
  1201. \param first The beginning of the range of values.
  1202. \param last The end of the range of values.
  1203. \return The number of removed values.
  1204. */
  1205. template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, typename Iterator>
  1206. inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
  1207. remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, Iterator first, Iterator last)
  1208. {
  1209. return tree.remove(first, last);
  1210. }
  1211. /*!
  1212. \brief Remove a range of values from the container.
  1213. Remove a range of values from the container. In contrast to the \c std::set or <tt>std::map erase()</tt> method
  1214. it removes values equal to these passed as a range. Furthermore this method removes only
  1215. one value for each one passed in the range, not all equal values.
  1216. It calls <tt>rtree::remove(Range const&)</tt>.
  1217. \ingroup rtree_functions
  1218. \param tree The spatial index.
  1219. \param rng The range of values.
  1220. \return The number of removed values.
  1221. */
  1222. template<typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator, typename Range>
  1223. inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
  1224. remove(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree, Range const& rng)
  1225. {
  1226. return tree.remove(rng);
  1227. }
  1228. /*!
  1229. \brief Finds values meeting passed predicates e.g. nearest to some Point and/or intersecting some Box.
  1230. This query function performs spatial and k-nearest neighbor searches. It allows to pass a set of predicates.
  1231. Values will be returned only if all predicates are met.
  1232. <b>Spatial predicates</b>
  1233. Spatial predicates may be generated by one of the functions listed below:
  1234. \li \c boost::geometry::index::contains(),
  1235. \li \c boost::geometry::index::covered_by(),
  1236. \li \c boost::geometry::index::covers(),
  1237. \li \c boost::geometry::index::disjoint(),
  1238. \li \c boost::geometry::index::intersects(),
  1239. \li \c boost::geometry::index::overlaps(),
  1240. \li \c boost::geometry::index::within(),
  1241. It is possible to negate spatial predicates:
  1242. \li <tt>! boost::geometry::index::contains()</tt>,
  1243. \li <tt>! boost::geometry::index::covered_by()</tt>,
  1244. \li <tt>! boost::geometry::index::covers()</tt>,
  1245. \li <tt>! boost::geometry::index::disjoint()</tt>,
  1246. \li <tt>! boost::geometry::index::intersects()</tt>,
  1247. \li <tt>! boost::geometry::index::overlaps()</tt>,
  1248. \li <tt>! boost::geometry::index::within()</tt>
  1249. <b>Satisfies predicate</b>
  1250. This is a special kind of predicate which allows to pass a user-defined function or function object which checks
  1251. if Value should be returned by the query. It's generated by:
  1252. \li \c boost::geometry::index::satisfies().
  1253. <b>Nearest predicate</b>
  1254. If the nearest predicate is passed a k-nearest neighbor search will be performed. This query will result
  1255. in returning k values to the output iterator. Only one nearest predicate may be passed to the query.
  1256. It may be generated by:
  1257. \li \c boost::geometry::index::nearest().
  1258. <b>Connecting predicates</b>
  1259. Predicates may be passed together connected with \c operator&&().
  1260. \par Example
  1261. \verbatim
  1262. // return elements intersecting box
  1263. bgi::query(tree, bgi::intersects(box), std::back_inserter(result));
  1264. // return elements intersecting poly but not within box
  1265. bgi::query(tree, bgi::intersects(poly) && !bgi::within(box), std::back_inserter(result));
  1266. // return elements overlapping box and meeting my_fun value predicate
  1267. bgi::query(tree, bgi::overlaps(box) && bgi::satisfies(my_fun), std::back_inserter(result));
  1268. // return 5 elements nearest to pt and elements are intersecting box
  1269. bgi::query(tree, bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result));
  1270. \endverbatim
  1271. \par Throws
  1272. If Value copy constructor or copy assignment throws.
  1273. \warning
  1274. Only one \c nearest() perdicate may be passed to the query. Passing more of them results in compile-time error.
  1275. \ingroup rtree_functions
  1276. \param tree The rtree.
  1277. \param predicates Predicates.
  1278. \param out_it The output iterator, e.g. generated by std::back_inserter().
  1279. \return The number of values found.
  1280. */
  1281. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
  1282. typename Predicates, typename OutIter> inline
  1283. typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::size_type
  1284. query(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree,
  1285. Predicates const& predicates,
  1286. OutIter out_it)
  1287. {
  1288. return tree.query(predicates, out_it);
  1289. }
  1290. /*!
  1291. \brief Returns the query iterator pointing at the begin of the query range.
  1292. This method returns the iterator which may be used to perform iterative queries. For the information
  1293. about the predicates which may be passed to this method see query().
  1294. \par Example
  1295. \verbatim
  1296. for ( Rtree::const_query_iterator it = qbegin(tree, bgi::nearest(pt, 10000)) ;
  1297. it != qend(tree) ; ++it )
  1298. {
  1299. // do something with value
  1300. if ( has_enough_nearest_values() )
  1301. break;
  1302. }
  1303. \endverbatim
  1304. \par Throws
  1305. If predicates copy throws.
  1306. If allocation throws.
  1307. \ingroup rtree_functions
  1308. \param tree The rtree.
  1309. \param predicates Predicates.
  1310. \return The iterator pointing at the begin of the query range.
  1311. */
  1312. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator,
  1313. typename Predicates> inline
  1314. typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_query_iterator
  1315. qbegin(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree,
  1316. Predicates const& predicates)
  1317. {
  1318. return tree.qbegin(predicates);
  1319. }
  1320. /*!
  1321. \brief Returns the query iterator pointing at the end of the query range.
  1322. This method returns the iterator which may be used to check if the query has ended.
  1323. \par Example
  1324. \verbatim
  1325. for ( Rtree::const_query_iterator it = qbegin(tree, bgi::nearest(pt, 10000)) ;
  1326. it != qend(tree) ; ++it )
  1327. {
  1328. // do something with value
  1329. if ( has_enough_nearest_values() )
  1330. break;
  1331. }
  1332. \endverbatim
  1333. \par Throws
  1334. Nothing
  1335. \ingroup rtree_functions
  1336. \return The iterator pointing at the end of the query range.
  1337. */
  1338. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator> inline
  1339. typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::const_query_iterator
  1340. qend(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
  1341. {
  1342. return tree.qend();
  1343. }
  1344. /*!
  1345. \brief Remove all values from the index.
  1346. It calls \c rtree::clear().
  1347. \ingroup rtree_functions
  1348. \param tree The spatial index.
  1349. */
  1350. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
  1351. inline void clear(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & tree)
  1352. {
  1353. return tree.clear();
  1354. }
  1355. /*!
  1356. \brief Get the number of values stored in the index.
  1357. It calls \c rtree::size().
  1358. \ingroup rtree_functions
  1359. \param tree The spatial index.
  1360. \return The number of values stored in the index.
  1361. */
  1362. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
  1363. inline size_t size(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
  1364. {
  1365. return tree.size();
  1366. }
  1367. /*!
  1368. \brief Query if there are no values stored in the index.
  1369. It calls \c rtree::empty().
  1370. \ingroup rtree_functions
  1371. \param tree The spatial index.
  1372. \return true if there are no values in the index.
  1373. */
  1374. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
  1375. inline bool empty(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
  1376. {
  1377. return tree.bounds();
  1378. }
  1379. /*!
  1380. \brief Get the box containing all stored values or an invalid box if the index has no values.
  1381. It calls \c rtree::envelope().
  1382. \ingroup rtree_functions
  1383. \param tree The spatial index.
  1384. \return The box containing all stored values or an invalid box.
  1385. */
  1386. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
  1387. inline typename rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator>::bounds_type
  1388. bounds(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> const& tree)
  1389. {
  1390. return tree.bounds();
  1391. }
  1392. /*!
  1393. \brief Exchanges the contents of the container with those of other.
  1394. It calls \c rtree::swap().
  1395. \ingroup rtree_functions
  1396. \param l The first rtree.
  1397. \param r The second rtree.
  1398. */
  1399. template <typename Value, typename Parameters, typename IndexableGetter, typename EqualTo, typename Allocator>
  1400. inline void swap(rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & l,
  1401. rtree<Value, Parameters, IndexableGetter, EqualTo, Allocator> & r)
  1402. {
  1403. return l.swap(r);
  1404. }
  1405. }}} // namespace boost::geometry::index
  1406. #include <boost/geometry/index/detail/config_end.hpp>
  1407. #endif // BOOST_GEOMETRY_INDEX_RTREE_HPP