Chris@16
|
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
|
Chris@16
|
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
|
Chris@16
|
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
|
Chris@16
|
6
|
Chris@16
|
7 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
|
Chris@16
|
8 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
|
Chris@16
|
9
|
Chris@16
|
10 // Use, modification and distribution is subject to the Boost Software License,
|
Chris@16
|
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
12 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
13
|
Chris@16
|
14 #ifndef BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
|
Chris@16
|
15 #define BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/range.hpp>
|
Chris@16
|
18 #include <boost/iterator.hpp>
|
Chris@16
|
19 #include <boost/iterator/iterator_facade.hpp>
|
Chris@16
|
20 #include <boost/iterator/iterator_categories.hpp>
|
Chris@16
|
21
|
Chris@16
|
22
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost { namespace geometry
|
Chris@16
|
25 {
|
Chris@16
|
26
|
Chris@16
|
27 /*!
|
Chris@16
|
28 \brief Iterator which iterates through a range, but adds first element at end of the range
|
Chris@16
|
29 \tparam Range range on which this class is based on
|
Chris@16
|
30 \ingroup iterators
|
Chris@101
|
31 \note It's const iterator treating the Range as one containing non-mutable elements.
|
Chris@101
|
32 For both "closing_iterator<Range> and "closing_iterator<Range const>
|
Chris@101
|
33 const reference is always returned when dereferenced.
|
Chris@16
|
34 \note This class is normally used from "closeable_view" if Close==true
|
Chris@16
|
35 */
|
Chris@16
|
36 template <typename Range>
|
Chris@16
|
37 struct closing_iterator
|
Chris@16
|
38 : public boost::iterator_facade
|
Chris@16
|
39 <
|
Chris@16
|
40 closing_iterator<Range>,
|
Chris@16
|
41 typename boost::range_value<Range>::type const,
|
Chris@16
|
42 boost::random_access_traversal_tag
|
Chris@16
|
43 >
|
Chris@16
|
44 {
|
Chris@101
|
45 typedef typename boost::range_difference<Range>::type difference_type;
|
Chris@101
|
46
|
Chris@16
|
47 /// Constructor including the range it is based on
|
Chris@16
|
48 explicit inline closing_iterator(Range& range)
|
Chris@16
|
49 : m_range(&range)
|
Chris@16
|
50 , m_iterator(boost::begin(range))
|
Chris@16
|
51 , m_end(boost::end(range))
|
Chris@101
|
52 , m_size(static_cast<difference_type>(boost::size(range)))
|
Chris@16
|
53 , m_index(0)
|
Chris@16
|
54 {}
|
Chris@16
|
55
|
Chris@16
|
56 /// Constructor to indicate the end of a range
|
Chris@16
|
57 explicit inline closing_iterator(Range& range, bool)
|
Chris@16
|
58 : m_range(&range)
|
Chris@16
|
59 , m_iterator(boost::end(range))
|
Chris@16
|
60 , m_end(boost::end(range))
|
Chris@101
|
61 , m_size(static_cast<difference_type>(boost::size(range)))
|
Chris@101
|
62 , m_index((m_size == 0) ? 0 : m_size + 1)
|
Chris@16
|
63 {}
|
Chris@16
|
64
|
Chris@16
|
65 /// Default constructor
|
Chris@16
|
66 explicit inline closing_iterator()
|
Chris@16
|
67 : m_range(NULL)
|
Chris@16
|
68 , m_size(0)
|
Chris@16
|
69 , m_index(0)
|
Chris@16
|
70 {}
|
Chris@16
|
71
|
Chris@16
|
72 private:
|
Chris@16
|
73 friend class boost::iterator_core_access;
|
Chris@16
|
74
|
Chris@16
|
75 inline typename boost::range_value<Range>::type const& dereference() const
|
Chris@16
|
76 {
|
Chris@16
|
77 return *m_iterator;
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 inline difference_type distance_to(closing_iterator<Range> const& other) const
|
Chris@16
|
81 {
|
Chris@16
|
82 return other.m_index - this->m_index;
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 inline bool equal(closing_iterator<Range> const& other) const
|
Chris@16
|
86 {
|
Chris@16
|
87 return this->m_range == other.m_range
|
Chris@16
|
88 && this->m_index == other.m_index;
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 inline void increment()
|
Chris@16
|
92 {
|
Chris@16
|
93 if (++m_index < m_size)
|
Chris@16
|
94 {
|
Chris@16
|
95 ++m_iterator;
|
Chris@16
|
96 }
|
Chris@16
|
97 else
|
Chris@16
|
98 {
|
Chris@16
|
99 update_iterator();
|
Chris@16
|
100 }
|
Chris@16
|
101 }
|
Chris@16
|
102
|
Chris@16
|
103 inline void decrement()
|
Chris@16
|
104 {
|
Chris@16
|
105 if (m_index-- < m_size)
|
Chris@16
|
106 {
|
Chris@16
|
107 --m_iterator;
|
Chris@16
|
108 }
|
Chris@16
|
109 else
|
Chris@16
|
110 {
|
Chris@16
|
111 update_iterator();
|
Chris@16
|
112 }
|
Chris@16
|
113 }
|
Chris@16
|
114
|
Chris@16
|
115 inline void advance(difference_type n)
|
Chris@16
|
116 {
|
Chris@16
|
117 if (m_index < m_size && m_index + n < m_size)
|
Chris@16
|
118 {
|
Chris@16
|
119 m_index += n;
|
Chris@16
|
120 m_iterator += n;
|
Chris@16
|
121 }
|
Chris@16
|
122 else
|
Chris@16
|
123 {
|
Chris@16
|
124 m_index += n;
|
Chris@16
|
125 update_iterator();
|
Chris@16
|
126 }
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 inline void update_iterator()
|
Chris@16
|
130 {
|
Chris@16
|
131 this->m_iterator = m_index <= m_size
|
Chris@16
|
132 ? boost::begin(*m_range) + (m_index % m_size)
|
Chris@16
|
133 : boost::end(*m_range)
|
Chris@16
|
134 ;
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 Range* m_range;
|
Chris@16
|
138 typename boost::range_iterator<Range>::type m_iterator;
|
Chris@16
|
139 typename boost::range_iterator<Range>::type m_end;
|
Chris@16
|
140 difference_type m_size;
|
Chris@16
|
141 difference_type m_index;
|
Chris@16
|
142 };
|
Chris@16
|
143
|
Chris@16
|
144
|
Chris@16
|
145 }} // namespace boost::geometry
|
Chris@16
|
146
|
Chris@16
|
147
|
Chris@16
|
148 #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP
|