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)
|
|
|
|
=============================================================================*/
|
2012-04-01 13:15:09 +00:00
|
|
|
#ifndef SPROUT_ITERATOR_DISTANCE_HPP
|
|
|
|
#define SPROUT_ITERATOR_DISTANCE_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
2012-12-15 08:41:20 +00:00
|
|
|
#include <type_traits>
|
2012-04-01 13:15:09 +00:00
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/next.hpp>
|
2013-02-26 07:14:04 +00:00
|
|
|
#include <sprout/iterator/type_traits/category.hpp>
|
2013-02-04 14:10:24 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2012-09-29 08:10:46 +00:00
|
|
|
#include <sprout/adl/not_found.hpp>
|
|
|
|
|
|
|
|
namespace sprout_adl {
|
2012-09-29 14:39:00 +00:00
|
|
|
sprout::not_found_via_adl iterator_distance(...);
|
2012-09-29 08:10:46 +00:00
|
|
|
} // namespace sprout_adl
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2012-09-26 09:42:43 +00:00
|
|
|
namespace iterator_detail {
|
2012-12-15 08:41:20 +00:00
|
|
|
template<typename RandomAccessIterator>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator>::value,
|
2012-12-15 08:41:20 +00:00
|
|
|
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
|
|
|
|
2012-04-04 08:48:02 +00:00
|
|
|
template<typename InputIterator>
|
2012-12-23 08:59:00 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type>
|
|
|
|
iterator_distance_impl_1(
|
|
|
|
sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type> const& current,
|
|
|
|
InputIterator last, typename std::iterator_traits<InputIterator>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type> type;
|
|
|
|
return current.first == last ? current
|
|
|
|
: n == 1 ? type(sprout::next(current.first), current.second + 1)
|
|
|
|
: sprout::iterator_detail::iterator_distance_impl_1(
|
|
|
|
sprout::iterator_detail::iterator_distance_impl_1(
|
|
|
|
current,
|
|
|
|
last, n / 2
|
|
|
|
),
|
|
|
|
last, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type>
|
|
|
|
iterator_distance_impl(
|
|
|
|
sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type> const& current,
|
|
|
|
InputIterator last, typename std::iterator_traits<InputIterator>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return current.first == last ? current
|
|
|
|
: sprout::iterator_detail::iterator_distance_impl(
|
|
|
|
sprout::iterator_detail::iterator_distance_impl_1(
|
|
|
|
current,
|
|
|
|
last, n
|
|
|
|
),
|
|
|
|
last, n * 2
|
|
|
|
)
|
2012-04-01 13:15:09 +00:00
|
|
|
;
|
|
|
|
}
|
2012-12-15 08:41:20 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
|
2013-02-23 06:21:27 +00:00
|
|
|
iterator_distance(InputIterator first, InputIterator last, std::input_iterator_tag*) {
|
2012-12-23 08:59:00 +00:00
|
|
|
typedef sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type> type;
|
|
|
|
return sprout::iterator_detail::iterator_distance_impl(type(first, 0), last, 1).second;
|
2012-12-15 08:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator>
|
2012-12-23 11:49:45 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
std::is_literal_type<InputIterator>::value,
|
|
|
|
typename std::iterator_traits<InputIterator>::difference_type
|
|
|
|
>::type
|
2012-12-15 08:41:20 +00:00
|
|
|
iterator_distance(InputIterator first, InputIterator last) {
|
|
|
|
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
|
|
|
|
return sprout::iterator_detail::iterator_distance(first, last, category());
|
|
|
|
}
|
2012-12-23 11:49:45 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
!std::is_literal_type<InputIterator>::value,
|
|
|
|
typename std::iterator_traits<InputIterator>::difference_type
|
|
|
|
>::type
|
|
|
|
iterator_distance(InputIterator first, InputIterator last) {
|
|
|
|
return std::distance(first, last);
|
|
|
|
}
|
2012-09-26 09:42:43 +00:00
|
|
|
} // 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;
|
2012-09-29 08:10:46 +00:00
|
|
|
using sprout_adl::iterator_distance;
|
2012-09-26 09:42:43 +00:00
|
|
|
return iterator_distance(first, last);
|
|
|
|
}
|
|
|
|
} // namespace sprout_iterator_detail
|
|
|
|
|
|
|
|
namespace sprout {
|
2012-04-01 13:15:09 +00:00
|
|
|
//
|
|
|
|
// distance
|
|
|
|
//
|
2012-12-15 08:41:20 +00:00
|
|
|
// effect:
|
|
|
|
// ADL callable iterator_distance(first, last) -> iterator_distance(first, last)
|
2013-01-03 08:01:50 +00:00
|
|
|
// otherwise, [first, last) is not LiteralType -> std::distance(first, last)
|
2012-12-23 11:49:45 +00:00
|
|
|
// otherwise, [first, last) is RandomAccessIterator && not Pointer -> last - first
|
2012-12-15 08:41:20 +00:00
|
|
|
// otherwise -> linearly count: first to last
|
|
|
|
//
|
2012-04-04 08:48:02 +00:00
|
|
|
template<typename InputIterator>
|
2012-07-15 07:04:05 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
|
2012-04-04 08:48:02 +00:00
|
|
|
distance(InputIterator first, InputIterator last) {
|
2012-09-26 09:42:43 +00:00
|
|
|
return sprout_iterator_detail::distance(first, last);
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ITERATOR_DISTANCE_HPP
|