annotate DEPENDENCIES/generic/include/boost/range/irange.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // Boost.Range library
Chris@16 2 //
Chris@16 3 // Copyright Neil Groves 2010. Use, modification and
Chris@16 4 // distribution is subject to the Boost Software License, Version
Chris@16 5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7 //
Chris@16 8 //
Chris@16 9 // For more information, see http://www.boost.org/libs/range/
Chris@16 10 //
Chris@16 11 #ifndef BOOST_RANGE_IRANGE_HPP_INCLUDED
Chris@16 12 #define BOOST_RANGE_IRANGE_HPP_INCLUDED
Chris@16 13
Chris@16 14 #include <boost/assert.hpp>
Chris@16 15 #include <boost/iterator/iterator_facade.hpp>
Chris@16 16 #include <boost/range/iterator_range.hpp>
Chris@16 17
Chris@16 18 namespace boost
Chris@16 19 {
Chris@16 20 namespace range_detail
Chris@16 21 {
Chris@16 22 // integer_iterator is an iterator over an integer sequence that
Chris@16 23 // is bounded only by the limits of the underlying integer
Chris@16 24 // representation.
Chris@16 25 //
Chris@16 26 // This is useful for implementing the irange(first, last)
Chris@16 27 // function.
Chris@16 28 //
Chris@16 29 // Note:
Chris@16 30 // This use of this iterator and irange is appreciably less
Chris@16 31 // performant than the corresponding hand-written integer
Chris@16 32 // loop on many compilers.
Chris@16 33 template<typename Integer>
Chris@16 34 class integer_iterator
Chris@16 35 : public boost::iterator_facade<
Chris@16 36 integer_iterator<Integer>,
Chris@16 37 Integer,
Chris@16 38 boost::random_access_traversal_tag,
Chris@16 39 Integer,
Chris@16 40 std::ptrdiff_t
Chris@16 41 >
Chris@16 42 {
Chris@16 43 typedef boost::iterator_facade<
Chris@16 44 integer_iterator<Integer>,
Chris@16 45 Integer,
Chris@16 46 boost::random_access_traversal_tag,
Chris@16 47 Integer,
Chris@16 48 std::ptrdiff_t
Chris@16 49 > base_t;
Chris@16 50 public:
Chris@16 51 typedef typename base_t::value_type value_type;
Chris@16 52 typedef typename base_t::difference_type difference_type;
Chris@16 53 typedef typename base_t::reference reference;
Chris@101 54 typedef std::random_access_iterator_tag iterator_category;
Chris@16 55
Chris@16 56 integer_iterator() : m_value() {}
Chris@16 57 explicit integer_iterator(value_type x) : m_value(x) {}
Chris@16 58
Chris@16 59 private:
Chris@16 60 void increment()
Chris@16 61 {
Chris@16 62 ++m_value;
Chris@16 63 }
Chris@16 64
Chris@16 65 void decrement()
Chris@16 66 {
Chris@16 67 --m_value;
Chris@16 68 }
Chris@16 69
Chris@16 70 void advance(difference_type offset)
Chris@16 71 {
Chris@16 72 m_value += offset;
Chris@16 73 }
Chris@16 74
Chris@16 75 difference_type distance_to(const integer_iterator& other) const
Chris@16 76 {
Chris@101 77 return is_signed<value_type>::value
Chris@101 78 ? (other.m_value - m_value)
Chris@101 79 : (other.m_value >= m_value)
Chris@101 80 ? static_cast<difference_type>(other.m_value - m_value)
Chris@101 81 : -static_cast<difference_type>(m_value - other.m_value);
Chris@16 82 }
Chris@16 83
Chris@16 84 bool equal(const integer_iterator& other) const
Chris@16 85 {
Chris@16 86 return m_value == other.m_value;
Chris@16 87 }
Chris@16 88
Chris@16 89 reference dereference() const
Chris@16 90 {
Chris@16 91 return m_value;
Chris@16 92 }
Chris@16 93
Chris@16 94 friend class ::boost::iterator_core_access;
Chris@16 95 value_type m_value;
Chris@16 96 };
Chris@16 97
Chris@16 98 // integer_iterator_with_step is similar in nature to the
Chris@16 99 // integer_iterator but provides the ability to 'move' in
Chris@16 100 // a number of steps specified at construction time.
Chris@16 101 //
Chris@16 102 // The three variable implementation provides the best guarantees
Chris@16 103 // of loop termination upon various combinations of input.
Chris@16 104 //
Chris@16 105 // While this design is less performant than some less
Chris@16 106 // safe alternatives, the use of ranges and iterators to
Chris@16 107 // perform counting will never be optimal anyhow, hence
Chris@16 108 // if optimal performance is desired a hand-coded loop
Chris@16 109 // is the solution.
Chris@16 110 template<typename Integer>
Chris@16 111 class integer_iterator_with_step
Chris@16 112 : public boost::iterator_facade<
Chris@16 113 integer_iterator_with_step<Integer>,
Chris@16 114 Integer,
Chris@16 115 boost::random_access_traversal_tag,
Chris@16 116 Integer,
Chris@16 117 std::ptrdiff_t
Chris@16 118 >
Chris@16 119 {
Chris@16 120 typedef boost::iterator_facade<
Chris@16 121 integer_iterator_with_step<Integer>,
Chris@16 122 Integer,
Chris@16 123 boost::random_access_traversal_tag,
Chris@16 124 Integer,
Chris@16 125 std::ptrdiff_t
Chris@16 126 > base_t;
Chris@16 127 public:
Chris@16 128 typedef typename base_t::value_type value_type;
Chris@16 129 typedef typename base_t::difference_type difference_type;
Chris@16 130 typedef typename base_t::reference reference;
Chris@101 131 typedef std::random_access_iterator_tag iterator_category;
Chris@16 132
Chris@16 133 integer_iterator_with_step(value_type first, difference_type step, value_type step_size)
Chris@16 134 : m_first(first)
Chris@16 135 , m_step(step)
Chris@16 136 , m_step_size(step_size)
Chris@16 137 {
Chris@16 138 }
Chris@16 139
Chris@16 140 private:
Chris@16 141 void increment()
Chris@16 142 {
Chris@16 143 ++m_step;
Chris@16 144 }
Chris@16 145
Chris@16 146 void decrement()
Chris@16 147 {
Chris@16 148 --m_step;
Chris@16 149 }
Chris@16 150
Chris@16 151 void advance(difference_type offset)
Chris@16 152 {
Chris@16 153 m_step += offset;
Chris@16 154 }
Chris@16 155
Chris@16 156 difference_type distance_to(const integer_iterator_with_step& other) const
Chris@16 157 {
Chris@16 158 return other.m_step - m_step;
Chris@16 159 }
Chris@16 160
Chris@16 161 bool equal(const integer_iterator_with_step& other) const
Chris@16 162 {
Chris@16 163 return m_step == other.m_step;
Chris@16 164 }
Chris@16 165
Chris@16 166 reference dereference() const
Chris@16 167 {
Chris@16 168 return m_first + (m_step * m_step_size);
Chris@16 169 }
Chris@16 170
Chris@16 171 friend class ::boost::iterator_core_access;
Chris@16 172 value_type m_first;
Chris@101 173 difference_type m_step;
Chris@16 174 difference_type m_step_size;
Chris@16 175 };
Chris@16 176
Chris@16 177 } // namespace range_detail
Chris@16 178
Chris@16 179 template<typename Integer>
Chris@16 180 class integer_range
Chris@16 181 : public iterator_range< range_detail::integer_iterator<Integer> >
Chris@16 182 {
Chris@16 183 typedef range_detail::integer_iterator<Integer> iterator_t;
Chris@16 184 typedef iterator_range<iterator_t> base_t;
Chris@16 185 public:
Chris@16 186 integer_range(Integer first, Integer last)
Chris@16 187 : base_t(iterator_t(first), iterator_t(last))
Chris@16 188 {
Chris@16 189 }
Chris@16 190 };
Chris@16 191
Chris@16 192 template<typename Integer>
Chris@16 193 class strided_integer_range
Chris@16 194 : public iterator_range< range_detail::integer_iterator_with_step<Integer> >
Chris@16 195 {
Chris@16 196 typedef range_detail::integer_iterator_with_step<Integer> iterator_t;
Chris@16 197 typedef iterator_range<iterator_t> base_t;
Chris@16 198 public:
Chris@16 199 template<typename Iterator>
Chris@16 200 strided_integer_range(Iterator first, Iterator last)
Chris@16 201 : base_t(first, last)
Chris@16 202 {
Chris@16 203 }
Chris@16 204 };
Chris@16 205
Chris@16 206 template<typename Integer>
Chris@16 207 integer_range<Integer>
Chris@16 208 irange(Integer first, Integer last)
Chris@16 209 {
Chris@16 210 BOOST_ASSERT( first <= last );
Chris@16 211 return integer_range<Integer>(first, last);
Chris@16 212 }
Chris@16 213
Chris@16 214 template<typename Integer, typename StepSize>
Chris@16 215 strided_integer_range<Integer>
Chris@16 216 irange(Integer first, Integer last, StepSize step_size)
Chris@16 217 {
Chris@16 218 BOOST_ASSERT( step_size != 0 );
Chris@16 219 BOOST_ASSERT( (step_size > 0) ? (last >= first) : (last <= first) );
Chris@16 220
Chris@16 221 typedef typename range_detail::integer_iterator_with_step<Integer> iterator_t;
Chris@16 222
Chris@16 223 const std::ptrdiff_t sz = static_cast<std::ptrdiff_t>(step_size >= 0 ? step_size : -step_size);
Chris@16 224 const Integer l = step_size >= 0 ? last : first;
Chris@16 225 const Integer f = step_size >= 0 ? first : last;
Chris@16 226 const std::ptrdiff_t num_steps = (l - f) / sz + ((l - f) % sz ? 1 : 0);
Chris@16 227 BOOST_ASSERT(num_steps >= 0);
Chris@16 228
Chris@16 229 return strided_integer_range<Integer>(
Chris@16 230 iterator_t(first, 0, step_size),
Chris@16 231 iterator_t(first, num_steps, step_size));
Chris@16 232 }
Chris@16 233
Chris@16 234 } // namespace boost
Chris@16 235
Chris@16 236 #endif // include guard