Sprout/sprout/iterator/distance.hpp

75 lines
2.7 KiB
C++
Raw Normal View History

2012-04-01 13:15:09 +00:00
#ifndef SPROUT_ITERATOR_DISTANCE_HPP
#define SPROUT_ITERATOR_DISTANCE_HPP
#include <iterator>
#include <type_traits>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/adl/not_found.hpp>
namespace sprout_adl {
2012-09-29 14:39:00 +00:00
sprout::not_found_via_adl iterator_distance(...);
} // namespace sprout_adl
2012-04-01 13:15:09 +00:00
namespace sprout {
namespace iterator_detail {
template<typename RandomAccessIterator>
inline SPROUT_CONSTEXPR typename std::enable_if<
!std::is_pointer<RandomAccessIterator>::value,
typename std::iterator_traits<RandomAccessIterator>::difference_type
>::type
iterator_distance(RandomAccessIterator first, RandomAccessIterator last, std::random_access_iterator_tag*) {
return last - first;
}
2012-04-01 13:15:09 +00:00
// Copyright (C) 2011 RiSK (sscrisk)
2012-04-04 08:48:02 +00:00
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
iterator_distance_impl(InputIterator first, InputIterator last) {
2012-04-01 13:15:09 +00:00
return first == last ? 0
: 1 + sprout::iterator_detail::iterator_distance_impl(sprout::next(first), last)
2012-04-01 13:15:09 +00:00
;
}
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
iterator_distance(InputIterator first, InputIterator last, void*) {
return sprout::iterator_detail::iterator_distance_impl(first, last);
}
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
iterator_distance(InputIterator first, InputIterator last) {
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
return sprout::iterator_detail::iterator_distance(first, last, category());
}
} // namespace iterator_detail
} // namespace sprout
namespace sprout_iterator_detail {
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last) {
using sprout::iterator_detail::iterator_distance;
using sprout_adl::iterator_distance;
return iterator_distance(first, last);
}
} // namespace sprout_iterator_detail
namespace sprout {
2012-04-01 13:15:09 +00:00
//
// distance
//
// effect:
// ADL callable iterator_distance(first, last) -> iterator_distance(first, last)
// [first, last) is RandomAccessIterator && not Pointer -> last - first
// otherwise -> linearly count: first to last
//
2012-04-04 08:48:02 +00:00
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
2012-04-04 08:48:02 +00:00
distance(InputIterator first, InputIterator last) {
return sprout_iterator_detail::distance(first, last);
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_DISTANCE_HPP