123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- // Boost.Geometry Index
- //
- // R-tree query iterators
- //
- // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
- //
- // Use, modification and distribution is subject to the Boost Software License,
- // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_QUERY_ITERATORS_HPP
- #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_QUERY_ITERATORS_HPP
- #include <boost/scoped_ptr.hpp>
- //#define BOOST_GEOMETRY_INDEX_DETAIL_QUERY_ITERATORS_USE_MOVE
- namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree { namespace iterators {
- template <typename Value, typename Allocators>
- struct end_query_iterator
- {
- typedef std::forward_iterator_tag iterator_category;
- typedef Value value_type;
- typedef typename Allocators::const_reference reference;
- typedef typename Allocators::difference_type difference_type;
- typedef typename Allocators::const_pointer pointer;
- reference operator*() const
- {
- BOOST_GEOMETRY_INDEX_ASSERT(false, "iterator not dereferencable");
- pointer p(0);
- return *p;
- }
- const value_type * operator->() const
- {
- BOOST_GEOMETRY_INDEX_ASSERT(false, "iterator not dereferencable");
- const value_type * p = 0;
- return p;
- }
- end_query_iterator & operator++()
- {
- BOOST_GEOMETRY_INDEX_ASSERT(false, "iterator not incrementable");
- return *this;
- }
- end_query_iterator operator++(int)
- {
- BOOST_GEOMETRY_INDEX_ASSERT(false, "iterator not incrementable");
- return *this;
- }
- friend bool operator==(end_query_iterator const& /*l*/, end_query_iterator const& /*r*/)
- {
- return true;
- }
- };
- template <typename Value, typename Options, typename Translator, typename Box, typename Allocators, typename Predicates>
- class spatial_query_iterator
- {
- typedef typename Options::parameters_type parameters_type;
- typedef visitors::spatial_query_incremental<Value, Options, Translator, Box, Allocators, Predicates> visitor_type;
- typedef typename visitor_type::node_pointer node_pointer;
- public:
- typedef std::forward_iterator_tag iterator_category;
- typedef Value value_type;
- typedef typename Allocators::const_reference reference;
- typedef typename Allocators::difference_type difference_type;
- typedef typename Allocators::const_pointer pointer;
- inline spatial_query_iterator()
- {}
- inline spatial_query_iterator(parameters_type const& par, Translator const& t, Predicates const& p)
- : m_visitor(par, t, p)
- {}
- inline spatial_query_iterator(node_pointer root, parameters_type const& par, Translator const& t, Predicates const& p)
- : m_visitor(par, t, p)
- {
- m_visitor.initialize(root);
- }
- reference operator*() const
- {
- return m_visitor.dereference();
- }
- const value_type * operator->() const
- {
- return boost::addressof(m_visitor.dereference());
- }
- spatial_query_iterator & operator++()
- {
- m_visitor.increment();
- return *this;
- }
- spatial_query_iterator operator++(int)
- {
- spatial_query_iterator temp = *this;
- this->operator++();
- return temp;
- }
- friend bool operator==(spatial_query_iterator const& l, spatial_query_iterator const& r)
- {
- return l.m_visitor == r.m_visitor;
- }
- friend bool operator==(spatial_query_iterator const& l, end_query_iterator<Value, Allocators> const& /*r*/)
- {
- return l.m_visitor.is_end();
- }
- friend bool operator==(end_query_iterator<Value, Allocators> const& /*l*/, spatial_query_iterator const& r)
- {
- return r.m_visitor.is_end();
- }
-
- private:
- visitor_type m_visitor;
- };
- template <typename Value, typename Options, typename Translator, typename Box, typename Allocators, typename Predicates, unsigned NearestPredicateIndex>
- class distance_query_iterator
- {
- typedef typename Options::parameters_type parameters_type;
- typedef visitors::distance_query_incremental<Value, Options, Translator, Box, Allocators, Predicates, NearestPredicateIndex> visitor_type;
- typedef typename visitor_type::node_pointer node_pointer;
- public:
- typedef std::forward_iterator_tag iterator_category;
- typedef Value value_type;
- typedef typename Allocators::const_reference reference;
- typedef typename Allocators::difference_type difference_type;
- typedef typename Allocators::const_pointer pointer;
- inline distance_query_iterator()
- {}
- inline distance_query_iterator(parameters_type const& par, Translator const& t, Predicates const& p)
- : m_visitor(par, t, p)
- {}
- inline distance_query_iterator(node_pointer root, parameters_type const& par, Translator const& t, Predicates const& p)
- : m_visitor(par, t, p)
- {
- m_visitor.initialize(root);
- }
- reference operator*() const
- {
- return m_visitor.dereference();
- }
- const value_type * operator->() const
- {
- return boost::addressof(m_visitor.dereference());
- }
- distance_query_iterator & operator++()
- {
- m_visitor.increment();
- return *this;
- }
- distance_query_iterator operator++(int)
- {
- distance_query_iterator temp = *this;
- this->operator++();
- return temp;
- }
- friend bool operator==(distance_query_iterator const& l, distance_query_iterator const& r)
- {
- return l.m_visitor == r.m_visitor;
- }
- friend bool operator==(distance_query_iterator const& l, end_query_iterator<Value, Allocators> const& /*r*/)
- {
- return l.m_visitor.is_end();
- }
- friend bool operator==(end_query_iterator<Value, Allocators> const& /*l*/, distance_query_iterator const& r)
- {
- return r.m_visitor.is_end();
- }
- private:
- visitor_type m_visitor;
- };
- template <typename L, typename R>
- inline bool operator!=(L const& l, R const& r)
- {
- return !(l == r);
- }
- template <typename Value, typename Allocators>
- class query_iterator_base
- {
- public:
- typedef std::forward_iterator_tag iterator_category;
- typedef Value value_type;
- typedef typename Allocators::const_reference reference;
- typedef typename Allocators::difference_type difference_type;
- typedef typename Allocators::const_pointer pointer;
- virtual ~query_iterator_base() {}
- virtual query_iterator_base * clone() const = 0;
-
- virtual bool is_end() const = 0;
- virtual reference dereference() const = 0;
- virtual void increment() = 0;
- virtual bool equals(query_iterator_base const&) const = 0;
- };
- template <typename Value, typename Allocators, typename Iterator>
- class query_iterator_wrapper
- : public query_iterator_base<Value, Allocators>
- {
- typedef query_iterator_base<Value, Allocators> base_t;
- public:
- typedef std::forward_iterator_tag iterator_category;
- typedef Value value_type;
- typedef typename Allocators::const_reference reference;
- typedef typename Allocators::difference_type difference_type;
- typedef typename Allocators::const_pointer pointer;
- query_iterator_wrapper() : m_iterator() {}
- explicit query_iterator_wrapper(Iterator const& it) : m_iterator(it) {}
- virtual base_t * clone() const { return new query_iterator_wrapper(m_iterator); }
- virtual bool is_end() const { return m_iterator == end_query_iterator<Value, Allocators>(); }
- virtual reference dereference() const { return *m_iterator; }
- virtual void increment() { ++m_iterator; }
- virtual bool equals(base_t const& r) const
- {
- const query_iterator_wrapper * p = dynamic_cast<const query_iterator_wrapper *>(boost::addressof(r));
- BOOST_GEOMETRY_INDEX_ASSERT(p, "iterators can't be compared");
- return m_iterator == p->m_iterator;
- }
- private:
- Iterator m_iterator;
- };
- template <typename Value, typename Allocators>
- class query_iterator
- {
- typedef query_iterator_base<Value, Allocators> iterator_base;
- typedef boost::scoped_ptr<iterator_base> iterator_ptr;
- public:
- typedef std::forward_iterator_tag iterator_category;
- typedef Value value_type;
- typedef typename Allocators::const_reference reference;
- typedef typename Allocators::difference_type difference_type;
- typedef typename Allocators::const_pointer pointer;
- query_iterator()
- {}
- template <typename It>
- query_iterator(It const& it)
- : m_ptr(static_cast<iterator_base*>(
- new query_iterator_wrapper<Value, Allocators, It>(it) ))
- {}
- query_iterator(end_query_iterator<Value, Allocators> const& /*it*/)
- {}
- query_iterator(query_iterator const& o)
- : m_ptr(o.m_ptr.get() ? o.m_ptr->clone() : 0)
- {}
- #ifndef BOOST_GEOMETRY_INDEX_DETAIL_QUERY_ITERATORS_USE_MOVE
- query_iterator & operator=(query_iterator const& o)
- {
- if ( this != boost::addressof(o) )
- {
- m_ptr.reset(o.m_ptr.get() ? o.m_ptr->clone() : 0);
- }
- return *this;
- }
- #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
- query_iterator(query_iterator && o)
- : m_ptr(0)
- {
- m_ptr.swap(o.m_ptr);
- }
- query_iterator & operator=(query_iterator && o)
- {
- if ( this != boost::addressof(o) )
- {
- m_ptr.swap(o.m_ptr);
- o.m_ptr.reset();
- }
- return *this;
- }
- #endif
- #else // !BOOST_GEOMETRY_INDEX_DETAIL_QUERY_ITERATORS_USE_MOVE
- private:
- BOOST_COPYABLE_AND_MOVABLE(query_iterator)
- public:
- query_iterator & operator=(BOOST_COPY_ASSIGN_REF(query_iterator) o)
- {
- if ( this != boost::addressof(o) )
- {
- m_ptr.reset(o.m_ptr.get() ? o.m_ptr->clone() : 0);
- }
- return *this;
- }
- query_iterator(BOOST_RV_REF(query_iterator) o)
- : m_ptr(0)
- {
- m_ptr.swap(o.m_ptr);
- }
- query_iterator & operator=(BOOST_RV_REF(query_iterator) o)
- {
- if ( this != boost::addressof(o) )
- {
- m_ptr.swap(o.m_ptr);
- o.m_ptr.reset();
- }
- return *this;
- }
- #endif // BOOST_GEOMETRY_INDEX_DETAIL_QUERY_ITERATORS_USE_MOVE
- reference operator*() const
- {
- return m_ptr->dereference();
- }
- const value_type * operator->() const
- {
- return boost::addressof(m_ptr->dereference());
- }
- query_iterator & operator++()
- {
- m_ptr->increment();
- return *this;
- }
- query_iterator operator++(int)
- {
- query_iterator temp = *this;
- this->operator++();
- return temp;
- }
- friend bool operator==(query_iterator const& l, query_iterator const& r)
- {
- if ( l.m_ptr.get() )
- {
- if ( r.m_ptr.get() )
- return l.m_ptr->equals(*r.m_ptr);
- else
- return l.m_ptr->is_end();
- }
- else
- {
- if ( r.m_ptr.get() )
- return r.m_ptr->is_end();
- else
- return true;
- }
- }
- private:
- iterator_ptr m_ptr;
- };
- }}}}}} // namespace boost::geometry::index::detail::rtree::iterators
- #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_QUERY_ITERATORS_HPP
|