#include "ratio.hpp" #include #include namespace ratio { bool Profile::valid() const { if (raw_low < -raw_limit || raw_low > raw_limit) { return false; } if (raw_high < -raw_limit || raw_high > raw_limit) { return false; } if (raw_low >= raw_high) { return false; } if (out_low < -output_limit || out_low > output_limit) { return false; } if (out_high < -output_limit || out_high > output_limit) { return false; } return true; } std::int64_t rounded_ratio(std::int64_t numerator, std::int64_t denominator) { const long double quotient = static_cast(numerator / denominator); return static_cast(std::round(quotient)); } Mapped map_sample(const Profile& profile, std::int64_t raw) { Mapped result; const std::int64_t bounded = std::max(profile.raw_low, std::min(profile.raw_high, raw)); const std::int64_t width = profile.raw_high - profile.raw_low; const std::int64_t displacement = bounded - profile.raw_low; const std::int64_t rise = profile.out_high - profile.out_low; const std::int64_t numerator = profile.out_low * width + displacement * rise; result.value = rounded_ratio(numerator, width); result.clipped = bounded != raw; return result; } void Statistics::clear() { samples = 0; weight = 0; raw_total = 0; mapped_total = 0; minimum = 0; maximum = 0; clipped = 0; } void Statistics::add(std::int64_t raw, const Mapped& value, std::uint64_t amount) { if (samples == 0) { minimum = value.value; maximum = value.value; } else { minimum = std::min(minimum, value.value); maximum = std::max(maximum, value.value); } ++samples; weight += amount; raw_total += raw * static_cast(amount); mapped_total += value.value * static_cast(amount); if (value.clipped) { ++clipped; } } std::int64_t Statistics::raw_mean() const { if (weight == 0) { return 0; } return rounded_ratio(raw_total, static_cast(weight)); } std::int64_t Statistics::mapped_mean() const { if (weight == 0) { return 0; } return rounded_ratio(mapped_total, static_cast(weight)); } }