1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
use super::gamedata::Mechanic;
use super::math::{Monoid, RecordFunc, Semigroup};
use std::fmt;
/// A simple wrapper for integers.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Counter(u32);
impl Semigroup for Counter {
#[inline]
fn combine(&self, other: &Counter) -> Counter {
Counter(self.0 + other.0)
}
}
impl Monoid for Counter {
#[inline]
fn mempty() -> Counter {
Counter(0)
}
}
/// Provides access to the mechanic log.
#[derive(Clone, Default)]
pub struct MechanicLog {
inner: RecordFunc<u64, (&'static Mechanic, u64), Counter>,
}
impl MechanicLog {
/// Increase the mechanic counter for the given mechanic and agent by one.
pub fn increase(&mut self, time: u64, mechanic: &'static Mechanic, agent: u64) {
self.inner.insert(time, (mechanic, agent), Counter(1));
}
/// Return the count of mechanics.
///
/// A function can be provided to filter entries by mechanic type and agent.
pub fn count<F: FnMut(&'static Mechanic, u64) -> bool>(&self, mut filter: F) -> u32 {
self.inner.tally_only(|(a, b)| filter(a, *b)).0
}
/// Return the count of mechanics between the two given times.
///
/// A function can be provided to filter entries by mechanic type and agent.
pub fn count_between<F: FnMut(&'static Mechanic, u64) -> bool>(
&self,
start: u64,
stop: u64,
mut filter: F,
) -> u32 {
self.inner
.between_only(&start, &stop, |(a, b)| filter(a, *b))
.0
}
}
impl fmt::Debug for MechanicLog {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MechanicLog {{ ... }}")
}
}
|