next_prior.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Boost next_prior.hpp header file ---------------------------------------//
  2. // (C) Copyright Dave Abrahams and Daniel Walker 1999-2003.
  3. // Copyright (c) Andrey Semashev 2017
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // See http://www.boost.org/libs/utility for documentation.
  9. // Revision History
  10. // 13 Dec 2003 Added next(x, n) and prior(x, n) (Daniel Walker)
  11. #ifndef BOOST_NEXT_PRIOR_HPP_INCLUDED
  12. #define BOOST_NEXT_PRIOR_HPP_INCLUDED
  13. #include <iterator>
  14. #include <boost/config.hpp>
  15. #include <boost/core/enable_if.hpp>
  16. #include <boost/type_traits/has_plus.hpp>
  17. #include <boost/type_traits/has_plus_assign.hpp>
  18. #include <boost/type_traits/has_minus.hpp>
  19. #include <boost/type_traits/has_minus_assign.hpp>
  20. #include <boost/iterator/advance.hpp>
  21. #include <boost/iterator/reverse_iterator.hpp>
  22. namespace boost {
  23. // Helper functions for classes like bidirectional iterators not supporting
  24. // operator+ and operator-
  25. //
  26. // Usage:
  27. // const std::list<T>::iterator p = get_some_iterator();
  28. // const std::list<T>::iterator prev = boost::prior(p);
  29. // const std::list<T>::iterator next = boost::next(prev, 2);
  30. // Contributed by Dave Abrahams
  31. namespace next_prior_detail {
  32. // The trait attempts to detect if the T type is an iterator. Class-type iterators are assumed
  33. // to have the nested type iterator_category. Strictly speaking, this is not required to be the
  34. // case (e.g. a user can specialize iterator_traits for T without defining T::iterator_category).
  35. // Still, this is a good heuristic in practice, and we can't do anything better anyway.
  36. // Since C++17 we can test for iterator_traits<T>::iterator_category presence instead as it is
  37. // required to be only present for iterators.
  38. template< typename T, typename Void = void >
  39. struct is_iterator_class
  40. {
  41. static BOOST_CONSTEXPR_OR_CONST bool value = false;
  42. };
  43. template< typename T >
  44. struct is_iterator_class<
  45. T,
  46. typename enable_if_has_type<
  47. #if !defined(BOOST_NO_CXX17_ITERATOR_TRAITS)
  48. typename std::iterator_traits< T >::iterator_category
  49. #else
  50. typename T::iterator_category
  51. #endif
  52. >::type
  53. >
  54. {
  55. static BOOST_CONSTEXPR_OR_CONST bool value = true;
  56. };
  57. template< typename T >
  58. struct is_iterator :
  59. public is_iterator_class< T >
  60. {
  61. };
  62. template< typename T >
  63. struct is_iterator< T* >
  64. {
  65. static BOOST_CONSTEXPR_OR_CONST bool value = true;
  66. };
  67. template< typename T, typename Distance, bool HasPlus = has_plus< T, Distance >::value >
  68. struct next_plus_impl;
  69. template< typename T, typename Distance >
  70. struct next_plus_impl< T, Distance, true >
  71. {
  72. static T call(T x, Distance n)
  73. {
  74. return x + n;
  75. }
  76. };
  77. template< typename T, typename Distance, bool HasPlusAssign = has_plus_assign< T, Distance >::value >
  78. struct next_plus_assign_impl :
  79. public next_plus_impl< T, Distance >
  80. {
  81. };
  82. template< typename T, typename Distance >
  83. struct next_plus_assign_impl< T, Distance, true >
  84. {
  85. static T call(T x, Distance n)
  86. {
  87. x += n;
  88. return x;
  89. }
  90. };
  91. template< typename T, typename Distance, bool IsIterator = is_iterator< T >::value >
  92. struct next_advance_impl :
  93. public next_plus_assign_impl< T, Distance >
  94. {
  95. };
  96. template< typename T, typename Distance >
  97. struct next_advance_impl< T, Distance, true >
  98. {
  99. static T call(T x, Distance n)
  100. {
  101. boost::iterators::advance(x, n);
  102. return x;
  103. }
  104. };
  105. template< typename T, typename Distance, bool HasMinus = has_minus< T, Distance >::value >
  106. struct prior_minus_impl;
  107. template< typename T, typename Distance >
  108. struct prior_minus_impl< T, Distance, true >
  109. {
  110. static T call(T x, Distance n)
  111. {
  112. return x - n;
  113. }
  114. };
  115. template< typename T, typename Distance, bool HasMinusAssign = has_minus_assign< T, Distance >::value >
  116. struct prior_minus_assign_impl :
  117. public prior_minus_impl< T, Distance >
  118. {
  119. };
  120. template< typename T, typename Distance >
  121. struct prior_minus_assign_impl< T, Distance, true >
  122. {
  123. static T call(T x, Distance n)
  124. {
  125. x -= n;
  126. return x;
  127. }
  128. };
  129. template< typename T, typename Distance, bool IsIterator = is_iterator< T >::value >
  130. struct prior_advance_impl :
  131. public prior_minus_assign_impl< T, Distance >
  132. {
  133. };
  134. template< typename T, typename Distance >
  135. struct prior_advance_impl< T, Distance, true >
  136. {
  137. static T call(T x, Distance n)
  138. {
  139. // Avoid negating n to sidestep possible integer overflow
  140. boost::iterators::reverse_iterator< T > rx(x);
  141. boost::iterators::advance(rx, n);
  142. return rx.base();
  143. }
  144. };
  145. } // namespace next_prior_detail
  146. template <class T>
  147. inline T next(T x) { return ++x; }
  148. template <class T, class Distance>
  149. inline T next(T x, Distance n)
  150. {
  151. return next_prior_detail::next_advance_impl< T, Distance >::call(x, n);
  152. }
  153. template <class T>
  154. inline T prior(T x) { return --x; }
  155. template <class T, class Distance>
  156. inline T prior(T x, Distance n)
  157. {
  158. return next_prior_detail::prior_advance_impl< T, Distance >::call(x, n);
  159. }
  160. } // namespace boost
  161. #endif // BOOST_NEXT_PRIOR_HPP_INCLUDED