query_iterators.hpp 11 KB

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