irange.hpp 7.8 KB

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