Skip to content

Commit

Permalink
Merge pull request #13 from ReagentX/feat/cs/reduce-allocations
Browse files Browse the repository at this point in the history
Feat/cs/reduce allocations
  • Loading branch information
ReagentX authored Feb 23, 2025
2 parents 7393315 + 1c74956 commit 119e6c4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add the following to your `Cargo.toml`:

```toml
[dependencies]
glicko_2 = "1.0.0"
glicko_2 = "1.1.0"
```

## Sample Usage
Expand All @@ -33,7 +33,7 @@ let mut opponent_4 = Rating::new(&tuning);
/// Rate our team against a vector of matchup results
algorithm::rate(
&mut team_to_update,
vec![(Outcome::Win, &mut opponent_1),
&mut [(Outcome::Win, &mut opponent_1),
(Outcome::Loss, &mut opponent_2),
(Outcome::Draw, &mut opponent_3),
]
Expand All @@ -43,7 +43,7 @@ algorithm::rate(
opponent_4.decay();

/// Print our updated rating
println!("{:?}", team_to_update); // { mu: 1500.0, phi: 255.40, sigma: 0.0059, is_scaled: false }
println!("{:?}", team_to_update); // Rating(μ=1500.0, φ=255.40, σ=0.0059)0059 }
```

### To get the odds one team will beat another
Expand All @@ -60,7 +60,7 @@ let mut rating_2 = Rating::new(&tuning);

/// Get odds (percent chance team_1 beats team_2)
let odds = game::odds(&mut rating_1, &mut rating_2);
println!("{}", odds); // 0.5, perfect odds since both teams have the same rating
println!("{odds}"); // 0.5, perfect odds since both teams have the same rating
```

### To determine the quality of a matchup
Expand All @@ -77,7 +77,7 @@ let mut rating_2 = Rating::new(&tuning);

/// Get odds (the advantage team 1 has over team 2)
let quality = game::quality(&mut rating_1, &mut rating_2);
println!("{}", quality); // 1.0, perfect matchup since both teams have the same rating
println!("{quality}"); // 1.0, perfect matchup since both teams have the same rating
```

### To update both team's ratings for a single matchup
Expand All @@ -96,8 +96,8 @@ let mut rating_2 = Rating::new(&tuning);
game::compete(&mut rating_1, &mut rating_2, false);

/// Print our updated ratings
println!("{:?}", rating_1); // { mu: 1646.47, phi: 307.84, sigma: 0.0059, is_scaled: false }
println!("{:?}", rating_2); // { mu: 1383.42, phi: 306.83, sigma: 0.0059, is_scaled: false }
println!("{rating_1}"); // Rating(μ=1646.47, φ=307.84, σ=0.0059)
println!("{rating_2}"); // Rating(μ=1383.42, φ=306.83, σ=0.0059)
```

## Rating
Expand Down
6 changes: 3 additions & 3 deletions src/glicko2/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,21 @@ fn determine_sigma(rating: &Rating, difference: &f64, variance: &f64) -> f64 {
/// use glicko_2::{Rating, Tuning, game::Outcome};
///
/// let tuning = Tuning::default();
///
///
/// let mut team_to_update = Rating::new(&tuning);
/// let mut opponent_1 = Rating::new(&tuning);
/// let mut opponent_2 = Rating::new(&tuning);
/// let mut opponent_3 = Rating::new(&tuning);
///
/// glicko_2::algorithm::rate(
/// &mut team_to_update,
/// vec![(Outcome::Win, &mut opponent_1),
/// &mut [(Outcome::Win, &mut opponent_1),
/// (Outcome::Loss, &mut opponent_2),
/// (Outcome::Draw, &mut opponent_3),
/// ]
/// );
/// ```
pub fn rate(rating: &mut Rating, outcomes: Vec<(Outcome, &mut Rating)>) {
pub fn rate(rating: &mut Rating, outcomes: &mut [(Outcome, &mut Rating)]) {
// Outcome is a list of outcomes for a set of games between two teams, i.e.
// a vector tuples like [(WIN, rating2), ...]

Expand Down
10 changes: 5 additions & 5 deletions src/glicko2/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use crate::glicko2::{algorithm, constants, rating::Rating};
pub fn compete(winner: &mut Rating, loser: &mut Rating, drawn: bool) {
// drawn is false if Team 1 beat Team 2
if drawn {
algorithm::rate(winner, vec![(Outcome::Draw, loser)]);
algorithm::rate(loser, vec![(Outcome::Draw, winner)]);
algorithm::rate(winner, &mut [(Outcome::Draw, loser)]);
algorithm::rate(loser, &mut [(Outcome::Draw, winner)]);
} else {
algorithm::rate(winner, vec![(Outcome::Win, loser)]);
algorithm::rate(loser, vec![(Outcome::Loss, winner)]);
algorithm::rate(winner, &mut [(Outcome::Win, loser)]);
algorithm::rate(loser, &mut [(Outcome::Loss, winner)]);
};
}

Expand Down Expand Up @@ -94,7 +94,7 @@ impl Outcome {
/// let loss = Outcome::Loss;
/// let loss_val = loss.val();
/// ```
pub fn val(&self) -> f64 {
pub const fn val(&self) -> f64 {
match self {
Outcome::Win => constants::WIN,
Outcome::Draw => constants::DRAW,
Expand Down
18 changes: 14 additions & 4 deletions src/glicko2/rating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub struct Rating<'a> {
pub mu: f64,
pub phi: f64,
pub sigma: f64,
pub is_scaled: bool,
pub(crate) is_scaled: bool,
pub(crate) tuning: &'a Tuning,
}

impl<'a> Rating<'a> {
impl Rating<'_> {
/// Create a new instance of a Rating based on the provided tuning parameters.
///
/// # Example
Expand Down Expand Up @@ -60,10 +60,10 @@ impl<'a> Rating<'a> {
/// # Example
/// ```
/// use glicko_2::{Rating, Tuning};
///
///
/// let tuning = Tuning::default();
/// let mut new_rating = Rating::new(&tuning);
///
///
/// new_rating.decay();
/// ```
pub fn decay(&mut self) {
Expand All @@ -75,3 +75,13 @@ impl<'a> Rating<'a> {
self.scale_up();
}
}

impl std::fmt::Display for Rating<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Rating(μ={:.2}, φ={:.2}, σ={:.4})",
self.mu, self.phi, self.sigma
)
}
}
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ mod rating_tests {
assert!(!new_rating.is_scaled);
}

#[test]
fn create_rating_string() {
let new_rating = Rating::new(&TUNING);
let rating_string = format!("{new_rating}");
assert_eq!(rating_string, "Rating(μ=1500.00, φ=350.00, σ=0.0060)");
}

#[test]
fn scale_down() {
let mut new_rating = Rating::new(&TUNING);
Expand Down Expand Up @@ -283,7 +290,7 @@ mod algorithm_tests {
};
algorithm::rate(
&mut new_rating,
vec![(game::Outcome::Win, &mut other_rating)],
&mut [(game::Outcome::Win, &mut other_rating)],
);
println!("{:?}", new_rating);
assert_eq!(new_rating.mu, 1643.2419919603035);
Expand Down

0 comments on commit 119e6c4

Please sign in to comment.