From bafd1004dd071d660605bc5f29367bea7b9c9b90 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Mon, 30 Apr 2012 22:18:45 +0900 Subject: [PATCH] add adaptor::copied --- sprout/iterator/sinusoid_iterator.hpp | 24 ++--- sprout/range/adaptor.hpp | 1 + sprout/range/adaptor/copied.hpp | 139 ++++++++++++++++++++++++++ sprout/range/adaptor/sinusoidal.hpp | 30 +++--- sprout/range/adaptor/transformed.hpp | 31 +++--- 5 files changed, 183 insertions(+), 42 deletions(-) create mode 100644 sprout/range/adaptor/copied.hpp diff --git a/sprout/iterator/sinusoid_iterator.hpp b/sprout/iterator/sinusoid_iterator.hpp index 806632d4..aff9171c 100644 --- a/sprout/iterator/sinusoid_iterator.hpp +++ b/sprout/iterator/sinusoid_iterator.hpp @@ -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()) {} 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() * frequency) {} template SPROUT_CONSTEXPR sinusoid_iterator(sinusoid_iterator const& it) - : frequency_(it.frequency_) + : index_(it.index_) + , frequency_(it.frequency_) , amplitude_(it.amplitude_) - , index_(it.index_) , d_(it.d_) {} template diff --git a/sprout/range/adaptor.hpp b/sprout/range/adaptor.hpp index fa9fc805..7108e00c 100644 --- a/sprout/range/adaptor.hpp +++ b/sprout/range/adaptor.hpp @@ -2,6 +2,7 @@ #define SPROUT_RANGE_ADAPTOR_HPP #include +#include #include #include diff --git a/sprout/range/adaptor/copied.hpp b/sprout/range/adaptor/copied.hpp new file mode 100644 index 00000000..f79b96e6 --- /dev/null +++ b/sprout/range/adaptor/copied.hpp @@ -0,0 +1,139 @@ +#ifndef SPROUT_RANGE_ADAPTOR_COPIED_HPP +#define SPROUT_RANGE_ADAPTOR_COPIED_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace adaptors { + // + // copied_range + // + template + class copied_range + : public sprout::range::range_container< + typename sprout::container_traits::iterator + > + , public sprout::detail::inherit_if_fixed_size + { + public: + typedef Range range_type; + typedef sprout::range::range_container< + typename sprout::container_traits::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 + SPROUT_CONSTEXPR operator Result() const { + return sprout::range::fixed::copy(*this, sprout::pit()); + } + }; + + // + // copy_holder + // + template + class copy_holder { + public: + typedef Result result_type; + private: + sprout::value_holder 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 + SPROUT_CONSTEXPR typename sprout::adaptors::copy_holder + operator()(Result const& result) { + return typename sprout::adaptors::copy_holder(result); + } + }; + + // + // copied + // + namespace { + SPROUT_STATIC_CONSTEXPR sprout::adaptors::copied_forwarder copied{}; + } // anonymous-namespace + + // + // operator| + // + template + SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm >::type + operator|(Range&& lhs, sprout::adaptors::copy_holder const& rhs) { + return sprout::range::fixed::copy(sprout::lvalue_forward(lhs), sprout::pit()); + } + template + inline SPROUT_CONSTEXPR sprout::adaptors::copied_range< + typename std::remove_reference::type>::type + > + operator|(Range&& lhs, sprout::adaptors::copied_forwarder const& rhs) { + return sprout::adaptors::copied_range< + typename std::remove_reference::type>::type + >( + sprout::lvalue_forward(lhs) + ); + } + } // namespace adaptors + + // + // container_construct_traits + // + template + struct container_construct_traits > { + public: + typedef typename sprout::container_construct_traits::copied_type copied_type; + public: + template + static SPROUT_CONSTEXPR copied_type deep_copy(Cont&& cont) { + return sprout::range::fixed::copy(sprout::forward(cont), sprout::pit()); + } + template + static SPROUT_CONSTEXPR copied_type make(Args&&... args) { + return sprout::make(sprout::forward(args)...); + } + template + static SPROUT_CONSTEXPR copied_type remake( + Cont&& cont, + typename sprout::container_traits >::difference_type size, + Args&&... args + ) + { + return sprout::remake(sprout::forward(cont), size, sprout::forward(args)...); + } + }; +} // namespace sprout + +#endif // #ifndef SPROUT_RANGE_ADAPTOR_COPIED_HPP diff --git a/sprout/range/adaptor/sinusoidal.hpp b/sprout/range/adaptor/sinusoidal.hpp index 249cf592..d8ec2f0f 100644 --- a/sprout/range/adaptor/sinusoidal.hpp +++ b/sprout/range/adaptor/sinusoidal.hpp @@ -12,10 +12,9 @@ #include #include #include -#include 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 - SPROUT_CONSTEXPR sprout::range::sinusoidal_range + SPROUT_CONSTEXPR sprout::adaptors::sinusoidal_range operator()( Value const& frequency = 1, Value const& amplitude = 1 ) { - return sprout::range::sinusoidal_range(frequency, amplitude); + return sprout::adaptors::sinusoidal_range(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 - inline SPROUT_CONSTEXPR sprout::range::sinusoidal_range< + inline SPROUT_CONSTEXPR sprout::adaptors::sinusoidal_range< Value, typename std::remove_reference::type>::type > - operator|(Range&& lhs, sprout::range::sinusoidal_range const& rhs) { - return sprout::range::sinusoidal_range< + operator|(Range&& lhs, sprout::adaptors::sinusoidal_range const& rhs) { + return sprout::adaptors::sinusoidal_range< Value, typename std::remove_reference::type>::type >( @@ -117,12 +116,13 @@ namespace sprout { rhs.amplitude() ); } - } // namespace range + } // namespace adaptors + // // container_construct_traits // template - struct container_construct_traits > { + struct container_construct_traits > { public: typedef typename sprout::container_construct_traits::copied_type copied_type; public: @@ -137,7 +137,7 @@ namespace sprout { template static SPROUT_CONSTEXPR copied_type remake( Cont&& cont, - typename sprout::container_traits >::difference_type size, + typename sprout::container_traits >::difference_type size, Args&&... args ) { diff --git a/sprout/range/adaptor/transformed.hpp b/sprout/range/adaptor/transformed.hpp index 42ea8abd..bbafc7df 100644 --- a/sprout/range/adaptor/transformed.hpp +++ b/sprout/range/adaptor/transformed.hpp @@ -15,7 +15,7 @@ #include namespace sprout { - namespace range { + namespace adaptors { // // transformed_range // @@ -133,12 +133,12 @@ namespace sprout { class transformed_forwarder { public: template - SPROUT_CONSTEXPR sprout::range::transform_holder< + SPROUT_CONSTEXPR sprout::adaptors::transform_holder< BinaryFunction, typename std::remove_reference::type>::type > operator()(RRange&& range, BinaryFunction func) { - return sprout::range::transform_holder< + return sprout::adaptors::transform_holder< BinaryFunction, typename std::remove_reference::type>::type > ( @@ -147,9 +147,9 @@ namespace sprout { ); } template - SPROUT_CONSTEXPR sprout::range::transform_holder + SPROUT_CONSTEXPR sprout::adaptors::transform_holder operator()(UnaryFunction func) { - return sprout::range::transform_holder(func); + return sprout::adaptors::transform_holder(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 - inline SPROUT_CONSTEXPR sprout::range::transformed_range< + inline SPROUT_CONSTEXPR sprout::adaptors::transformed_range< BinaryFunction, typename std::remove_reference::type>::type, RRange > - operator|(LRange&& lhs, sprout::range::transform_holder const& rhs) { - return sprout::range::transformed_range< + operator|(LRange&& lhs, sprout::adaptors::transform_holder const& rhs) { + return sprout::adaptors::transformed_range< BinaryFunction, typename std::remove_reference::type>::type, RRange @@ -181,12 +181,12 @@ namespace sprout { ); } template - inline SPROUT_CONSTEXPR sprout::range::transformed_range< + inline SPROUT_CONSTEXPR sprout::adaptors::transformed_range< UnaryFunction, typename std::remove_reference::type>::type > - operator|(Range&& lhs, sprout::range::transform_holder const& rhs) { - return sprout::range::transformed_range< + operator|(Range&& lhs, sprout::adaptors::transform_holder const& rhs) { + return sprout::adaptors::transformed_range< UnaryFunction, typename std::remove_reference::type>::type >( @@ -194,12 +194,13 @@ namespace sprout { sprout::lvalue_forward(lhs) ); } - } // namespace range + } // namespace adaptors + // // container_construct_traits // template - struct container_construct_traits > { + struct container_construct_traits > { public: typedef typename sprout::container_construct_traits::copied_type copied_type; public: @@ -214,7 +215,7 @@ namespace sprout { template static SPROUT_CONSTEXPR copied_type remake( Cont&& cont, - typename sprout::container_traits >::difference_type size, + typename sprout::container_traits >::difference_type size, Args&&... args ) {