Skip to content

Commit

Permalink
Change functions from taking Vecs to taking Slices
Browse files Browse the repository at this point in the history
  • Loading branch information
atomflunder committed Oct 27, 2022
1 parent 21b1682 commit a4854a9
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This is a broad overview of the changes that have been made over the lifespan of this library.

## v0.19.2 - 2022-10-27

- Change every function that takes a Vec to now take a Slice

## v0.19.1 - 2022-10-26

- Add more From implementations for rating structs
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "skillratings"
version = "0.19.1"
version = "0.19.2"
edition = "2021"
description = "Calculate a player's skill rating using algorithms like Elo, Glicko, Glicko-2, TrueSkill and many more."
readme = "README.md"
Expand Down
18 changes: 9 additions & 9 deletions src/dwz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub fn dwz(
#[must_use]
/// The "traditional" way of calculating a DWZ Rating of a player in a rating period or tournament.
///
/// Takes in a player as an [`DWZRating`] and their results as a Vec of tuples containing the opponent as an [`DWZRating`]
/// Takes in a player as an [`DWZRating`] and their results as a Slice of tuples containing the opponent as an [`DWZRating`]
/// and the outcome of the game as an [`Outcome`](Outcomes).
///
/// All of the outcomes are from the perspective of the player.
Expand Down Expand Up @@ -258,7 +258,7 @@ pub fn dwz(
/// assert!((new_player.rating.round() - 1635.0).abs() < f64::EPSILON);
/// assert_eq!(new_player.index, 18);
/// ```
pub fn dwz_rating_period(player: &DWZRating, results: &Vec<(DWZRating, Outcomes)>) -> DWZRating {
pub fn dwz_rating_period(player: &DWZRating, results: &[(DWZRating, Outcomes)]) -> DWZRating {
// DWZ was designed to be used in tournaments, so we do not need to loop over the opponents here.
let points = results.iter().map(|r| r.1.to_chess_points()).sum();

Expand Down Expand Up @@ -324,7 +324,7 @@ pub fn expected_score(player_one: &DWZRating, player_two: &DWZRating) -> (f64, f
/// consider using [`DWZRating::from()`](DWZRating) if you have an [`EloRating`](crate::elo::EloRating)
/// or [`DWZRating::new()`](DWZRating) if not.
///
/// Takes in the player's age and their results as a Vec of tuples containing the opponent and the outcome.
/// Takes in the player's age and their results as a Slice of tuples containing the opponent and the outcome.
/// If the actual player's age is unavailable or unknown, choose something `>25`.
///
/// This only returns a DWZ rating if the results include at least 5 matches,
Expand Down Expand Up @@ -378,7 +378,7 @@ pub fn expected_score(player_one: &DWZRating, player_two: &DWZRating) -> (f64, f
/// assert!((player.rating - 1491.0).abs() < f64::EPSILON);
/// assert_eq!(player.index, 1);
/// ```
pub fn get_first_dwz(player_age: usize, results: &Vec<(DWZRating, Outcomes)>) -> Option<DWZRating> {
pub fn get_first_dwz(player_age: usize, results: &[(DWZRating, Outcomes)]) -> Option<DWZRating> {
if results.len() < 5 {
return None;
}
Expand Down Expand Up @@ -709,7 +709,7 @@ mod tests {
#[allow(clippy::unwrap_used)]
let player = get_first_dwz(
26,
&vec![
&[
(o1, Outcomes::WIN),
(o2, Outcomes::DRAW),
(o3, Outcomes::LOSS),
Expand All @@ -724,7 +724,7 @@ mod tests {

let all_win_player = get_first_dwz(
17,
&vec![
&[
(o1, Outcomes::WIN),
(o2, Outcomes::WIN),
(o3, Outcomes::WIN),
Expand All @@ -737,7 +737,7 @@ mod tests {

let all_lose_player = get_first_dwz(
17,
&vec![
&[
(o1, Outcomes::LOSS),
(o2, Outcomes::LOSS),
(o3, Outcomes::LOSS),
Expand All @@ -750,7 +750,7 @@ mod tests {

let less_than_5 = get_first_dwz(
32,
&vec![
&[
(o1, Outcomes::LOSS),
(o2, Outcomes::WIN),
(o3, Outcomes::DRAW),
Expand Down Expand Up @@ -796,7 +796,7 @@ mod tests {
#[allow(clippy::unwrap_used)]
let bad_player = get_first_dwz(
26,
&vec![
&[
(o1, Outcomes::LOSS),
(o2, Outcomes::DRAW),
(o3, Outcomes::LOSS),
Expand Down
4 changes: 2 additions & 2 deletions src/egf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub fn egf(
#[must_use]
/// Calculates an [`EGFRating`] in a traditional way using a rating period, commonly used for tournaments.
///
/// Takes in a player as an [`EGFRating`], their results as a Vec of tuples containing the opponent as an [`EGFRating`],
/// Takes in a player as an [`EGFRating`], their results as a Slice of tuples containing the opponent as an [`EGFRating`],
/// the outcome of the game as an [`Outcome`](Outcomes), and an [`EGFConfig`] where you can specify handicaps for the players.
///
/// ---
Expand Down Expand Up @@ -235,7 +235,7 @@ pub fn egf(
/// ```
pub fn egf_rating_period(
player: &EGFRating,
results: &Vec<(EGFRating, Outcomes, EGFConfig)>,
results: &[(EGFRating, Outcomes, EGFConfig)],
) -> EGFRating {
// According to the EGF, the rating values are not updated in between games.
// Only the change in rating is kept track of.
Expand Down
6 changes: 3 additions & 3 deletions src/elo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ pub fn elo(
)
}

#[must_use]
/// Calculates an [`EloRating`] in a non-traditional way using a rating period,
/// for compatibility with the other algorithms.
///
Expand Down Expand Up @@ -218,10 +219,9 @@ pub fn elo(
///
/// assert!((new_player.rating.round() - 1210.0).abs() < f64::EPSILON);
/// ```
#[must_use]
pub fn elo_rating_period(
player: &EloRating,
results: &Vec<(EloRating, Outcomes)>,
results: &[(EloRating, Outcomes)],
config: &EloConfig,
) -> EloRating {
let mut player_rating = player.rating;
Expand Down Expand Up @@ -322,7 +322,7 @@ mod tests {

let new_player = elo_rating_period(
&player,
&vec![
&[
(opponent1, Outcomes::WIN),
(opponent2, Outcomes::DRAW),
(opponent3, Outcomes::LOSS),
Expand Down
8 changes: 4 additions & 4 deletions src/glicko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,14 @@ pub fn glicko(
)
}

#[must_use]
/// The "traditional" way of calculating a [`GlickoRating`] of a player in a rating period.
///
/// Note that in this case, all of the matches are considered to be played at once.
/// This means that the player will not get updated in-between matches, as you might expect.
/// This will result in *slightly* different results than if you were to use the [`glicko`] function in a loop.
///
/// Takes in a player as an [`GlickoRating`] and their results as a Vec of tuples containing the opponent as an [`GlickoRating`],
/// Takes in a player as an [`GlickoRating`] and their results as a Slice of tuples containing the opponent as an [`GlickoRating`],
/// the outcome of the game as an [`Outcome`](Outcomes) and a [`GlickoConfig`].
///
/// The outcome of the match is in the perspective of the player.
Expand Down Expand Up @@ -293,10 +294,9 @@ pub fn glicko(
/// assert!((new_player.rating.round() - 1464.0).abs() < f64::EPSILON);
/// assert!((new_player.deviation.round() - 151.0).abs() < f64::EPSILON);
/// ```
#[must_use]
pub fn glicko_rating_period(
player: &GlickoRating,
results: &Vec<(GlickoRating, Outcomes)>,
results: &[(GlickoRating, Outcomes)],
config: &GlickoConfig,
) -> GlickoRating {
let q = 10_f64.ln() / 400.0;
Expand Down Expand Up @@ -558,7 +558,7 @@ mod tests {

let (np, _) = glicko(&player, &opponent, &Outcomes::WIN);

let rp = glicko_rating_period(&player, &vec![(opponent, Outcomes::WIN)], &config);
let rp = glicko_rating_period(&player, &[(opponent, Outcomes::WIN)], &config);

assert_eq!(rp, np);
}
Expand Down
8 changes: 4 additions & 4 deletions src/glicko2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,14 @@ pub fn glicko2(
(player_one_new, player_two_new)
}

#[must_use]
/// The "traditional" way of calculating a [`Glicko2Rating`] of a player in a rating period.
///
/// Note that in this case, all of the matches are considered to be played at once.
/// This means that the player will not get updated in-between matches, as you might expect.
/// This will result in *slightly* different results than if you were to use the [`glicko2`] function in a loop.
///
/// Takes in a player as an [`Glicko2Rating`] and their results as a Vec of tuples containing the opponent as an [`Glicko2Rating`],
/// Takes in a player as an [`Glicko2Rating`] and their results as a Slice of tuples containing the opponent as an [`Glicko2Rating`],
/// the outcome of the game as an [`Outcome`](Outcomes) and a [`Glicko2Config`].
///
/// The outcome of the match is in the perspective of the player.
Expand Down Expand Up @@ -337,10 +338,9 @@ pub fn glicko2(
/// assert!((new_player.deviation.round() - 152.0).abs() < f64::EPSILON);
/// assert!((new_player.volatility - 0.059995984286488495).abs() < f64::EPSILON);
/// ```
#[must_use]
pub fn glicko2_rating_period(
player: &Glicko2Rating,
results: &Vec<(Glicko2Rating, Outcomes)>,
results: &[(Glicko2Rating, Outcomes)],
config: &Glicko2Config,
) -> Glicko2Rating {
if results.is_empty() {
Expand Down Expand Up @@ -855,7 +855,7 @@ mod tests {

let (np, _) = glicko2(&player, &opponent, &Outcomes::WIN, &config);

let rp = glicko2_rating_period(&player, &vec![(opponent, Outcomes::WIN)], &config);
let rp = glicko2_rating_period(&player, &[(opponent, Outcomes::WIN)], &config);

assert_eq!(rp, np);
}
Expand Down
9 changes: 4 additions & 5 deletions src/glicko_boost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ pub fn glicko_boost(
#[must_use]
/// The "traditional" way of calculating a [`GlickoBoostRating`] of a player in a rating period.
///
/// Takes in a player as an [`GlickoBoostRating`] and their results as a Vec of tuples containing the opponent as a [`GlickoBoostRating`],
/// Takes in a player as an [`GlickoBoostRating`] and their results as a Slice of tuples containing the opponent as a [`GlickoBoostRating`],
/// the outcome of the game as an [`Outcome`](Outcomes) and a [`bool`] specifying if the player was playing as player one, and a [`GlickoBoostConfig`].
///
/// ---
Expand Down Expand Up @@ -377,7 +377,7 @@ pub fn glicko_boost(
/// ```
pub fn glicko_boost_rating_period(
player: &GlickoBoostRating,
results: &Vec<(GlickoBoostRating, Outcomes, bool)>,
results: &[(GlickoBoostRating, Outcomes, bool)],
config: &GlickoBoostConfig,
) -> GlickoBoostRating {
let q = 10_f64.ln() / 400.0;
Expand Down Expand Up @@ -650,8 +650,7 @@ mod tests {
let config = GlickoBoostConfig::new();

let (np, _) = glicko_boost(&player, &opponent, &Outcomes::WIN, &config);
let rp =
glicko_boost_rating_period(&player, &vec![(opponent, Outcomes::WIN, true)], &config);
let rp = glicko_boost_rating_period(&player, &[(opponent, Outcomes::WIN, true)], &config);

assert_eq!(np, rp);
}
Expand Down Expand Up @@ -734,7 +733,7 @@ mod tests {
};

let decayed_player = decay_deviation(&player, &GlickoBoostConfig::new());
let rp_player = glicko_boost_rating_period(&player, &vec![], &GlickoBoostConfig::new());
let rp_player = glicko_boost_rating_period(&player, &[], &GlickoBoostConfig::new());

assert_eq!(decayed_player, rp_player);
assert!((decayed_player.deviation - 64.669_444_203_475_88).abs() < f64::EPSILON);
Expand Down
7 changes: 2 additions & 5 deletions src/ingo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub fn ingo(
#[must_use]
/// The "traditional" way of calculating a [`IngoRating`] of a player in a rating period.
///
/// Takes in a player as an [`IngoRating`] and their results as a Vec of tuples containing the opponent as an [`IngoRating`],
/// Takes in a player as an [`IngoRating`] and their results as a Slice of tuples containing the opponent as an [`IngoRating`],
/// and the outcome of the game as an [`Outcome`](Outcomes)
///
/// The outcome of the match is in the perspective of the player.
Expand Down Expand Up @@ -220,10 +220,7 @@ pub fn ingo(
///
/// assert!((new_player.rating.round() - 126.0).abs() < f64::EPSILON);
/// ```
pub fn ingo_rating_period(
player: &IngoRating,
results: &Vec<(IngoRating, Outcomes)>,
) -> IngoRating {
pub fn ingo_rating_period(player: &IngoRating, results: &[(IngoRating, Outcomes)]) -> IngoRating {
// Ingo was meant to be used in tournaments, so we do not need to loop over the opponents here.
let development = age_to_devcoefficent(player.age);

Expand Down
10 changes: 5 additions & 5 deletions src/sticko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ pub fn sticko(
#[must_use]
/// The "traditional" way of calculating a [`StickoRating`] of a player in a rating period.
///
/// Takes in a player as an [`StickoRating`] and their results as a Vec of tuples containing the opponent as an [`StickoRating`],
/// Takes in a player as an [`StickoRating`] and their results as a Slice of tuples containing the opponent as an [`StickoRating`],
/// the outcome of the game as an [`Outcome`](Outcomes) and a [`bool`] specifying if the player was playing as player one, and a [`StickoConfig`].
///
/// ---
Expand Down Expand Up @@ -397,7 +397,7 @@ pub fn sticko(
/// ```
pub fn sticko_rating_period(
player: &StickoRating,
results: &Vec<(StickoRating, Outcomes, bool)>,
results: &[(StickoRating, Outcomes, bool)],
config: &StickoConfig,
) -> StickoRating {
let q = 10_f64.ln() / 400.0;
Expand Down Expand Up @@ -696,7 +696,7 @@ mod tests {

let new_player = sticko_rating_period(
&player,
&vec![
&[
(opponent1, Outcomes::WIN, true),
(opponent2, Outcomes::LOSS, true),
(opponent3, Outcomes::LOSS, true),
Expand All @@ -707,7 +707,7 @@ mod tests {
assert!((new_player.rating.round() - 1464.0).abs() < f64::EPSILON);
assert!((new_player.deviation - 151.398_902_447_969_33).abs() < f64::EPSILON);

let after_player = sticko_rating_period(&player, &vec![], &config);
let after_player = sticko_rating_period(&player, &[], &config);

assert_eq!(player, after_player);
}
Expand All @@ -733,7 +733,7 @@ mod tests {

let (np, _) = sticko(&player, &opponent, &Outcomes::WIN, &config);

let rp = sticko_rating_period(&player, &vec![(opponent, Outcomes::WIN, true)], &config);
let rp = sticko_rating_period(&player, &[(opponent, Outcomes::WIN, true)], &config);

assert_eq!(rp, np);
}
Expand Down
Loading

0 comments on commit a4854a9

Please sign in to comment.