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.hpp> 19 #include <boost/iterator/iterator_facade.hpp> 20 #include <boost/iterator/iterator_categories.hpp> 21 22 23 24 namespace boost { namespace geometry 25 { 26 27 /*! 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 30 \ingroup iterators 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 35 */ 36 template <typename Range> 37 struct closing_iterator 38 : public boost::iterator_facade 39 < 40 closing_iterator<Range>, 41 typename boost::range_value<Range>::type const, 42 boost::random_access_traversal_tag 43 > 44 { 45 typedef typename boost::range_difference<Range>::type difference_type; 46 47 /// Constructor including the range it is based on closing_iteratorboost::geometry::closing_iterator48 explicit inline closing_iterator(Range& range) 49 : m_range(&range) 50 , m_iterator(boost::begin(range)) 51 , m_end(boost::end(range)) 52 , m_size(static_cast<difference_type>(boost::size(range))) 53 , m_index(0) 54 {} 55 56 /// Constructor to indicate the end of a range closing_iteratorboost::geometry::closing_iterator57 explicit inline closing_iterator(Range& range, bool) 58 : m_range(&range) 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) 63 {} 64 65 /// Default constructor closing_iteratorboost::geometry::closing_iterator66 explicit inline closing_iterator() 67 : m_range(NULL) 68 , m_size(0) 69 , m_index(0) 70 {} 71 72 private: 73 friend class boost::iterator_core_access; 74 dereferenceboost::geometry::closing_iterator75 inline typename boost::range_value<Range>::type const& dereference() const 76 { 77 return *m_iterator; 78 } 79 distance_toboost::geometry::closing_iterator80 inline difference_type distance_to(closing_iterator<Range> const& other) const 81 { 82 return other.m_index - this->m_index; 83 } 84 equalboost::geometry::closing_iterator85 inline bool equal(closing_iterator<Range> const& other) const 86 { 87 return this->m_range == other.m_range 88 && this->m_index == other.m_index; 89 } 90 incrementboost::geometry::closing_iterator91 inline void increment() 92 { 93 if (++m_index < m_size) 94 { 95 ++m_iterator; 96 } 97 else 98 { 99 update_iterator(); 100 } 101 } 102 decrementboost::geometry::closing_iterator103 inline void decrement() 104 { 105 if (m_index-- < m_size) 106 { 107 --m_iterator; 108 } 109 else 110 { 111 update_iterator(); 112 } 113 } 114 advanceboost::geometry::closing_iterator115 inline void advance(difference_type n) 116 { 117 if (m_index < m_size && m_index + n < m_size) 118 { 119 m_index += n; 120 m_iterator += n; 121 } 122 else 123 { 124 m_index += n; 125 update_iterator(); 126 } 127 } 128 update_iteratorboost::geometry::closing_iterator129 inline void update_iterator() 130 { 131 this->m_iterator = m_index <= m_size 132 ? boost::begin(*m_range) + (m_index % m_size) 133 : boost::end(*m_range) 134 ; 135 } 136 137 Range* 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; 142 }; 143 144 145 }} // namespace boost::geometry 146 147 148 #endif // BOOST_GEOMETRY_ITERATORS_CLOSING_ITERATOR_HPP 149