| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#; |
| 2 | |
| 3 | use craterust-analyzer cargo histogram 1.5.0 crate/::{Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#, Errorrust-analyzer cargo histogram 1.5.0 errors/Error#}; |
| 4 | |
| 5 | /// A validated quantile value in the inclusive range `0.0..=1.0`. |
| 6 | /// |
| 7 | /// This newtype ensures that only valid quantile values (finite, non-NaN, |
| 8 | /// within `[0.0, 1.0]`) are used as keys in [`QuantilesResult`]. Because |
| 9 | /// NaN and infinity are excluded by construction, `Quantile` safely |
| 10 | /// implements [`Eq`] and [`Ord`]. |
| 11 | /// |
| 12 | /// # Examples |
| 13 | /// |
| 14 | /// ``` |
| 15 | /// use histogram::Quantile; |
| 16 | /// |
| 17 | /// let q = Quantile::new(0.99).unwrap(); |
| 18 | /// assert_eq!(q.as_f64(), 0.99); |
| 19 | /// |
| 20 | /// // Invalid values are rejected |
| 21 | /// assert!(Quantile::new(1.5).is_err()); |
| 22 | /// assert!(Quantile::new(f64::NAN).is_err()); |
| 23 | /// ``` |
| 24 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 25 | pub struct Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#(f64); |
| 26 | |
| 27 | impl Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile# { |
| 28 | /// Create a new `Quantile` from a value in `0.0..=1.0`. |
| 29 | /// |
| 30 | /// Returns [`Error::InvalidQuantile`] if the value is outside the |
| 31 | /// valid range or is NaN/infinite. |
| 32 | pub fn newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(value: f64) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile], Errorrust-analyzer cargo histogram 1.5.0 errors/Error#> { |
| 33 | if (0.0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#1.0).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]contains().(&value) { |
| 34 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile](value)) |
| 35 | } else { |
| 36 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo histogram 1.5.0 errors/Error#::InvalidQuantilerust-analyzer cargo histogram 1.5.0 errors/Error#InvalidQuantile#) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Returns the underlying `f64` value. |
| 41 | pub fn as_f64rust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]as_f64().(self) -> f64 { |
| 42 | self.0rust-analyzer cargo histogram 1.5.0 quantile/Quantile#0. |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<f64> for Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile# { |
| 47 | type Errorrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][`TryFrom<f64>`]Error# = Errorrust-analyzer cargo histogram 1.5.0 errors/Error#; |
| 48 | |
| 49 | fn try_fromrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][`TryFrom<f64>`]try_from().(value: f64) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][`TryFrom<f64>`], Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][`TryFrom<f64>`]::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#> { |
| 50 | Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][`TryFrom<f64>`]::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(value) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# for Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile# { |
| 55 | fn eqrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][`PartialEq<Self>`]eq().(&self, other: &Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][`PartialEq<Self>`]) -> bool { |
| 56 | self.0rust-analyzer cargo histogram 1.5.0 quantile/Quantile#0..to_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]to_bits().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().other.0rust-analyzer cargo histogram 1.5.0 quantile/Quantile#0..to_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]to_bits().() |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# for Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile# {} |
| 61 | |
| 62 | impl PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd# for Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile# { |
| 63 | fn partial_cmprust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][`PartialOrd<Self>`]partial_cmp().(&self, other: &Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][`PartialOrd<Self>`]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#> { |
| 64 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(self.cmprust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][Ord]cmp().(other)) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# for Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile# { |
| 69 | fn cmprust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][Ord]cmp().(&self, other: &Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][Ord]) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering# { |
| 70 | self.0rust-analyzer cargo histogram 1.5.0 quantile/Quantile#0..partial_cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]partial_cmp().(&other.0rust-analyzer cargo histogram 1.5.0 quantile/Quantile#0.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile# { |
| 75 | fn fmtrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 76 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}", self.0rust-analyzer cargo histogram 1.5.0 quantile/Quantile#0.) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// The result of a quantile query on a histogram. |
| 81 | /// |
| 82 | /// Contains the requested quantile-to-bucket mappings, the total sample |
| 83 | /// count, and the min/max buckets (the first and last non-zero buckets, |
| 84 | /// representing p0 and p100 respectively). |
| 85 | /// |
| 86 | /// Entries are stored in a [`BTreeMap`] keyed by [`Quantile`], providing |
| 87 | /// sorted iteration and efficient keyed lookups. |
| 88 | /// |
| 89 | /// # Examples |
| 90 | /// |
| 91 | /// ``` |
| 92 | /// use histogram::{Histogram, SampleQuantiles, Quantile}; |
| 93 | /// |
| 94 | /// let mut h = Histogram::new(7, 64).unwrap(); |
| 95 | /// for v in 1..=100 { |
| 96 | /// h.increment(v).unwrap(); |
| 97 | /// } |
| 98 | /// |
| 99 | /// let result = h.quantiles(&[0.5, 0.99]).unwrap().unwrap(); |
| 100 | /// assert_eq!(result.entries().len(), 2); |
| 101 | /// assert!(result.total_count() > 0); |
| 102 | /// |
| 103 | /// // Keyed lookup |
| 104 | /// let q99 = Quantile::new(0.99).unwrap(); |
| 105 | /// let bucket = result.get(&q99).unwrap(); |
| 106 | /// println!("p99: {}-{}", bucket.start(), bucket.end()); |
| 107 | /// ``` |
| 108 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 109 | pub struct QuantilesResultrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult# { |
| 110 | entriesrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#entries.: BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#, Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#>, |
| 111 | total_countrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#total_count.: u128, |
| 112 | minrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#min.: Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#, |
| 113 | maxrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#max.: Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#, |
| 114 | } |
| 115 | |
| 116 | impl QuantilesResultrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult# { |
| 117 | /// Creates a new `QuantilesResult`. |
| 118 | pub(craterust-analyzer cargo histogram 1.5.0 crate/) fn newrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]new().( |
| 119 | entries: BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#, Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#>, |
| 120 | total_count: u128, |
| 121 | min: Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#, |
| 122 | max: Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#, |
| 123 | ) -> Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult] { |
| 124 | Selfrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult] { |
| 125 | entriesrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#entries., |
| 126 | total_countrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#total_count., |
| 127 | minrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#min., |
| 128 | maxrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#max., |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// Returns the quantile-to-bucket mappings, sorted by quantile. |
| 133 | pub fn entriesrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]entries().(&self) -> &BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#, Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#> { |
| 134 | &self.entriesrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#entries. |
| 135 | } |
| 136 | |
| 137 | /// Returns the quantile-to-bucket mappings, sorted by quantile. |
| 138 | /// |
| 139 | /// Consumes self, returning the mapping by value instead of reference. |
| 140 | pub fn into_entriesrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]into_entries().(self) -> BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#, Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#> { |
| 141 | self.entriesrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#entries. |
| 142 | } |
| 143 | |
| 144 | /// Look up the bucket for a specific quantile. |
| 145 | pub fn getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&self, quantile: &Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#> { |
| 146 | self.entriesrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#entries..getrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]get().(quantile) |
| 147 | } |
| 148 | |
| 149 | /// Returns the total number of observations across all buckets. |
| 150 | pub fn total_countrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]total_count().(&self) -> u128 { |
| 151 | self.total_countrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#total_count. |
| 152 | } |
| 153 | |
| 154 | /// Returns the minimum bucket (first non-zero bucket, i.e. p0). |
| 155 | pub fn minrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]min().(&self) -> &Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket# { |
| 156 | &self.minrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#min. |
| 157 | } |
| 158 | |
| 159 | /// Returns the maximum bucket (last non-zero bucket, i.e. p100). |
| 160 | pub fn maxrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]max().(&self) -> &Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket# { |
| 161 | &self.maxrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#max. |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// Trait for computing quantiles from histogram data. |
| 166 | /// |
| 167 | /// Implemented for [`crate::Histogram`] and [`crate::SparseHistogram`]. |
| 168 | /// [`crate::AtomicHistogram`] does not implement this trait — use |
| 169 | /// [`AtomicHistogram::load()`](crate::AtomicHistogram::load) to get a |
| 170 | /// `Histogram` snapshot first. |
| 171 | /// |
| 172 | /// # Examples |
| 173 | /// |
| 174 | /// ``` |
| 175 | /// use histogram::{Histogram, SparseHistogram, SampleQuantiles}; |
| 176 | /// |
| 177 | /// let mut h = Histogram::new(7, 64).unwrap(); |
| 178 | /// h.increment(100).unwrap(); |
| 179 | /// |
| 180 | /// // Works on both Histogram and SparseHistogram |
| 181 | /// fn show_p99(h: &impl SampleQuantiles) { |
| 182 | /// if let Ok(Some(result)) = h.quantile(0.99) { |
| 183 | /// let bucket = result.entries().values().next().unwrap(); |
| 184 | /// println!("p99: {}-{}", bucket.start(), bucket.end()); |
| 185 | /// } |
| 186 | /// } |
| 187 | /// |
| 188 | /// show_p99(&h); |
| 189 | /// show_p99(&SparseHistogram::from(&h)); |
| 190 | /// ``` |
| 191 | pub trait SampleQuantilesrust-analyzer cargo histogram 1.5.0 quantile/SampleQuantiles# { |
| 192 | /// Compute quantiles for the given values. |
| 193 | /// |
| 194 | /// Each value in `quantiles` must be in `0.0..=1.0`. Returns |
| 195 | /// `Err(Error::InvalidQuantile)` if any value is out of range. |
| 196 | /// Returns `Ok(None)` if the histogram is empty. |
| 197 | fn quantilesrust-analyzer cargo histogram 1.5.0 quantile/SampleQuantiles#quantiles().(&self, quantiles: &[f64]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<QuantilesResultrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#>, Errorrust-analyzer cargo histogram 1.5.0 errors/Error#>; |
| 198 | |
| 199 | /// Compute a single quantile. Convenience wrapper around [`quantiles`](Self::quantiles). |
| 200 | fn quantilerust-analyzer cargo histogram 1.5.0 quantile/SampleQuantiles#quantile().(&self, quantile: f64) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<QuantilesResultrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#>, Errorrust-analyzer cargo histogram 1.5.0 errors/Error#> { |
| 201 | self.quantilesrust-analyzer cargo histogram 1.5.0 quantile/SampleQuantiles#quantiles().(&[quantile]) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | #[cfg(test)] |
| 206 | mod testsrust-analyzer cargo histogram 1.5.0 quantile/tests/ { |
| 207 | use superrust-analyzer cargo histogram 1.5.0 quantile/::*; |
| 208 | use craterust-analyzer cargo histogram 1.5.0 crate/::{Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#, SparseHistogramrust-analyzer cargo histogram 1.5.0 sparse/SparseHistogram#}; |
| 209 | |
| 210 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 211 | fn quantile_validationrust-analyzer cargo histogram 1.5.0 quantile/tests/quantile_validation().() { |
| 212 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.0).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 213 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.5).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 214 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(1.0).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 215 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().0.1).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 216 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(1.1).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 217 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(f64::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]NAN.).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 218 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(f64::INFINITYrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]INFINITY.).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 219 | } |
| 220 | |
| 221 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 222 | fn quantile_orderingrust-analyzer cargo histogram 1.5.0 quantile/tests/quantile_ordering().() { |
| 223 | let q1 = Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.5).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 224 | let q2 = Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.99).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 225 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(q1 <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). q2); |
| 226 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(q1, Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.5).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 227 | } |
| 228 | |
| 229 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 230 | fn empty_histogram_returns_nonerust-analyzer cargo histogram 1.5.0 quantile/tests/empty_histogram_returns_none().() { |
| 231 | let h = Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#::newrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]new().(7, 64).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 232 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(h.quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(&[0.5, 0.99]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 233 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(h.quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(0.5).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 234 | |
| 235 | let s = SparseHistogramrust-analyzer cargo histogram 1.5.0 sparse/SparseHistogram#::fromrust-analyzer cargo histogram 1.5.0 sparse/impl#[SparseHistogram][`From<&Histogram>`]from().(&h); |
| 236 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(s.quantilesrust-analyzer cargo histogram 1.5.0 sparse/impl#[SparseHistogram]quantiles().(&[0.5, 0.99]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 237 | } |
| 238 | |
| 239 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 240 | fn invalid_quantile_returns_errorrust-analyzer cargo histogram 1.5.0 quantile/tests/invalid_quantile_returns_error().() { |
| 241 | let h = Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#::newrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]new().(7, 64).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 242 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(h.quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(&[1.5]), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo histogram 1.5.0 errors/Error#::InvalidQuantilerust-analyzer cargo histogram 1.5.0 errors/Error#InvalidQuantile#)); |
| 243 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(h.quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(&[-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().0.1]), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo histogram 1.5.0 errors/Error#::InvalidQuantilerust-analyzer cargo histogram 1.5.0 errors/Error#InvalidQuantile#)); |
| 244 | } |
| 245 | |
| 246 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 247 | fn basic_quantilesrust-analyzer cargo histogram 1.5.0 quantile/tests/basic_quantiles().() { |
| 248 | let mut h = Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#::newrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]new().(7, 64).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 249 | for v in 0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#100 { |
| 250 | h.incrementrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]increment().(v).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 251 | } |
| 252 | |
| 253 | let result = h.quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(&[0.5, 0.9, 0.99]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 254 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.entriesrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]entries().().lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]len().(), 3); |
| 255 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.total_countrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]total_count().(), 101); |
| 256 | |
| 257 | // min bucket should contain 0 |
| 258 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.minrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]min().().startrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]start().(), 0); |
| 259 | // max bucket should contain 100 |
| 260 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.maxrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]max().().endrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]end().(), 100); |
| 261 | |
| 262 | // p50 should be around 50 |
| 263 | let q50 = Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.5).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 264 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&q50).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().endrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]end().(), 50); |
| 265 | |
| 266 | // p99 should be around 99 |
| 267 | let q99 = Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.99).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 268 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&q99).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().endrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]end().(), 99); |
| 269 | } |
| 270 | |
| 271 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 272 | fn single_samplerust-analyzer cargo histogram 1.5.0 quantile/tests/single_sample().() { |
| 273 | let mut h = Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#::newrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]new().(7, 64).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 274 | h.incrementrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]increment().(42).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 275 | |
| 276 | let result = h.quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(0.5).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 277 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.total_countrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]total_count().(), 1); |
| 278 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.minrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]min().().endrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]end().(), 42); |
| 279 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.maxrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]max().().endrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]end().(), 42); |
| 280 | } |
| 281 | |
| 282 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 283 | fn histogram_and_sparse_agreerust-analyzer cargo histogram 1.5.0 quantile/tests/histogram_and_sparse_agree().() { |
| 284 | let mut h = Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#::newrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]new().(4, 10).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 285 | for v in 1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#1024 { |
| 286 | h.incrementrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]increment().(v).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 287 | } |
| 288 | let s = SparseHistogramrust-analyzer cargo histogram 1.5.0 sparse/SparseHistogram#::fromrust-analyzer cargo histogram 1.5.0 sparse/impl#[SparseHistogram][`From<&Histogram>`]from().(&h); |
| 289 | |
| 290 | let quantiles = &[0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99, 0.999]; |
| 291 | |
| 292 | let hr = h.quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(quantiles).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 293 | let sr = s.quantilesrust-analyzer cargo histogram 1.5.0 sparse/impl#[SparseHistogram]quantiles().(quantiles).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 294 | |
| 295 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hr.total_countrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]total_count().(), sr.total_countrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]total_count().()); |
| 296 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hr.minrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]min().().rangerust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]range().(), sr.minrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]min().().rangerust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]range().()); |
| 297 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hr.maxrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]max().().rangerust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]range().(), sr.maxrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]max().().rangerust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]range().()); |
| 298 | |
| 299 | for (hq, sq) in hr.entriesrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]entries().().iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(sr.entriesrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]entries().().iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]iter().()) { |
| 300 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hq.0, sq.0); |
| 301 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hq.1.rangerust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]range().(), sq.1.rangerust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]range().()); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 306 | fn duplicate_quantiles_dedupedrust-analyzer cargo histogram 1.5.0 quantile/tests/duplicate_quantiles_deduped().() { |
| 307 | let mut h = Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#::newrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]new().(7, 64).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 308 | h.incrementrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]increment().(50).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 309 | |
| 310 | let result = h.quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(&[0.5, 0.5, 0.99, 0.99]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 311 | // Duplicates are deduped in the BTreeMap |
| 312 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.entriesrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]entries().().lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]len().(), 2); |
| 313 | } |
| 314 | } |