Sprout/sprout/iterator/advance.hpp

57 lines
1.8 KiB
C++
Raw Normal View History

2013-10-05 04:35:26 +00:00
/*=============================================================================
Copyright (c) 2011-2019 Bolero MURAKAMI
2013-10-05 04:35:26 +00:00
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)
=============================================================================*/
#ifndef SPROUT_ITERATOR_ADVANCE_HPP
#define SPROUT_ITERATOR_ADVANCE_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/assert.hpp>
#include <sprout/math/greater_equal.hpp>
namespace sprout {
namespace detail {
template<typename InputIterator, typename Distance>
inline SPROUT_CXX14_CONSTEXPR void
advance_impl(InputIterator& it, Distance n, std::input_iterator_tag*) {
SPROUT_ASSERT(sprout::math::greater_equal(n, 0));
for (; 0 < n; --n) {
++it;
}
}
template<typename BidirectionalIterator, typename Distance>
inline SPROUT_CXX14_CONSTEXPR void
advance_impl(BidirectionalIterator& it, Distance n, std::bidirectional_iterator_tag*) {
if (n > 0) {
for (; 0 < n; --n) {
++it;
}
} else {
for (; n < 0; ++n) {
--it;
}
}
}
template<typename RandomAccessIterator, typename Distance>
inline SPROUT_CXX14_CONSTEXPR void
advance_impl(RandomAccessIterator& it, Distance n, std::random_access_iterator_tag*) {
it += n;
}
} // namespace detail
//
// advance
//
2015-07-15 10:42:31 +00:00
template<typename Iterator, typename Distance = typename std::iterator_traits<Iterator>::difference_type>
2013-10-05 04:35:26 +00:00
inline SPROUT_CXX14_CONSTEXPR void
2015-07-15 10:42:31 +00:00
advance(Iterator& it, Distance n = 1) {
2013-10-05 04:35:26 +00:00
typedef typename std::iterator_traits<Iterator>::iterator_category* category;
sprout::detail::advance_impl(it, n, category());
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_ADVANCE_HPP