Skip to content

Commit

Permalink
Fix some clippy lints
Browse files Browse the repository at this point in the history
Mainly just using usizes
  • Loading branch information
atomflunder committed Jun 8, 2024
1 parent 0f58d05 commit 0a4c8be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
22 changes: 11 additions & 11 deletions src/trueskill/factor_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::gaussian::Gaussian;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Variable {
pub gaussian: Gaussian,
messages: HashMap<u32, Gaussian>,
messages: HashMap<usize, Gaussian>,
}

impl Variable {
Expand All @@ -27,15 +27,15 @@ 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;

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;
Expand All @@ -54,14 +54,14 @@ impl Variable {
}

pub struct PriorFactor {
id: u32,
id: usize,
pub variable: Rc<RefCell<Variable>>,
val: Gaussian,
dynamic: f64,
}

impl PriorFactor {
pub fn new(id: u32, variable: Rc<RefCell<Variable>>, val: Gaussian, dynamic: f64) -> Self {
pub fn new(id: usize, variable: Rc<RefCell<Variable>>, val: Gaussian, dynamic: f64) -> Self {
variable.borrow_mut().messages.entry(id).or_default();

Self {
Expand All @@ -80,15 +80,15 @@ impl PriorFactor {
}

pub struct LikelihoodFactor {
id: u32,
id: usize,
mean: Rc<RefCell<Variable>>,
value: Rc<RefCell<Variable>>,
variance: f64,
}

impl LikelihoodFactor {
pub fn new(
id: u32,
id: usize,
mean: Rc<RefCell<Variable>>,
value: Rc<RefCell<Variable>>,
variance: f64,
Expand Down Expand Up @@ -132,15 +132,15 @@ impl LikelihoodFactor {
}

pub struct SumFactor {
id: u32,
id: usize,
sum: Rc<RefCell<Variable>>,
terms: Vec<Rc<RefCell<Variable>>>,
coeffs: Vec<f64>,
}

impl SumFactor {
pub fn new(
id: u32,
id: usize,
sum: Rc<RefCell<Variable>>,
terms: Vec<Rc<RefCell<Variable>>>,
coeffs: Vec<f64>,
Expand Down Expand Up @@ -228,7 +228,7 @@ impl SumFactor {
}

pub struct TruncateFactor {
id: u32,
id: usize,
variable: Rc<RefCell<Variable>>,
v_func: Box<dyn Fn(f64, f64) -> f64>,
w_func: Box<dyn Fn(f64, f64) -> f64>,
Expand All @@ -237,7 +237,7 @@ pub struct TruncateFactor {

impl TruncateFactor {
pub fn new(
id: u32,
id: usize,
variable: Rc<RefCell<Variable>>,
v_func: Box<dyn Fn(f64, f64) -> f64>,
w_func: Box<dyn Fn(f64, f64) -> f64>,
Expand Down
22 changes: 12 additions & 10 deletions src/trueskill/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,16 +1596,16 @@ fn run_schedule(
draw_probability: f64,
min_delta: f64,
) -> Vec<PriorFactor> {
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 += <usize as TryInto<u32>>::try_into(rating_layer.len()).unwrap();
id += rating_layer.len();
let mut perf_layer = build_perf_layer(rating_vars, perf_vars, beta, id);
id += <usize as TryInto<u32>>::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 += <usize as TryInto<u32>>::try_into(team_perf_layer.len()).unwrap();
id += team_perf_layer.len();

for factor in &mut rating_layer {
factor.down();
Expand All @@ -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 += <usize as TryInto<u32>>::try_into(team_diff_len).unwrap();
id += team_diff_len;

let mut trunc_layer = build_trunc_layer(
team_diff_vars,
sorted_teams_and_ranks,
Expand Down Expand Up @@ -1669,7 +1670,7 @@ fn build_rating_layer(
rating_vars: &[Rc<RefCell<Variable>>],
flattened_ratings: &[TrueSkillRating],
tau: f64,
starting_id: u32,
starting_id: usize,
) -> Vec<PriorFactor> {
let mut v = Vec::with_capacity(rating_vars.len());
let mut i = starting_id;
Expand All @@ -1690,7 +1691,7 @@ fn build_perf_layer(
rating_vars: &[Rc<RefCell<Variable>>],
perf_vars: &[Rc<RefCell<Variable>>],
beta: f64,
starting_id: u32,
starting_id: usize,
) -> Vec<LikelihoodFactor> {
let beta_sq = beta.powi(2);
let mut v = Vec::with_capacity(rating_vars.len());
Expand All @@ -1712,7 +1713,7 @@ fn build_team_perf_layer(
team_perf_vars: &[Rc<RefCell<Variable>>],
perf_vars: &[Rc<RefCell<Variable>>],
team_sizes: &[usize],
starting_id: u32,
starting_id: usize,
) -> Vec<SumFactor> {
let mut v = Vec::with_capacity(team_perf_vars.len());
let mut i = starting_id;
Expand All @@ -1738,7 +1739,7 @@ fn build_team_perf_layer(
fn build_team_diff_layer(
team_diff_vars: &[Rc<RefCell<Variable>>],
team_perf_vars: &[Rc<RefCell<Variable>>],
starting_id: u32,
starting_id: usize,
) -> Vec<SumFactor> {
let mut v = Vec::with_capacity(team_diff_vars.len());
let mut i = starting_id;
Expand All @@ -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<TruncateFactor> {
fn v_w(diff: f64, draw_margin: f64) -> f64 {
let x = diff - draw_margin;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0a4c8be

Please sign in to comment.