aboutsummaryrefslogtreecommitdiff
path: root/src/statistics/mechanics.rs
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2018-07-07 03:41:40 +0200
committerDaniel Schadt <kingdread@gmx.de>2018-07-07 03:41:40 +0200
commitc1236e68f32511332c79d2195876eb51aaaa7c5e (patch)
treef59f1f038e35131dd1ebcb39d77932af15c16549 /src/statistics/mechanics.rs
parent32055313bd4ac5a9246ce92fd06156a370e14b17 (diff)
downloadevtclib-c1236e68f32511332c79d2195876eb51aaaa7c5e.tar.gz
evtclib-c1236e68f32511332c79d2195876eb51aaaa7c5e.tar.bz2
evtclib-c1236e68f32511332c79d2195876eb51aaaa7c5e.zip
more readability
Diffstat (limited to 'src/statistics/mechanics.rs')
-rw-r--r--src/statistics/mechanics.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/statistics/mechanics.rs b/src/statistics/mechanics.rs
index 5a16204..0cf6f24 100644
--- a/src/statistics/mechanics.rs
+++ b/src/statistics/mechanics.rs
@@ -3,8 +3,9 @@ use super::math::{Monoid, RecordFunc, Semigroup};
use std::fmt;
+/// A simple wrapper for integers.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
-pub struct Counter(u32);
+struct Counter(u32);
impl Semigroup for Counter {
#[inline]
@@ -20,19 +21,38 @@ impl Monoid for Counter {
}
}
+/// 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 {