2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2011-2013 Bolero MURAKAMI
|
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
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)
|
|
|
|
=============================================================================*/
|
2011-09-30 15:04:03 +00:00
|
|
|
#ifndef SPROUT_ITERATOR_NEXT_HPP
|
|
|
|
#define SPROUT_ITERATOR_NEXT_HPP
|
2011-10-26 06:57:31 +00:00
|
|
|
|
2011-09-30 15:04:03 +00:00
|
|
|
#include <iterator>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2012-12-12 08:46:36 +00:00
|
|
|
#include <sprout/iterator/next_fwd.hpp>
|
|
|
|
#include <sprout/iterator/prev_fwd.hpp>
|
2012-09-29 08:10:46 +00:00
|
|
|
#include <sprout/adl/not_found.hpp>
|
2013-03-18 10:12:21 +00:00
|
|
|
#include <sprout/assert.hpp>
|
2012-09-29 08:10:46 +00:00
|
|
|
|
|
|
|
namespace sprout_adl {
|
2012-09-29 14:39:00 +00:00
|
|
|
sprout::not_found_via_adl iterator_next(...);
|
2012-09-29 08:10:46 +00:00
|
|
|
} // namespace sprout_adl
|
2011-09-30 15:04:03 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2012-09-26 07:31:11 +00:00
|
|
|
namespace iterator_detail {
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
InputIterator
|
|
|
|
single_pass_next(InputIterator it, typename std::iterator_traits<InputIterator>::difference_type n = 1) {
|
|
|
|
std::advance(it, n);
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2012-04-04 08:48:02 +00:00
|
|
|
template<typename RandomAccessIterator>
|
2012-07-15 07:04:05 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
2012-09-26 07:31:11 +00:00
|
|
|
std::is_literal_type<RandomAccessIterator>::value,
|
|
|
|
RandomAccessIterator
|
2012-10-05 15:58:56 +00:00
|
|
|
>::type
|
|
|
|
next_impl(RandomAccessIterator const& it, std::random_access_iterator_tag*) {
|
2012-09-26 07:31:11 +00:00
|
|
|
return it + 1;
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2012-04-04 08:48:02 +00:00
|
|
|
template<typename ForwardIterator>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR ForwardIterator
|
2013-02-23 06:21:27 +00:00
|
|
|
next_impl(ForwardIterator const& it, std::forward_iterator_tag*) {
|
2012-09-26 07:31:11 +00:00
|
|
|
return std::next(it);
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
next_impl(InputIterator const& it, std::input_iterator_tag*) {
|
|
|
|
return sprout::iterator_detail::single_pass_next(it);
|
|
|
|
}
|
2011-09-30 15:04:03 +00:00
|
|
|
|
2012-04-04 08:48:02 +00:00
|
|
|
template<typename RandomAccessIterator>
|
2012-07-15 07:04:05 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
2012-09-26 07:31:11 +00:00
|
|
|
std::is_literal_type<RandomAccessIterator>::value,
|
|
|
|
RandomAccessIterator
|
2012-10-05 15:58:56 +00:00
|
|
|
>::type
|
|
|
|
next_impl(
|
|
|
|
RandomAccessIterator const& it, typename std::iterator_traits<RandomAccessIterator>::difference_type n,
|
2011-09-30 15:04:03 +00:00
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
2012-09-26 07:31:11 +00:00
|
|
|
return it + n;
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2012-12-12 08:46:36 +00:00
|
|
|
|
|
|
|
template<typename BidirectionalIterator>
|
|
|
|
inline SPROUT_CONSTEXPR BidirectionalIterator
|
|
|
|
next_impl_2_neg(BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n) {
|
|
|
|
return n == -1 ? sprout::prev(it)
|
|
|
|
: sprout::iterator_detail::next_impl_2_neg(
|
|
|
|
sprout::iterator_detail::next_impl_2_neg(it, n / 2),
|
|
|
|
n - (n / 2)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
next_impl_2(InputIterator const& it, typename std::iterator_traits<InputIterator>::difference_type n) {
|
2012-12-12 08:46:36 +00:00
|
|
|
return n == 1 ? sprout::next(it)
|
|
|
|
: sprout::iterator_detail::next_impl_2(
|
|
|
|
sprout::iterator_detail::next_impl_2(it, n / 2),
|
|
|
|
n - (n / 2)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BidirectionalIterator>
|
|
|
|
inline SPROUT_CONSTEXPR BidirectionalIterator
|
|
|
|
next_impl_1(
|
|
|
|
BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n,
|
|
|
|
std::bidirectional_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return n == 0 ? it
|
|
|
|
: n > 0 ? sprout::iterator_detail::next_impl_2(it, n)
|
|
|
|
: sprout::iterator_detail::next_impl_2_neg(it, n)
|
|
|
|
;
|
|
|
|
}
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
2012-12-12 08:46:36 +00:00
|
|
|
next_impl_1(
|
2013-08-14 14:24:33 +00:00
|
|
|
InputIterator const& it, typename std::iterator_traits<InputIterator>::difference_type n,
|
|
|
|
std::input_iterator_tag*
|
2012-12-12 08:46:36 +00:00
|
|
|
)
|
|
|
|
{
|
2013-03-18 10:12:21 +00:00
|
|
|
return SPROUT_ASSERT(n >= 0),
|
|
|
|
n == 0 ? it
|
|
|
|
: sprout::iterator_detail::next_impl_2(it, n)
|
2012-12-12 08:46:36 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
2012-12-12 08:46:36 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
2013-08-14 14:24:33 +00:00
|
|
|
std::is_literal_type<InputIterator>::value,
|
|
|
|
InputIterator
|
2012-12-12 08:46:36 +00:00
|
|
|
>::type
|
|
|
|
next_impl(
|
2013-08-14 14:24:33 +00:00
|
|
|
InputIterator const& it, typename std::iterator_traits<InputIterator>::difference_type n,
|
|
|
|
std::input_iterator_tag*
|
2012-12-12 08:46:36 +00:00
|
|
|
)
|
|
|
|
{
|
2013-08-14 14:24:33 +00:00
|
|
|
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
|
2012-12-12 08:46:36 +00:00
|
|
|
return sprout::iterator_detail::next_impl_1(it, n, category());
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
!std::is_literal_type<ForwardIterator>::value,
|
|
|
|
ForwardIterator
|
|
|
|
>::type
|
2012-10-05 15:58:56 +00:00
|
|
|
next_impl(
|
|
|
|
ForwardIterator const& it, typename std::iterator_traits<ForwardIterator>::difference_type n,
|
2013-02-23 06:21:27 +00:00
|
|
|
std::forward_iterator_tag*
|
2011-09-30 15:04:03 +00:00
|
|
|
)
|
|
|
|
{
|
2012-09-26 07:31:11 +00:00
|
|
|
return std::next(it, n);
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
!std::is_literal_type<InputIterator>::value,
|
|
|
|
InputIterator
|
|
|
|
>::type
|
|
|
|
next_impl(
|
|
|
|
InputIterator const& it, typename std::iterator_traits<InputIterator>::difference_type n,
|
|
|
|
std::input_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::iterator_detail::single_pass_next(it, n);
|
|
|
|
}
|
2012-09-26 07:31:11 +00:00
|
|
|
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
iterator_next(InputIterator const& it) {
|
|
|
|
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
|
2012-09-26 07:31:11 +00:00
|
|
|
return sprout::iterator_detail::next_impl(it, category());
|
|
|
|
}
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
iterator_next(InputIterator const& it, typename std::iterator_traits<InputIterator>::difference_type n) {
|
|
|
|
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
|
2012-09-26 07:31:11 +00:00
|
|
|
return sprout::iterator_detail::next_impl(it, n, category());
|
|
|
|
}
|
|
|
|
} // namespace iterator_detail
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
namespace sprout_iterator_detail {
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
next(InputIterator const& it) {
|
2012-09-26 07:31:11 +00:00
|
|
|
using sprout::iterator_detail::iterator_next;
|
2012-09-29 08:10:46 +00:00
|
|
|
using sprout_adl::iterator_next;
|
2012-09-26 07:31:11 +00:00
|
|
|
return iterator_next(it);
|
|
|
|
}
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
next(InputIterator const& it, typename std::iterator_traits<InputIterator>::difference_type n) {
|
2012-09-26 07:31:11 +00:00
|
|
|
using sprout::iterator_detail::iterator_next;
|
2012-09-29 08:10:46 +00:00
|
|
|
using sprout_adl::iterator_next;
|
2012-09-26 07:31:11 +00:00
|
|
|
return iterator_next(it, n);
|
|
|
|
}
|
|
|
|
} // namespace sprout_iterator_detail
|
|
|
|
|
|
|
|
namespace sprout {
|
2011-09-30 15:04:03 +00:00
|
|
|
//
|
|
|
|
// next
|
|
|
|
//
|
2012-12-12 08:46:36 +00:00
|
|
|
// effect:
|
|
|
|
// ADL callable iterator_next(it) -> iterator_next(it)
|
|
|
|
// it is RandomAccessIterator && LiteralType -> it + 1
|
2013-08-14 14:24:33 +00:00
|
|
|
// it is ForwardIterator -> std::next(it)
|
|
|
|
// otherwise -> single_pass_next(it)
|
2012-12-12 08:46:36 +00:00
|
|
|
//
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
next(InputIterator const& it) {
|
2012-09-26 07:31:11 +00:00
|
|
|
return sprout_iterator_detail::next(it);
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2012-12-12 08:46:36 +00:00
|
|
|
//
|
|
|
|
// effect:
|
|
|
|
// ADL callable iterator_next(it, n) -> iterator_next(it, n)
|
|
|
|
// it is RandomAccessIterator && LiteralType -> it + n
|
2013-08-14 14:24:33 +00:00
|
|
|
// it is LiteralType && n >= 0 -> sprout::next(it)...
|
|
|
|
// it is LiteralType && n < 0 -> sprout::prev(it)...
|
|
|
|
// it is ForwardIterator -> std::next(it, n)
|
|
|
|
// otherwise -> single_pass_next(it, n)
|
2012-12-12 08:46:36 +00:00
|
|
|
//
|
2013-08-14 14:24:33 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
next(InputIterator const& it, typename std::iterator_traits<InputIterator>::difference_type n) {
|
2012-09-26 07:31:11 +00:00
|
|
|
return sprout_iterator_detail::next(it, n);
|
2011-10-01 15:19:13 +00:00
|
|
|
}
|
2011-09-30 15:04:03 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ITERATOR_NEXT_HPP
|