hibitset/iter/
drain.rs
1use iter::BitIter;
2use util::*;
3use DrainableBitSet;
4
5pub struct DrainBitIter<'a, T: 'a> {
9 iter: BitIter<&'a mut T>,
10}
11
12impl<'a, T: DrainableBitSet> DrainBitIter<'a, T> {
13 pub fn new(set: &'a mut T, masks: [usize; LAYERS], prefix: [u32; LAYERS - 1]) -> Self {
18 DrainBitIter {
19 iter: BitIter::new(set, masks, prefix),
20 }
21 }
22}
23
24impl<'a, T> Iterator for DrainBitIter<'a, T>
25where
26 T: DrainableBitSet,
27{
28 type Item = Index;
29
30 fn next(&mut self) -> Option<Self::Item> {
31 let next = self.iter.next();
32 if let Some(next) = next {
33 self.iter.set.remove(next);
34 }
35 next
36 }
37}
38
39#[test]
40fn drain_all() {
41 use {BitSet, BitSetLike};
42 let mut bit_set: BitSet = (0..10000).filter(|i| i % 2 == 0).collect();
43 bit_set.drain().for_each(|_| {});
44 assert_eq!(0, bit_set.iter().count());
45}