pub struct Distribution { /* private fields */ }Expand description
Owns a sorted snapshot of observed values and answers arbitrary quantile queries.
Construction sorts the input once; queries are O(1) index lookups.
Input is f64 throughout — Duration callers convert to milliseconds at the call site.
Implementations§
Source§impl Distribution
impl Distribution
Sourcepub fn from_unsorted(values: Vec<f64>) -> Self
pub fn from_unsorted(values: Vec<f64>) -> Self
Sorts values and takes ownership. O(n log n).
Use from_sorted when values are already sorted to skip the sort step.
Sourcepub fn from_sorted(values: Vec<f64>) -> Self
pub fn from_sorted(values: Vec<f64>) -> Self
Constructs a Distribution from an already-sorted Vec<f64>.
Caller is responsible for ensuring the input is sorted in ascending order. No sort is performed; construction is O(1).
Sourcepub fn quantile(&self, p: f64) -> f64
pub fn quantile(&self, p: f64) -> f64
Returns the value at quantile p in [0.0, 1.0] using the floor-index formula.
idx = (n as f64 * p).floor() as usize, clamped to [0, n-1].
Returns 0.0 for an empty distribution.
This formula is equivalent to the integer formula n * p / 100 (for integer
percentile values 0–100) used in the ASCII table renderer, ensuring both
output paths report identical percentile values.
Sourcepub fn value_at(&self, idx: usize) -> f64
pub fn value_at(&self, idx: usize) -> f64
Returns the value at a pre-computed index into the sorted backing array.
Use this when the caller has already computed the index via the integer formula
(n * p / 100).min(n - 1) and wants to avoid the redundant float round-trip
through quantile(idx as f64 / n as f64).
§Panics
Panics if idx >= self.len(). The caller is responsible for clamping the index
to [0, n-1] before calling this method.