• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
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.
6 
7 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
8 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
9 
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)
13 
14 #ifndef BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
15 #define BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
16 
17 #include <boost/range.hpp>
18 #include <boost/iterator/iterator_facade.hpp>
19 #include <boost/iterator/iterator_categories.hpp>
20 
21 
22 
23 namespace boost { namespace geometry
24 {
25 
26 /*!
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
29 \ingroup iterators
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
34 */
35 template <typename Range>
36 struct closing_iterator
37     : public boost::iterator_facade
38     <
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
44     >
45 {
46 private:
47     typedef boost::iterator_facade
48         <
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
54         > base_type;
55 
56 public:
57     typedef typename base_type::reference reference;
58     typedef typename base_type::difference_type difference_type;
59 
60     /// Constructor including the range it is based on
closing_iteratorboost::geometry::closing_iterator61     explicit inline closing_iterator(Range& range)
62         : m_range(&range)
63         , m_iterator(boost::begin(range))
64         , m_end(boost::end(range))
65         , m_size(static_cast<difference_type>(boost::size(range)))
66         , m_index(0)
67     {}
68 
69     /// Constructor to indicate the end of a range
closing_iteratorboost::geometry::closing_iterator70     explicit inline closing_iterator(Range& range, bool)
71         : m_range(&range)
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)
76     {}
77 
78     /// Default constructor
closing_iteratorboost::geometry::closing_iterator79     explicit inline closing_iterator()
80         : m_range(NULL)
81         , m_size(0)
82         , m_index(0)
83     {}
84 
85 private:
86     friend class boost::iterator_core_access;
87 
dereferenceboost::geometry::closing_iterator88     inline reference dereference() const
89     {
90         return *m_iterator;
91     }
92 
distance_toboost::geometry::closing_iterator93     inline difference_type distance_to(closing_iterator<Range> const& other) const
94     {
95         return other.m_index - this->m_index;
96     }
97 
equalboost::geometry::closing_iterator98     inline bool equal(closing_iterator<Range> const& other) const
99     {
100         return this->m_range == other.m_range
101             && this->m_index == other.m_index;
102     }
103 
incrementboost::geometry::closing_iterator104     inline void increment()
105     {
106         if (++m_index < m_size)
107         {
108             ++m_iterator;
109         }
110         else
111         {
112             update_iterator();
113         }
114     }
115 
decrementboost::geometry::closing_iterator116     inline void decrement()
117     {
118         if (m_index-- < m_size)
119         {
120             --m_iterator;
121         }
122         else
123         {
124             update_iterator();
125         }
126     }
127 
advanceboost::geometry::closing_iterator128     inline void advance(difference_type n)
129     {
130         if (m_index < m_size && m_index + n < m_size)
131         {
132             m_index += n;
133             m_iterator += n;
134         }
135         else
136         {
137             m_index += n;
138             update_iterator();
139         }
140     }
141 
update_iteratorboost::geometry::closing_iterator142     inline void update_iterator()
143     {
144         this->m_iterator = m_index <= m_size
145             ? boost::begin(*m_range) + (m_index % m_size)
146             : boost::end(*m_range)
147             ;
148     }
149 
150     Range* 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;
155 };
156 
157 
158 }} // namespace boost::geometry
159 
160 
161 #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
162