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 <iterator>
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>
27
28namespace boost {
29
30// Helper functions for classes like bidirectional iterators not supporting
31// operator+ and operator-
32//
33// Usage:
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);
37
38// Contributed by Dave Abrahams
39
40namespace next_prior_detail {
41
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.
48template< typename T, typename Void = void >
49struct is_iterator
50{
51 static BOOST_CONSTEXPR_OR_CONST bool value = false;
52};
53
54template< typename T >
55struct is_iterator< T, typename enable_if_has_type< typename T::iterator_category >::type >
56{
57 static BOOST_CONSTEXPR_OR_CONST bool value = true;
58};
59
60template< typename T >
61struct is_iterator< T*, void >
62{
63 static BOOST_CONSTEXPR_OR_CONST bool value = true;
64};
65
66
67template< typename T, typename Distance, bool HasPlus = has_plus< T, Distance >::value >
68struct next_plus_impl;
69
70template< typename T, typename Distance >
71struct next_plus_impl< T, Distance, true >
72{
73 static T call(T x, Distance n)
74 {
75 return x + n;
76 }
77};
78
79template< typename T, typename Distance, bool HasPlusAssign = has_plus_assign< T, Distance >::value >
80struct next_plus_assign_impl :
81 public next_plus_impl< T, Distance >
82{
83};
84
85template< typename T, typename Distance >
86struct next_plus_assign_impl< T, Distance, true >
87{
88 static T call(T x, Distance n)
89 {
90 x += n;
91 return x;
92 }
93};
94
95template< typename T, typename Distance, bool IsIterator = is_iterator< T >::value >
96struct next_advance_impl :
97 public next_plus_assign_impl< T, Distance >
98{
99};
100
101template< typename T, typename Distance >
102struct next_advance_impl< T, Distance, true >
103{
104 static T call(T x, Distance n)
105 {
106 boost::iterators::advance(x, n);
107 return x;
108 }
109};
110
111
112template< typename T, typename Distance, bool HasMinus = has_minus< T, Distance >::value >
113struct prior_minus_impl;
114
115template< typename T, typename Distance >
116struct prior_minus_impl< T, Distance, true >
117{
118 static T call(T x, Distance n)
119 {
120 return x - n;
121 }
122};
123
124template< typename T, typename Distance, bool HasMinusAssign = has_minus_assign< T, Distance >::value >
125struct prior_minus_assign_impl :
126 public prior_minus_impl< T, Distance >
127{
128};
129
130template< typename T, typename Distance >
131struct prior_minus_assign_impl< T, Distance, true >
132{
133 static T call(T x, Distance n)
134 {
135 x -= n;
136 return x;
137 }
138};
139
140template< typename T, typename Distance, bool IsIterator = is_iterator< T >::value >
141struct prior_advance_impl :
142 public prior_minus_assign_impl< T, Distance >
143{
144};
145
146template< typename T, typename Distance >
147struct prior_advance_impl< T, Distance, true >
148{
149 static T call(T x, Distance n)
150 {
151 // Avoid negating n to sidestep possible integer overflow
152 boost::iterators::reverse_iterator< T > rx(x);
153 boost::iterators::advance(rx, n);
154 return rx.base();
155 }
156};
157
158} // namespace next_prior_detail
159
160template <class T>
161inline T next(T x) { return ++x; }
162
163template <class T, class Distance>
164inline T next(T x, Distance n)
165{
166 return next_prior_detail::next_advance_impl< T, Distance >::call(x, n);
167}
168
169template <class T>
170inline T prior(T x) { return --x; }
171
172template <class T, class Distance>
173inline T prior(T x, Distance n)
174{
175 return next_prior_detail::prior_advance_impl< T, Distance >::call(x, n);
176}
177
178} // namespace boost
179
180#endif // BOOST_NEXT_PRIOR_HPP_INCLUDED
181