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

add move_iterator

This commit is contained in:
bolero-MURAKAMI 2014-03-20 23:52:08 +09:00
parent 01efe1e793
commit 3fb83e0fbe
4 changed files with 318 additions and 0 deletions

View file

@ -10,6 +10,7 @@
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/iterator/reverse_iterator.hpp> #include <sprout/iterator/reverse_iterator.hpp>
#include <sprout/iterator/move_iterator.hpp>
#include <sprout/iterator/transform_iterator.hpp> #include <sprout/iterator/transform_iterator.hpp>
#include <sprout/iterator/replace_iterator.hpp> #include <sprout/iterator/replace_iterator.hpp>
#include <sprout/iterator/replace_if_iterator.hpp> #include <sprout/iterator/replace_if_iterator.hpp>

View file

@ -0,0 +1,228 @@
/*=============================================================================
Copyright (c) 2011-2014 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)
=============================================================================*/
#ifndef SPROUT_ITERATOR_MOVE_ITERATOR_HPP
#define SPROUT_ITERATOR_MOVE_ITERATOR_HPP
#include <iterator>
#include <utility>
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/iterator/prev.hpp>
#include <sprout/iterator/distance.hpp>
#include <sprout/iterator/const_iterator_cast.hpp>
#include <sprout/utility/swap.hpp>
#include <sprout/utility/move.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
//
// move_iterator
//
template<typename Iterator>
class move_iterator
: public std::iterator<
typename std::iterator_traits<Iterator>::iterator_category,
typename std::iterator_traits<Iterator>::value_type,
typename std::iterator_traits<Iterator>::difference_type,
typename std::iterator_traits<Iterator>::pointer,
typename std::iterator_traits<Iterator>::reference
>
{
public:
typedef Iterator iterator_type;
typedef typename std::iterator_traits<iterator_type>::iterator_category iterator_category;
typedef typename std::iterator_traits<iterator_type>::value_type value_type;
typedef typename std::iterator_traits<iterator_type>::difference_type difference_type;
typedef Iterator pointer;
typedef value_type&& reference;
protected:
iterator_type current;
public:
SPROUT_CONSTEXPR move_iterator() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
SPROUT_CONSTEXPR move_iterator(move_iterator const& other)
: current(other.current)
{}
explicit SPROUT_CONSTEXPR move_iterator(iterator_type it)
: current(it)
{}
template<typename U>
SPROUT_CONSTEXPR move_iterator(move_iterator<U> const& it)
: current(it.base())
{}
template<typename U>
SPROUT_CXX14_CONSTEXPR move_iterator& operator=(move_iterator<U> const& it) {
move_iterator temp(it);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR iterator_type base() const {
return current;
}
SPROUT_CONSTEXPR reference operator*() const {
return sprout::move(*current);
}
SPROUT_CONSTEXPR pointer operator->() const {
return current;
}
SPROUT_CXX14_CONSTEXPR move_iterator& operator++() {
++current;
return *this;
}
SPROUT_CXX14_CONSTEXPR move_iterator operator++(int) {
move_iterator result(*this);
++current;
return result;
}
SPROUT_CXX14_CONSTEXPR move_iterator& operator--() {
--current;
return *this;
}
SPROUT_CXX14_CONSTEXPR move_iterator operator--(int) {
move_iterator temp(*this);
--current;
return temp;
}
SPROUT_CONSTEXPR move_iterator operator+(difference_type n) const {
return move_iterator(current + n);
}
SPROUT_CONSTEXPR move_iterator operator-(difference_type n) const {
return move_iterator(current - n);
}
SPROUT_CXX14_CONSTEXPR move_iterator& operator+=(difference_type n) {
move_iterator temp(current + n);
temp.swap(*this);
return *this;
}
SPROUT_CXX14_CONSTEXPR move_iterator& operator-=(difference_type n) {
move_iterator temp(current - n);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return sprout::move(current[n]);
}
SPROUT_CONSTEXPR move_iterator next() const {
return move_iterator(sprout::next(current));
}
SPROUT_CONSTEXPR move_iterator prev() const {
return move_iterator(sprout::prev(current));
}
SPROUT_CXX14_CONSTEXPR void swap(move_iterator& other)
SPROUT_NOEXCEPT_EXPR(
SPROUT_NOEXCEPT_EXPR(swap(current, other.current))
)
{
swap(current, other.current);
}
};
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator==(sprout::move_iterator<Iterator1> const& lhs, sprout::move_iterator<Iterator2> const& rhs) {
return lhs.base() == rhs.base();
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::move_iterator<Iterator1> const& lhs, sprout::move_iterator<Iterator2> const& rhs) {
return !(lhs == rhs);
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator<(sprout::move_iterator<Iterator1> const& lhs, sprout::move_iterator<Iterator2> const& rhs) {
return lhs.base() < rhs.base();
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator>(sprout::move_iterator<Iterator1> const& lhs, sprout::move_iterator<Iterator2> const& rhs) {
return rhs < lhs;
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator<=(sprout::move_iterator<Iterator1> const& lhs, sprout::move_iterator<Iterator2> const& rhs) {
return !(rhs < lhs);
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator>=(sprout::move_iterator<Iterator1> const& lhs, sprout::move_iterator<Iterator2> const& rhs) {
return !(lhs < rhs);
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR decltype(std::declval<Iterator1>() - std::declval<Iterator2>())
operator-(sprout::move_iterator<Iterator1> const& lhs, sprout::move_iterator<Iterator2> const& rhs) {
return lhs.base() - rhs.base();
}
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::move_iterator<Iterator>
operator+(
typename sprout::move_iterator<Iterator>::difference_type n,
sprout::move_iterator<Iterator> const& it
)
{
return it + n;
}
//
// make_move_iterator
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::move_iterator<Iterator>
make_move_iterator(Iterator it) {
return sprout::move_iterator<Iterator>(it);
}
//
// swap
//
template<typename Iterator>
inline SPROUT_CXX14_CONSTEXPR void
swap(sprout::move_iterator<Iterator>& lhs, sprout::move_iterator<Iterator>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
//
// iterator_next
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::move_iterator<Iterator>
iterator_next(sprout::move_iterator<Iterator> const& it) {
return it.next();
}
//
// iterator_prev
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::move_iterator<Iterator>
iterator_prev(sprout::move_iterator<Iterator> const& it) {
return it.prev();
}
//
// is_const_iterator_cast_convertible
//
template<typename FromIterator, typename ToIterator>
struct is_const_iterator_cast_convertible<sprout::move_iterator<FromIterator>, sprout::move_iterator<ToIterator> >
: public sprout::is_const_iterator_cast_convertible<FromIterator, ToIterator>
{};
//
// const_iterator_conversion
//
template<
typename T,
typename Iterator,
typename sprout::enabler_if<sprout::is_const_iterator_cast_convertible<sprout::move_iterator<Iterator>, T>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
const_iterator_conversion(sprout::move_iterator<Iterator> const& it) {
return T(sprout::const_iterator_cast<typename T::iterator_type>(it.base()));
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_MOVE_ITERATOR_HPP

View file

@ -10,6 +10,7 @@
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/range/adaptor/copied.hpp> #include <sprout/range/adaptor/copied.hpp>
#include <sprout/range/adaptor/moved.hpp>
#include <sprout/range/adaptor/transformed.hpp> #include <sprout/range/adaptor/transformed.hpp>
#include <sprout/range/adaptor/replaced.hpp> #include <sprout/range/adaptor/replaced.hpp>
#include <sprout/range/adaptor/replaced_if.hpp> #include <sprout/range/adaptor/replaced_if.hpp>

View file

@ -0,0 +1,88 @@
/*=============================================================================
Copyright (c) 2011-2014 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)
=============================================================================*/
#ifndef SPROUT_RANGE_ADAPTOR_MOVED_HPP
#define SPROUT_RANGE_ADAPTOR_MOVED_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/container/traits.hpp>
#include <sprout/container/functions.hpp>
#include <sprout/iterator/move_iterator.hpp>
#include <sprout/range/adaptor/detail/adapted_range_default.hpp>
#include <sprout/type_traits/lvalue_reference.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/lvalue_forward.hpp>
namespace sprout {
namespace adaptors {
//
// moved_range
//
template<typename Range>
class moved_range
: public sprout::adaptors::detail::adapted_range_default<
Range,
sprout::move_iterator<typename sprout::container_traits<Range>::iterator>
>
{
public:
typedef sprout::adaptors::detail::adapted_range_default<
Range,
sprout::move_iterator<typename sprout::container_traits<Range>::iterator>
> base_type;
typedef typename base_type::range_type range_type;
typedef typename base_type::iterator iterator;
public:
SPROUT_CONSTEXPR moved_range() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
moved_range(moved_range const&) = default;
explicit SPROUT_CONSTEXPR moved_range(range_type& range)
: base_type(
iterator(sprout::end(range)),
iterator(sprout::begin(range))
)
{}
};
//
// moved_forwarder
//
class moved_forwarder {};
//
// moved
//
namespace {
SPROUT_STATIC_CONSTEXPR sprout::adaptors::moved_forwarder moved = {};
} // anonymous-namespace
//
// operator|
//
template<typename Range>
inline SPROUT_CONSTEXPR sprout::adaptors::moved_range<
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>
operator|(Range&& lhs, sprout::adaptors::moved_forwarder) {
return sprout::adaptors::moved_range<
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>(
sprout::lvalue_forward<Range>(lhs)
);
}
} // namespace adaptors
//
// container_construct_traits
//
template<typename Range>
struct container_construct_traits<sprout::adaptors::moved_range<Range> >
: public sprout::container_construct_traits<typename sprout::adaptors::moved_range<Range>::base_type>
{};
} // namespace sprout
#endif // #ifndef SPROUT_RANGE_ADAPTOR_MOVED_HPP