From 2775f78e8eba9ef2f4981578466c16d10cd6d496 Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Tue, 6 Mar 2018 19:31:09 +0100 Subject: [PATCH] Expose finding of bin for given sample --- src/histogram.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/histogram.rs b/src/histogram.rs index 4e0fe47..19233e7 100644 --- a/src/histogram.rs +++ b/src/histogram.rs @@ -79,25 +79,37 @@ macro_rules! define_histogram { }) } + /// Find the index of the bin corresponding to the given sample. + /// + /// Fails if the sample is out of range of the histogram. + #[inline] + pub fn find(&self, x: f64) -> Result { + // We made sure our ranges are valid at construction, so we can + // safely unwrap. + match self.range.binary_search_by(|p| p.partial_cmp(&x).unwrap()) { + Ok(i) if i < LEN => { + Ok(i) + }, + Err(i) if i > 0 && i < LEN + 1 => { + Ok(i - 1) + }, + _ => { + Err(()) + }, + } + } + /// Add a sample to the histogram. /// /// Fails if the sample is out of range of the histogram. #[inline] pub fn add(&mut self, x: f64) -> Result<(), ()> { - // We made sure our ranges are valid at construction, so we can - // safely unwrap. - match self.range.binary_search_by(|p| p.partial_cmp(&x).unwrap()) { - Ok(i) if i < LEN => { - self.bin[i] += 1; - }, - Err(i) if i > 0 && i < LEN + 1 => { - self.bin[i - 1] += 1; - }, - _ => { - return Err(()); - }, + if let Ok(i) = self.find(x) { + self.bin[i] += 1; + Ok(()) + } else { + Err(()) } - Ok(()) } /// Return the ranges of the histogram.