Minor performance tweaks
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone Build is failing

This commit is contained in:
2022-12-25 12:55:09 +01:00
parent 055fbfba78
commit 43bc811704
4 changed files with 23 additions and 28 deletions

View File

@@ -1,5 +1,4 @@
use hashbrown::hash_map::Entry::{Occupied, Vacant};
use hashbrown::HashMap;
use indexmap::IndexSet;
use std::fmt::Debug;
use crate::defines::LevelInt;
@@ -12,48 +11,42 @@ pub trait LearnableMoves: Debug {
/// Gets all moves a Pokemon can learn when leveling up to a specific level.
fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec<StringKey>>;
/// Gets the distinct moves a Pokemon can learn through leveling up.
fn get_distinct_level_moves(&self) -> &Vec<StringKey>;
fn get_distinct_level_moves(&self) -> &IndexSet<StringKey>;
}
/// The storage of the moves a Pokemon can learn.
#[derive(Default, PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug)]
pub struct LearnableMovesImpl {
/// A map of the moves a Pokemon can learn per level.
learned_by_level: HashMap<LevelInt, Vec<StringKey>>,
learned_by_level: Vec<Vec<StringKey>>,
/// A list of the distinct moves a Pokemon can learn through leveling up.
distinct_level_moves: Vec<StringKey>,
distinct_level_moves: IndexSet<StringKey>,
}
impl LearnableMovesImpl {
/// Instantiates a new Learnable Moves.
pub fn new() -> Self {
Self::default()
pub fn new(max_level: LevelInt) -> Self {
Self {
learned_by_level: vec![Vec::new(); (max_level + 1) as usize],
distinct_level_moves: Default::default(),
}
}
}
impl LearnableMoves for LearnableMovesImpl {
/// Adds a new level move the Pokemon can learn.
fn add_level_move(&mut self, level: LevelInt, m: &StringKey) {
match self.learned_by_level.entry(level) {
Occupied(x) => {
x.into_mut().push(m.clone());
}
Vacant(_) => {
self.learned_by_level.insert(level, vec![m.clone()]);
}
}
if !self.distinct_level_moves.contains(m) {
self.distinct_level_moves.push(m.clone());
}
self.learned_by_level.get_mut(level as usize).unwrap().push(m.clone());
self.distinct_level_moves.insert(m.clone());
}
/// Gets all moves a Pokemon can learn when leveling up to a specific level.
fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec<StringKey>> {
self.learned_by_level.get(&level)
self.learned_by_level.get(level as usize)
}
/// Gets the distinct moves a Pokemon can learn through leveling up.
fn get_distinct_level_moves(&self) -> &Vec<StringKey> {
fn get_distinct_level_moves(&self) -> &IndexSet<StringKey> {
&self.distinct_level_moves
}
}
@@ -64,7 +57,7 @@ pub(crate) mod tests {
#[test]
fn adds_level_moves() {
let mut moves = LearnableMovesImpl::new();
let mut moves = LearnableMovesImpl::new(100);
moves.add_level_move(1, &"foo".into());
moves.add_level_move(1, &"bar".into());
@@ -76,7 +69,7 @@ pub(crate) mod tests {
#[test]
fn adds_two_same_moves_at_different_level() {
let mut moves = LearnableMovesImpl::new();
let mut moves = LearnableMovesImpl::new(100);
moves.add_level_move(1, &"foo".into());
moves.add_level_move(5, &"foo".into());