query_iterators.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // Boost.Geometry Index
  2. //
  3. // R-tree query iterators
  4. //
  5. // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // This file was modified by Oracle on 2019.
  8. // Modifications copyright (c) 2019 Oracle and/or its affiliates.
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. //
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_QUERY_ITERATORS_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_QUERY_ITERATORS_HPP
  16. #include <boost/scoped_ptr.hpp>
  17. //#define BOOST_GEOMETRY_INDEX_DETAIL_QUERY_ITERATORS_USE_MOVE
  18. namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree { namespace iterators {
  19. template <typename Value, typename Allocators>
  20. struct end_query_iterator
  21. {
  22. typedef std::forward_iterator_tag iterator_category;
  23. typedef Value value_type;
  24. typedef typename Allocators::const_reference reference;
  25. typedef typename Allocators::difference_type difference_type;
  26. typedef typename Allocators::const_pointer pointer;
  27. reference operator*() const
  28. {
  29. BOOST_GEOMETRY_INDEX_ASSERT(false, "iterator not dereferencable");
  30. pointer p(0);
  31. return *p;
  32. }
  33. const value_type * operator->() const
  34. {
  35. BOOST_GEOMETRY_INDEX_ASSERT(false, "iterator not dereferencable");
  36. const value_type * p = 0;
  37. return p;
  38. }
  39. end_query_iterator & operator++()
  40. {
  41. BOOST_GEOMETRY_INDEX_ASSERT(false, "iterator not incrementable");
  42. return *this;
  43. }
  44. end_query_iterator operator++(int)
  45. {
  46. BOOST_GEOMETRY_INDEX_ASSERT(false, "iterator not incrementable");
  47. return *this;
  48. }
  49. friend bool operator==(end_query_iterator const& /*l*/, end_query_iterator const& /*r*/)
  50. {
  51. return true;
  52. }
  53. };
  54. template <typename MembersHolder, typename Predicates>
  55. class spatial_query_iterator
  56. {
  57. typedef typename MembersHolder::parameters_type parameters_type;
  58. typedef typename MembersHolder::translator_type translator_type;
  59. typedef typename MembersHolder::allocators_type allocators_type;
  60. typedef visitors::spatial_query_incremental<MembersHolder, Predicates> visitor_type;
  61. typedef typename visitor_type::node_pointer node_pointer;
  62. public:
  63. typedef std::forward_iterator_tag iterator_category;
  64. typedef typename MembersHolder::value_type value_type;
  65. typedef typename allocators_type::const_reference reference;
  66. typedef typename allocators_type::difference_type difference_type;
  67. typedef typename allocators_type::const_pointer pointer;
  68. inline spatial_query_iterator()
  69. {}
  70. inline spatial_query_iterator(parameters_type const& par, translator_type const& t, Predicates const& p)
  71. : m_visitor(par, t, p)
  72. {}
  73. inline spatial_query_iterator(node_pointer root, parameters_type const& par, translator_type const& t, Predicates const& p)
  74. : m_visitor(par, t, p)
  75. {
  76. m_visitor.initialize(root);
  77. }
  78. reference operator*() const
  79. {
  80. return m_visitor.dereference();
  81. }
  82. const value_type * operator->() const
  83. {
  84. return boost::addressof(m_visitor.dereference());
  85. }
  86. spatial_query_iterator & operator++()
  87. {
  88. m_visitor.increment();
  89. return *this;
  90. }
  91. spatial_query_iterator operator++(int)
  92. {
  93. spatial_query_iterator temp = *this;
  94. this->operator++();
  95. return temp;
  96. }
  97. friend bool operator==(spatial_query_iterator const& l, spatial_query_iterator const& r)
  98. {
  99. return l.m_visitor == r.m_visitor;
  100. }
  101. friend bool operator==(spatial_query_iterator const& l, end_query_iterator<value_type, allocators_type> const& /*r*/)
  102. {
  103. return l.m_visitor.is_end();
  104. }
  105. friend bool operator==(end_query_iterator<value_type, allocators_type> const& /*l*/, spatial_query_iterator const& r)
  106. {
  107. return r.m_visitor.is_end();
  108. }
  109. private:
  110. visitor_type m_visitor;
  111. };
  112. template <typename MembersHolder, typename Predicates, unsigned NearestPredicateIndex>
  113. class distance_query_iterator
  114. {
  115. typedef typename MembersHolder::parameters_type parameters_type;
  116. typedef typename MembersHolder::translator_type translator_type;
  117. typedef typename MembersHolder::allocators_type allocators_type;
  118. typedef visitors::distance_query_incremental<MembersHolder, Predicates, NearestPredicateIndex> visitor_type;
  119. typedef typename visitor_type::node_pointer node_pointer;
  120. public:
  121. typedef std::forward_iterator_tag iterator_category;
  122. typedef typename MembersHolder::value_type value_type;
  123. typedef typename allocators_type::const_reference reference;
  124. typedef typename allocators_type::difference_type difference_type;
  125. typedef typename allocators_type::const_pointer pointer;
  126. inline distance_query_iterator()
  127. {}
  128. inline distance_query_iterator(parameters_type const& par, translator_type const& t, Predicates const& p)
  129. : m_visitor(par, t, p)
  130. {}
  131. inline distance_query_iterator(node_pointer root, parameters_type const& par, translator_type const& t, Predicates const& p)
  132. : m_visitor(par, t, p)
  133. {
  134. m_visitor.initialize(root);
  135. }
  136. reference operator*() const
  137. {
  138. return m_visitor.dereference();
  139. }
  140. const value_type * operator->() const
  141. {
  142. return boost::addressof(m_visitor.dereference());
  143. }
  144. distance_query_iterator & operator++()
  145. {
  146. m_visitor.increment();
  147. return *this;
  148. }
  149. distance_query_iterator operator++(int)
  150. {
  151. distance_query_iterator temp = *this;
  152. this->operator++();
  153. return temp;
  154. }
  155. friend bool operator==(distance_query_iterator const& l, distance_query_iterator const& r)
  156. {
  157. return l.m_visitor == r.m_visitor;
  158. }
  159. friend bool operator==(distance_query_iterator const& l, end_query_iterator<value_type, allocators_type> const& /*r*/)
  160. {
  161. return l.m_visitor.is_end();
  162. }
  163. friend bool operator==(end_query_iterator<value_type, allocators_type> const& /*l*/, distance_query_iterator const& r)
  164. {
  165. return r.m_visitor.is_end();
  166. }
  167. private:
  168. visitor_type m_visitor;
  169. };
  170. template <typename L, typename R>
  171. inline bool operator!=(L const& l, R const& r)
  172. {
  173. return !(l == r);
  174. }
  175. template <typename Value, typename Allocators>
  176. class query_iterator_base
  177. {
  178. public:
  179. typedef std::forward_iterator_tag iterator_category;
  180. typedef Value value_type;
  181. typedef typename Allocators::const_reference reference;
  182. typedef typename Allocators::difference_type difference_type;
  183. typedef typename Allocators::const_pointer pointer;
  184. virtual ~query_iterator_base() {}
  185. virtual query_iterator_base * clone() const = 0;
  186. virtual bool is_end() const = 0;
  187. virtual reference dereference() const = 0;
  188. virtual void increment() = 0;
  189. virtual bool equals(query_iterator_base const&) const = 0;
  190. };
  191. template <typename Value, typename Allocators, typename Iterator>
  192. class query_iterator_wrapper
  193. : public query_iterator_base<Value, Allocators>
  194. {
  195. typedef query_iterator_base<Value, Allocators> base_t;
  196. public:
  197. typedef std::forward_iterator_tag iterator_category;
  198. typedef Value value_type;
  199. typedef typename Allocators::const_reference reference;
  200. typedef typename Allocators::difference_type difference_type;
  201. typedef typename Allocators::const_pointer pointer;
  202. query_iterator_wrapper() : m_iterator() {}
  203. explicit query_iterator_wrapper(Iterator const& it) : m_iterator(it) {}
  204. virtual base_t * clone() const { return new query_iterator_wrapper(m_iterator); }
  205. virtual bool is_end() const { return m_iterator == end_query_iterator<Value, Allocators>(); }
  206. virtual reference dereference() const { return *m_iterator; }
  207. virtual void increment() { ++m_iterator; }
  208. virtual bool equals(base_t const& r) const
  209. {
  210. const query_iterator_wrapper * p = dynamic_cast<const query_iterator_wrapper *>(boost::addressof(r));
  211. BOOST_GEOMETRY_INDEX_ASSERT(p, "iterators can't be compared");
  212. return m_iterator == p->m_iterator;
  213. }
  214. private:
  215. Iterator m_iterator;
  216. };
  217. template <typename Value, typename Allocators>
  218. class query_iterator
  219. {
  220. typedef query_iterator_base<Value, Allocators> iterator_base;
  221. typedef boost::scoped_ptr<iterator_base> iterator_ptr;
  222. public:
  223. typedef std::forward_iterator_tag iterator_category;
  224. typedef Value value_type;
  225. typedef typename Allocators::const_reference reference;
  226. typedef typename Allocators::difference_type difference_type;
  227. typedef typename Allocators::const_pointer pointer;
  228. query_iterator()
  229. {}
  230. template <typename It>
  231. query_iterator(It const& it)
  232. : m_ptr(static_cast<iterator_base*>(
  233. new query_iterator_wrapper<Value, Allocators, It>(it) ))
  234. {}
  235. query_iterator(end_query_iterator<Value, Allocators> const& /*it*/)
  236. {}
  237. query_iterator(query_iterator const& o)
  238. : m_ptr(o.m_ptr.get() ? o.m_ptr->clone() : 0)
  239. {}
  240. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_QUERY_ITERATORS_USE_MOVE
  241. query_iterator & operator=(query_iterator const& o)
  242. {
  243. if ( this != boost::addressof(o) )
  244. {
  245. m_ptr.reset(o.m_ptr.get() ? o.m_ptr->clone() : 0);
  246. }
  247. return *this;
  248. }
  249. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  250. query_iterator(query_iterator && o)
  251. : m_ptr(0)
  252. {
  253. m_ptr.swap(o.m_ptr);
  254. }
  255. query_iterator & operator=(query_iterator && o)
  256. {
  257. if ( this != boost::addressof(o) )
  258. {
  259. m_ptr.swap(o.m_ptr);
  260. o.m_ptr.reset();
  261. }
  262. return *this;
  263. }
  264. #endif
  265. #else // !BOOST_GEOMETRY_INDEX_DETAIL_QUERY_ITERATORS_USE_MOVE
  266. private:
  267. BOOST_COPYABLE_AND_MOVABLE(query_iterator)
  268. public:
  269. query_iterator & operator=(BOOST_COPY_ASSIGN_REF(query_iterator) o)
  270. {
  271. if ( this != boost::addressof(o) )
  272. {
  273. m_ptr.reset(o.m_ptr.get() ? o.m_ptr->clone() : 0);
  274. }
  275. return *this;
  276. }
  277. query_iterator(BOOST_RV_REF(query_iterator) o)
  278. : m_ptr(0)
  279. {
  280. m_ptr.swap(o.m_ptr);
  281. }
  282. query_iterator & operator=(BOOST_RV_REF(query_iterator) o)
  283. {
  284. if ( this != boost::addressof(o) )
  285. {
  286. m_ptr.swap(o.m_ptr);
  287. o.m_ptr.reset();
  288. }
  289. return *this;
  290. }
  291. #endif // BOOST_GEOMETRY_INDEX_DETAIL_QUERY_ITERATORS_USE_MOVE
  292. reference operator*() const
  293. {
  294. return m_ptr->dereference();
  295. }
  296. const value_type * operator->() const
  297. {
  298. return boost::addressof(m_ptr->dereference());
  299. }
  300. query_iterator & operator++()
  301. {
  302. m_ptr->increment();
  303. return *this;
  304. }
  305. query_iterator operator++(int)
  306. {
  307. query_iterator temp = *this;
  308. this->operator++();
  309. return temp;
  310. }
  311. friend bool operator==(query_iterator const& l, query_iterator const& r)
  312. {
  313. if ( l.m_ptr.get() )
  314. {
  315. if ( r.m_ptr.get() )
  316. return l.m_ptr->equals(*r.m_ptr);
  317. else
  318. return l.m_ptr->is_end();
  319. }
  320. else
  321. {
  322. if ( r.m_ptr.get() )
  323. return r.m_ptr->is_end();
  324. else
  325. return true;
  326. }
  327. }
  328. private:
  329. iterator_ptr m_ptr;
  330. };
  331. }}}}}} // namespace boost::geometry::index::detail::rtree::iterators
  332. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_QUERY_ITERATORS_HPP