Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:35:41

0001 // Boost.Geometry (aka GGL, Generic Geometry Library)
0002 
0003 // Copyright (c) 2024 Barend Gehrels, Amsterdam, the Netherlands.
0004 
0005 // Use, modification and distribution is subject to the Boost Software License,
0006 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0007 // http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 #ifndef BOOST_GEOMETRY_VIEWS_ENUMERATE_VIEW_HPP
0010 #define BOOST_GEOMETRY_VIEWS_ENUMERATE_VIEW_HPP
0011 
0012 #include <boost/iterator/iterator_facade.hpp>
0013 #include <boost/iterator/iterator_categories.hpp>
0014 #include <boost/range/begin.hpp>
0015 #include <boost/range/end.hpp>
0016 #include <boost/range/difference_type.hpp>
0017 #include <boost/range/reference.hpp>
0018 #include <boost/range/value_type.hpp>
0019 
0020 #include <boost/geometry/util/type_traits_std.hpp>
0021 
0022 namespace boost { namespace geometry
0023 {
0024 
0025 namespace util
0026 {
0027 
0028 // This view is a range of values, each with an index
0029 // It is used to iterate over a range, and to get the index of the value
0030 // It is used in the enumerate function
0031 // The typename Range can either be const or non-const
0032 template <typename Range>
0033 struct enumerated_view
0034 {
0035     // The return value of the iterator
0036     struct value_with_index
0037     {
0038         using type = util::transcribe_const_t
0039             <
0040                 Range,
0041                 typename boost::range_value<Range>::type
0042             >;
0043 
0044         // Member variable index contains the zero-based index of the value in the range
0045         std::size_t const index;
0046 
0047         // Member variable value contains a const or non-const reference to the value itself
0048         type& value;  
0049     };
0050 
0051 private:
0052     // Iterator implementation, not exposed.
0053     struct enumerating_iterator
0054         : public boost::iterator_facade
0055         <
0056             enumerating_iterator,
0057             value_with_index const,
0058             boost::random_access_traversal_tag,
0059             value_with_index const,
0060             typename boost::range_difference<Range>::type
0061         >
0062     {
0063         using reference = value_with_index;
0064         using difference_type = typename boost::range_difference<Range>::type;
0065 
0066         // Constructor with the range it handles
0067         explicit inline enumerating_iterator(Range& range)
0068             : m_begin(boost::begin(range))
0069             , m_end(boost::end(range))
0070             , m_iterator(boost::begin(range))
0071         {}
0072 
0073         // Constructor to indicate the end of a range
0074         explicit inline enumerating_iterator(Range& range, bool)
0075             : m_begin(boost::begin(range))
0076             , m_end(boost::end(range))
0077             , m_iterator(boost::end(range))
0078         {}
0079 
0080         // There is no default constructor
0081         enumerating_iterator() = delete;
0082 
0083         inline reference dereference() const
0084         {
0085             constexpr difference_type zero = 0;
0086             const std::size_t index = (std::max)(zero, std::distance(m_begin, m_iterator));
0087             const value_with_index result{index, *m_iterator};
0088             return result;
0089         }
0090 
0091         inline difference_type distance_to(enumerating_iterator const& other) const
0092         {
0093             return std::distance(other.m_iterator, m_iterator);
0094         }
0095 
0096         inline bool equal(enumerating_iterator const& other) const
0097         {
0098             return
0099                 m_begin == other.m_begin 
0100                 && m_end == other.m_end 
0101                 && m_iterator == other.m_iterator;
0102         }
0103 
0104         inline void increment()
0105         {
0106             ++m_iterator;
0107         }
0108 
0109         inline void decrement()
0110         {
0111             --m_iterator;
0112         }
0113 
0114         inline void advance(difference_type n)
0115         {
0116             std::advance(m_iterator, n);
0117         }
0118 
0119         const typename boost::range_iterator<Range>::type m_begin;
0120         const typename boost::range_iterator<Range>::type m_end;
0121 
0122         typename boost::range_iterator<Range>::type m_iterator;
0123     };
0124 
0125 public:
0126     using iterator = enumerating_iterator;
0127     using const_iterator = enumerating_iterator;
0128 
0129     explicit inline enumerated_view(Range& range)
0130         : m_begin(range)
0131         , m_end(range, true)
0132     {}
0133 
0134     inline iterator begin() const { return m_begin; }
0135     inline iterator end() const { return m_end; }
0136 
0137 private:
0138     const iterator m_begin;
0139     const iterator m_end;
0140 };
0141 
0142 // Helper function to create the enumerated view, for a const range
0143 template <typename Range>
0144 inline auto enumerate(Range const& range)
0145 {
0146     return util::enumerated_view<Range const>(range);    
0147 }
0148 
0149 // Helper function to create the enumerated view, for a non-const range
0150 template <typename Range>
0151 inline auto enumerate(Range& range)
0152 {
0153     return util::enumerated_view<Range>(range);    
0154 }
0155 
0156 }}} // boost::geometry::util
0157 
0158 
0159 #endif // BOOST_GEOMETRY_VIEWS_ENUMERATE_VIEW_HPP