1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/iterator/prev.hpp

106 lines
3.4 KiB
C++
Raw Normal View History

2011-09-30 15:04:03 +00:00
#ifndef SPROUT_ITERATOR_PREV_HPP
#define SPROUT_ITERATOR_PREV_HPP
#include <iterator>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/adl/not_found.hpp>
namespace sprout_adl {
sprout::adl_not_found iterator_prev(...);
} // namespace sprout_adl
2011-09-30 15:04:03 +00:00
namespace sprout {
namespace iterator_detail {
2012-04-04 08:48:02 +00:00
template<typename RandomAccessIterator>
inline SPROUT_CONSTEXPR typename std::enable_if<
std::is_literal_type<RandomAccessIterator>::value,
RandomAccessIterator
2011-09-30 15:04:03 +00:00
>::type prev_impl(
RandomAccessIterator const& it,
2011-09-30 15:04:03 +00:00
std::random_access_iterator_tag*
)
{
return it - 1;
2011-09-30 15:04:03 +00:00
}
2012-04-04 08:48:02 +00:00
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator prev_impl(
BidirectionalIterator const& it,
2011-09-30 15:04:03 +00:00
void*
)
{
return std::prev(it);
2011-09-30 15:04:03 +00:00
}
2012-04-04 08:48:02 +00:00
template<typename RandomAccessIterator>
inline SPROUT_CONSTEXPR typename std::enable_if<
std::is_literal_type<RandomAccessIterator>::value,
RandomAccessIterator
2011-09-30 15:04:03 +00:00
>::type prev_impl(
RandomAccessIterator const& it,
typename std::iterator_traits<RandomAccessIterator>::difference_type n,
2011-09-30 15:04:03 +00:00
std::random_access_iterator_tag*
)
{
return it - n;
2011-09-30 15:04:03 +00:00
}
2012-04-04 08:48:02 +00:00
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator prev_impl(
BidirectionalIterator const& it,
typename std::iterator_traits<BidirectionalIterator>::difference_type n,
2011-09-30 15:04:03 +00:00
void*
)
{
return std::prev(it, n);
2011-09-30 15:04:03 +00:00
}
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
iterator_prev(BidirectionalIterator const& it) {
typedef typename std::iterator_traits<BidirectionalIterator>::iterator_category* category;
return sprout::iterator_detail::prev_impl(it, category());
}
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
iterator_prev(BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n) {
typedef typename std::iterator_traits<BidirectionalIterator>::iterator_category* category;
return sprout::iterator_detail::prev_impl(it, n, category());
}
} // namespace iterator_detail
} // namespace sprout
namespace sprout_iterator_detail {
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev(BidirectionalIterator const& it) {
using sprout::iterator_detail::iterator_prev;
using sprout_adl::iterator_prev;
return iterator_prev(it);
}
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev(BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n) {
using sprout::iterator_detail::iterator_prev;
using sprout_adl::iterator_prev;
return iterator_prev(it, n);
}
} // namespace sprout_iterator_detail
namespace sprout {
2011-09-30 15:04:03 +00:00
//
// prev
//
2012-04-04 08:48:02 +00:00
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev(BidirectionalIterator const& it) {
return sprout_iterator_detail::prev(it);
2011-09-30 15:04:03 +00:00
}
2012-04-04 08:48:02 +00:00
template<typename BidirectionalIterator>
inline SPROUT_CONSTEXPR BidirectionalIterator
prev(BidirectionalIterator const& it, typename std::iterator_traits<BidirectionalIterator>::difference_type n) {
return sprout_iterator_detail::prev(it, n);
2011-10-01 15:19:13 +00:00
}
2011-09-30 15:04:03 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_PREV_HPP