1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
8 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
15 #define BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
17 #include <boost/range.hpp>
18 #include <boost/iterator/iterator_facade.hpp>
19 #include <boost/iterator/iterator_categories.hpp>
23 namespace boost { namespace geometry
27 \brief Iterator which iterates through a range, but adds first element at end of the range
28 \tparam Range range on which this class is based on
30 \note It's const iterator treating the Range as one containing non-mutable elements.
31 For both "closing_iterator<Range> and "closing_iterator<Range const>
32 const reference is always returned when dereferenced.
33 \note This class is normally used from "closeable_view" if Close==true
35 template <typename Range>
36 struct closing_iterator
37 : public boost::iterator_facade
39 closing_iterator<Range>,
40 typename boost::range_value<Range>::type const,
41 boost::random_access_traversal_tag
44 typedef typename boost::range_difference<Range>::type difference_type;
46 /// Constructor including the range it is based on
47 explicit inline closing_iterator(Range& range)
49 , m_iterator(boost::begin(range))
50 , m_end(boost::end(range))
51 , m_size(static_cast<difference_type>(boost::size(range)))
55 /// Constructor to indicate the end of a range
56 explicit inline closing_iterator(Range& range, bool)
58 , m_iterator(boost::end(range))
59 , m_end(boost::end(range))
60 , m_size(static_cast<difference_type>(boost::size(range)))
61 , m_index((m_size == 0) ? 0 : m_size + 1)
64 /// Default constructor
65 explicit inline closing_iterator()
72 friend class boost::iterator_core_access;
74 inline typename boost::range_value<Range>::type const& dereference() const
79 inline difference_type distance_to(closing_iterator<Range> const& other) const
81 return other.m_index - this->m_index;
84 inline bool equal(closing_iterator<Range> const& other) const
86 return this->m_range == other.m_range
87 && this->m_index == other.m_index;
90 inline void increment()
92 if (++m_index < m_size)
102 inline void decrement()
104 if (m_index-- < m_size)
114 inline void advance(difference_type n)
116 if (m_index < m_size && m_index + n < m_size)
128 inline void update_iterator()
130 this->m_iterator = m_index <= m_size
131 ? boost::begin(*m_range) + (m_index % m_size)
132 : boost::end(*m_range)
137 typename boost::range_iterator<Range>::type m_iterator;
138 typename boost::range_iterator<Range>::type m_end;
139 difference_type m_size;
140 difference_type m_index;
144 }} // namespace boost::geometry
147 #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP