Sprout/sprout/compost/effects/compressed.hpp

136 lines
4 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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)
=============================================================================*/
2012-11-17 09:44:24 +00:00
#ifndef SPROUT_COMPOST_EFFECTS_COMPRESEDS_HPP
#define SPROUT_COMPOST_EFFECTS_COMPRESEDS_HPP
2012-11-09 13:33:11 +00:00
#include <sprout/config.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/range/adaptor/transformed.hpp>
namespace sprout {
namespace compost {
namespace detail {
template<typename Value, typename T>
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<typename Value, typename T = void>
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<typename Value>
struct compress_value<Value, void> {
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<typename T>
SPROUT_CONSTEXPR T
operator()(T const& x) const {
return sprout::compost::detail::compress_value(x, threshold_, ratio_, gain_);
}
};
namespace effects {
//
// compress_holder
//
template<typename T>
class compress_holder {
public:
typedef T value_type;
private:
value_type threshold_;
value_type ratio_;
public:
2014-01-13 15:18:06 +00:00
SPROUT_CONSTEXPR compress_holder() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
2012-11-09 13:33:11 +00:00
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<typename T>
SPROUT_CONSTEXPR sprout::compost::effects::compress_holder<T>
2013-02-19 17:23:20 +00:00
operator()(T const& threshold, T const& ratio) const {
2012-11-09 13:33:11 +00:00
return sprout::compost::effects::compress_holder<T>(threshold, ratio);
}
};
//
// compressed
//
namespace {
2012-12-17 14:10:23 +00:00
SPROUT_STATIC_CONSTEXPR sprout::compost::effects::compressed_forwarder compressed = {};
2012-11-09 13:33:11 +00:00
} // anonymous-namespace
//
// operator|
//
template<typename Range, typename T>
inline SPROUT_CONSTEXPR auto
operator|(Range&& lhs, sprout::compost::effects::compress_holder<T> const& rhs)
-> decltype(
SPROUT_FORWARD(Range, lhs)
2012-11-09 13:33:11 +00:00
| sprout::adaptors::transformed(sprout::compost::compress_value<T>(rhs.threshold(), rhs.ratio()))
)
{
return SPROUT_FORWARD(Range, lhs)
2012-11-09 13:33:11 +00:00
| sprout::adaptors::transformed(sprout::compost::compress_value<T>(rhs.threshold(), rhs.ratio()))
;
}
} // namespace effects
using sprout::compost::effects::compressed;
2012-11-09 13:33:11 +00:00
} // namespace compost
} // namespace sprout
2012-11-17 09:44:24 +00:00
#endif // #ifndef SPROUT_COMPOST_EFFECTS_COMPRESEDS_HPP