next_prior.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <boost/config.hpp>
  14. #include <boost/type_traits/has_plus.hpp>
  15. #include <boost/type_traits/has_plus_assign.hpp>
  16. #include <boost/type_traits/has_minus.hpp>
  17. #include <boost/type_traits/has_minus_assign.hpp>
  18. #include <boost/iterator/is_iterator.hpp>
  19. #include <boost/iterator/advance.hpp>
  20. #include <boost/iterator/reverse_iterator.hpp>
  21. namespace boost {
  22. // Helper functions for classes like bidirectional iterators not supporting
  23. // operator+ and operator-
  24. //
  25. // Usage:
  26. // const std::list<T>::iterator p = get_some_iterator();
  27. // const std::list<T>::iterator prev = boost::prior(p);
  28. // const std::list<T>::iterator next = boost::next(prev, 2);
  29. // Contributed by Dave Abrahams
  30. namespace next_prior_detail {
  31. template< typename T, typename Distance, bool HasPlus = has_plus< T, Distance >::value >
  32. struct next_plus_impl;
  33. template< typename T, typename Distance >
  34. struct next_plus_impl< T, Distance, true >
  35. {
  36. static T call(T x, Distance n)
  37. {
  38. return x + n;
  39. }
  40. };
  41. template< typename T, typename Distance, bool HasPlusAssign = has_plus_assign< T, Distance >::value >
  42. struct next_plus_assign_impl :
  43. public next_plus_impl< T, Distance >
  44. {
  45. };
  46. template< typename T, typename Distance >
  47. struct next_plus_assign_impl< T, Distance, true >
  48. {
  49. static T call(T x, Distance n)
  50. {
  51. x += n;
  52. return x;
  53. }
  54. };
  55. template< typename T, typename Distance, bool IsIterator = boost::iterators::is_iterator< T >::value >
  56. struct next_advance_impl :
  57. public next_plus_assign_impl< T, Distance >
  58. {
  59. };
  60. template< typename T, typename Distance >
  61. struct next_advance_impl< T, Distance, true >
  62. {
  63. static T call(T x, Distance n)
  64. {
  65. boost::iterators::advance(x, n);
  66. return x;
  67. }
  68. };
  69. template< typename T, typename Distance, bool HasMinus = has_minus< T, Distance >::value >
  70. struct prior_minus_impl;
  71. template< typename T, typename Distance >
  72. struct prior_minus_impl< T, Distance, true >
  73. {
  74. static T call(T x, Distance n)
  75. {
  76. return x - n;
  77. }
  78. };
  79. template< typename T, typename Distance, bool HasMinusAssign = has_minus_assign< T, Distance >::value >
  80. struct prior_minus_assign_impl :
  81. public prior_minus_impl< T, Distance >
  82. {
  83. };
  84. template< typename T, typename Distance >
  85. struct prior_minus_assign_impl< T, Distance, true >
  86. {
  87. static T call(T x, Distance n)
  88. {
  89. x -= n;
  90. return x;
  91. }
  92. };
  93. template< typename T, typename Distance, bool IsIterator = boost::iterators::is_iterator< T >::value >
  94. struct prior_advance_impl :
  95. public prior_minus_assign_impl< T, Distance >
  96. {
  97. };
  98. template< typename T, typename Distance >
  99. struct prior_advance_impl< T, Distance, true >
  100. {
  101. static T call(T x, Distance n)
  102. {
  103. // Avoid negating n to sidestep possible integer overflow
  104. boost::iterators::reverse_iterator< T > rx(x);
  105. boost::iterators::advance(rx, n);
  106. return rx.base();
  107. }
  108. };
  109. } // namespace next_prior_detail
  110. template <class T>
  111. inline T next(T x) { return ++x; }
  112. template <class T, class Distance>
  113. inline T next(T x, Distance n)
  114. {
  115. return next_prior_detail::next_advance_impl< T, Distance >::call(x, n);
  116. }
  117. template <class T>
  118. inline T prior(T x) { return --x; }
  119. template <class T, class Distance>
  120. inline T prior(T x, Distance n)
  121. {
  122. return next_prior_detail::prior_advance_impl< T, Distance >::call(x, n);
  123. }
  124. } // namespace boost
  125. #endif // BOOST_NEXT_PRIOR_HPP_INCLUDED