annotate DEPENDENCIES/generic/include/boost/range/irange.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
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@16 54
Chris@16 55 integer_iterator() : m_value() {}
Chris@16 56 explicit integer_iterator(value_type x) : m_value(x) {}
Chris@16 57
Chris@16 58 private:
Chris@16 59 void increment()
Chris@16 60 {
Chris@16 61 ++m_value;
Chris@16 62 }
Chris@16 63
Chris@16 64 void decrement()
Chris@16 65 {
Chris@16 66 --m_value;
Chris@16 67 }
Chris@16 68
Chris@16 69 void advance(difference_type offset)
Chris@16 70 {
Chris@16 71 m_value += offset;
Chris@16 72 }
Chris@16 73
Chris@16 74 difference_type distance_to(const integer_iterator& other) const
Chris@16 75 {
Chris@16 76 return other.m_value - m_value;
Chris@16 77 }
Chris@16 78
Chris@16 79 bool equal(const integer_iterator& other) const
Chris@16 80 {
Chris@16 81 return m_value == other.m_value;
Chris@16 82 }
Chris@16 83
Chris@16 84 reference dereference() const
Chris@16 85 {
Chris@16 86 return m_value;
Chris@16 87 }
Chris@16 88
Chris@16 89 friend class ::boost::iterator_core_access;
Chris@16 90 value_type m_value;
Chris@16 91 };
Chris@16 92
Chris@16 93 // integer_iterator_with_step is similar in nature to the
Chris@16 94 // integer_iterator but provides the ability to 'move' in
Chris@16 95 // a number of steps specified at construction time.
Chris@16 96 //
Chris@16 97 // The three variable implementation provides the best guarantees
Chris@16 98 // of loop termination upon various combinations of input.
Chris@16 99 //
Chris@16 100 // While this design is less performant than some less
Chris@16 101 // safe alternatives, the use of ranges and iterators to
Chris@16 102 // perform counting will never be optimal anyhow, hence
Chris@16 103 // if optimal performance is desired a hand-coded loop
Chris@16 104 // is the solution.
Chris@16 105 template<typename Integer>
Chris@16 106 class integer_iterator_with_step
Chris@16 107 : public boost::iterator_facade<
Chris@16 108 integer_iterator_with_step<Integer>,
Chris@16 109 Integer,
Chris@16 110 boost::random_access_traversal_tag,
Chris@16 111 Integer,
Chris@16 112 std::ptrdiff_t
Chris@16 113 >
Chris@16 114 {
Chris@16 115 typedef boost::iterator_facade<
Chris@16 116 integer_iterator_with_step<Integer>,
Chris@16 117 Integer,
Chris@16 118 boost::random_access_traversal_tag,
Chris@16 119 Integer,
Chris@16 120 std::ptrdiff_t
Chris@16 121 > base_t;
Chris@16 122 public:
Chris@16 123 typedef typename base_t::value_type value_type;
Chris@16 124 typedef typename base_t::difference_type difference_type;
Chris@16 125 typedef typename base_t::reference reference;
Chris@16 126
Chris@16 127 integer_iterator_with_step(value_type first, difference_type step, value_type step_size)
Chris@16 128 : m_first(first)
Chris@16 129 , m_step(step)
Chris@16 130 , m_step_size(step_size)
Chris@16 131 {
Chris@16 132 }
Chris@16 133
Chris@16 134 private:
Chris@16 135 void increment()
Chris@16 136 {
Chris@16 137 ++m_step;
Chris@16 138 }
Chris@16 139
Chris@16 140 void decrement()
Chris@16 141 {
Chris@16 142 --m_step;
Chris@16 143 }
Chris@16 144
Chris@16 145 void advance(difference_type offset)
Chris@16 146 {
Chris@16 147 m_step += offset;
Chris@16 148 }
Chris@16 149
Chris@16 150 difference_type distance_to(const integer_iterator_with_step& other) const
Chris@16 151 {
Chris@16 152 return other.m_step - m_step;
Chris@16 153 }
Chris@16 154
Chris@16 155 bool equal(const integer_iterator_with_step& other) const
Chris@16 156 {
Chris@16 157 return m_step == other.m_step;
Chris@16 158 }
Chris@16 159
Chris@16 160 reference dereference() const
Chris@16 161 {
Chris@16 162 return m_first + (m_step * m_step_size);
Chris@16 163 }
Chris@16 164
Chris@16 165 friend class ::boost::iterator_core_access;
Chris@16 166 value_type m_first;
Chris@16 167 value_type m_step;
Chris@16 168 difference_type m_step_size;
Chris@16 169 };
Chris@16 170
Chris@16 171 } // namespace range_detail
Chris@16 172
Chris@16 173 template<typename Integer>
Chris@16 174 class integer_range
Chris@16 175 : public iterator_range< range_detail::integer_iterator<Integer> >
Chris@16 176 {
Chris@16 177 typedef range_detail::integer_iterator<Integer> iterator_t;
Chris@16 178 typedef iterator_range<iterator_t> base_t;
Chris@16 179 public:
Chris@16 180 integer_range(Integer first, Integer last)
Chris@16 181 : base_t(iterator_t(first), iterator_t(last))
Chris@16 182 {
Chris@16 183 }
Chris@16 184 };
Chris@16 185
Chris@16 186 template<typename Integer>
Chris@16 187 class strided_integer_range
Chris@16 188 : public iterator_range< range_detail::integer_iterator_with_step<Integer> >
Chris@16 189 {
Chris@16 190 typedef range_detail::integer_iterator_with_step<Integer> iterator_t;
Chris@16 191 typedef iterator_range<iterator_t> base_t;
Chris@16 192 public:
Chris@16 193 template<typename Iterator>
Chris@16 194 strided_integer_range(Iterator first, Iterator last)
Chris@16 195 : base_t(first, last)
Chris@16 196 {
Chris@16 197 }
Chris@16 198 };
Chris@16 199
Chris@16 200 template<typename Integer>
Chris@16 201 integer_range<Integer>
Chris@16 202 irange(Integer first, Integer last)
Chris@16 203 {
Chris@16 204 BOOST_ASSERT( first <= last );
Chris@16 205 return integer_range<Integer>(first, last);
Chris@16 206 }
Chris@16 207
Chris@16 208 template<typename Integer, typename StepSize>
Chris@16 209 strided_integer_range<Integer>
Chris@16 210 irange(Integer first, Integer last, StepSize step_size)
Chris@16 211 {
Chris@16 212 BOOST_ASSERT( step_size != 0 );
Chris@16 213 BOOST_ASSERT( (step_size > 0) ? (last >= first) : (last <= first) );
Chris@16 214
Chris@16 215 typedef typename range_detail::integer_iterator_with_step<Integer> iterator_t;
Chris@16 216
Chris@16 217 const std::ptrdiff_t sz = static_cast<std::ptrdiff_t>(step_size >= 0 ? step_size : -step_size);
Chris@16 218 const Integer l = step_size >= 0 ? last : first;
Chris@16 219 const Integer f = step_size >= 0 ? first : last;
Chris@16 220 const std::ptrdiff_t num_steps = (l - f) / sz + ((l - f) % sz ? 1 : 0);
Chris@16 221 BOOST_ASSERT(num_steps >= 0);
Chris@16 222
Chris@16 223 return strided_integer_range<Integer>(
Chris@16 224 iterator_t(first, 0, step_size),
Chris@16 225 iterator_t(first, num_steps, step_size));
Chris@16 226 }
Chris@16 227
Chris@16 228 } // namespace boost
Chris@16 229
Chris@16 230 #endif // include guard