rtree.hpp 80 KB

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