/*============================================================================= Copyright (c) 2011-2017 Bolero MURAKAMI 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) =============================================================================*/ #ifndef SPROUT_COMPOST_EFFECTS_COMPRESEDS_HPP #define SPROUT_COMPOST_EFFECTS_COMPRESEDS_HPP #include #include #include namespace sprout { namespace compost { namespace detail { template SPROUT_CONSTEXPR T compress_value(T const& x, Value const& threshold, Value const& ratio, Value const& gain) { return (x > threshold ? threshold + (x - threshold) * ratio : x < -threshold ? -threshold + (x + threshold) * ratio : x ) * gain ; } } // namespace detail // // compress_value // template struct compress_value { public: typedef Value value_type; typedef T argument_type; typedef T result_type; private: value_type threshold_; value_type ratio_; value_type gain_; public: SPROUT_CONSTEXPR compress_value(Value const& threshold, Value const& ratio) : threshold_(threshold), ratio_(ratio), gain_(1 / (threshold + (1 - threshold) * ratio)) {} SPROUT_CONSTEXPR result_type operator()(T const& x) const { return sprout::compost::detail::compress_value(x, threshold_, ratio_, gain_); } }; template struct compress_value { public: typedef Value value_type; private: value_type threshold_; value_type ratio_; value_type gain_; public: SPROUT_CONSTEXPR compress_value(Value const& threshold, Value const& ratio) : threshold_(threshold), ratio_(ratio), gain_(1 / (threshold + (1 - threshold) * ratio)) {} template SPROUT_CONSTEXPR T operator()(T const& x) const { return sprout::compost::detail::compress_value(x, threshold_, ratio_, gain_); } }; namespace effects { // // compress_holder // template class compress_holder { public: typedef T value_type; private: value_type threshold_; value_type ratio_; public: SPROUT_CONSTEXPR compress_holder() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL compress_holder(compress_holder const&) = default; SPROUT_CONSTEXPR compress_holder(value_type const& threshold, value_type const& ratio) : threshold_(threshold), ratio_(ratio) {} SPROUT_CONSTEXPR value_type const& threshold() const { return threshold_; } SPROUT_CONSTEXPR value_type const& ratio() const { return ratio_; } }; // // compressed_forwarder // class compressed_forwarder { public: template SPROUT_CONSTEXPR sprout::compost::effects::compress_holder operator()(T const& threshold, T const& ratio) const { return sprout::compost::effects::compress_holder(threshold, ratio); } }; // // compressed // namespace { SPROUT_STATIC_CONSTEXPR sprout::compost::effects::compressed_forwarder compressed = {}; } // anonymous-namespace // // operator| // template inline SPROUT_CONSTEXPR auto operator|(Range&& lhs, sprout::compost::effects::compress_holder const& rhs) -> decltype( SPROUT_FORWARD(Range, lhs) | sprout::adaptors::transformed(sprout::compost::compress_value(rhs.threshold(), rhs.ratio())) ) { return SPROUT_FORWARD(Range, lhs) | sprout::adaptors::transformed(sprout::compost::compress_value(rhs.threshold(), rhs.ratio())) ; } } // namespace effects using sprout::compost::effects::compressed; } // namespace compost } // namespace sprout #endif // #ifndef SPROUT_COMPOST_EFFECTS_COMPRESEDS_HPP