rtree.hpp 74 KB

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