From 0a4c8bef81532ec5252e7576ee879d0d92e40cf6 Mon Sep 17 00:00:00 2001 From: atomflunder <80397293+atomflunder@users.noreply.github.com> Date: Sat, 8 Jun 2024 19:04:02 +0200 Subject: [PATCH] Fix some clippy lints Mainly just using usizes --- src/trueskill/factor_graph.rs | 22 +++++++++++----------- src/trueskill/mod.rs | 22 ++++++++++++---------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/trueskill/factor_graph.rs b/src/trueskill/factor_graph.rs index 8d9ac76..9ba3521 100644 --- a/src/trueskill/factor_graph.rs +++ b/src/trueskill/factor_graph.rs @@ -7,7 +7,7 @@ use super::gaussian::Gaussian; #[derive(Clone, Debug, Default, PartialEq)] pub struct Variable { pub gaussian: Gaussian, - messages: HashMap, + messages: HashMap, } impl Variable { @@ -27,7 +27,7 @@ impl Variable { delta } - fn update_message(&mut self, factor_id: u32, message: Gaussian) -> f64 { + fn update_message(&mut self, factor_id: usize, message: Gaussian) -> f64 { let old_message = self.messages[&factor_id]; let v = self.messages.entry(factor_id).or_default(); *v = message; @@ -35,7 +35,7 @@ impl Variable { self.set(self.gaussian / old_message * message) } - fn update_value(&mut self, factor_id: u32, val: Gaussian) -> f64 { + fn update_value(&mut self, factor_id: usize, val: Gaussian) -> f64 { let old_message = self.messages[&factor_id]; let v = self.messages.entry(factor_id).or_default(); *v = val * old_message / self.gaussian; @@ -54,14 +54,14 @@ impl Variable { } pub struct PriorFactor { - id: u32, + id: usize, pub variable: Rc>, val: Gaussian, dynamic: f64, } impl PriorFactor { - pub fn new(id: u32, variable: Rc>, val: Gaussian, dynamic: f64) -> Self { + pub fn new(id: usize, variable: Rc>, val: Gaussian, dynamic: f64) -> Self { variable.borrow_mut().messages.entry(id).or_default(); Self { @@ -80,7 +80,7 @@ impl PriorFactor { } pub struct LikelihoodFactor { - id: u32, + id: usize, mean: Rc>, value: Rc>, variance: f64, @@ -88,7 +88,7 @@ pub struct LikelihoodFactor { impl LikelihoodFactor { pub fn new( - id: u32, + id: usize, mean: Rc>, value: Rc>, variance: f64, @@ -132,7 +132,7 @@ impl LikelihoodFactor { } pub struct SumFactor { - id: u32, + id: usize, sum: Rc>, terms: Vec>>, coeffs: Vec, @@ -140,7 +140,7 @@ pub struct SumFactor { impl SumFactor { pub fn new( - id: u32, + id: usize, sum: Rc>, terms: Vec>>, coeffs: Vec, @@ -228,7 +228,7 @@ impl SumFactor { } pub struct TruncateFactor { - id: u32, + id: usize, variable: Rc>, v_func: Box f64>, w_func: Box f64>, @@ -237,7 +237,7 @@ pub struct TruncateFactor { impl TruncateFactor { pub fn new( - id: u32, + id: usize, variable: Rc>, v_func: Box f64>, w_func: Box f64>, diff --git a/src/trueskill/mod.rs b/src/trueskill/mod.rs index ac66cdb..dd8a05a 100644 --- a/src/trueskill/mod.rs +++ b/src/trueskill/mod.rs @@ -1596,16 +1596,16 @@ fn run_schedule( draw_probability: f64, min_delta: f64, ) -> Vec { - assert!(!(min_delta <= 0.0), "min_delta must be greater than 0"); + assert!((min_delta > 0.0), "min_delta must be greater than 0"); let mut id = 0; let mut rating_layer = build_rating_layer(rating_vars, flattened_ratings, tau, id); - id += >::try_into(rating_layer.len()).unwrap(); + id += rating_layer.len(); let mut perf_layer = build_perf_layer(rating_vars, perf_vars, beta, id); - id += >::try_into(perf_layer.len()).unwrap(); + id += perf_layer.len(); let mut team_perf_layer = build_team_perf_layer(team_perf_vars, perf_vars, team_sizes, id); - id += >::try_into(team_perf_layer.len()).unwrap(); + id += team_perf_layer.len(); for factor in &mut rating_layer { factor.down(); @@ -1619,7 +1619,8 @@ fn run_schedule( let mut team_diff_layer = build_team_diff_layer(team_diff_vars, team_perf_vars, id); let team_diff_len = team_diff_layer.len(); - id += >::try_into(team_diff_len).unwrap(); + id += team_diff_len; + let mut trunc_layer = build_trunc_layer( team_diff_vars, sorted_teams_and_ranks, @@ -1669,7 +1670,7 @@ fn build_rating_layer( rating_vars: &[Rc>], flattened_ratings: &[TrueSkillRating], tau: f64, - starting_id: u32, + starting_id: usize, ) -> Vec { let mut v = Vec::with_capacity(rating_vars.len()); let mut i = starting_id; @@ -1690,7 +1691,7 @@ fn build_perf_layer( rating_vars: &[Rc>], perf_vars: &[Rc>], beta: f64, - starting_id: u32, + starting_id: usize, ) -> Vec { let beta_sq = beta.powi(2); let mut v = Vec::with_capacity(rating_vars.len()); @@ -1712,7 +1713,7 @@ fn build_team_perf_layer( team_perf_vars: &[Rc>], perf_vars: &[Rc>], team_sizes: &[usize], - starting_id: u32, + starting_id: usize, ) -> Vec { let mut v = Vec::with_capacity(team_perf_vars.len()); let mut i = starting_id; @@ -1738,7 +1739,7 @@ fn build_team_perf_layer( fn build_team_diff_layer( team_diff_vars: &[Rc>], team_perf_vars: &[Rc>], - starting_id: u32, + starting_id: usize, ) -> Vec { let mut v = Vec::with_capacity(team_diff_vars.len()); let mut i = starting_id; @@ -1760,7 +1761,7 @@ fn build_trunc_layer( sorted_teams_and_ranks: &[(&[TrueSkillRating], MultiTeamOutcome)], draw_probability: f64, beta: f64, - starting_id: u32, + starting_id: usize, ) -> Vec { fn v_w(diff: f64, draw_margin: f64) -> f64 { let x = diff - draw_margin; @@ -1797,6 +1798,7 @@ fn build_trunc_layer( panic!("floating point error"); } + #[allow(clippy::suboptimal_flops)] fn w_d(diff: f64, draw_margin: f64) -> f64 { let abs_diff = diff.abs(); let a = draw_margin - abs_diff;