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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
//! This module provides some basic mathematical structures.
use std::iter;
use std::ops::{Mul, Sub};
/// A semigroup.
///
/// This trait lets you combine elements by a binary operation.
pub trait Semigroup {
fn combine(&self, other: &Self) -> Self;
}
/// A monoid.
///
/// Extends the semigroup with a "neutral" element.
///
/// # Laws
///
/// ```raw
/// mempty.combine(x) == x
/// x.combine(mempty) == x
/// ```
pub trait Monoid: Semigroup {
fn mempty() -> Self;
}
#[derive(Debug, Clone)]
#[doc(hidden)]
pub struct Record<X, T, D> {
x: X,
tag: T,
data: D,
}
/// A function that records tagged data points.
///
/// This represents a "function" as a list of increases at soem discrete points.
/// Think about it as a generalized damage log. Increases can be tagged by some
/// arbitrary data, for example which the agent ID, the skill ID, the target,
/// ...
///
/// This offers methods to get the value at a specific point (by "summing up"
/// all increments before that point), between two points and in total. It also
/// offers variants that allow you to filter the increments by their tag.
///
/// Type parameters:
///
/// * `X` domain of the function. Must have a defined `Ord`ering.
/// * `T` tag for each data point. Can be arbitrary.
/// * `D` actual data. Must be [`Monoid`](trait.Monoid.html), so that it can be
/// summed up.
#[derive(Clone)]
pub struct RecordFunc<X, T, D> {
data: Vec<Record<X, T, D>>,
}
impl<X, T, D> RecordFunc<X, T, D>
where
X: Ord,
D: Monoid,
{
/// Create a new `RecordFunc`.
pub fn new() -> Self {
RecordFunc { data: Vec::new() }
}
#[doc(hidden)]
pub fn data(&self) -> &[Record<X, T, D>] {
&self.data
}
/// Insert a data point into the record func.
///
/// Note that you should supply the *increment*, not the *absolute value*!
pub fn insert(&mut self, x: X, tag: T, data: D) {
// Usually, the list will be built up in order, which means we can
// always append to the end. Check for this special case to make it
// faster.
if self.data.last().map(|r| r.x < x).unwrap_or(true) {
self.data.push(Record { x, tag, data });
} else {
let index = match self.data.binary_search_by(|r| r.x.cmp(&x)) {
Ok(i) => i,
Err(i) => i,
};
self.data.insert(index, Record { x, tag, data });
}
//self.data.sort_by(|a, b| a.x.cmp(&b.x));
}
/// Get the amount of data points saved.
pub fn len(&self) -> usize {
self.data.len()
}
/// Check whether there are no records.
pub fn is_emtpy(&self) -> bool {
self.data.is_empty()
}
/// Get the absolute value at the specific point.
#[inline]
pub fn get(&self, x: &X) -> D {
self.get_only(x, |_| true)
}
/// Get the absolute value at the specific point by only considering
/// increments where the predicate holds.
pub fn get_only<F: FnMut(&T) -> bool>(&self, x: &X, mut predicate: F) -> D {
self.data
.iter()
.take_while(|record| record.x <= *x)
.filter(|record| predicate(&record.tag))
.fold(D::mempty(), |a, b| a.combine(&b.data))
}
/// Get the increments between the two given points.
#[inline]
pub fn between(&self, a: &X, b: &X) -> D {
self.between_only(a, b, |_| true)
}
/// Get the increments between the two given points by only considering
/// increments where the predicate holds.
pub fn between_only<F: FnMut(&T) -> bool>(&self, a: &X, b: &X, mut predicate: F) -> D {
self.data
.iter()
.skip_while(|record| record.x < *a)
.take_while(|record| record.x <= *b)
.filter(|record| predicate(&record.tag))
.fold(D::mempty(), |a, b| a.combine(&b.data))
}
/// Get the sum of all increments.
#[inline]
pub fn tally(&self) -> D {
self.tally_only(|_| true)
}
/// Get the sum of all increments by only considering increments where the
/// predicate holds.
pub fn tally_only<F: FnMut(&T) -> bool>(&self, mut predicate: F) -> D {
self.data
.iter()
.filter(|record| predicate(&record.tag))
.fold(D::mempty(), |a, b| a.combine(&b.data))
}
}
impl<X, T, D, A> RecordFunc<X, T, D>
where
X: Ord + Sub<X, Output = X> + Copy,
D: Monoid + Mul<X, Output = A>,
A: Monoid,
{
#[inline]
pub fn integral(&self, a: &X, b: &X) -> A {
self.integral_only(a, b, |_| true)
}
pub fn integral_only<F: FnMut(&T) -> bool>(&self, a: &X, b: &X, mut predicate: F) -> A {
let points = self
.data
.iter()
.skip_while(|record| record.x < *a)
.take_while(|record| record.x <= *b)
.filter(|record| predicate(&record.tag))
.map(|record| record.x)
.chain(iter::once(*b))
.collect::<Vec<_>>();
let mut area = A::mempty();
let mut last = *a;
for point in points {
let diff = point - last;
let value = self.get_only(&last, &mut predicate);
area = area.combine(&(value * diff));
last = point;
}
area
}
}
impl<X: Ord, T, D: Monoid> Default for RecordFunc<X, T, D> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod test {
use super::*;
#[derive(Debug, PartialEq, Eq)]
struct Integer(u32);
impl Semigroup for Integer {
fn combine(&self, other: &Self) -> Self {
Integer(self.0 + other.0)
}
}
impl Monoid for Integer {
fn mempty() -> Self {
Integer(0)
}
}
fn create() -> RecordFunc<u32, u8, Integer> {
let mut result = RecordFunc::new();
result.insert(6, 1, Integer(6));
result.insert(4, 0, Integer(5));
result.insert(0, 1, Integer(3));
result.insert(2, 0, Integer(4));
result
}
#[test]
fn recordfunc_get() {
let rf = create();
assert_eq!(rf.get(&3), Integer(7));
assert_eq!(rf.get(&4), Integer(12));
}
#[test]
fn recordfunc_get_only() {
let rf = create();
assert_eq!(rf.get_only(&3, |t| *t == 0), Integer(4));
}
#[test]
fn recordfunc_between() {
let rf = create();
assert_eq!(rf.between(&1, &5), Integer(9));
}
}
|