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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#![unstable(feature = "enumset",
reason = "matches collection reform specification, \
waiting for dust to settle",
issue = "0")]
use core::marker;
use core::fmt;
use core::iter::FromIterator;
use core::ops::{Sub, BitOr, BitAnd, BitXor};
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EnumSet<E> {
bits: usize,
marker: marker::PhantomData<E>,
}
impl<E> Copy for EnumSet<E> {}
impl<E> Clone for EnumSet<E> {
fn clone(&self) -> EnumSet<E> {
*self
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<E: CLike + fmt::Debug> fmt::Debug for EnumSet<E> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_set().entries(self).finish()
}
}
pub trait CLike {
fn to_usize(&self) -> usize;
fn from_usize(usize) -> Self;
}
fn bit<E: CLike>(e: &E) -> usize {
use core::mem;
let value = e.to_usize();
let bits = mem::size_of::<usize>() * 8;
assert!(value < bits,
"EnumSet only supports up to {} variants.",
bits - 1);
1 << value
}
impl<E: CLike> EnumSet<E> {
pub fn new() -> EnumSet<E> {
EnumSet {
bits: 0,
marker: marker::PhantomData,
}
}
pub fn len(&self) -> usize {
self.bits.count_ones() as usize
}
pub fn is_empty(&self) -> bool {
self.bits == 0
}
pub fn clear(&mut self) {
self.bits = 0;
}
pub fn is_disjoint(&self, other: &EnumSet<E>) -> bool {
(self.bits & other.bits) == 0
}
pub fn is_superset(&self, other: &EnumSet<E>) -> bool {
(self.bits & other.bits) == other.bits
}
pub fn is_subset(&self, other: &EnumSet<E>) -> bool {
other.is_superset(self)
}
pub fn union(&self, e: EnumSet<E>) -> EnumSet<E> {
EnumSet {
bits: self.bits | e.bits,
marker: marker::PhantomData,
}
}
pub fn intersection(&self, e: EnumSet<E>) -> EnumSet<E> {
EnumSet {
bits: self.bits & e.bits,
marker: marker::PhantomData,
}
}
pub fn insert(&mut self, e: E) -> bool {
let result = !self.contains(&e);
self.bits |= bit(&e);
result
}
pub fn remove(&mut self, e: &E) -> bool {
let result = self.contains(e);
self.bits &= !bit(e);
result
}
pub fn contains(&self, e: &E) -> bool {
(self.bits & bit(e)) != 0
}
pub fn iter(&self) -> Iter<E> {
Iter::new(self.bits)
}
}
impl<E: CLike> Sub for EnumSet<E> {
type Output = EnumSet<E>;
fn sub(self, e: EnumSet<E>) -> EnumSet<E> {
EnumSet {
bits: self.bits & !e.bits,
marker: marker::PhantomData,
}
}
}
impl<E: CLike> BitOr for EnumSet<E> {
type Output = EnumSet<E>;
fn bitor(self, e: EnumSet<E>) -> EnumSet<E> {
EnumSet {
bits: self.bits | e.bits,
marker: marker::PhantomData,
}
}
}
impl<E: CLike> BitAnd for EnumSet<E> {
type Output = EnumSet<E>;
fn bitand(self, e: EnumSet<E>) -> EnumSet<E> {
EnumSet {
bits: self.bits & e.bits,
marker: marker::PhantomData,
}
}
}
impl<E: CLike> BitXor for EnumSet<E> {
type Output = EnumSet<E>;
fn bitxor(self, e: EnumSet<E>) -> EnumSet<E> {
EnumSet {
bits: self.bits ^ e.bits,
marker: marker::PhantomData,
}
}
}
pub struct Iter<E> {
index: usize,
bits: usize,
marker: marker::PhantomData<E>,
}
impl<E> Clone for Iter<E> {
fn clone(&self) -> Iter<E> {
Iter {
index: self.index,
bits: self.bits,
marker: marker::PhantomData,
}
}
}
impl<E: CLike> Iter<E> {
fn new(bits: usize) -> Iter<E> {
Iter {
index: 0,
bits: bits,
marker: marker::PhantomData,
}
}
}
impl<E: CLike> Iterator for Iter<E> {
type Item = E;
fn next(&mut self) -> Option<E> {
if self.bits == 0 {
return None;
}
while (self.bits & 1) == 0 {
self.index += 1;
self.bits >>= 1;
}
let elem = CLike::from_usize(self.index);
self.index += 1;
self.bits >>= 1;
Some(elem)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let exact = self.bits.count_ones() as usize;
(exact, Some(exact))
}
}
impl<E: CLike> FromIterator<E> for EnumSet<E> {
fn from_iter<I: IntoIterator<Item = E>>(iter: I) -> EnumSet<E> {
let mut ret = EnumSet::new();
ret.extend(iter);
ret
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, E> IntoIterator for &'a EnumSet<E> where E: CLike
{
type Item = E;
type IntoIter = Iter<E>;
fn into_iter(self) -> Iter<E> {
self.iter()
}
}
impl<E: CLike> Extend<E> for EnumSet<E> {
fn extend<I: IntoIterator<Item = E>>(&mut self, iter: I) {
for element in iter {
self.insert(element);
}
}
}
#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, E: 'a + CLike + Copy> Extend<&'a E> for EnumSet<E> {
fn extend<I: IntoIterator<Item = &'a E>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}