Chris@16: // Boost.Geometry (aka GGL, Generic Geometry Library) Chris@16: Chris@16: // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. Chris@16: // Copyright (c) 2008-2012 Bruno Lalande, Paris, France. Chris@16: // Copyright (c) 2009-2012 Mateusz Loskot, London, UK. Chris@16: Chris@16: // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library Chris@16: // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands. Chris@16: Chris@16: // Use, modification and distribution is subject to the Boost Software License, Chris@16: // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP Chris@16: #define BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: Chris@16: namespace boost { namespace geometry Chris@16: { Chris@16: Chris@16: /*! Chris@16: \brief Iterator which iterates through a range, but adds first element at end of the range Chris@16: \tparam Range range on which this class is based on Chris@16: \ingroup iterators Chris@101: \note It's const iterator treating the Range as one containing non-mutable elements. Chris@101: For both "closing_iterator and "closing_iterator Chris@101: const reference is always returned when dereferenced. Chris@16: \note This class is normally used from "closeable_view" if Close==true Chris@16: */ Chris@16: template Chris@16: struct closing_iterator Chris@16: : public boost::iterator_facade Chris@16: < Chris@16: closing_iterator, Chris@16: typename boost::range_value::type const, Chris@16: boost::random_access_traversal_tag Chris@16: > Chris@16: { Chris@101: typedef typename boost::range_difference::type difference_type; Chris@101: Chris@16: /// Constructor including the range it is based on Chris@16: explicit inline closing_iterator(Range& range) Chris@16: : m_range(&range) Chris@16: , m_iterator(boost::begin(range)) Chris@16: , m_end(boost::end(range)) Chris@101: , m_size(static_cast(boost::size(range))) Chris@16: , m_index(0) Chris@16: {} Chris@16: Chris@16: /// Constructor to indicate the end of a range Chris@16: explicit inline closing_iterator(Range& range, bool) Chris@16: : m_range(&range) Chris@16: , m_iterator(boost::end(range)) Chris@16: , m_end(boost::end(range)) Chris@101: , m_size(static_cast(boost::size(range))) Chris@101: , m_index((m_size == 0) ? 0 : m_size + 1) Chris@16: {} Chris@16: Chris@16: /// Default constructor Chris@16: explicit inline closing_iterator() Chris@16: : m_range(NULL) Chris@16: , m_size(0) Chris@16: , m_index(0) Chris@16: {} Chris@16: Chris@16: private: Chris@16: friend class boost::iterator_core_access; Chris@16: Chris@16: inline typename boost::range_value::type const& dereference() const Chris@16: { Chris@16: return *m_iterator; Chris@16: } Chris@16: Chris@16: inline difference_type distance_to(closing_iterator const& other) const Chris@16: { Chris@16: return other.m_index - this->m_index; Chris@16: } Chris@16: Chris@16: inline bool equal(closing_iterator const& other) const Chris@16: { Chris@16: return this->m_range == other.m_range Chris@16: && this->m_index == other.m_index; Chris@16: } Chris@16: Chris@16: inline void increment() Chris@16: { Chris@16: if (++m_index < m_size) Chris@16: { Chris@16: ++m_iterator; Chris@16: } Chris@16: else Chris@16: { Chris@16: update_iterator(); Chris@16: } Chris@16: } Chris@16: Chris@16: inline void decrement() Chris@16: { Chris@16: if (m_index-- < m_size) Chris@16: { Chris@16: --m_iterator; Chris@16: } Chris@16: else Chris@16: { Chris@16: update_iterator(); Chris@16: } Chris@16: } Chris@16: Chris@16: inline void advance(difference_type n) Chris@16: { Chris@16: if (m_index < m_size && m_index + n < m_size) Chris@16: { Chris@16: m_index += n; Chris@16: m_iterator += n; Chris@16: } Chris@16: else Chris@16: { Chris@16: m_index += n; Chris@16: update_iterator(); Chris@16: } Chris@16: } Chris@16: Chris@16: inline void update_iterator() Chris@16: { Chris@16: this->m_iterator = m_index <= m_size Chris@16: ? boost::begin(*m_range) + (m_index % m_size) Chris@16: : boost::end(*m_range) Chris@16: ; Chris@16: } Chris@16: Chris@16: Range* m_range; Chris@16: typename boost::range_iterator::type m_iterator; Chris@16: typename boost::range_iterator::type m_end; Chris@16: difference_type m_size; Chris@16: difference_type m_index; Chris@16: }; Chris@16: Chris@16: Chris@16: }} // namespace boost::geometry Chris@16: Chris@16: Chris@16: #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP