Sprout/sprout/range/adaptor/transformed.hpp

230 lines
7.4 KiB
C++
Raw Normal View History

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/pit.hpp>
#include <sprout/container/traits.hpp>
#include <sprout/container/functions.hpp>
#include <sprout/iterator/transform_iterator.hpp>
#include <sprout/range/range_container.hpp>
#include <sprout/range/algorithm/copy.hpp>
#include <sprout/type_traits/lvalue_reference.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/lvalue_forward.hpp>
#include <sprout/utility/value_holder.hpp>
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::range::range_container<
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 sprout::detail::container_nosy_static_size<LRange>
, public sprout::detail::container_nosy_fixed_size<LRange>
2012-04-30 08:30:47 +00:00
{
public:
typedef BinaryFunction functor_type;
typedef LRange range_type;
typedef RRange range2_type;
typedef sprout::range::range_container<
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;
typedef typename base_type::size_type size_type;
public:
SPROUT_CONSTEXPR transformed_range() = default;
SPROUT_CONSTEXPR transformed_range(transformed_range const&) = default;
2012-04-30 08:30:47 +00:00
SPROUT_CONSTEXPR transformed_range(functor_type func, range_type& range1, range2_type& range2)
: 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::range::range_container<
sprout::transform_iterator<
UnaryFunction,
2012-04-30 08:30:47 +00:00
typename sprout::container_traits<Range>::iterator
>
>
, public sprout::detail::container_nosy_static_size<Range>
, public sprout::detail::container_nosy_fixed_size<Range>
2012-04-30 08:30:47 +00:00
{
public:
typedef UnaryFunction functor_type;
typedef Range range_type;
typedef sprout::range::range_container<
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:
SPROUT_CONSTEXPR transformed_range() = default;
SPROUT_CONSTEXPR transformed_range(transformed_range const&) = default;
2012-04-30 08:30:47 +00:00
SPROUT_CONSTEXPR transformed_range(functor_type func, range_type& range)
: 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:
functor_type func_;
sprout::value_holder<range2_type&> range_;
public:
transform_holder() = default;
transform_holder(transform_holder const&) = default;
SPROUT_CONSTEXPR transform_holder(functor_type func, range2_type& range)
: func_(func)
, range_(range)
{}
SPROUT_CONSTEXPR functor_type functor() const {
return func_;
}
SPROUT_CONSTEXPR range2_type& range() const {
return range_;
}
};
template<typename UnaryFunction>
class transform_holder<UnaryFunction, void> {
public:
typedef UnaryFunction functor_type;
private:
functor_type func_;
public:
transform_holder() = default;
transform_holder(transform_holder const&) = default;
explicit SPROUT_CONSTEXPR transform_holder(functor_type func)
: func_(func)
{}
SPROUT_CONSTEXPR functor_type functor() const {
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
>
operator()(RRange&& range, BinaryFunction func) {
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
> (
func,
sprout::lvalue_forward<RRange>(range)
);
}
template<typename UnaryFunction>
2012-04-30 13:18:45 +00:00
SPROUT_CONSTEXPR sprout::adaptors::transform_holder<UnaryFunction>
2012-04-30 08:30:47 +00:00
operator()(UnaryFunction func) {
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-04-30 13:18:45 +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>
2012-04-30 13:18:45 +00:00
struct container_construct_traits<sprout::adaptors::transformed_range<UnaryOrBinaryFunction, LRange, RRange> > {
2012-04-30 08:30:47 +00:00
public:
typedef typename sprout::container_construct_traits<LRange>::copied_type copied_type;
public:
template<typename Cont>
static SPROUT_CONSTEXPR copied_type deep_copy(Cont&& cont) {
return sprout::range::fixed::copy(sprout::forward<Cont>(cont), sprout::pit<copied_type>());
}
template<typename... Args>
static SPROUT_CONSTEXPR copied_type make(Args&&... args) {
return sprout::make<copied_type>(sprout::forward<Args>(args)...);
}
template<typename Cont, typename... Args>
static SPROUT_CONSTEXPR copied_type remake(
Cont&& cont,
2012-04-30 13:18:45 +00:00
typename sprout::container_traits<sprout::adaptors::transformed_range<UnaryOrBinaryFunction, LRange, RRange> >::difference_type size,
2012-04-30 08:30:47 +00:00
Args&&... args
)
{
return sprout::remake<copied_type>(sprout::forward<Cont>(cont), size, sprout::forward<Args>(args)...);
}
};
} // namespace sprout
#endif // #ifndef SPROUT_RANGE_ADAPTOR_TRANSFORMD_HPP