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
use HashSet;
use Hash;
// #[derive(Debug, PartialEq)]
// pub struct CustomSet<T> {
// set: Vec<T>,
// }
// impl<T> CustomSet<T>
// where
// // dedup relies on PartialEq. Tests pass without it, but it should be there
// T: PartialEq + Clone + Ord,
// {
// pub fn new(input: &[T]) -> Self {
// let mut vec: Vec<T> = input.into_iter().cloned().collect();
// vec.sort_unstable();
// vec.dedup();
// Self { set: vec }
// }
// pub fn contains(&self, element: &T) -> bool {
// self.set.contains(element)
// }
// pub fn add(&mut self, element: T) {
// self.set.push(element);
// self.set.sort_unstable();
// self.set.dedup();
// }
// pub fn is_subset(&self, other: &Self) -> bool {
// self.set.iter().all(|item| other.contains(item))
// }
// pub fn is_empty(&self) -> bool {
// self.set.is_empty()
// }
// pub fn is_disjoint(&self, other: &Self) -> bool {
// self.set.iter().all(|item| !other.contains(item))
// }
// pub fn intersection(&self, other: &Self) -> Self {
// let vec = self
// .set
// .iter()
// .filter(|item| other.contains(item))
// .cloned()
// .collect();
// Self { set: vec }
// }
// pub fn difference(&self, other: &Self) -> Self {
// let vec = self
// .set
// .iter()
// .filter(|item| !other.contains(item))
// .cloned()
// .collect();
// Self { set: vec }
// }
// pub fn union(&self, other: &Self) -> Self {
// let mut vec = self.set.clone();
// vec.extend_from_slice(&other.set);
// vec.sort_unstable();
// vec.dedup();
// Self { set: vec }
// }
// }
// #[derive(Debug, PartialEq, Eq)]
// pub struct CustomSet<T> {
// values: Vec<T>,
// // We fake using T here, so the compiler does not complain that
// // "parameter `T` is never used". Delete when no longer needed.
// phantom: std::marker::PhantomData<T>,
// }
//
// impl<T: Clone + PartialEq + Ord> CustomSet<T> {
// pub fn new(_input: &[T]) -> Self {
// let mut data = _input.to_vec();
// data.sort();
//
// CustomSet {
// values: data,
// phantom: Default::default(),
// }
// }
//
// pub fn contains(&self, _element: &T) -> bool {
// self.values.contains(_element)
// }
//
// pub fn add(&mut self, _element: T) {
// if !self.values.contains(&_element) {
// self.values.push(_element);
// }
//
// self.values.sort()
// }
//
// pub fn is_subset(&self, _other: &Self) -> bool {
// match (self.values.len(), _other.values.len()) {
// (0, 0) | (0, _) => true,
// (_, _) => {
// _other.values.windows(self.values.len()).any(|x| x == self.values)
// }
// }
// }
//
// pub fn is_empty(&self) -> bool {
// self.values.is_empty()
// }
//
// pub fn is_disjoint(&self, _other: &Self) -> bool {
// match (self.values.len(), _other.values.len()) {
// (0, 0) | (_, 0) | (0, _) => true,
// (_, _) => {
// let first = self.values.first().unwrap();
// let last = self.values.last().unwrap();
//
// !_other.values.iter().any(|x| x == first || x == last)
// }
// }
// }
//
// #[must_use]
// pub fn intersection(&self, _other: &Self) -> Self {
// let intersection: Vec<T> = self.values
// .iter()
// .filter(|&x| _other.contains(x))
// .cloned()
// .collect();
//
// CustomSet::new(&intersection)
// }
//
// #[must_use]
// pub fn difference(&self, _other: &Self) -> Self {
// let difference: Vec<T> = self.values
// .iter()
// .filter(|&x| !_other.contains(x))
// .cloned()
// .collect();
//
// CustomSet::new(&difference)
// }
//
// #[must_use]
// pub fn union(&self, _other: &Self) -> Self {
// let mut union: Vec<T> = self.values.to_vec();
// for value in &_other.values {
// if !self.values.contains(&value) {
// union.push(value.clone());
// }
// }
// // let mut values = self.values.clone();
// // values.extend(other.values.clone());
//
// CustomSet::new(&union)
// }
// }