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

View file

@ -2,6 +2,7 @@
#define SPROUT_RANGE_ADAPTOR_HPP #define SPROUT_RANGE_ADAPTOR_HPP
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/range/adaptor/copied.hpp>
#include <sprout/range/adaptor/transformed.hpp> #include <sprout/range/adaptor/transformed.hpp>
#include <sprout/range/adaptor/sinusoidal.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/type_traits/lvalue_reference.hpp>
#include <sprout/utility/forward.hpp> #include <sprout/utility/forward.hpp>
#include <sprout/utility/lvalue_forward.hpp> #include <sprout/utility/lvalue_forward.hpp>
#include <sprout/utility/value_holder.hpp>
namespace sprout { namespace sprout {
namespace range { namespace adaptors {
// //
// sinusoidal_range // sinusoidal_range
// //
@ -43,8 +42,8 @@ namespace sprout {
value_type const& amplitude = 1 value_type const& amplitude = 1
) )
: base_type( : base_type(
iterator(frequency, amplitude, 0), iterator(0, frequency, amplitude),
iterator(frequency, amplitude, sprout::size(range)) iterator(sprout::size(range), frequency, amplitude)
) )
{} {}
}; };
@ -70,8 +69,8 @@ namespace sprout {
value_type const& amplitude = 1 value_type const& amplitude = 1
) )
: base_type( : base_type(
iterator(frequency, amplitude, 0), iterator(0, frequency, amplitude),
iterator(frequency, amplitude, -1) iterator(-1, frequency, amplitude)
) )
{} {}
}; };
@ -82,13 +81,13 @@ namespace sprout {
class sinusoidal_forwarder { class sinusoidal_forwarder {
public: public:
template<typename Value> template<typename Value>
SPROUT_CONSTEXPR sprout::range::sinusoidal_range<Value> SPROUT_CONSTEXPR sprout::adaptors::sinusoidal_range<Value>
operator()( operator()(
Value const& frequency = 1, Value const& frequency = 1,
Value const& amplitude = 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 // sinusoidal
// //
namespace { namespace {
SPROUT_STATIC_CONSTEXPR sprout::range::sinusoidal_forwarder sinusoidal{}; SPROUT_STATIC_CONSTEXPR sprout::adaptors::sinusoidal_forwarder sinusoidal{};
} // anonymous-namespace } // anonymous-namespace
// //
// operator| // operator|
// //
template<typename Range, typename Value> template<typename Range, typename Value>
inline SPROUT_CONSTEXPR sprout::range::sinusoidal_range< inline SPROUT_CONSTEXPR sprout::adaptors::sinusoidal_range<
Value, Value,
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
> >
operator|(Range&& lhs, sprout::range::sinusoidal_range<Value> const& rhs) { operator|(Range&& lhs, sprout::adaptors::sinusoidal_range<Value> const& rhs) {
return sprout::range::sinusoidal_range< return sprout::adaptors::sinusoidal_range<
Value, Value,
typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type typename std::remove_reference<typename sprout::lvalue_reference<Range>::type>::type
>( >(
@ -117,12 +116,13 @@ namespace sprout {
rhs.amplitude() rhs.amplitude()
); );
} }
} // namespace range } // namespace adaptors
// //
// container_construct_traits // container_construct_traits
// //
template<typename Value, typename Range> 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: public:
typedef typename sprout::container_construct_traits<Range>::copied_type copied_type; typedef typename sprout::container_construct_traits<Range>::copied_type copied_type;
public: public:
@ -137,7 +137,7 @@ namespace sprout {
template<typename Cont, typename... Args> template<typename Cont, typename... Args>
static SPROUT_CONSTEXPR copied_type remake( static SPROUT_CONSTEXPR copied_type remake(
Cont&& cont, 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 Args&&... args
) )
{ {

View file

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