use std::sync::Arc;
use parking_lot::RwLock;
use sketches_ddsketch::DDSketch;
#[derive(Default, Clone)]
pub struct Histogram(Arc<RwLock<DDSketch>>);
impl std::fmt::Debug for Histogram {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Histogram").finish_non_exhaustive()
}
}
impl Histogram {
pub(crate) fn new() -> Self {
Self(Default::default())
}
pub fn update(&self, value: f64) {
self.0.write().add(value);
}
#[expect(clippy::expect_used)]
pub fn quantile(&self, quantile: f64) -> Option<f64> {
assert!(
(0.0..=1.0).contains(&quantile),
"quantile must be between 0.0 and 1.0"
);
self.0
.read()
.quantile(quantile)
.expect("quantile range checked")
}
pub fn total(&self) -> f64 {
self.0.read().sum().unwrap_or_default()
}
pub fn count(&self) -> usize {
self.0.read().count()
}
pub fn is_empty(&self) -> bool {
self.0.read().count() == 0
}
}