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
use util::*;
use BitSetLike;
#[cfg(feature="parallel")]
pub use self::parallel::{BitParIter, BitProducer};
#[cfg(feature="parallel")]
mod parallel;
#[derive(Debug)]
pub struct BitIter<T> {
set: T,
masks: [usize; 4],
prefix: [u32; 3],
}
impl<T> BitIter<T> {
pub fn new(set: T, masks: [usize; 4], prefix: [u32; 3]) -> Self {
BitIter {
set: set,
masks: masks,
prefix: prefix,
}
}
}
impl<T: BitSetLike> BitIter<T> {
pub fn contains(&self, i: Index) -> bool {
self.set.contains(i)
}
}
impl<T> Iterator for BitIter<T>
where T: BitSetLike
{
type Item = Index;
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.masks[0] != 0 {
let bit = self.masks[0].trailing_zeros();
self.masks[0] &= !(1 << bit);
return Some(self.prefix[0] | bit);
}
if self.masks[1] != 0 {
let bit = self.masks[1].trailing_zeros();
self.masks[1] &= !(1 << bit);
let idx = self.prefix[1] | bit;
self.masks[0] = self.set.layer0(idx as usize);
self.prefix[0] = idx << BITS;
continue;
}
if self.masks[2] != 0 {
let bit = self.masks[2].trailing_zeros();
self.masks[2] &= !(1 << bit);
let idx = self.prefix[2] | bit;
self.masks[1] = self.set.layer1(idx as usize);
self.prefix[1] = idx << BITS;
continue;
}
if self.masks[3] != 0 {
let bit = self.masks[3].trailing_zeros();
self.masks[3] &= !(1 << bit);
self.masks[2] = self.set.layer2(bit as usize);
self.prefix[2] = bit << BITS;
continue;
}
return None;
}
}
}