williamr@2
|
1 |
// Boost next_prior.hpp header file ---------------------------------------//
|
williamr@2
|
2 |
|
williamr@2
|
3 |
// (C) Copyright Dave Abrahams and Daniel Walker 1999-2003. Distributed under the Boost
|
williamr@2
|
4 |
// Software License, Version 1.0. (See accompanying file
|
williamr@2
|
5 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
6 |
|
williamr@2
|
7 |
// See http://www.boost.org/libs/utility for documentation.
|
williamr@2
|
8 |
|
williamr@2
|
9 |
// Revision History
|
williamr@2
|
10 |
// 13 Dec 2003 Added next(x, n) and prior(x, n) (Daniel Walker)
|
williamr@2
|
11 |
|
williamr@2
|
12 |
#ifndef BOOST_NEXT_PRIOR_HPP_INCLUDED
|
williamr@2
|
13 |
#define BOOST_NEXT_PRIOR_HPP_INCLUDED
|
williamr@2
|
14 |
|
williamr@2
|
15 |
#include <iterator>
|
williamr@2
|
16 |
|
williamr@2
|
17 |
namespace boost {
|
williamr@2
|
18 |
|
williamr@2
|
19 |
// Helper functions for classes like bidirectional iterators not supporting
|
williamr@2
|
20 |
// operator+ and operator-
|
williamr@2
|
21 |
//
|
williamr@2
|
22 |
// Usage:
|
williamr@2
|
23 |
// const std::list<T>::iterator p = get_some_iterator();
|
williamr@2
|
24 |
// const std::list<T>::iterator prev = boost::prior(p);
|
williamr@2
|
25 |
// const std::list<T>::iterator next = boost::next(prev, 2);
|
williamr@2
|
26 |
|
williamr@2
|
27 |
// Contributed by Dave Abrahams
|
williamr@2
|
28 |
|
williamr@2
|
29 |
template <class T>
|
williamr@2
|
30 |
inline T next(T x) { return ++x; }
|
williamr@2
|
31 |
|
williamr@2
|
32 |
template <class T, class Distance>
|
williamr@2
|
33 |
inline T next(T x, Distance n)
|
williamr@2
|
34 |
{
|
williamr@2
|
35 |
std::advance(x, n);
|
williamr@2
|
36 |
return x;
|
williamr@2
|
37 |
}
|
williamr@2
|
38 |
|
williamr@2
|
39 |
template <class T>
|
williamr@2
|
40 |
inline T prior(T x) { return --x; }
|
williamr@2
|
41 |
|
williamr@2
|
42 |
template <class T, class Distance>
|
williamr@2
|
43 |
inline T prior(T x, Distance n)
|
williamr@2
|
44 |
{
|
williamr@2
|
45 |
std::advance(x, -n);
|
williamr@2
|
46 |
return x;
|
williamr@2
|
47 |
}
|
williamr@2
|
48 |
|
williamr@2
|
49 |
} // namespace boost
|
williamr@2
|
50 |
|
williamr@2
|
51 |
#endif // BOOST_NEXT_PRIOR_HPP_INCLUDED
|