Skip to content

Commit

Permalink
Merge branch 'master' into fix-more-simple-clippy-warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
YeungOnion authored May 1, 2024
2 parents ee95157 + 4d94513 commit 0bbc5bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/distribution/empirical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ use rand::Rng;
use std::collections::BTreeMap;

#[derive(Clone, Debug, PartialEq)]
struct NonNAN<T>(T);
struct NonNan<T>(T);

impl<T: PartialEq> Eq for NonNAN<T> {}
impl<T: PartialEq> Eq for NonNan<T> {}

impl<T: PartialOrd> PartialOrd for NonNAN<T> {
impl<T: PartialOrd> PartialOrd for NonNan<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
Some(self.cmp(other))
}
}

impl<T: PartialOrd> Ord for NonNAN<T> {
impl<T: PartialOrd> Ord for NonNan<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
self.0.partial_cmp(&other.0).unwrap()
}
}

Expand All @@ -42,7 +42,7 @@ pub struct Empirical {
sum: f64,
mean_and_var: Option<(f64, f64)>,
// keys are data points, values are number of data points with equal value
data: BTreeMap<NonNAN<f64>, u64>,
data: BTreeMap<NonNan<f64>, u64>,
}

impl Empirical {
Expand Down Expand Up @@ -86,13 +86,13 @@ impl Empirical {
self.mean_and_var = Some((data_point, 0.));
}
}
*self.data.entry(NonNAN(data_point)).or_insert(0) += 1;
*self.data.entry(NonNan(data_point)).or_insert(0) += 1;
}
}
pub fn remove(&mut self, data_point: f64) {
if !data_point.is_nan() {
if let (Some(val), Some((mean, var))) =
(self.data.remove(&NonNAN(data_point)), self.mean_and_var)
(self.data.remove(&NonNan(data_point)), self.mean_and_var)
{
if val == 1 && self.data.is_empty() {
self.mean_and_var = None;
Expand All @@ -105,7 +105,7 @@ impl Empirical {
var - (self.sum - 1.) * (data_point - mean) * (data_point - mean) / self.sum;
self.sum -= 1.;
if val != 1 {
self.data.insert(NonNAN(data_point), val - 1);
self.data.insert(NonNan(data_point), val - 1);
};
self.mean_and_var = Some((mean, var));
}
Expand Down
15 changes: 15 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,18 @@ impl fmt::Display for StatsError {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn assert_sync<T: Sync>() {}
fn assert_send<T: Send>() {}

#[test]
fn test_sync_send() {
// Error types should implement Sync and Send
let _ = assert_sync::<StatsError>();
let _ = assert_send::<StatsError>();
}
}

0 comments on commit 0bbc5bb

Please sign in to comment.