Sprout/sprout/range/adaptor/transformed.hpp

215 lines
7 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2017 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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_RANGE_ADAPTOR_TRANSFORMD_HPP
#define SPROUT_RANGE_ADAPTOR_TRANSFORMD_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/container/traits.hpp>
#include <sprout/container/functions.hpp>
#include <sprout/iterator/transform_iterator.hpp>
#include <sprout/range/adaptor/detail/adapted_range_default.hpp>
2012-04-30 08:30:47 +00:00
#include <sprout/type_traits/lvalue_reference.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/lvalue_forward.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/value_holder/value_holder.hpp>
2012-04-30 08:30:47 +00:00
namespace sprout {
2012-04-30 13:18:45 +00:00
namespace adaptors {
2012-04-30 08:30:47 +00:00
//
// transformed_range
//
template<typename BinaryFunction, typename LRange, typename RRange = void>
class transformed_range
: public sprout::adaptors::detail::adapted_range_default<
LRange,
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<
BinaryFunction,
2012-04-30 08:30:47 +00:00
typename sprout::container_traits<LRange>::iterator,
typename sprout::container_traits<RRange>::iterator
>
>
{
public:
typedef BinaryFunction functor_type;
typedef LRange range1_type;
2012-04-30 08:30:47 +00:00
typedef RRange range2_type;
typedef sprout::adaptors::detail::adapted_range_default<
LRange,
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<
BinaryFunction,
2012-04-30 08:30:47 +00:00
typename sprout::container_traits<LRange>::iterator,
typename sprout::container_traits<RRange>::iterator
>
> base_type;
typedef typename base_type::iterator iterator;
public:
2014-01-13 15:18:06 +00:00
SPROUT_CONSTEXPR transformed_range() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
2012-05-22 12:16:16 +00:00
transformed_range(transformed_range const&) = default;
2012-12-02 09:16:10 +00:00
SPROUT_CONSTEXPR transformed_range(BinaryFunction func, range1_type& range1, range2_type& range2)
2012-04-30 08:30:47 +00:00
: base_type(
iterator(sprout::begin(range1), sprout::begin(range2), func),
iterator(sprout::end(range1), sprout::end(range2), func)
)
{}
};
template<typename UnaryFunction, typename Range>
class transformed_range<UnaryFunction, Range, void>
: public sprout::adaptors::detail::adapted_range_default<
Range,
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<
UnaryFunction,
2012-04-30 08:30:47 +00:00
typename sprout::container_traits<Range>::iterator
>
>
{
public:
typedef UnaryFunction functor_type;
typedef Range range1_type;
typedef sprout::adaptors::detail::adapted_range_default<
Range,
2012-04-30 08:30:47 +00:00
sprout::transform_iterator<
UnaryFunction,
2012-04-30 08:30:47 +00:00
typename sprout::container_traits<Range>::iterator
>
> base_type;
typedef typename base_type::iterator iterator;
public:
2014-01-13 15:18:06 +00:00
SPROUT_CONSTEXPR transformed_range() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
2012-05-22 12:16:16 +00:00
transformed_range(transformed_range const&) = default;
2012-12-02 09:16:10 +00:00
SPROUT_CONSTEXPR transformed_range(UnaryFunction func, range1_type& range)
2012-04-30 08:30:47 +00:00
: base_type(
iterator(sprout::begin(range), func),
iterator(sprout::end(range), func)
)
{}
};
//
// transform_holder
//
template<typename BinaryFunction, typename RRange = void>
class transform_holder {
public:
typedef BinaryFunction functor_type;
typedef RRange range2_type;
private:
2012-12-02 09:16:10 +00:00
BinaryFunction func_;
2012-04-30 08:30:47 +00:00
sprout::value_holder<range2_type&> range_;
public:
2014-01-13 15:18:06 +00:00
SPROUT_CONSTEXPR transform_holder() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
2012-04-30 08:30:47 +00:00
transform_holder(transform_holder const&) = default;
2012-12-02 09:16:10 +00:00
SPROUT_CONSTEXPR transform_holder(BinaryFunction func, range2_type& range)
2012-04-30 08:30:47 +00:00
: func_(func)
, range_(range)
{}
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 range2_type& range() const {
return range_;
}
};
template<typename UnaryFunction>
class transform_holder<UnaryFunction, void> {
public:
typedef UnaryFunction functor_type;
private:
2012-12-02 09:16:10 +00:00
UnaryFunction func_;
2012-04-30 08:30:47 +00:00
public:
2014-01-13 15:18:06 +00:00
SPROUT_CONSTEXPR transform_holder() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
2012-04-30 08:30:47 +00:00
transform_holder(transform_holder const&) = default;
2012-12-02 09:16:10 +00:00
explicit SPROUT_CONSTEXPR transform_holder(UnaryFunction func)
2012-04-30 08:30:47 +00:00
: func_(func)
{}
2012-12-02 09:16:10 +00:00
SPROUT_CONSTEXPR UnaryFunction functor() const {
2012-04-30 08:30:47 +00:00
return func_;
}
};
//
// transformed_forwarder
//
class transformed_forwarder {
public:
template<typename RRange, typename BinaryFunction>
2012-04-30 13:18:45 +00:00
SPROUT_CONSTEXPR sprout::adaptors::transform_holder<
2012-04-30 08:30:47 +00:00
BinaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<RRange>::type>::type
>
2013-02-19 17:23:20 +00:00
operator()(RRange&& range, BinaryFunction func) const {
2012-04-30 13:18:45 +00:00
return sprout::adaptors::transform_holder<
2012-04-30 08:30:47 +00:00
BinaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<RRange>::type>::type
>(
2012-04-30 08:30:47 +00:00
func,
sprout::lvalue_forward<RRange>(range)
);
}
template<typename UnaryFunction>
2012-04-30 13:18:45 +00:00
SPROUT_CONSTEXPR sprout::adaptors::transform_holder<UnaryFunction>
2013-02-19 17:23:20 +00:00
operator()(UnaryFunction func) const {
2012-04-30 13:18:45 +00:00
return sprout::adaptors::transform_holder<UnaryFunction>(func);
2012-04-30 08:30:47 +00:00
}
};
//
// transformed
//
namespace {
2012-12-17 14:10:23 +00:00
SPROUT_STATIC_CONSTEXPR sprout::adaptors::transformed_forwarder transformed = {};
2012-04-30 08:30:47 +00:00
} // anonymous-namespace
//
// operator|
//
template<typename LRange, typename BinaryFunction, typename RRange>
2012-04-30 13:18:45 +00:00
inline SPROUT_CONSTEXPR sprout::adaptors::transformed_range<
2012-04-30 08:30:47 +00:00
BinaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<LRange>::type>::type,
RRange
>
2012-04-30 13:18:45 +00:00
operator|(LRange&& lhs, sprout::adaptors::transform_holder<BinaryFunction, RRange> const& rhs) {
return sprout::adaptors::transformed_range<
2012-04-30 08:30:47 +00:00
BinaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<LRange>::type>::type,
RRange
>(
rhs.functor(),
sprout::lvalue_forward<LRange>(lhs),
rhs.range()
);
}
template<typename Range, typename UnaryFunction>
2012-04-30 13:18:45 +00:00
inline SPROUT_CONSTEXPR sprout::adaptors::transformed_range<
2012-04-30 08:30:47 +00:00
UnaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>
2012-04-30 13:18:45 +00:00
operator|(Range&& lhs, sprout::adaptors::transform_holder<UnaryFunction> const& rhs) {
return sprout::adaptors::transformed_range<
2012-04-30 08:30:47 +00:00
UnaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>(
rhs.functor(),
sprout::lvalue_forward<Range>(lhs)
);
}
2012-04-30 13:18:45 +00:00
} // namespace adaptors
2012-04-30 08:30:47 +00:00
//
// container_construct_traits
//
template<typename UnaryOrBinaryFunction, typename LRange, typename RRange>
struct container_construct_traits<sprout::adaptors::transformed_range<UnaryOrBinaryFunction, LRange, RRange> >
: public sprout::container_construct_traits<typename sprout::adaptors::transformed_range<UnaryOrBinaryFunction, LRange, RRange>::base_type>
{};
2012-04-30 08:30:47 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_RANGE_ADAPTOR_TRANSFORMD_HPP