1 // Boost next_prior.hpp header file ---------------------------------------//
3 // (C) Copyright Dave Abrahams and Daniel Walker 1999-2003.
4 // Copyright (c) Andrey Semashev 2017
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)
10 // See http://www.boost.org/libs/utility for documentation.
13 // 13 Dec 2003 Added next(x, n) and prior(x, n) (Daniel Walker)
15 #ifndef BOOST_NEXT_PRIOR_HPP_INCLUDED
16 #define BOOST_NEXT_PRIOR_HPP_INCLUDED
19 #include <boost/config.hpp>
20 #include <boost/core/enable_if.hpp>
21 #include <boost/type_traits/has_plus.hpp>
22 #include <boost/type_traits/has_plus_assign.hpp>
23 #include <boost/type_traits/has_minus.hpp>
24 #include <boost/type_traits/has_minus_assign.hpp>
25 #include <boost/iterator/advance.hpp>
26 #include <boost/iterator/reverse_iterator.hpp>
30 // Helper functions for classes like bidirectional iterators not supporting
31 // operator+ and operator-
34 // const std::list<T>::iterator p = get_some_iterator();
35 // const std::list<T>::iterator prev = boost::prior(p);
36 // const std::list<T>::iterator next = boost::next(prev, 2);
38 // Contributed by Dave Abrahams
40 namespace next_prior_detail {
42 // The trait attempts to detect if the T type is an iterator. Class-type iterators are assumed
43 // to have the nested type iterator_category. Strictly speaking, this is not required to be the
44 // case (e.g. a user can specialize iterator_traits for T without defining T::iterator_category).
45 // Still, this is a good heuristic in practice, and we can't do anything better anyway.
46 // Since C++17 we can test for iterator_traits<T>::iterator_category presence instead as it is
47 // required to be only present for iterators.
48 template< typename T, typename Void = void >
49 struct is_iterator_class
51 static BOOST_CONSTEXPR_OR_CONST bool value = false;
54 template< typename T >
55 struct is_iterator_class<
57 typename enable_if_has_type<
58 #if !defined(BOOST_NO_CXX17_ITERATOR_TRAITS)
59 typename std::iterator_traits< T >::iterator_category
61 typename T::iterator_category
66 static BOOST_CONSTEXPR_OR_CONST bool value = true;
69 template< typename T >
71 public is_iterator_class< T >
75 template< typename T >
76 struct is_iterator< T* >
78 static BOOST_CONSTEXPR_OR_CONST bool value = true;
82 template< typename T, typename Distance, bool HasPlus = has_plus< T, Distance >::value >
83 struct next_plus_impl;
85 template< typename T, typename Distance >
86 struct next_plus_impl< T, Distance, true >
88 static T call(T x, Distance n)
94 template< typename T, typename Distance, bool HasPlusAssign = has_plus_assign< T, Distance >::value >
95 struct next_plus_assign_impl :
96 public next_plus_impl< T, Distance >
100 template< typename T, typename Distance >
101 struct next_plus_assign_impl< T, Distance, true >
103 static T call(T x, Distance n)
110 template< typename T, typename Distance, bool IsIterator = is_iterator< T >::value >
111 struct next_advance_impl :
112 public next_plus_assign_impl< T, Distance >
116 template< typename T, typename Distance >
117 struct next_advance_impl< T, Distance, true >
119 static T call(T x, Distance n)
121 boost::iterators::advance(x, n);
127 template< typename T, typename Distance, bool HasMinus = has_minus< T, Distance >::value >
128 struct prior_minus_impl;
130 template< typename T, typename Distance >
131 struct prior_minus_impl< T, Distance, true >
133 static T call(T x, Distance n)
139 template< typename T, typename Distance, bool HasMinusAssign = has_minus_assign< T, Distance >::value >
140 struct prior_minus_assign_impl :
141 public prior_minus_impl< T, Distance >
145 template< typename T, typename Distance >
146 struct prior_minus_assign_impl< T, Distance, true >
148 static T call(T x, Distance n)
155 template< typename T, typename Distance, bool IsIterator = is_iterator< T >::value >
156 struct prior_advance_impl :
157 public prior_minus_assign_impl< T, Distance >
161 template< typename T, typename Distance >
162 struct prior_advance_impl< T, Distance, true >
164 static T call(T x, Distance n)
166 // Avoid negating n to sidestep possible integer overflow
167 boost::iterators::reverse_iterator< T > rx(x);
168 boost::iterators::advance(rx, n);
173 } // namespace next_prior_detail
176 inline T next(T x) { return ++x; }
178 template <class T, class Distance>
179 inline T next(T x, Distance n)
181 return next_prior_detail::next_advance_impl< T, Distance >::call(x, n);
185 inline T prior(T x) { return --x; }
187 template <class T, class Distance>
188 inline T prior(T x, Distance n)
190 return next_prior_detail::prior_advance_impl< T, Distance >::call(x, n);
195 #endif // BOOST_NEXT_PRIOR_HPP_INCLUDED