Sprout/sprout/iterator/transform_iterator.hpp

452 lines
15 KiB
C++
Raw Normal View History

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:
transform_iterator() = default;
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>
transform_iterator& operator=(transform_iterator<U, V, W> const& it) {
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);
}
transform_iterator& operator++() {
++current;
++current2;
return *this;
}
transform_iterator operator++(int) {
transform_iterator result(*this);
++current;
++current2;
return result;
}
transform_iterator& operator--() {
--current;
--current2;
return *this;
}
transform_iterator operator--(int) {
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);
}
transform_iterator& operator+=(difference_type n) {
transform_iterator temp(current + n, current2 + n, func);
temp.swap(*this);
return *this;
}
transform_iterator& operator-=(difference_type n) {
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);
}
2012-10-05 15:58:56 +00:00
void swap(transform_iterator& other)
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:
transform_iterator() = default;
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>
transform_iterator& operator=(transform_iterator<U, V> const& it) {
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);
}
transform_iterator& operator++() {
++current;
return *this;
}
transform_iterator operator++(int) {
transform_iterator result(*this);
++current;
return result;
}
transform_iterator& operator--() {
--current;
return *this;
}
transform_iterator operator--(int) {
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);
}
transform_iterator& operator+=(difference_type n) {
transform_iterator temp(current + n, func);
temp.swap(*this);
return *this;
}
transform_iterator& operator-=(difference_type n) {
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);
}
2012-10-05 15:58:56 +00:00
void swap(transform_iterator& other)
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>
2012-10-05 15:58:56 +00:00
inline void
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_distance
//
template<typename UnaryOrBinaryFunction, typename LIterator, typename RIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator> >::difference_type
iterator_distance(
sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator> first,
sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator> last
)
{
return last - first;
}
//
// 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();
}
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(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator> const& it,
typename sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator>::difference_type n
)
{
return it + n;
}
//
// 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();
}
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(
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator> const& it,
typename sprout::transform_iterator<UnaryOrBinaryFunction, LIterator, RIterator>::difference_type n
)
{
return it - n;
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_TRANSFORM_ITERATOR_HPP