use std::sync::Arc;
use super::locale::validate_token;
use crate::{Error, Result};
use num_bigint::BigInt;
pub(crate) mod selection;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NumericSelection<T> {
PerValue,
WholeSet,
Fixed(T),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NumericPromotion {
KeepThreshold,
PromoteOnRounding,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SiPrefix(i8);
const SI: &[(i8, &str)] = &[
(-30, "q"),
(-27, "r"),
(-24, "y"),
(-21, "z"),
(-18, "a"),
(-15, "f"),
(-12, "p"),
(-9, "n"),
(-6, "ยต"),
(-3, "m"),
(-2, "c"),
(-1, "d"),
(0, ""),
(1, "da"),
(2, "h"),
(3, "k"),
(6, "M"),
(9, "G"),
(12, "T"),
(15, "P"),
(18, "E"),
(21, "Z"),
(24, "Y"),
(27, "R"),
(30, "Q"),
];
impl SiPrefix {
pub fn from_exponent(exponent: i8) -> Result<Self> {
if SI.iter().any(|(e, _)| *e == exponent) {
Ok(Self(exponent))
} else {
Err(Error::Data("unsupported SI prefix exponent".into()))
}
}
pub const fn exponent(&self) -> i8 {
self.0
}
pub fn symbol(&self) -> &str {
SI.iter()
.find(|(e, _)| *e == self.0)
.expect("validated SI prefix")
.1
}
}
#[derive(Debug, Clone)]
pub enum NumericFactor {
AutomaticDecimal,
Value(f64),
PowerOfTen(String),
}
impl PartialEq for NumericFactor {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::AutomaticDecimal, Self::AutomaticDecimal) => true,
(Self::Value(a), Self::Value(b)) => a.to_bits() == b.to_bits(),
(Self::PowerOfTen(a), Self::PowerOfTen(b)) => a == b,
_ => false,
}
}
}
impl Eq for NumericFactor {}
impl NumericFactor {
pub(super) fn try_copy(&self) -> Result<Self> {
Ok(match self {
Self::AutomaticDecimal => Self::AutomaticDecimal,
Self::Value(value) => Self::Value(*value),
Self::PowerOfTen(value) => {
Self::PowerOfTen(crate::data::source::copy_source_string(value)?)
}
})
}
fn observe(&self, round: &mut crate::layout::kernel::FeedbackRound) -> Result<()> {
match self {
Self::AutomaticDecimal => round.observe_count(0),
Self::Value(value) => {
round.observe_count(1)?;
round.observe_number(*value)
}
Self::PowerOfTen(value) => {
round.observe_count(2)?;
round.observe_string(value)
}
}
}
fn normalize(self) -> Result<Self> {
match self {
Self::Value(value) if !value.is_finite() || value == 0.0 => Err(Error::Data(
"numeric divisor must be nonzero and finite".into(),
)),
Self::PowerOfTen(source) => {
let digits = source.strip_prefix(['+', '-']).unwrap_or(&source);
if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) {
return Err(Error::Data(
"decimal factor exponent must be a signed ASCII integer".into(),
));
}
let exponent = BigInt::parse_bytes(source.as_bytes(), 10)
.ok_or_else(|| Error::Data("invalid decimal factor exponent".into()))?;
Ok(Self::PowerOfTen(exponent.to_string()))
}
_ => Ok(self),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NumericCompactRule(Arc<CompactRule>);
#[derive(Debug, PartialEq, Eq)]
struct CompactRule {
threshold: u64,
divisor: u64,
prefix: String,
suffix: String,
}
impl NumericCompactRule {
pub fn new(
threshold: f64,
divisor: f64,
prefix: impl Into<String>,
suffix: impl Into<String>,
) -> Result<Self> {
if !threshold.is_finite() || !divisor.is_finite() || divisor <= 0.0 {
return Err(Error::Data(
"compact thresholds must be finite and divisors positive finite".into(),
));
}
let (prefix, suffix) = (prefix.into(), suffix.into());
validate_token(&prefix, true)?;
validate_token(&suffix, true)?;
Ok(Self(Arc::new(CompactRule {
threshold: if threshold == 0.0 {
0
} else {
threshold.to_bits()
},
divisor: divisor.to_bits(),
prefix,
suffix,
})))
}
pub fn threshold(&self) -> f64 {
f64::from_bits(self.0.threshold)
}
pub fn divisor(&self) -> f64 {
f64::from_bits(self.0.divisor)
}
pub fn prefix(&self) -> &str {
&self.0.prefix
}
pub fn suffix(&self) -> &str {
&self.0.suffix
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum PrefixMode {
None,
Si(NumericSelection<SiPrefix>, NumericPromotion),
Compact(
Arc<Vec<NumericCompactRule>>,
NumericSelection<usize>,
NumericPromotion,
),
Factor(NumericFactor),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NumericPrefixes(pub(super) Arc<PrefixMode>);
impl NumericPrefixes {
pub(super) fn observe(&self, round: &mut crate::layout::kernel::FeedbackRound) -> Result<()> {
match self.0.as_ref() {
PrefixMode::None => round.observe_count(0),
PrefixMode::Si(selection, promotion) => {
round.observe_count(1)?;
observe_selection(
*selection,
|prefix| usize::from(prefix.0.unsigned_abs()) * 2 + usize::from(prefix.0 < 0),
round,
)?;
round.observe_count(*promotion as usize)
}
PrefixMode::Compact(rules, selection, promotion) => {
round.observe_count(2)?;
observe_selection(*selection, |index| index, round)?;
round.observe_count(*promotion as usize)?;
round.observe_count(rules.len())?;
for rule in rules.iter() {
round.observe_number(rule.threshold())?;
round.observe_number(rule.divisor())?;
round.observe_string(rule.prefix())?;
round.observe_string(rule.suffix())?;
}
Ok(())
}
PrefixMode::Factor(factor) => {
round.observe_count(3)?;
factor.observe(round)
}
}
}
pub fn none() -> Self {
Self(Arc::new(PrefixMode::None))
}
pub fn si(selection: NumericSelection<SiPrefix>, promotion: NumericPromotion) -> Result<Self> {
fixed_promotion(matches!(selection, NumericSelection::Fixed(_)), promotion)?;
Ok(Self(Arc::new(PrefixMode::Si(selection, promotion))))
}
pub fn decimal_compact(
selection: NumericSelection<usize>,
promotion: NumericPromotion,
) -> Result<Self> {
let rules = crate::data::source::collect_source(
[(1e3, "K"), (1e6, "M"), (1e9, "B"), (1e12, "T")]
.into_iter()
.map(|(threshold, suffix)| {
NumericCompactRule::new(threshold, threshold, "", suffix)
}),
)?;
Self::compact(rules, selection, promotion)
}
pub fn compact(
rules: Vec<NumericCompactRule>,
selection: NumericSelection<usize>,
promotion: NumericPromotion,
) -> Result<Self> {
fixed_promotion(matches!(selection, NumericSelection::Fixed(_)), promotion)?;
if rules
.windows(2)
.any(|pair| pair[0].threshold() >= pair[1].threshold())
{
return Err(Error::Data(
"compact thresholds must be strictly increasing".into(),
));
}
if let NumericSelection::Fixed(index) = selection {
if index >= rules.len() {
return Err(Error::Data(
"fixed compact rule index is out of range".into(),
));
}
}
if rules.is_empty() {
Ok(Self::none())
} else {
Ok(Self(Arc::new(PrefixMode::Compact(
Arc::new(rules),
selection,
promotion,
))))
}
}
pub fn factor(value: NumericFactor) -> Result<Self> {
Ok(Self(Arc::new(PrefixMode::Factor(value.normalize()?))))
}
}
fn observe_selection<T>(
selection: NumericSelection<T>,
index: impl FnOnce(T) -> usize,
round: &mut crate::layout::kernel::FeedbackRound,
) -> Result<()> {
match selection {
NumericSelection::PerValue => round.observe_count(0),
NumericSelection::WholeSet => round.observe_count(1),
NumericSelection::Fixed(value) => {
round.observe_count(2)?;
round.observe_count(index(value))
}
}
}
fn fixed_promotion(fixed: bool, promotion: NumericPromotion) -> Result<()> {
if fixed && promotion != NumericPromotion::KeepThreshold {
Err(Error::Data(
"fixed prefix selection requires KeepThreshold".into(),
))
} else {
Ok(())
}
}