counting_iterator.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright David Abrahams 2003.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef COUNTING_ITERATOR_DWA200348_HPP
  6. # define COUNTING_ITERATOR_DWA200348_HPP
  7. # include <boost/iterator/iterator_adaptor.hpp>
  8. # include <boost/detail/numeric_traits.hpp>
  9. # include <boost/mpl/bool.hpp>
  10. # include <boost/mpl/if.hpp>
  11. # include <boost/mpl/identity.hpp>
  12. # include <boost/mpl/eval_if.hpp>
  13. namespace boost {
  14. namespace iterators {
  15. template <
  16. class Incrementable
  17. , class CategoryOrTraversal
  18. , class Difference
  19. >
  20. class counting_iterator;
  21. namespace detail
  22. {
  23. // Try to detect numeric types at compile time in ways compatible
  24. // with the limitations of the compiler and library.
  25. template <class T>
  26. struct is_numeric_impl
  27. {
  28. // For a while, this wasn't true, but we rely on it below. This is a regression assert.
  29. BOOST_STATIC_ASSERT(::boost::is_integral<char>::value);
  30. # ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  31. BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits<T>::is_specialized);
  32. # else
  33. # if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
  34. BOOST_STATIC_CONSTANT(
  35. bool, value = (
  36. boost::is_convertible<int,T>::value
  37. && boost::is_convertible<T,int>::value
  38. ));
  39. # else
  40. BOOST_STATIC_CONSTANT(bool, value = ::boost::is_arithmetic<T>::value);
  41. # endif
  42. # endif
  43. };
  44. template <class T>
  45. struct is_numeric
  46. : mpl::bool_<(::boost::iterators::detail::is_numeric_impl<T>::value)>
  47. {};
  48. # if defined(BOOST_HAS_LONG_LONG)
  49. template <>
  50. struct is_numeric< ::boost::long_long_type>
  51. : mpl::true_ {};
  52. template <>
  53. struct is_numeric< ::boost::ulong_long_type>
  54. : mpl::true_ {};
  55. # endif
  56. // Some compilers fail to have a numeric_limits specialization
  57. template <>
  58. struct is_numeric<wchar_t>
  59. : mpl::true_ {};
  60. template <class T>
  61. struct numeric_difference
  62. {
  63. typedef typename boost::detail::numeric_traits<T>::difference_type type;
  64. };
  65. BOOST_STATIC_ASSERT(is_numeric<int>::value);
  66. template <class Incrementable, class CategoryOrTraversal, class Difference>
  67. struct counting_iterator_base
  68. {
  69. typedef typename detail::ia_dflt_help<
  70. CategoryOrTraversal
  71. , mpl::eval_if<
  72. is_numeric<Incrementable>
  73. , mpl::identity<random_access_traversal_tag>
  74. , iterator_traversal<Incrementable>
  75. >
  76. >::type traversal;
  77. typedef typename detail::ia_dflt_help<
  78. Difference
  79. , mpl::eval_if<
  80. is_numeric<Incrementable>
  81. , numeric_difference<Incrementable>
  82. , iterator_difference<Incrementable>
  83. >
  84. >::type difference;
  85. typedef iterator_adaptor<
  86. counting_iterator<Incrementable, CategoryOrTraversal, Difference> // self
  87. , Incrementable // Base
  88. , Incrementable // Value
  89. # ifndef BOOST_ITERATOR_REF_CONSTNESS_KILLS_WRITABILITY
  90. const // MSVC won't strip this. Instead we enable Thomas'
  91. // criterion (see boost/iterator/detail/facade_iterator_category.hpp)
  92. # endif
  93. , traversal
  94. , Incrementable const& // reference
  95. , difference
  96. > type;
  97. };
  98. // Template class distance_policy_select -- choose a policy for computing the
  99. // distance between counting_iterators at compile-time based on whether or not
  100. // the iterator wraps an integer or an iterator, using "poor man's partial
  101. // specialization".
  102. template <bool is_integer> struct distance_policy_select;
  103. // A policy for wrapped iterators
  104. template <class Difference, class Incrementable1, class Incrementable2>
  105. struct iterator_distance
  106. {
  107. static Difference distance(Incrementable1 x, Incrementable2 y)
  108. {
  109. return y - x;
  110. }
  111. };
  112. // A policy for wrapped numbers
  113. template <class Difference, class Incrementable1, class Incrementable2>
  114. struct number_distance
  115. {
  116. static Difference distance(Incrementable1 x, Incrementable2 y)
  117. {
  118. return boost::detail::numeric_distance(x, y);
  119. }
  120. };
  121. }
  122. template <
  123. class Incrementable
  124. , class CategoryOrTraversal = use_default
  125. , class Difference = use_default
  126. >
  127. class counting_iterator
  128. : public detail::counting_iterator_base<
  129. Incrementable, CategoryOrTraversal, Difference
  130. >::type
  131. {
  132. typedef typename detail::counting_iterator_base<
  133. Incrementable, CategoryOrTraversal, Difference
  134. >::type super_t;
  135. friend class iterator_core_access;
  136. public:
  137. typedef typename super_t::difference_type difference_type;
  138. counting_iterator() { }
  139. counting_iterator(counting_iterator const& rhs) : super_t(rhs.base()) {}
  140. counting_iterator(Incrementable x)
  141. : super_t(x)
  142. {
  143. }
  144. # if 0
  145. template<class OtherIncrementable>
  146. counting_iterator(
  147. counting_iterator<OtherIncrementable, CategoryOrTraversal, Difference> const& t
  148. , typename enable_if_convertible<OtherIncrementable, Incrementable>::type* = 0
  149. )
  150. : super_t(t.base())
  151. {}
  152. # endif
  153. private:
  154. typename super_t::reference dereference() const
  155. {
  156. return this->base_reference();
  157. }
  158. template <class OtherIncrementable>
  159. difference_type
  160. distance_to(counting_iterator<OtherIncrementable, CategoryOrTraversal, Difference> const& y) const
  161. {
  162. typedef typename mpl::if_<
  163. detail::is_numeric<Incrementable>
  164. , detail::number_distance<difference_type, Incrementable, OtherIncrementable>
  165. , detail::iterator_distance<difference_type, Incrementable, OtherIncrementable>
  166. >::type d;
  167. return d::distance(this->base(), y.base());
  168. }
  169. };
  170. // Manufacture a counting iterator for an arbitrary incrementable type
  171. template <class Incrementable>
  172. inline counting_iterator<Incrementable>
  173. make_counting_iterator(Incrementable x)
  174. {
  175. typedef counting_iterator<Incrementable> result_t;
  176. return result_t(x);
  177. }
  178. } // namespace iterators
  179. using iterators::counting_iterator;
  180. using iterators::make_counting_iterator;
  181. } // namespace boost
  182. #endif // COUNTING_ITERATOR_DWA200348_HPP