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

432 lines
15 KiB
C++
Raw Normal View History

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-30 08:30:47 +00:00
#ifndef SPROUT_ITERATOR_TRANSFORM_ITERATOR_HPP
#define SPROUT_ITERATOR_TRANSFORM_ITERATOR_HPP
#include <iterator>
#include <utility>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/iterator/prev.hpp>
#include <sprout/iterator/distance.hpp>
2012-11-16 16:59:56 +00:00
#include <sprout/iterator/type_traits/common.hpp>
2012-10-05 15:58:56 +00:00
#include <sprout/utility/swap.hpp>
2012-04-30 08:30:47 +00:00
namespace sprout {
//
// transform_iterator
//
template<typename BinaryFunction, typename LIterator, typename RIterator = void>
class transform_iterator
: public std::iterator<
2012-11-16 16:59:56 +00:00
typename sprout::common_iterator_category<LIterator, RIterator>::type,
2012-04-30 08:30:47 +00:00
typename std::remove_reference<
typename std::result_of<
BinaryFunction (
typename std::iterator_traits<LIterator>::reference,
typename std::iterator_traits<RIterator>::reference
)
>::type
>::type,
typename std::iterator_traits<LIterator>::difference_type,
typename std::remove_reference<
typename std::result_of<
BinaryFunction (
typename std::iterator_traits<LIterator>::reference,
typename std::iterator_traits<RIterator>::reference
)
>::type
>::type*,
typename std::result_of<
BinaryFunction (
typename std::iterator_traits<LIterator>::reference,
typename std::iterator_traits<RIterator>::reference
)
>::type
>
{
public:
typedef BinaryFunction functor_type;
typedef LIterator iterator_type;
typedef RIterator iterator2_type;
2012-11-16 16:59:56 +00:00
typedef typename sprout::common_iterator_category<LIterator, RIterator>::type iterator_category;
2012-04-30 08:30:47 +00:00
typedef typename std::result_of<
BinaryFunction (
typename std::iterator_traits<LIterator>::reference,
typename std::iterator_traits<RIterator>::reference
)
>::type reference;
typedef typename std::remove_reference<reference>::type value_type;
typedef typename std::iterator_traits<iterator_type>::difference_type difference_type;
typedef value_type* pointer;
protected:
iterator_type current;
iterator2_type current2;
2012-12-02 09:16:10 +00:00
BinaryFunction func;
2012-04-30 08:30:47 +00:00
public:
SPROUT_CONSTEXPR transform_iterator()
: current(), current2(), func()
{}
2012-04-30 08:30:47 +00:00
transform_iterator(transform_iterator const&) = default;
SPROUT_CONSTEXPR transform_iterator(iterator_type it, iterator2_type it2)
: current(it)
, current2(it2)
{}
2012-12-02 09:16:10 +00:00
SPROUT_CONSTEXPR transform_iterator(iterator_type it, iterator2_type it2, BinaryFunction func)
2012-04-30 08:30:47 +00:00
: current(it)
, current2(it2)
, func(func)
{}
template<typename U, typename V, typename W>
SPROUT_CONSTEXPR transform_iterator(transform_iterator<U, V, W> const& it)
: current(it.current)
, current2(it.current2)
, func(it.func)
{}
template<typename U, typename V, typename W>
SPROUT_CXX14_CONSTEXPR transform_iterator& operator=(transform_iterator<U, V, W> const& it) {
2012-04-30 08:30:47 +00:00
transform_iterator temp(it);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR iterator_type base() const {
return current;
}
SPROUT_CONSTEXPR iterator_type base2() const {
return current2;
}
2012-12-02 09:16:10 +00:00
SPROUT_CONSTEXPR BinaryFunction functor() const {
2012-04-30 08:30:47 +00:00
return func;
}
SPROUT_CONSTEXPR reference operator*() const {
return func(*current, *current2);
}
SPROUT_CONSTEXPR pointer operator->() const {
return &func(*current, *current2);
}
SPROUT_CXX14_CONSTEXPR transform_iterator& operator++() {
2012-04-30 08:30:47 +00:00
++current;
++current2;
return *this;
}
SPROUT_CXX14_CONSTEXPR transform_iterator operator++(int) {
2012-04-30 08:30:47 +00:00
transform_iterator result(*this);
++current;
++current2;
return result;
}
SPROUT_CXX14_CONSTEXPR transform_iterator& operator--() {
2012-04-30 08:30:47 +00:00
--current;
--current2;
return *this;
}
SPROUT_CXX14_CONSTEXPR transform_iterator operator--(int) {
2012-04-30 08:30:47 +00:00
transform_iterator temp(*this);
--current;
--current2;
return temp;
}
SPROUT_CONSTEXPR transform_iterator operator+(difference_type n) const {
return transform_iterator(current + n, current2 + n, func);
}
SPROUT_CONSTEXPR transform_iterator operator-(difference_type n) const {
return transform_iterator(current - n, current2 - n, func);
}
SPROUT_CXX14_CONSTEXPR transform_iterator& operator+=(difference_type n) {
2012-04-30 08:30:47 +00:00
transform_iterator temp(current + n, current2 + n, func);
temp.swap(*this);
return *this;
}
SPROUT_CXX14_CONSTEXPR transform_iterator& operator-=(difference_type n) {
2012-04-30 08:30:47 +00:00
transform_iterator temp(current - n, current2 - n, func);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return func(current[n], current2[n]);
}
SPROUT_CONSTEXPR transform_iterator next() const {
return transform_iterator(sprout::next(current), sprout::next(current2), func);
}
SPROUT_CONSTEXPR transform_iterator prev() const {
return transform_iterator(sprout::prev(current), sprout::prev(current2), func);
}
SPROUT_CXX14_CONSTEXPR void swap(transform_iterator& other)
2012-10-05 15:58:56 +00:00
SPROUT_NOEXCEPT_EXPR(
SPROUT_NOEXCEPT_EXPR(sprout::swap(current, other.current))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(current2, other.current2))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(func, other.func))
)
{
sprout::swap(current, other.current);
sprout::swap(current2, other.current2);
sprout::swap(func, other.func);
2012-04-30 08:30:47 +00:00
}
};
//
// transform_iterator
//
template<typename UnaryFunction, typename Iterator>
class transform_iterator<UnaryFunction, Iterator, void>
: public std::iterator<
typename std::iterator_traits<Iterator>::iterator_category,
typename std::remove_reference<
typename std::result_of<
UnaryFunction (typename std::iterator_traits<Iterator>::reference)
>::type
>::type,
typename std::iterator_traits<Iterator>::difference_type,
typename std::remove_reference<
typename std::result_of<
UnaryFunction (typename std::iterator_traits<Iterator>::reference)
>::type
>::type*,
typename std::result_of<
UnaryFunction (typename std::iterator_traits<Iterator>::reference)
>::type
>
{
public:
typedef UnaryFunction functor_type;
typedef Iterator iterator_type;
typedef typename std::iterator_traits<iterator_type>::iterator_category iterator_category;
typedef typename std::result_of<
UnaryFunction (typename std::iterator_traits<Iterator>::reference)
>::type reference;
typedef typename std::remove_reference<reference>::type value_type;
typedef typename std::iterator_traits<iterator_type>::difference_type difference_type;
typedef value_type* pointer;
protected:
iterator_type current;
2012-12-02 09:16:10 +00:00
UnaryFunction func;
2012-04-30 08:30:47 +00:00
public:
SPROUT_CONSTEXPR transform_iterator()
: current(), func()
{}
2012-04-30 08:30:47 +00:00
transform_iterator(transform_iterator const&) = default;
explicit SPROUT_CONSTEXPR transform_iterator(iterator_type it)
: current(it)
{}
2012-12-02 09:16:10 +00:00
SPROUT_CONSTEXPR transform_iterator(iterator_type it, UnaryFunction func)
2012-04-30 08:30:47 +00:00
: current(it)
, func(func)
{}
template<typename U, typename V>
SPROUT_CONSTEXPR transform_iterator(transform_iterator<U, V> const& it)
: current(it.current)
, func(it.func)
{}
template<typename U, typename V>
SPROUT_CXX14_CONSTEXPR transform_iterator& operator=(transform_iterator<U, V> const& it) {
2012-04-30 08:30:47 +00:00
transform_iterator temp(it);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR iterator_type base() const {
return current;
}
2012-12-02 09:16:10 +00:00
SPROUT_CONSTEXPR UnaryFunction functor() const {
2012-04-30 08:30:47 +00:00
return func;
}
SPROUT_CONSTEXPR reference operator*() const {
return func(*current);
}
SPROUT_CONSTEXPR pointer operator->() const {
return &func(*current);
}
SPROUT_CXX14_CONSTEXPR transform_iterator& operator++() {
2012-04-30 08:30:47 +00:00
++current;
return *this;
}
SPROUT_CXX14_CONSTEXPR transform_iterator operator++(int) {
2012-04-30 08:30:47 +00:00
transform_iterator result(*this);
++current;
return result;
}
SPROUT_CXX14_CONSTEXPR transform_iterator& operator--() {
2012-04-30 08:30:47 +00:00
--current;
return *this;
}
SPROUT_CXX14_CONSTEXPR transform_iterator operator--(int) {
2012-04-30 08:30:47 +00:00
transform_iterator temp(*this);
--current;
return temp;
}
SPROUT_CONSTEXPR transform_iterator operator+(difference_type n) const {
return transform_iterator(current + n, func);
}
SPROUT_CONSTEXPR transform_iterator operator-(difference_type n) const {
return transform_iterator(current - n, func);
}
SPROUT_CXX14_CONSTEXPR transform_iterator& operator+=(difference_type n) {
2012-04-30 08:30:47 +00:00
transform_iterator temp(current + n, func);
temp.swap(*this);
return *this;
}
SPROUT_CXX14_CONSTEXPR transform_iterator& operator-=(difference_type n) {
2012-04-30 08:30:47 +00:00
transform_iterator temp(current - n, func);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return func(current[n]);
}
SPROUT_CONSTEXPR transform_iterator next() const {
return transform_iterator(sprout::next(current), func);
}
SPROUT_CONSTEXPR transform_iterator prev() const {
return transform_iterator(sprout::prev(current), func);
}
SPROUT_CXX14_CONSTEXPR void swap(transform_iterator& other)
2012-10-05 15:58:56 +00:00
SPROUT_NOEXCEPT_EXPR(
SPROUT_NOEXCEPT_EXPR(sprout::swap(current, other.current))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(func, other.func))
)
{
sprout::swap(current, other.current);
sprout::swap(func, other.func);
2012-04-30 08:30:47 +00:00
}
};
template<
typename UnaryOrBinaryFunction1, typename LIterator1, typename RIterator1,
typename UnaryOrBinaryFunction2, typename LIterator2, typename RIterator2
>
inline SPROUT_CONSTEXPR bool operator==(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction1, LIterator1, RIterator1> const& lhs,
sprout::transform_iterator<UnaryOrBinaryFunction2, LIterator2, RIterator2> const& rhs
)
{
return lhs.base() == rhs.base();
}
template<
typename UnaryOrBinaryFunction1, typename LIterator1, typename RIterator1,
typename UnaryOrBinaryFunction2, typename LIterator2, typename RIterator2
>
inline SPROUT_CONSTEXPR bool operator!=(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction1, LIterator1, RIterator1> const& lhs,
sprout::transform_iterator<UnaryOrBinaryFunction2, LIterator2, RIterator2> const& rhs
)
{
return !(lhs == rhs);
}
template<
typename UnaryOrBinaryFunction1, typename LIterator1, typename RIterator1,
typename UnaryOrBinaryFunction2, typename LIterator2, typename RIterator2
>
inline SPROUT_CONSTEXPR bool operator<(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction1, LIterator1, RIterator1> const& lhs,
sprout::transform_iterator<UnaryOrBinaryFunction2, LIterator2, RIterator2> const& rhs
)
{
return lhs.base() < rhs.base();
}
template<
typename UnaryOrBinaryFunction1, typename LIterator1, typename RIterator1,
typename UnaryOrBinaryFunction2, typename LIterator2, typename RIterator2
>
inline SPROUT_CONSTEXPR bool operator>(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction1, LIterator1, RIterator1> const& lhs,
sprout::transform_iterator<UnaryOrBinaryFunction2, LIterator2, RIterator2> const& rhs
)
{
return rhs < lhs;
}
template<
typename UnaryOrBinaryFunction1, typename LIterator1, typename RIterator1,
typename UnaryOrBinaryFunction2, typename LIterator2, typename RIterator2
>
inline SPROUT_CONSTEXPR bool operator<=(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction1, LIterator1, RIterator1> const& lhs,
sprout::transform_iterator<UnaryOrBinaryFunction2, LIterator2, RIterator2> const& rhs
)
{
return !(rhs < lhs);
}
template<
typename UnaryOrBinaryFunction1, typename LIterator1, typename RIterator1,
typename UnaryOrBinaryFunction2, typename LIterator2, typename RIterator2
>
inline SPROUT_CONSTEXPR bool operator>=(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction1, LIterator1, RIterator1> const& lhs,
sprout::transform_iterator<UnaryOrBinaryFunction2, LIterator2, RIterator2> const& rhs
)
{
return !(lhs < rhs);
}
template<
typename UnaryOrBinaryFunction1, typename LIterator1, typename RIterator1,
typename UnaryOrBinaryFunction2, typename LIterator2, typename RIterator2
>
inline SPROUT_CONSTEXPR decltype(std::declval<LIterator1>() - std::declval<LIterator2>()) operator-(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction1, LIterator1, RIterator1> const& lhs,
sprout::transform_iterator<UnaryOrBinaryFunction2, LIterator2, RIterator2> const& rhs
)
{
return lhs.base() - rhs.base();
}
template<typename UnaryOrBinaryFunction, typename LIterator, typename RIterator>
inline SPROUT_CONSTEXPR sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator> operator+(
2012-04-30 08:30:47 +00:00
typename sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator>::difference_type n,
sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator> const& it
)
{
return it + n;
}
//
// make_transform_iterator
//
template<typename BinaryFunction, typename LIterator, typename RIterator>
inline SPROUT_CONSTEXPR sprout::transform_iterator<BinaryFunction, LIterator, RIterator>
2012-04-30 08:30:47 +00:00
make_transform_iterator(LIterator it1, RIterator it2, BinaryFunction func) {
return sprout::transform_iterator<BinaryFunction, LIterator, RIterator>(it1, it2, func);
}
template<typename UnaryFunction, typename Iterator>
inline SPROUT_CONSTEXPR sprout::transform_iterator<UnaryFunction, Iterator>
2012-04-30 08:30:47 +00:00
make_transform_iterator(Iterator it, UnaryFunction func) {
return sprout::transform_iterator<UnaryFunction, Iterator>(it, func);
}
//
// swap
//
template<typename UnaryOrBinaryFunction, typename LIterator, typename RIterator>
inline SPROUT_CXX14_CONSTEXPR void
2012-10-05 15:58:56 +00:00
swap(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator>& lhs,
sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator>& rhs
)
2012-10-05 15:58:56 +00:00
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
2012-04-30 08:30:47 +00:00
{
lhs.swap(rhs);
}
//
// iterator_next
2012-04-30 08:30:47 +00:00
//
template<typename UnaryOrBinaryFunction, typename LIterator, typename RIterator>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator>
iterator_next(sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator> const& it) {
2012-04-30 08:30:47 +00:00
return it.next();
}
//
// iterator_prev
2012-04-30 08:30:47 +00:00
//
template<typename UnaryOrBinaryFunction, typename LIterator, typename RIterator>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator>
iterator_prev(sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator> const& it) {
2012-04-30 08:30:47 +00:00
return it.prev();
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_TRANSFORM_ITERATOR_HPP