2014-12-07 21:40:20 +00:00
|
|
|
// Boost next_prior.hpp header file ---------------------------------------//
|
|
|
|
|
2018-01-01 20:31:13 +00:00
|
|
|
// (C) Copyright Dave Abrahams and Daniel Walker 1999-2003.
|
|
|
|
// Copyright (c) Andrey Semashev 2017
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
2014-12-07 21:40:20 +00:00
|
|
|
|
|
|
|
// See http://www.boost.org/libs/utility for documentation.
|
|
|
|
|
|
|
|
// Revision History
|
|
|
|
// 13 Dec 2003 Added next(x, n) and prior(x, n) (Daniel Walker)
|
|
|
|
|
|
|
|
#ifndef BOOST_NEXT_PRIOR_HPP_INCLUDED
|
|
|
|
#define BOOST_NEXT_PRIOR_HPP_INCLUDED
|
|
|
|
|
2018-01-01 20:31:13 +00:00
|
|
|
#include <boost/config.hpp>
|
2014-12-07 21:40:20 +00:00
|
|
|
#include <boost/type_traits/has_plus.hpp>
|
|
|
|
#include <boost/type_traits/has_plus_assign.hpp>
|
|
|
|
#include <boost/type_traits/has_minus.hpp>
|
|
|
|
#include <boost/type_traits/has_minus_assign.hpp>
|
2023-11-04 19:30:42 +00:00
|
|
|
#include <boost/iterator/is_iterator.hpp>
|
2018-01-01 20:31:13 +00:00
|
|
|
#include <boost/iterator/advance.hpp>
|
|
|
|
#include <boost/iterator/reverse_iterator.hpp>
|
2014-12-07 21:40:20 +00:00
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
|
|
|
|
// Helper functions for classes like bidirectional iterators not supporting
|
|
|
|
// operator+ and operator-
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
// const std::list<T>::iterator p = get_some_iterator();
|
|
|
|
// const std::list<T>::iterator prev = boost::prior(p);
|
|
|
|
// const std::list<T>::iterator next = boost::next(prev, 2);
|
|
|
|
|
|
|
|
// Contributed by Dave Abrahams
|
|
|
|
|
|
|
|
namespace next_prior_detail {
|
|
|
|
|
2018-01-01 20:31:13 +00:00
|
|
|
template< typename T, typename Distance, bool HasPlus = has_plus< T, Distance >::value >
|
|
|
|
struct next_plus_impl;
|
|
|
|
|
2014-12-07 21:40:20 +00:00
|
|
|
template< typename T, typename Distance >
|
2018-01-01 20:31:13 +00:00
|
|
|
struct next_plus_impl< T, Distance, true >
|
2014-12-07 21:40:20 +00:00
|
|
|
{
|
|
|
|
static T call(T x, Distance n)
|
|
|
|
{
|
|
|
|
return x + n;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template< typename T, typename Distance, bool HasPlusAssign = has_plus_assign< T, Distance >::value >
|
2018-01-01 20:31:13 +00:00
|
|
|
struct next_plus_assign_impl :
|
|
|
|
public next_plus_impl< T, Distance >
|
2014-12-07 21:40:20 +00:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template< typename T, typename Distance >
|
2018-01-01 20:31:13 +00:00
|
|
|
struct next_plus_assign_impl< T, Distance, true >
|
2014-12-07 21:40:20 +00:00
|
|
|
{
|
|
|
|
static T call(T x, Distance n)
|
|
|
|
{
|
|
|
|
x += n;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-04 19:30:42 +00:00
|
|
|
template< typename T, typename Distance, bool IsIterator = boost::iterators::is_iterator< T >::value >
|
2018-01-01 20:31:13 +00:00
|
|
|
struct next_advance_impl :
|
|
|
|
public next_plus_assign_impl< T, Distance >
|
2014-12-07 21:40:20 +00:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2018-01-01 20:31:13 +00:00
|
|
|
template< typename T, typename Distance >
|
|
|
|
struct next_advance_impl< T, Distance, true >
|
2014-12-07 21:40:20 +00:00
|
|
|
{
|
|
|
|
static T call(T x, Distance n)
|
|
|
|
{
|
2018-01-01 20:31:13 +00:00
|
|
|
boost::iterators::advance(x, n);
|
2014-12-07 21:40:20 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template< typename T, typename Distance, bool HasMinus = has_minus< T, Distance >::value >
|
2018-01-01 20:31:13 +00:00
|
|
|
struct prior_minus_impl;
|
2014-12-07 21:40:20 +00:00
|
|
|
|
|
|
|
template< typename T, typename Distance >
|
2018-01-01 20:31:13 +00:00
|
|
|
struct prior_minus_impl< T, Distance, true >
|
2014-12-07 21:40:20 +00:00
|
|
|
{
|
|
|
|
static T call(T x, Distance n)
|
|
|
|
{
|
|
|
|
return x - n;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template< typename T, typename Distance, bool HasMinusAssign = has_minus_assign< T, Distance >::value >
|
2018-01-01 20:31:13 +00:00
|
|
|
struct prior_minus_assign_impl :
|
|
|
|
public prior_minus_impl< T, Distance >
|
2014-12-07 21:40:20 +00:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template< typename T, typename Distance >
|
2018-01-01 20:31:13 +00:00
|
|
|
struct prior_minus_assign_impl< T, Distance, true >
|
2014-12-07 21:40:20 +00:00
|
|
|
{
|
|
|
|
static T call(T x, Distance n)
|
|
|
|
{
|
|
|
|
x -= n;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-04 19:30:42 +00:00
|
|
|
template< typename T, typename Distance, bool IsIterator = boost::iterators::is_iterator< T >::value >
|
2018-01-01 20:31:13 +00:00
|
|
|
struct prior_advance_impl :
|
|
|
|
public prior_minus_assign_impl< T, Distance >
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template< typename T, typename Distance >
|
|
|
|
struct prior_advance_impl< T, Distance, true >
|
|
|
|
{
|
|
|
|
static T call(T x, Distance n)
|
|
|
|
{
|
|
|
|
// Avoid negating n to sidestep possible integer overflow
|
|
|
|
boost::iterators::reverse_iterator< T > rx(x);
|
|
|
|
boost::iterators::advance(rx, n);
|
|
|
|
return rx.base();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-07 21:40:20 +00:00
|
|
|
} // namespace next_prior_detail
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline T next(T x) { return ++x; }
|
|
|
|
|
|
|
|
template <class T, class Distance>
|
|
|
|
inline T next(T x, Distance n)
|
|
|
|
{
|
2018-01-01 20:31:13 +00:00
|
|
|
return next_prior_detail::next_advance_impl< T, Distance >::call(x, n);
|
2014-12-07 21:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline T prior(T x) { return --x; }
|
|
|
|
|
|
|
|
template <class T, class Distance>
|
|
|
|
inline T prior(T x, Distance n)
|
|
|
|
{
|
2018-01-01 20:31:13 +00:00
|
|
|
return next_prior_detail::prior_advance_impl< T, Distance >::call(x, n);
|
2014-12-07 21:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace boost
|
|
|
|
|
|
|
|
#endif // BOOST_NEXT_PRIOR_HPP_INCLUDED
|