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.hpp>
19 #include <boost/iterator/iterator_facade.hpp>
20 #include <boost/iterator/iterator_categories.hpp>
24 namespace boost { namespace geometry
28 \brief Iterator which iterates through a range, but adds first element at end of the range
29 \tparam Range range on which this class is based on
31 \note It's const iterator treating the Range as one containing non-mutable elements.
32 For both "closing_iterator<Range> and "closing_iterator<Range const>
33 const reference is always returned when dereferenced.
34 \note This class is normally used from "closeable_view" if Close==true
36 template <typename Range>
37 struct closing_iterator
38 : public boost::iterator_facade
40 closing_iterator<Range>,
41 typename boost::range_value<Range>::type const,
42 boost::random_access_traversal_tag
45 typedef typename boost::range_difference<Range>::type difference_type;
47 /// Constructor including the range it is based on
48 explicit inline closing_iterator(Range& range)
50 , m_iterator(boost::begin(range))
51 , m_end(boost::end(range))
52 , m_size(static_cast<difference_type>(boost::size(range)))
56 /// Constructor to indicate the end of a range
57 explicit inline closing_iterator(Range& range, bool)
59 , m_iterator(boost::end(range))
60 , m_end(boost::end(range))
61 , m_size(static_cast<difference_type>(boost::size(range)))
62 , m_index((m_size == 0) ? 0 : m_size + 1)
65 /// Default constructor
66 explicit inline closing_iterator()
73 friend class boost::iterator_core_access;
75 inline typename boost::range_value<Range>::type const& dereference() const
80 inline difference_type distance_to(closing_iterator<Range> const& other) const
82 return other.m_index - this->m_index;
85 inline bool equal(closing_iterator<Range> const& other) const
87 return this->m_range == other.m_range
88 && this->m_index == other.m_index;
91 inline void increment()
93 if (++m_index < m_size)
103 inline void decrement()
105 if (m_index-- < m_size)
115 inline void advance(difference_type n)
117 if (m_index < m_size && m_index + n < m_size)
129 inline void update_iterator()
131 this->m_iterator = m_index <= m_size
132 ? boost::begin(*m_range) + (m_index % m_size)
133 : boost::end(*m_range)
138 typename boost::range_iterator<Range>::type m_iterator;
139 typename boost::range_iterator<Range>::type m_end;
140 difference_type m_size;
141 difference_type m_index;
145 }} // namespace boost::geometry
148 #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP