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
use crate::set::Set;
mod union;
mod intersection;
mod difference;
mod difference_by_key;
mod symmetric_difference;
pub use self::union::Union;
pub use self::intersection::Intersection;
pub use self::difference::Difference;
pub use self::difference_by_key::DifferenceByKey;
pub use self::symmetric_difference::SymmetricDifference;
#[derive(Clone)]
pub struct OpBuilder<'a, T: 'a> {
slices: Vec<&'a Set<T>>,
}
impl<'a, T> OpBuilder<'a, T> {
pub fn new() -> Self {
Self { slices: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> Self {
Self { slices: Vec::with_capacity(capacity) }
}
pub fn from_vec(slices: Vec<&'a Set<T>>) -> Self {
Self { slices }
}
pub fn reserve(&mut self, additional: usize) {
self.slices.reserve(additional);
}
pub fn add(mut self, set: &'a Set<T>) -> Self {
self.push(set);
self
}
pub fn push(&mut self, set: &'a Set<T>) {
self.slices.push(set);
}
pub fn union(self) -> Union<'a, T> {
Union::new(self.slices)
}
pub fn intersection(self) -> Intersection<'a, T> {
Intersection::new(self.slices)
}
pub fn difference(self) -> Difference<'a, T> {
Difference::new(self.slices)
}
pub fn symmetric_difference(self) -> SymmetricDifference<'a, T> {
SymmetricDifference::new(self.slices)
}
}
#[derive(Clone)]
pub struct OpBuilderByKey<'a, T: 'a, U: 'a, F, G, K>
where F: Fn(&T) -> K,
G: Fn(&U) -> K,
K: Ord,
{
base: &'a Set<T>,
others: Vec<&'a Set<U>>,
f: F,
g: G,
}
impl<'a, T, U, F, G, K> OpBuilderByKey<'a, T, U, F, G, K>
where F: Fn(&T) -> K,
G: Fn(&U) -> K,
K: Ord,
{
pub fn new(base: &'a Set<T>, f: F, g: G) -> Self {
Self { base, others: Vec::new(), f, g }
}
pub fn from_vec(base: &'a Set<T>, others: Vec<&'a Set<U>>, f: F, g: G) -> Self {
Self { base, others, f, g }
}
pub fn push(&mut self, set: &'a Set<U>) {
self.others.push(set);
}
pub fn add(mut self, set: &'a Set<U>) -> Self {
self.push(set);
self
}
pub fn difference(self) -> DifferenceByKey<'a, T, U, F, G, K> {
DifferenceByKey::new(self.base, self.others, self.f, self.g)
}
}