]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/geometry/iterators/closing_iterator.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / geometry / iterators / closing_iterator.hpp
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 >
43 {
44 typedef typename boost::range_difference<Range>::type difference_type;
45
46 /// Constructor including the range it is based on
47 explicit inline closing_iterator(Range& range)
48 : m_range(&range)
49 , m_iterator(boost::begin(range))
50 , m_end(boost::end(range))
51 , m_size(static_cast<difference_type>(boost::size(range)))
52 , m_index(0)
53 {}
54
55 /// Constructor to indicate the end of a range
56 explicit inline closing_iterator(Range& range, bool)
57 : m_range(&range)
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)
62 {}
63
64 /// Default constructor
65 explicit inline closing_iterator()
66 : m_range(NULL)
67 , m_size(0)
68 , m_index(0)
69 {}
70
71 private:
72 friend class boost::iterator_core_access;
73
74 inline typename boost::range_value<Range>::type const& dereference() const
75 {
76 return *m_iterator;
77 }
78
79 inline difference_type distance_to(closing_iterator<Range> const& other) const
80 {
81 return other.m_index - this->m_index;
82 }
83
84 inline bool equal(closing_iterator<Range> const& other) const
85 {
86 return this->m_range == other.m_range
87 && this->m_index == other.m_index;
88 }
89
90 inline void increment()
91 {
92 if (++m_index < m_size)
93 {
94 ++m_iterator;
95 }
96 else
97 {
98 update_iterator();
99 }
100 }
101
102 inline void decrement()
103 {
104 if (m_index-- < m_size)
105 {
106 --m_iterator;
107 }
108 else
109 {
110 update_iterator();
111 }
112 }
113
114 inline void advance(difference_type n)
115 {
116 if (m_index < m_size && m_index + n < m_size)
117 {
118 m_index += n;
119 m_iterator += n;
120 }
121 else
122 {
123 m_index += n;
124 update_iterator();
125 }
126 }
127
128 inline void update_iterator()
129 {
130 this->m_iterator = m_index <= m_size
131 ? boost::begin(*m_range) + (m_index % m_size)
132 : boost::end(*m_range)
133 ;
134 }
135
136 Range* 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;
141 };
142
143
144 }} // namespace boost::geometry
145
146
147 #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP