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
use std::fmt::Debug;
use std::hash::Hash;
pub trait Count: Copy {
fn zero() -> Self;
fn add(&mut self, other: Self);
}
impl Count for u64 {
fn zero() -> Self {
0
}
fn add(&mut self, other: Self) {
*self += other;
}
}
impl Count for (u64, u64) {
fn zero() -> Self {
(0, 0)
}
fn add(&mut self, other: Self) {
self.0 += other.0;
self.1 += other.1;
}
}
pub trait Actor: Debug + Clone + Hash + Eq + Ord {}
impl<A: Debug + Clone + Hash + Eq + Ord> Actor for A {}
pub trait EventSet: Clone + Debug + Default {
type EventIter: Iterator<Item = u64>;
fn new() -> Self;
fn from_event(event: u64) -> Self {
let mut eset = Self::new();
eset.add_event(event);
eset
}
fn from_event_range(start: u64, end: u64) -> Self {
let mut eset = Self::new();
eset.add_event_range(start, end);
eset
}
fn from_events<I: IntoIterator<Item = u64>>(iter: I) -> Self {
let mut eset = Self::new();
for event in iter {
eset.add_event(event);
}
eset
}
fn next_event(&mut self) -> u64;
fn add_event(&mut self, event: u64) -> bool;
fn add_event_range(&mut self, start: u64, end: u64) -> bool {
let mut res = false;
(start..=end).for_each(|event| {
let added = self.add_event(event);
res = res || added;
});
res
}
fn is_event(&self, event: u64) -> bool;
fn events(&self) -> (u64, Vec<u64>);
fn frontier(&self) -> u64;
fn join(&mut self, other: &Self);
fn event_iter(self) -> Self::EventIter;
}
pub fn subtract_iter<E, S>(from: E, subtract: S) -> SubtractIter<E, S>
where
E: EventSet,
S: EventSet,
{
SubtractIter {
event_iter: from.event_iter(),
subtract,
}
}
pub struct SubtractIter<E: EventSet, S> {
event_iter: E::EventIter,
subtract: S,
}
impl<E, S> Iterator for SubtractIter<E, S>
where
E: EventSet,
S: EventSet,
{
type Item = u64;
fn next(&mut self) -> Option<Self::Item> {
match self.event_iter.next() {
Some(event) => {
if self.subtract.is_event(event) {
self.next()
} else {
Some(event)
}
}
None => None,
}
}
}