use crate::{Error, Result};
pub(super) const MOST_BOUNDS: usize = 128;
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Buckets {
Bounds(Vec<f64>),
Exponential,
}
impl From<&[f64]> for Buckets {
fn from(bounds: &[f64]) -> Buckets {
Buckets::Bounds(bounds.to_vec())
}
}
impl<const N: usize> From<&[f64; N]> for Buckets {
fn from(bounds: &[f64; N]) -> Buckets {
Buckets::Bounds(bounds.to_vec())
}
}
impl From<Vec<f64>> for Buckets {
fn from(bounds: Vec<f64>) -> Buckets {
Buckets::Bounds(bounds)
}
}
#[derive(Clone, Debug, PartialEq)]
pub(super) enum Bounds {
Given(Vec<f64>),
Exponential,
}
impl Bounds {
pub(super) fn of(buckets: &Buckets) -> Result<Bounds> {
match buckets {
Buckets::Bounds(given) => Bounds::given(given),
Buckets::Exponential => Ok(Bounds::Exponential),
}
}
fn given(given: &[f64]) -> Result<Bounds> {
let bounds: Vec<f64> = given.iter().copied().filter(|bound| *bound != f64::INFINITY).collect();
if bounds.is_empty() || bounds.len() > MOST_BOUNDS {
return Err(Error::invalid(format!("buckets: 1 to {MOST_BOUNDS} bounds, `+Inf` added by the store")));
}
if let Some(bound) = bounds.iter().find(|bound| !bound.is_finite() || **bound < 0.0) {
return Err(Error::invalid(format!("buckets: a bound of {bound}, where a bound is a number of 0 or more")));
}
if bounds.windows(2).any(|pair| pair[1] <= pair[0]) {
return Err(Error::invalid("buckets: bounds in ascending order, each once"));
}
Ok(Bounds::Given(bounds))
}
pub(super) fn buckets(&self) -> usize {
match self {
Bounds::Given(bounds) => bounds.len() + 1,
Bounds::Exponential => 0,
}
}
pub(super) fn labels(&self) -> Vec<String> {
match self {
Bounds::Given(bounds) => bounds.iter().map(|bound| le(*bound)).chain(["+Inf".to_owned()]).collect(),
Bounds::Exponential => Vec::new(),
}
}
pub(super) fn text(&self) -> String {
match self {
Bounds::Given(bounds) => {
format!("[{}]", bounds.iter().map(|bound| le(*bound)).collect::<Vec<_>>().join(","))
}
Bounds::Exponential => "exponential".to_owned(),
}
}
}
pub(super) fn bucket_of(bounds: &[f64], value: f64) -> usize {
bounds.partition_point(|bound| *bound < value)
}
pub(super) fn bound_of(text: &str) -> Option<f64> {
let bound = text.parse::<f64>().ok()?;
(bound >= 0.0).then_some(bound + 0.0)
}
pub fn exponential_buckets(start: f64, factor: f64, count: usize) -> Vec<f64> {
std::iter::successors(Some(start), |bound| Some(bound * factor)).take(count).collect()
}
pub(super) fn le(bound: f64) -> String {
if bound == f64::INFINITY {
return "+Inf".to_owned();
}
if bound == 0.0 {
return "0".to_owned();
}
let scientific = format!("{bound:e}");
let (mantissa, exponent) = scientific.split_once('e').unwrap_or((&scientific, "0"));
let digits: String = mantissa.chars().filter(char::is_ascii_digit).collect();
let point = exponent.parse::<i32>().unwrap_or(0) + 1;
let count = digits.len() as i32;
match point {
point if count <= point && point <= 21 => digits + &"0".repeat((point - count) as usize),
point if 0 < point && point <= 21 => format!("{}.{}", &digits[..point as usize], &digits[point as usize..]),
point if -6 < point && point <= 0 => format!("0.{}{digits}", "0".repeat(point.unsigned_abs() as usize)),
point => {
let power = point - 1;
let sign = if power < 0 { '-' } else { '+' };
let (first, rest) = digits.split_at(1);
let fraction = if rest.is_empty() { String::new() } else { format!(".{rest}") };
format!("{first}{fraction}e{sign}{}", power.unsigned_abs())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_bound_is_written_as_javascript_writes_the_number() {
let cases = [
(5.0, "5"),
(0.25, "0.25"),
(2.5, "2.5"),
(1000.0, "1000"),
(0.1, "0.1"),
(123_456_789.125, "123456789.125"),
(1e20, "100000000000000000000"),
(1e21, "1e+21"),
(1.5e21, "1.5e+21"),
(0.000_001, "0.000001"),
(1e-7, "1e-7"),
(1.5e-7, "1.5e-7"),
(-0.0, "0"),
(f64::INFINITY, "+Inf"),
];
for (bound, text) in cases {
assert_eq!(le(bound), text, "{bound:e}");
if bound.is_finite() {
assert_eq!(text.parse::<f64>().unwrap(), bound, "it reads back");
}
}
}
#[test]
fn bounds_ascend_from_zero_and_the_store_adds_infinity() {
let given = |bounds: &[f64]| Bounds::of(&Buckets::from(bounds));
let bounds = given(&[5.0, 10.0, 25.0]).unwrap();
assert_eq!(bounds.labels(), ["5", "10", "25", "+Inf"]);
assert_eq!(bounds.text(), "[5,10,25]");
let at = |value: f64| bucket_of(&[5.0, 10.0, 25.0], value);
assert_eq!((at(0.0), at(5.0), at(5.5), at(25.0), at(26.0)), (0, 0, 1, 2, 3));
assert_eq!(given(&[1.0, f64::INFINITY]).unwrap().labels(), ["1", "+Inf"], "an infinity given is the store's");
for refused in [&[][..], &[3.0, 2.0], &[1.0, 1.0], &[-1.0, 1.0], &[f64::NAN], &[f64::INFINITY]] {
assert!(given(refused).is_err(), "{refused:?}");
}
assert!(given(&exponential_buckets(1.0, 2.0, MOST_BOUNDS + 1)).is_err());
assert_eq!(exponential_buckets(1.0, 2.0, 14).last(), Some(&8192.0));
let grid = Bounds::of(&Buckets::Exponential).unwrap();
assert_eq!((grid.buckets(), grid.labels(), grid.text()), (0, Vec::<String>::new(), "exponential".to_owned()));
}
#[test]
fn a_bound_reads_however_a_program_spelt_it() {
let cases = [("5", Some(5.0)), ("5.0", Some(5.0)), ("+Inf", Some(f64::INFINITY)), ("-0", Some(0.0))];
for (text, bound) in cases {
assert_eq!(bound_of(text), bound, "{text}");
}
assert_eq!(bound_of("-0").map(f64::to_bits), Some(0), "the bound 0, not -0");
for refused in ["", "five", "-1", "NaN", "-Inf", "5 "] {
assert_eq!(bound_of(refused), None, "{refused}");
}
}
}