2011-09-01 02:48:32 +00:00
|
|
|
#ifndef SPROUT_DETAIL_ITERATOR_HPP
|
|
|
|
#define SPROUT_DETAIL_ITERATOR_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <sprout/config.hpp>
|
2011-10-01 15:19:13 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
2011-09-01 02:48:32 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
|
|
|
//
|
|
|
|
// distance
|
|
|
|
//
|
|
|
|
template<typename Iterator>
|
|
|
|
SPROUT_CONSTEXPR typename std::iterator_traits<Iterator>::difference_type distance(
|
|
|
|
Iterator first,
|
|
|
|
Iterator last
|
|
|
|
)
|
|
|
|
{
|
2011-10-01 15:19:13 +00:00
|
|
|
return first == last ? 0 : 1 + sprout::detail::distance(sprout::next(first), last);
|
2011-09-01 02:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Iterator>
|
|
|
|
SPROUT_CONSTEXPR typename std::iterator_traits<Iterator>::difference_type bidirectional_distance_impl(
|
|
|
|
Iterator first1,
|
|
|
|
Iterator first2,
|
|
|
|
Iterator last,
|
|
|
|
typename std::iterator_traits<Iterator>::difference_type current = 1
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return first1 == last
|
|
|
|
? current
|
|
|
|
: first2 == last
|
|
|
|
? -current
|
2011-10-01 15:19:13 +00:00
|
|
|
: sprout::detail::bidirectional_distance_impl(sprout::next(first1), sprout::prev(first2), last, current + 1)
|
2011-09-01 02:48:32 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// bidirectional_distance
|
|
|
|
//
|
|
|
|
template<typename Iterator>
|
|
|
|
SPROUT_CONSTEXPR typename std::iterator_traits<Iterator>::difference_type bidirectional_distance(
|
|
|
|
Iterator first,
|
|
|
|
Iterator last
|
|
|
|
)
|
|
|
|
{
|
2011-10-01 15:19:13 +00:00
|
|
|
return first == last ? 0 : sprout::detail::bidirectional_distance_impl(sprout::next(first), sprout::prev(first), last);
|
2011-09-01 02:48:32 +00:00
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_DETAIL_ITERATOR_HPP
|