rtree.hpp 75 KB

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