summaryrefslogtreecommitdiff
path: root/boost/next_prior.hpp
diff options
context:
space:
mode:
authorDongHun Kwak <dh0128.kwak@samsung.com>2017-09-13 11:24:46 +0900
committerDongHun Kwak <dh0128.kwak@samsung.com>2017-09-13 11:25:39 +0900
commit4fadd968fa12130524c8380f33fcfe25d4de79e5 (patch)
treefd26a490cd15388d42fc6652b3c5c13012e7f93e /boost/next_prior.hpp
parentb5c87084afaef42b2d058f68091be31988a6a874 (diff)
downloadboost-upstream/1.65.0.tar.gz
boost-upstream/1.65.0.tar.bz2
boost-upstream/1.65.0.zip
Imported Upstream version 1.65.0upstream/1.65.0
Change-Id: Icf8400b375482cb11bcf77440a6934ba360d6ba4 Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
Diffstat (limited to 'boost/next_prior.hpp')
-rw-r--r--boost/next_prior.hpp125
1 files changed, 70 insertions, 55 deletions
diff --git a/boost/next_prior.hpp b/boost/next_prior.hpp
index 7854ec436a..bad46ed654 100644
--- a/boost/next_prior.hpp
+++ b/boost/next_prior.hpp
@@ -1,8 +1,11 @@
// Boost next_prior.hpp header file ---------------------------------------//
-// (C) Copyright Dave Abrahams and Daniel Walker 1999-2003. 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)
+// (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)
// See http://www.boost.org/libs/utility for documentation.
@@ -13,17 +16,14 @@
#define BOOST_NEXT_PRIOR_HPP_INCLUDED
#include <iterator>
-#if defined(_MSC_VER) && _MSC_VER <= 1310
-#include <boost/mpl/and.hpp>
-#include <boost/type_traits/is_integral.hpp>
-#endif
-#include <boost/type_traits/is_unsigned.hpp>
-#include <boost/type_traits/integral_promotion.hpp>
-#include <boost/type_traits/make_signed.hpp>
+#include <boost/config.hpp>
+#include <boost/core/enable_if.hpp>
#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>
+#include <boost/iterator/advance.hpp>
+#include <boost/iterator/reverse_iterator.hpp>
namespace boost {
@@ -39,18 +39,36 @@ namespace boost {
namespace next_prior_detail {
-template< typename T, typename Distance, bool HasPlus = has_plus< T, Distance >::value >
-struct next_impl2
+// The trait attempts to detect if the T type is an iterator. Class-type iterators are assumed
+// to have the nested type iterator_category. Strictly speaking, this is not required to be the
+// case (e.g. a user can specialize iterator_traits for T without defining T::iterator_category).
+// Still, this is a good heuristic in practice, and we can't do anything better anyway.
+// Since C++17 we can test for iterator_traits<T>::iterator_category presence instead as it is
+// required to be only present for iterators.
+template< typename T, typename Void = void >
+struct is_iterator
{
- static T call(T x, Distance n)
- {
- std::advance(x, n);
- return x;
- }
+ static BOOST_CONSTEXPR_OR_CONST bool value = false;
+};
+
+template< typename T >
+struct is_iterator< T, typename enable_if_has_type< typename T::iterator_category >::type >
+{
+ static BOOST_CONSTEXPR_OR_CONST bool value = true;
};
+template< typename T >
+struct is_iterator< T*, void >
+{
+ static BOOST_CONSTEXPR_OR_CONST bool value = true;
+};
+
+
+template< typename T, typename Distance, bool HasPlus = has_plus< T, Distance >::value >
+struct next_plus_impl;
+
template< typename T, typename Distance >
-struct next_impl2< T, Distance, true >
+struct next_plus_impl< T, Distance, true >
{
static T call(T x, Distance n)
{
@@ -58,15 +76,14 @@ struct next_impl2< T, Distance, true >
}
};
-
template< typename T, typename Distance, bool HasPlusAssign = has_plus_assign< T, Distance >::value >
-struct next_impl1 :
- public next_impl2< T, Distance >
+struct next_plus_assign_impl :
+ public next_plus_impl< T, Distance >
{
};
template< typename T, typename Distance >
-struct next_impl1< T, Distance, true >
+struct next_plus_assign_impl< T, Distance, true >
{
static T call(T x, Distance n)
{
@@ -75,47 +92,28 @@ struct next_impl1< T, Distance, true >
}
};
-
-template<
- typename T,
- typename Distance,
- typename PromotedDistance = typename integral_promotion< Distance >::type,
-#if !defined(_MSC_VER) || _MSC_VER > 1310
- bool IsUInt = is_unsigned< PromotedDistance >::value
-#else
- // MSVC 7.1 has problems with applying is_unsigned to non-integral types
- bool IsUInt = mpl::and_< is_integral< PromotedDistance >, is_unsigned< PromotedDistance > >::value
-#endif
->
-struct prior_impl3
+template< typename T, typename Distance, bool IsIterator = is_iterator< T >::value >
+struct next_advance_impl :
+ public next_plus_assign_impl< T, Distance >
{
- static T call(T x, Distance n)
- {
- std::advance(x, -n);
- return x;
- }
};
-template< typename T, typename Distance, typename PromotedDistance >
-struct prior_impl3< T, Distance, PromotedDistance, true >
+template< typename T, typename Distance >
+struct next_advance_impl< T, Distance, true >
{
static T call(T x, Distance n)
{
- typedef typename make_signed< PromotedDistance >::type signed_distance;
- std::advance(x, -static_cast< signed_distance >(static_cast< PromotedDistance >(n)));
+ boost::iterators::advance(x, n);
return x;
}
};
template< typename T, typename Distance, bool HasMinus = has_minus< T, Distance >::value >
-struct prior_impl2 :
- public prior_impl3< T, Distance >
-{
-};
+struct prior_minus_impl;
template< typename T, typename Distance >
-struct prior_impl2< T, Distance, true >
+struct prior_minus_impl< T, Distance, true >
{
static T call(T x, Distance n)
{
@@ -123,15 +121,14 @@ struct prior_impl2< T, Distance, true >
}
};
-
template< typename T, typename Distance, bool HasMinusAssign = has_minus_assign< T, Distance >::value >
-struct prior_impl1 :
- public prior_impl2< T, Distance >
+struct prior_minus_assign_impl :
+ public prior_minus_impl< T, Distance >
{
};
template< typename T, typename Distance >
-struct prior_impl1< T, Distance, true >
+struct prior_minus_assign_impl< T, Distance, true >
{
static T call(T x, Distance n)
{
@@ -140,6 +137,24 @@ struct prior_impl1< T, Distance, true >
}
};
+template< typename T, typename Distance, bool IsIterator = is_iterator< T >::value >
+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();
+ }
+};
+
} // namespace next_prior_detail
template <class T>
@@ -148,7 +163,7 @@ inline T next(T x) { return ++x; }
template <class T, class Distance>
inline T next(T x, Distance n)
{
- return next_prior_detail::next_impl1< T, Distance >::call(x, n);
+ return next_prior_detail::next_advance_impl< T, Distance >::call(x, n);
}
template <class T>
@@ -157,7 +172,7 @@ inline T prior(T x) { return --x; }
template <class T, class Distance>
inline T prior(T x, Distance n)
{
- return next_prior_detail::prior_impl1< T, Distance >::call(x, n);
+ return next_prior_detail::prior_advance_impl< T, Distance >::call(x, n);
}
} // namespace boost