counting_iterator.hpp 6.1 KB

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