rtree.hpp 75 KB

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