rtree.hpp 75 KB

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