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