| 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 | |
| 7 | #ifndef BOOST_ITERATOR_ADVANCE_HPP |
| 8 | #define BOOST_ITERATOR_ADVANCE_HPP |
| 9 | |
| 10 | #include <boost/config.hpp> |
| 11 | #include <boost/detail/workaround.hpp> |
| 12 | #include <boost/iterator/iterator_categories.hpp> |
| 13 | |
| 14 | namespace boost { |
| 15 | namespace iterators { |
| 16 | |
| 17 | namespace detail { |
| 18 | template <typename InputIterator, typename Distance> |
| 19 | inline BOOST_CXX14_CONSTEXPR void |
| 20 | advance_impl( |
| 21 | InputIterator& it |
| 22 | , Distance n |
| 23 | , incrementable_traversal_tag |
| 24 | ) |
| 25 | { |
| 26 | while (n > 0) { |
| 27 | ++it; |
| 28 | --n; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | #if BOOST_WORKAROUND(BOOST_GCC_VERSION, >= 40600) |
| 33 | // type-limits warning issued below when n is an unsigned integral |
| 34 | #pragma GCC diagnostic push |
| 35 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 36 | #endif |
| 37 | |
| 38 | template <typename BidirectionalIterator, typename Distance> |
| 39 | inline BOOST_CXX14_CONSTEXPR void |
| 40 | advance_impl( |
| 41 | BidirectionalIterator& it |
| 42 | , Distance n |
| 43 | , bidirectional_traversal_tag |
| 44 | ) |
| 45 | { |
| 46 | if (n >= 0) { |
| 47 | while (n > 0) { |
| 48 | ++it; |
| 49 | --n; |
| 50 | } |
| 51 | } |
| 52 | else { |
| 53 | while (n < 0) { |
| 54 | --it; |
| 55 | ++n; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #if BOOST_WORKAROUND(BOOST_GCC_VERSION, >= 40600) |
| 61 | #pragma GCC diagnostic pop |
| 62 | #endif |
| 63 | |
| 64 | template <typename RandomAccessIterator, typename Distance> |
| 65 | inline BOOST_CXX14_CONSTEXPR void |
| 66 | advance_impl( |
| 67 | RandomAccessIterator& it |
| 68 | , Distance n |
| 69 | , random_access_traversal_tag |
| 70 | ) |
| 71 | { |
| 72 | it += n; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | namespace advance_adl_barrier { |
| 77 | template <typename InputIterator, typename Distance> |
| 78 | inline BOOST_CXX14_CONSTEXPR void |
| 79 | advance(InputIterator& it, Distance n) |
| 80 | { |
| 81 | detail::advance_impl( |
| 82 | it, n, typename iterator_traversal<InputIterator>::type() |
| 83 | ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | using namespace advance_adl_barrier; |
| 88 | |
| 89 | } // namespace iterators |
| 90 | |
| 91 | using namespace iterators::advance_adl_barrier; |
| 92 | |
| 93 | } // namespace boost |
| 94 | |
| 95 | #endif |
| 96 | |