use std::collections::HashMap;
use std::fmt;
#[derive(Hash, Eq, PartialEq, strum_macros::Display)]
pub enum Transition {
CPY,
DEL,
NEW,
DLG,
PPG,
}
pub struct Perf {
pub cycles: usize,
pub hits: HashMap<Transition, usize>,
pub ticks: HashMap<Transition, usize>,
}
impl Perf {
pub fn new() -> Perf {
Perf {
ticks: HashMap::new(),
hits: HashMap::new(),
cycles: 0,
}
}
pub fn tick(&mut self, t: Transition) {
*self.ticks.entry(t).or_insert(0) += 1;
}
pub fn hit(&mut self, t: Transition) {
*self.hits.entry(t).or_insert(0) += 1;
}
pub fn total_hits(&self) -> usize {
self.hits.values().fold(0, |sum, x| sum + x)
}
}
impl fmt::Display for Perf {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut lines = vec![];
lines.push(format!("Cycles: {}", self.cycles));
lines.push("Ticks:".to_string());
lines.extend(self.ticks.iter().map(|(t, c)| format!("\t{}: {}", t, c)));
lines.push("Hits:".to_string());
lines.extend(self.hits.iter().map(|(t, c)| format!("\t{}: {}", t, c)));
f.write_str(lines.join("\n").as_str())
}
}
#[test]
pub fn simple_increment() {
let mut perf = Perf::new();
perf.hit(Transition::DEL);
assert!(perf.to_string().contains("DEL: 1"));
}