advance.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (C) 2017 Michel Morin.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_ITERATOR_ADVANCE_HPP
  7. #define BOOST_ITERATOR_ADVANCE_HPP
  8. #include <boost/config.hpp>
  9. #include <boost/detail/workaround.hpp>
  10. #include <boost/iterator/iterator_categories.hpp>
  11. namespace boost {
  12. namespace iterators {
  13. namespace detail {
  14. template <typename InputIterator, typename Distance>
  15. inline BOOST_CXX14_CONSTEXPR void
  16. advance_impl(
  17. InputIterator& it
  18. , Distance n
  19. , incrementable_traversal_tag
  20. )
  21. {
  22. while (n > 0) {
  23. ++it;
  24. --n;
  25. }
  26. }
  27. #if BOOST_WORKAROUND(BOOST_GCC_VERSION, >= 40600)
  28. // type-limits warning issued below when n is an unsigned integral
  29. #pragma GCC diagnostic push
  30. #pragma GCC diagnostic ignored "-Wtype-limits"
  31. #endif
  32. template <typename BidirectionalIterator, typename Distance>
  33. inline BOOST_CXX14_CONSTEXPR void
  34. advance_impl(
  35. BidirectionalIterator& it
  36. , Distance n
  37. , bidirectional_traversal_tag
  38. )
  39. {
  40. if (n >= 0) {
  41. while (n > 0) {
  42. ++it;
  43. --n;
  44. }
  45. }
  46. else {
  47. while (n < 0) {
  48. --it;
  49. ++n;
  50. }
  51. }
  52. }
  53. #if BOOST_WORKAROUND(BOOST_GCC_VERSION, >= 40600)
  54. #pragma GCC diagnostic pop
  55. #endif
  56. template <typename RandomAccessIterator, typename Distance>
  57. inline BOOST_CXX14_CONSTEXPR void
  58. advance_impl(
  59. RandomAccessIterator& it
  60. , Distance n
  61. , random_access_traversal_tag
  62. )
  63. {
  64. it += n;
  65. }
  66. }
  67. namespace advance_adl_barrier {
  68. template <typename InputIterator, typename Distance>
  69. inline BOOST_CXX14_CONSTEXPR void
  70. advance(InputIterator& it, Distance n)
  71. {
  72. detail::advance_impl(
  73. it, n, typename iterator_traversal<InputIterator>::type()
  74. );
  75. }
  76. }
  77. using namespace advance_adl_barrier;
  78. } // namespace iterators
  79. using namespace iterators::advance_adl_barrier;
  80. } // namespace boost
  81. #endif