irange.hpp 7.2 KB

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