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
27namespace 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
39namespace next_prior_detail {
40
41template< typename T, typename Distance, bool HasPlus = has_plus< T, Distance >::value >
42struct next_plus_impl;
43
44template< typename T, typename Distance >
45struct next_plus_impl< T, Distance, true >
46{
47 static T call(T x, Distance n)
48 {
49 return x + n;
50 }
51};
52
53template< typename T, typename Distance, bool HasPlusAssign = has_plus_assign< T, Distance >::value >
54struct next_plus_assign_impl :
55 public next_plus_impl< T, Distance >
56{
57};
58
59template< typename T, typename Distance >
60struct 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
69template< typename T, typename Distance, bool IsIterator = boost::iterators::is_iterator< T >::value >
70struct next_advance_impl :
71 public next_plus_assign_impl< T, Distance >
72{
73};
74
75template< typename T, typename Distance >
76struct 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
86template< typename T, typename Distance, bool HasMinus = has_minus< T, Distance >::value >
87struct prior_minus_impl;
88
89template< typename T, typename Distance >
90struct prior_minus_impl< T, Distance, true >
91{
92 static T call(T x, Distance n)
93 {
94 return x - n;
95 }
96};
97
98template< typename T, typename Distance, bool HasMinusAssign = has_minus_assign< T, Distance >::value >
99struct prior_minus_assign_impl :
100 public prior_minus_impl< T, Distance >
101{
102};
103
104template< typename T, typename Distance >
105struct 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
114template< typename T, typename Distance, bool IsIterator = boost::iterators::is_iterator< T >::value >
115struct prior_advance_impl :
116 public prior_minus_assign_impl< T, Distance >
117{
118};
119
120template< typename T, typename Distance >
121struct 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
134template <class T>
135inline T next(T x) { return ++x; }
136
137template <class T, class Distance>
138inline T next(T x, Distance n)
139{
140 return next_prior_detail::next_advance_impl< T, Distance >::call(x, n);
141}
142
143template <class T>
144inline T prior(T x) { return --x; }
145
146template <class T, class Distance>
147inline 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

source code of boost/libs/iterator/include/boost/next_prior.hpp