rtree.hpp 81 KB

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