rtree.hpp 80 KB

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