fix next, prev

This commit is contained in:
bolero-MURAKAMI 2012-12-12 17:46:36 +09:00
parent 75228465fe
commit cf9f6670b6
18 changed files with 243 additions and 0 deletions

View file

@ -76,6 +76,11 @@ namespace sprout {
} //namespace detail
// 25.2.8 Adjacent find
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename ForwardIterator, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator
adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred) {

View file

@ -58,6 +58,11 @@ namespace sprout {
} //namespace detail
// 25.2.1 All of
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator, typename Predicate>
inline SPROUT_CONSTEXPR bool
all_of(InputIterator first, InputIterator last, Predicate pred) {

View file

@ -58,6 +58,11 @@ namespace sprout {
} //namespace detail
// 25.2.2 Any of
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator, typename Predicate>
inline SPROUT_CONSTEXPR bool
any_of(InputIterator first, InputIterator last, Predicate pred) {

View file

@ -58,6 +58,11 @@ namespace sprout {
} //namespace detail
// 25.2.9 Count
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator, typename T>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
count(InputIterator first, InputIterator last, T const& value) {

View file

@ -58,6 +58,11 @@ namespace sprout {
} //namespace detail
// 25.2.9 Count
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator, typename Predicate>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
count_if(InputIterator first, InputIterator last, Predicate pred) {

View file

@ -62,6 +62,11 @@ namespace sprout {
} //namespace detail
// 25.2.11 Equal
//
// recursion depth:
// [first1, last1), first2 are RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR bool
equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) {

View file

@ -60,6 +60,11 @@ namespace sprout {
} //namespace detail
// 25.2.5 Find
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator, typename T>
inline SPROUT_CONSTEXPR InputIterator
find(InputIterator first, InputIterator last, T const& value) {

View file

@ -77,6 +77,13 @@ namespace sprout {
} //namespace detail
// 25.2.7 Find first
//
// recursion depth:
// [first1, last1) is RandomAccessIterator -> O(log N1)
// otherwise -> O(N1)
// [first2, last2) is RandomAccessIterator -> O(log N2)
// otherwise -> O(N2)
//
template<typename InputIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR InputIterator1
find_first_of(

View file

@ -60,6 +60,11 @@ namespace sprout {
} //namespace detail
// 25.2.5 Find
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator, typename Predicate>
inline SPROUT_CONSTEXPR InputIterator
find_if(InputIterator first, InputIterator last, Predicate pred) {

View file

@ -60,6 +60,11 @@ namespace sprout {
} //namespace detail
// 25.2.5 Find
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator, typename Predicate>
inline SPROUT_CONSTEXPR InputIterator
find_if_not(InputIterator first, InputIterator last, Predicate pred) {

View file

@ -76,6 +76,11 @@ namespace sprout {
} // namespace detail
// 25.2.12 Is permutation
//
// recursion depth:
// [first1, last1), first2 are RandomAccessIterator -> O(log N)
// otherwise -> O(N^2)
//
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR bool
is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate pred) {

View file

@ -77,6 +77,11 @@ namespace sprout {
} //namespace detail
// 25.2.10 Mismatch
//
// recursion depth:
// [first1, last1), first2 are RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) {

View file

@ -56,6 +56,11 @@ namespace sprout {
} //namespace detail
// 25.2.3 None of
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template <typename InputIterator, typename Predicate>
inline SPROUT_CONSTEXPR bool
none_of(InputIterator first, InputIterator last, Predicate pred) {

View file

@ -19,6 +19,10 @@ namespace sprout {
//
// one_of
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator, typename Predicate>
inline SPROUT_CONSTEXPR bool
one_of(InputIterator first, InputIterator last, Predicate pred) {

View file

@ -3,7 +3,10 @@
#include <iterator>
#include <type_traits>
#include <stdexcept>
#include <sprout/config.hpp>
#include <sprout/iterator/next_fwd.hpp>
#include <sprout/iterator/prev_fwd.hpp>
#include <sprout/adl/not_found.hpp>
namespace sprout_adl {
@ -38,8 +41,71 @@ namespace sprout {
{
return it + n;
}
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)
)
;
}
template<typename ForwardIterator>
inline SPROUT_CONSTEXPR ForwardIterator
next_impl_2(ForwardIterator const& it, typename std::iterator_traits<ForwardIterator>::difference_type n) {
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)
;
}
template<typename ForwardIterator>
inline SPROUT_CONSTEXPR ForwardIterator
next_impl_1(
ForwardIterator const& it, typename std::iterator_traits<ForwardIterator>::difference_type n,
void*
)
{
return n == 0 ? it
: n > 0 ? sprout::iterator_detail::next_impl_2(it, n)
: throw std::domain_error("next: nagative distance is invalid ForwardIterator")
;
}
template<typename ForwardIterator>
inline SPROUT_CONSTEXPR typename std::enable_if<
std::is_literal_type<ForwardIterator>::value,
ForwardIterator
>::type
next_impl(
ForwardIterator const& it, typename std::iterator_traits<ForwardIterator>::difference_type n,
void*
)
{
typedef typename std::iterator_traits<ForwardIterator>::iterator_category* category;
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
next_impl(
ForwardIterator const& it, typename std::iterator_traits<ForwardIterator>::difference_type n,
void*
@ -84,11 +150,23 @@ namespace sprout {
//
// next
//
// effect:
// ADL callable iterator_next(it) -> iterator_next(it)
// it is RandomAccessIterator && LiteralType -> it + 1
// otherwise -> std::next(it)
//
template<typename ForwardIterator>
inline SPROUT_CONSTEXPR ForwardIterator
next(ForwardIterator const& it) {
return sprout_iterator_detail::next(it);
}
//
// effect:
// ADL callable iterator_next(it, n) -> iterator_next(it, n)
// it is RandomAccessIterator && LiteralType -> it + n
// it is LiteralType -> sprout::next(it)... || sprout::prev(it)...
// otherwise -> std::next(it, n)
//
template<typename ForwardIterator>
inline SPROUT_CONSTEXPR ForwardIterator
next(ForwardIterator const& it, typename std::iterator_traits<ForwardIterator>::difference_type n) {

View file

@ -0,0 +1,20 @@
#ifndef SPROUT_ITERATOR_NEXT_FWD_HPP
#define SPROUT_ITERATOR_NEXT_FWD_HPP
#include <iterator>
#include <sprout/config.hpp>
namespace sprout {
//
// next
//
template<typename ForwardIterator>
inline SPROUT_CONSTEXPR ForwardIterator
next(ForwardIterator const& it);
template<typename ForwardIterator>
inline SPROUT_CONSTEXPR ForwardIterator
next(ForwardIterator const& it, typename std::iterator_traits<ForwardIterator>::difference_type n);
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_NEXT_FWD_HPP

View file

@ -4,6 +4,8 @@
#include <iterator>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/iterator/next_fwd.hpp>
#include <sprout/iterator/prev_fwd.hpp>
#include <sprout/adl/not_found.hpp>
namespace sprout_adl {
@ -38,8 +40,48 @@ namespace sprout {
{
return it - n;
}
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev_impl_1_neg(BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n) {
return n == -1 ? sprout::next(it)
: sprout::iterator_detail::prev_impl_1_neg(
sprout::iterator_detail::prev_impl_1_neg(it, n / 2),
n - (n / 2)
)
;
}
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev_impl_1(BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n) {
return n == 1 ? sprout::prev(it)
: sprout::iterator_detail::prev_impl_1(
sprout::iterator_detail::prev_impl_1(it, n / 2),
n - (n / 2)
)
;
}
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR typename std::enable_if<
std::is_literal_type<BidirectionalIterator>::value,
BidirectionalIterator
>::type
prev_impl(
BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n,
void*
)
{
return n == 0 ? it
: n > 0 ? sprout::iterator_detail::prev_impl_1(it, n)
: sprout::iterator_detail::prev_impl_1_neg(it, n)
;
}
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR typename std::enable_if<
!std::is_literal_type<BidirectionalIterator>::value,
BidirectionalIterator
>::type
prev_impl(
BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n,
void*
@ -84,11 +126,23 @@ namespace sprout {
//
// prev
//
// effect:
// ADL callable iterator_prev(it) -> iterator_prev(it)
// it is RandomAccessIterator && LiteralType -> it - 1
// otherwise -> std::prev(it)
//
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev(BidirectionalIterator const& it) {
return sprout_iterator_detail::prev(it);
}
//
// effect:
// ADL callable iterator_prev(it, n) -> iterator_prev(it, n)
// it is RandomAccessIterator && LiteralType -> it - n
// it is LiteralType -> sprout::prev(it)... || sprout::next(it)...
// otherwise -> std::prev(it, n)
//
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev(BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n) {

View file

@ -0,0 +1,20 @@
#ifndef SPROUT_ITERATOR_PREV_FWD_HPP
#define SPROUT_ITERATOR_PREV_FWD_HPP
#include <iterator>
#include <sprout/config.hpp>
namespace sprout {
//
// prev
//
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev(BidirectionalIterator const& it);
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev(BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n);
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_PREV_FWD_HPP