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
use ::Data;
use lattice::Lattice;
use collection::Lookup;
use collection::compact::Compact;
use collection::trace::{Trace, TraceRef};
impl<K, L, T> Trace for Count<K, T, L> where K: Data+Ord+'static, L: Lookup<K, Offset>+'static, T: Lattice+'static {
type Key = K;
type Index = T;
type Value = ();
fn set_difference(&mut self, time: T, accumulation: Compact<K, ()>) {
let keys = accumulation.keys;
let vals = accumulation.vals;
let time_index = self.times.len();
self.links.reserve(keys.len());
for (key, wgt) in keys.into_iter().zip(vals.into_iter().map(|(_v,w)| w)) {
let next_position = Offset::new(self.links.len());
let prev_position = self.keys.entry_or_insert(key, || next_position);
if &prev_position.val() == &next_position.val() {
self.links.push(ListEntry {
time: time_index as u32,
wgts: wgt,
next: None
});
}
else {
self.links.push(ListEntry {
time: time_index as u32,
wgts: wgt,
next: Some(*prev_position)
});
*prev_position = next_position;
}
}
self.times.push(time);
}
}
impl<'a,K,L,T> TraceRef<'a,K,T,()> for &'a Count<K,T,L> where K: Ord+'a, L: Lookup<K, Offset>+'a, T: Lattice+'a {
type VIterator = WeightIterator<'a>;
type TIterator = CountIterator<'a,K,T,L>;
fn trace(self, key: &K) -> Self::TIterator {
CountIterator {
trace: self,
next0: self.keys.get_ref(key).map(|&x|x),
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct Offset {
dataz: u32,
}
impl Offset {
#[inline(always)]
fn new(offset: usize) -> Offset {
assert!(offset < ((!0u32) as usize));
Offset { dataz: (!0u32) - offset as u32 }
}
#[inline(always)]
fn val(&self) -> usize { ((!0u32) - self.dataz) as usize }
}
struct ListEntry {
time: u32,
wgts: i32,
next: Option<Offset>,
}
pub struct Count<K, T, L> {
phantom: ::std::marker::PhantomData<K>,
links: Vec<ListEntry>,
times: Vec<T>,
keys: L,
silly: (),
}
impl<K, L, T> Count<K, T, L> where K: Data+Ord+'static, L: Lookup<K, Offset>+'static, T: Lattice+'static {
pub fn get_count(&self, key: &K, time: &T) -> i32 {
let mut sum = 0;
for wgt in Trace::trace(self, key).filter(|x| x.0 <= time).map(|mut x| x.1.next().unwrap().1) {
sum += wgt;
}
sum
}
}
impl<K: Eq, L: Lookup<K, Offset>, T> Count<K, T, L> {
pub fn new(l: L) -> Count<K, T, L> {
Count {
phantom: ::std::marker::PhantomData,
links: Vec::new(),
times: Vec::new(),
keys: l,
silly: (),
}
}
}
pub struct CountIterator<'a, K: Eq+'a, T: 'a, L: Lookup<K, Offset>+'a> {
trace: &'a Count<K, T, L>,
next0: Option<Offset>,
}
impl<'a, K: Eq, T, L> Iterator for CountIterator<'a, K, T, L>
where K: Ord+'a,
T: Lattice+'a,
L: Lookup<K, Offset>+'a {
type Item = (&'a T, WeightIterator<'a>);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.next0.map(|position| {
let time_index = self.trace.links[position.val()].time as usize;
let result = (&self.trace.times[time_index], WeightIterator { weight: self.trace.links[position.val()].wgts, silly: &self.trace.silly });
self.next0 = self.trace.links[position.val()].next;
result
})
}
}
impl<'a, K: Eq, T, L> Clone for CountIterator<'a, K, T, L>
where K: Ord+'a,
T: Lattice+'a,
L: Lookup<K, Offset>+'a {
fn clone(&self) -> CountIterator<'a, K, T, L> {
CountIterator {
trace: self.trace,
next0: self.next0,
}
}
}
pub struct WeightIterator<'a> {
weight: i32,
silly: &'a (),
}
impl<'a> Iterator for WeightIterator<'a> {
type Item = (&'a (), i32);
fn next(&mut self) -> Option<(&'a (), i32)> {
if self.weight == 0 { None }
else {
let result = self.weight;
self.weight = 0;
Some((self.silly, result))
}
}
}
impl<'a> Clone for WeightIterator<'a> {
fn clone(&self) -> WeightIterator<'a> {
WeightIterator {
weight: self.weight,
silly: self.silly,
}
}
}