add adaptor::copied

This commit is contained in:
bolero-MURAKAMI 2012-04-30 22:18:45 +09:00
parent cb830f141d
commit bafd1004dd
5 changed files with 183 additions and 42 deletions

View file

@ -33,40 +33,40 @@ namespace sprout {
typedef value_type* pointer;
typedef value_type reference;
private:
difference_type index_;
value_type frequency_;
value_type amplitude_;
difference_type index_;
value_type d_;
private:
explicit SPROUT_CONSTEXPR sinusoid_iterator(sinusoid_iterator const& other, difference_type index)
: frequency_(other.frequency_)
: index_(index)
, frequency_(other.frequency_)
, amplitude_(other.amplitude_)
, index_(index)
, d_(other.d_)
{}
public:
SPROUT_CONSTEXPR sinusoid_iterator()
: frequency_(1)
: index_()
, frequency_(1)
, amplitude_(1)
, index_()
, d_(value_type(2) * sprout::math::pi<value_type>())
{}
sinusoid_iterator(sinusoid_iterator const&) = default;
explicit SPROUT_CONSTEXPR sinusoid_iterator(
value_type const& frequency,
value_type const& amplitude = 1,
difference_type index = 0
difference_type index,
value_type const& frequency = 1,
value_type const& amplitude = 1
)
: frequency_(frequency)
: index_(index)
, frequency_(frequency)
, amplitude_(amplitude)
, index_(index)
, d_(value_type(2) * sprout::math::pi<value_type>() * frequency)
{}
template<typename U>
SPROUT_CONSTEXPR sinusoid_iterator(sinusoid_iterator<U> const& it)
: frequency_(it.frequency_)
: index_(it.index_)
, frequency_(it.frequency_)
, amplitude_(it.amplitude_)
, index_(it.index_)
, d_(it.d_)
{}
template<typename U>

View file

@ -2,6 +2,7 @@
#define SPROUT_RANGE_ADAPTOR_HPP
#include <sprout/config.hpp>
#include <sprout/range/adaptor/copied.hpp>
#include <sprout/range/adaptor/transformed.hpp>
#include <sprout/range/adaptor/sinusoidal.hpp>

View file

@ -0,0 +1,139 @@
#ifndef SPROUT_RANGE_ADAPTOR_COPIED_HPP
#define SPROUT_RANGE_ADAPTOR_COPIED_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/sinusoid_iterator.hpp>
#include <sprout/algorithm/fixed/result_of.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 {
namespace adaptors {
//
// copied_range
//
template<typename Range>
class copied_range
: public sprout::range::range_container<
typename sprout::container_traits<Range>::iterator
>
, public sprout::detail::inherit_if_fixed_size<Range>
{
public:
typedef Range range_type;
typedef sprout::range::range_container<
typename sprout::container_traits<Range>::iterator
> base_type;
typedef typename base_type::iterator iterator;
public:
copied_range() = default;
copied_range(copied_range const&) = default;
explicit SPROUT_CONSTEXPR copied_range(range_type& range)
: base_type(
sprout::begin(range),
sprout::end(range)
)
{}
template<typename Result>
SPROUT_CONSTEXPR operator Result() const {
return sprout::range::fixed::copy(*this, sprout::pit<Result>());
}
};
//
// copy_holder
//
template<typename Result>
class copy_holder {
public:
typedef Result result_type;
private:
sprout::value_holder<result_type&> result_;
public:
copy_holder() = default;
copy_holder(copy_holder const&) = default;
explicit SPROUT_CONSTEXPR copy_holder(result_type& result)
: result_(result)
{}
SPROUT_CONSTEXPR result_type result() const {
return result_;
}
};
//
// copied_forwarder
//
class copied_forwarder {
public:
template<typename Result>
SPROUT_CONSTEXPR typename sprout::adaptors::copy_holder<Result const>
operator()(Result const& result) {
return typename sprout::adaptors::copy_holder<Result const>(result);
}
};
//
// copied
//
namespace {
SPROUT_STATIC_CONSTEXPR sprout::adaptors::copied_forwarder copied{};
} // anonymous-namespace
//
// operator|
//
template<typename Range, typename Result>
SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<sprout::pit<Result> >::type
operator|(Range&& lhs, sprout::adaptors::copy_holder<Result> const& rhs) {
return sprout::range::fixed::copy(sprout::lvalue_forward<Range>(lhs), sprout::pit<Result>());
}
template<typename Range>
inline SPROUT_CONSTEXPR sprout::adaptors::copied_range<
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>
operator|(Range&& lhs, sprout::adaptors::copied_forwarder const& rhs) {
return sprout::adaptors::copied_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::copied_range<Range> > {
public:
typedef typename sprout::container_construct_traits<Range>::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,
typename sprout::container_traits<sprout::adaptors::copied_range<Range> >::difference_type size,
Args&&... args
)
{
return sprout::remake<copied_type>(sprout::forward<Cont>(cont), size, sprout::forward<Args>(args)...);
}
};
} // namespace sprout
#endif // #ifndef SPROUT_RANGE_ADAPTOR_COPIED_HPP

View file

@ -12,10 +12,9 @@
#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 {
namespace range {
namespace adaptors {
//
// sinusoidal_range
//
@ -43,8 +42,8 @@ namespace sprout {
value_type const& amplitude = 1
)
: base_type(
iterator(frequency, amplitude, 0),
iterator(frequency, amplitude, sprout::size(range))
iterator(0, frequency, amplitude),
iterator(sprout::size(range), frequency, amplitude)
)
{}
};
@ -70,8 +69,8 @@ namespace sprout {
value_type const& amplitude = 1
)
: base_type(
iterator(frequency, amplitude, 0),
iterator(frequency, amplitude, -1)
iterator(0, frequency, amplitude),
iterator(-1, frequency, amplitude)
)
{}
};
@ -82,13 +81,13 @@ namespace sprout {
class sinusoidal_forwarder {
public:
template<typename Value>
SPROUT_CONSTEXPR sprout::range::sinusoidal_range<Value>
SPROUT_CONSTEXPR sprout::adaptors::sinusoidal_range<Value>
operator()(
Value const& frequency = 1,
Value const& amplitude = 1
)
{
return sprout::range::sinusoidal_range<Value>(frequency, amplitude);
return sprout::adaptors::sinusoidal_range<Value>(frequency, amplitude);
}
};
@ -96,19 +95,19 @@ namespace sprout {
// sinusoidal
//
namespace {
SPROUT_STATIC_CONSTEXPR sprout::range::sinusoidal_forwarder sinusoidal{};
SPROUT_STATIC_CONSTEXPR sprout::adaptors::sinusoidal_forwarder sinusoidal{};
} // anonymous-namespace
//
// operator|
//
template<typename Range, typename Value>
inline SPROUT_CONSTEXPR sprout::range::sinusoidal_range<
inline SPROUT_CONSTEXPR sprout::adaptors::sinusoidal_range<
Value,
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>
operator|(Range&& lhs, sprout::range::sinusoidal_range<Value> const& rhs) {
return sprout::range::sinusoidal_range<
operator|(Range&& lhs, sprout::adaptors::sinusoidal_range<Value> const& rhs) {
return sprout::adaptors::sinusoidal_range<
Value,
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>(
@ -117,12 +116,13 @@ namespace sprout {
rhs.amplitude()
);
}
} // namespace range
} // namespace adaptors
//
// container_construct_traits
//
template<typename Value, typename Range>
struct container_construct_traits<sprout::range::sinusoidal_range<Value, Range> > {
struct container_construct_traits<sprout::adaptors::sinusoidal_range<Value, Range> > {
public:
typedef typename sprout::container_construct_traits<Range>::copied_type copied_type;
public:
@ -137,7 +137,7 @@ namespace sprout {
template<typename Cont, typename... Args>
static SPROUT_CONSTEXPR copied_type remake(
Cont&& cont,
typename sprout::container_traits<sprout::range::sinusoidal_range<Value, Range> >::difference_type size,
typename sprout::container_traits<sprout::adaptors::sinusoidal_range<Value, Range> >::difference_type size,
Args&&... args
)
{

View file

@ -15,7 +15,7 @@
#include <sprout/utility/value_holder.hpp>
namespace sprout {
namespace range {
namespace adaptors {
//
// transformed_range
//
@ -133,12 +133,12 @@ namespace sprout {
class transformed_forwarder {
public:
template<typename RRange, typename BinaryFunction>
SPROUT_CONSTEXPR sprout::range::transform_holder<
SPROUT_CONSTEXPR sprout::adaptors::transform_holder<
BinaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<RRange>::type>::type
>
operator()(RRange&& range, BinaryFunction func) {
return sprout::range::transform_holder<
return sprout::adaptors::transform_holder<
BinaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<RRange>::type>::type
> (
@ -147,9 +147,9 @@ namespace sprout {
);
}
template<typename UnaryFunction>
SPROUT_CONSTEXPR sprout::range::transform_holder<UnaryFunction>
SPROUT_CONSTEXPR sprout::adaptors::transform_holder<UnaryFunction>
operator()(UnaryFunction func) {
return sprout::range::transform_holder<UnaryFunction>(func);
return sprout::adaptors::transform_holder<UnaryFunction>(func);
}
};
@ -157,20 +157,20 @@ namespace sprout {
// transformed
//
namespace {
SPROUT_STATIC_CONSTEXPR sprout::range::transformed_forwarder transformed{};
SPROUT_STATIC_CONSTEXPR sprout::adaptors::transformed_forwarder transformed{};
} // anonymous-namespace
//
// operator|
//
template<typename LRange, typename BinaryFunction, typename RRange>
inline SPROUT_CONSTEXPR sprout::range::transformed_range<
inline SPROUT_CONSTEXPR sprout::adaptors::transformed_range<
BinaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<LRange>::type>::type,
RRange
>
operator|(LRange&& lhs, sprout::range::transform_holder<BinaryFunction, RRange> const& rhs) {
return sprout::range::transformed_range<
operator|(LRange&& lhs, sprout::adaptors::transform_holder<BinaryFunction, RRange> const& rhs) {
return sprout::adaptors::transformed_range<
BinaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<LRange>::type>::type,
RRange
@ -181,12 +181,12 @@ namespace sprout {
);
}
template<typename Range, typename UnaryFunction>
inline SPROUT_CONSTEXPR sprout::range::transformed_range<
inline SPROUT_CONSTEXPR sprout::adaptors::transformed_range<
UnaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>
operator|(Range&& lhs, sprout::range::transform_holder<UnaryFunction> const& rhs) {
return sprout::range::transformed_range<
operator|(Range&& lhs, sprout::adaptors::transform_holder<UnaryFunction> const& rhs) {
return sprout::adaptors::transformed_range<
UnaryFunction,
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>(
@ -194,12 +194,13 @@ namespace sprout {
sprout::lvalue_forward<Range>(lhs)
);
}
} // namespace range
} // namespace adaptors
//
// container_construct_traits
//
template<typename UnaryOrBinaryFunction, typename LRange, typename RRange>
struct container_construct_traits<sprout::range::transformed_range<UnaryOrBinaryFunction, LRange, RRange> > {
struct container_construct_traits<sprout::adaptors::transformed_range<UnaryOrBinaryFunction, LRange, RRange> > {
public:
typedef typename sprout::container_construct_traits<LRange>::copied_type copied_type;
public:
@ -214,7 +215,7 @@ namespace sprout {
template<typename Cont, typename... Args>
static SPROUT_CONSTEXPR copied_type remake(
Cont&& cont,
typename sprout::container_traits<sprout::range::transformed_range<UnaryOrBinaryFunction, LRange, RRange> >::difference_type size,
typename sprout::container_traits<sprout::adaptors::transformed_range<UnaryOrBinaryFunction, LRange, RRange> >::difference_type size,
Args&&... args
)
{