counting_iterator.hpp 6.2 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 BOOST_ITERATOR_COUNTING_ITERATOR_DWA200348_HPP
  6. #define BOOST_ITERATOR_COUNTING_ITERATOR_DWA200348_HPP
  7. #include <limits>
  8. #include <type_traits>
  9. #include <boost/config.hpp>
  10. #include <boost/core/use_default.hpp>
  11. #include <boost/detail/numeric_traits.hpp>
  12. #include <boost/iterator/iterator_adaptor.hpp>
  13. #include <boost/iterator/iterator_categories.hpp>
  14. #include <boost/iterator/detail/if_default.hpp>
  15. #include <boost/iterator/detail/eval_if_default.hpp>
  16. #include <boost/iterator/detail/type_traits/type_identity.hpp>
  17. namespace boost {
  18. namespace iterators {
  19. template<
  20. typename Incrementable,
  21. typename CategoryOrTraversal,
  22. typename Difference
  23. >
  24. class counting_iterator;
  25. namespace detail {
  26. // Try to detect numeric types at compile time in ways compatible
  27. // with the limitations of the compiler and library.
  28. template< typename T >
  29. struct is_numeric :
  30. public std::integral_constant< bool, std::numeric_limits< T >::is_specialized >
  31. {};
  32. template<>
  33. struct is_numeric< long long > :
  34. public std::true_type
  35. {};
  36. template<>
  37. struct is_numeric< unsigned long long > :
  38. public std::true_type
  39. {};
  40. #if defined(BOOST_HAS_INT128)
  41. template<>
  42. struct is_numeric< boost::int128_type > :
  43. public std::true_type
  44. {};
  45. template<>
  46. struct is_numeric< boost::uint128_type > :
  47. public std::true_type
  48. {};
  49. #endif
  50. // Some compilers fail to have a numeric_limits specialization
  51. template<>
  52. struct is_numeric< wchar_t > :
  53. public std::true_type
  54. {};
  55. template< typename T >
  56. struct numeric_difference
  57. {
  58. using type = typename boost::detail::numeric_traits< T >::difference_type;
  59. };
  60. #if defined(BOOST_HAS_INT128)
  61. // std::numeric_limits, which is used by numeric_traits, is not specialized for __int128 in some standard libraries
  62. template<>
  63. struct numeric_difference< boost::int128_type >
  64. {
  65. using type = boost::int128_type;
  66. };
  67. template<>
  68. struct numeric_difference< boost::uint128_type >
  69. {
  70. using type = boost::int128_type;
  71. };
  72. #endif
  73. template< typename Incrementable, typename CategoryOrTraversal, typename Difference, bool IsNumeric = is_numeric< Incrementable >::value >
  74. struct counting_iterator_types
  75. {
  76. using traversal = detail::eval_if_default_t<
  77. CategoryOrTraversal,
  78. iterator_traversal< Incrementable >
  79. >;
  80. using difference = detail::eval_if_default_t<
  81. Difference,
  82. iterator_difference< Incrementable >
  83. >;
  84. };
  85. template< typename Incrementable, typename CategoryOrTraversal, typename Difference >
  86. struct counting_iterator_types< Incrementable, CategoryOrTraversal, Difference, true >
  87. {
  88. using traversal = detail::if_default_t<
  89. CategoryOrTraversal,
  90. random_access_traversal_tag
  91. >;
  92. using difference = detail::eval_if_default_t<
  93. Difference,
  94. numeric_difference< Incrementable >
  95. >;
  96. };
  97. template< typename Incrementable, typename CategoryOrTraversal, typename Difference >
  98. struct counting_iterator_base
  99. {
  100. using iterator_types = counting_iterator_types< Incrementable, CategoryOrTraversal, Difference >;
  101. using type = iterator_adaptor<
  102. counting_iterator< Incrementable, CategoryOrTraversal, Difference >, // self
  103. Incrementable, // Base
  104. #ifndef BOOST_ITERATOR_REF_CONSTNESS_KILLS_WRITABILITY
  105. const // MSVC won't strip this. Instead we enable Thomas'
  106. // criterion (see boost/iterator/detail/facade_iterator_category.hpp)
  107. #endif
  108. Incrementable, // Value
  109. typename iterator_types::traversal,
  110. Incrementable const&, // reference
  111. typename iterator_types::difference
  112. >;
  113. };
  114. // A distance calculation policy for wrapped iterators
  115. template< typename Difference, typename Incrementable1, typename Incrementable2 >
  116. struct iterator_distance
  117. {
  118. static Difference distance(Incrementable1 x, Incrementable2 y)
  119. {
  120. return y - x;
  121. }
  122. };
  123. // A distance calculation policy for wrapped numbers
  124. template< typename Difference, typename Incrementable1, typename Incrementable2 >
  125. struct number_distance
  126. {
  127. static Difference distance(Incrementable1 x, Incrementable2 y)
  128. {
  129. return boost::detail::numeric_distance(x, y);
  130. }
  131. };
  132. } // namespace detail
  133. template<
  134. typename Incrementable,
  135. typename CategoryOrTraversal = use_default,
  136. typename Difference = use_default
  137. >
  138. class counting_iterator :
  139. public detail::counting_iterator_base< Incrementable, CategoryOrTraversal, Difference >::type
  140. {
  141. friend class iterator_core_access;
  142. private:
  143. using super_t = typename detail::counting_iterator_base<
  144. Incrementable, CategoryOrTraversal, Difference
  145. >::type;
  146. public:
  147. using reference = typename super_t::reference;
  148. using difference_type = typename super_t::difference_type;
  149. counting_iterator() = default;
  150. counting_iterator(counting_iterator const&) = default;
  151. counting_iterator& operator=(counting_iterator const&) = default;
  152. counting_iterator(Incrementable x) :
  153. super_t(x)
  154. {
  155. }
  156. private:
  157. reference dereference() const
  158. {
  159. return this->base_reference();
  160. }
  161. template< typename OtherIncrementable >
  162. difference_type
  163. distance_to(counting_iterator< OtherIncrementable, CategoryOrTraversal, Difference > const& y) const
  164. {
  165. using distance_traits = typename std::conditional<
  166. detail::is_numeric< Incrementable >::value,
  167. detail::number_distance< difference_type, Incrementable, OtherIncrementable >,
  168. detail::iterator_distance< difference_type, Incrementable, OtherIncrementable >
  169. >::type;
  170. return distance_traits::distance(this->base(), y.base());
  171. }
  172. };
  173. // Manufacture a counting iterator for an arbitrary incrementable type
  174. template< typename Incrementable >
  175. inline counting_iterator< Incrementable > make_counting_iterator(Incrementable x)
  176. {
  177. return counting_iterator< Incrementable >(x);
  178. }
  179. } // namespace iterators
  180. using iterators::counting_iterator;
  181. using iterators::make_counting_iterator;
  182. } // namespace boost
  183. #endif // BOOST_ITERATOR_COUNTING_ITERATOR_DWA200348_HPP