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,
42 typename boost::range_reference<Range const>::type,
43 typename boost::range_difference<Range>::type
47 typedef boost::iterator_facade
49 closing_iterator<Range>,
50 typename boost::range_value<Range>::type const,
51 boost::random_access_traversal_tag,
52 typename boost::range_reference<Range const>::type,
53 typename boost::range_difference<Range>::type
57 typedef typename base_type::reference reference;
58 typedef typename base_type::difference_type difference_type;
60 /// Constructor including the range it is based on
61 explicit inline closing_iterator(Range& range)
63 , m_iterator(boost::begin(range))
64 , m_end(boost::end(range))
65 , m_size(static_cast<difference_type>(boost::size(range)))
69 /// Constructor to indicate the end of a range
70 explicit inline closing_iterator(Range& range, bool)
72 , m_iterator(boost::end(range))
73 , m_end(boost::end(range))
74 , m_size(static_cast<difference_type>(boost::size(range)))
75 , m_index((m_size == 0) ? 0 : m_size + 1)
78 /// Default constructor
79 explicit inline closing_iterator()
86 friend class boost::iterator_core_access;
88 inline reference dereference() const
93 inline difference_type distance_to(closing_iterator<Range> const& other) const
95 return other.m_index - this->m_index;
98 inline bool equal(closing_iterator<Range> const& other) const
100 return this->m_range == other.m_range
101 && this->m_index == other.m_index;
104 inline void increment()
106 if (++m_index < m_size)
116 inline void decrement()
118 if (m_index-- < m_size)
128 inline void advance(difference_type n)
130 if (m_index < m_size && m_index + n < m_size)
142 inline void update_iterator()
144 this->m_iterator = m_index <= m_size
145 ? boost::begin(*m_range) + (m_index % m_size)
146 : boost::end(*m_range)
151 typename boost::range_iterator<Range>::type m_iterator;
152 typename boost::range_iterator<Range>::type m_end;
153 difference_type m_size;
154 difference_type m_index;
158 }} // namespace boost::geometry
161 #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP