bit-set 0.10.1

A set of bits
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
use crate::{local_prelude::*, util};

#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(
    feature = "borsh",
    derive(borsh::BorshDeserialize, borsh::BorshSerialize)
)]
#[cfg_attr(
    feature = "miniserde",
    derive(miniserde::Deserialize, miniserde::Serialize)
)]
pub struct BitSet<B: BitBlock = u32> {
    pub(crate) bit_vec: BitVec<B>,
}

impl<B: BitBlock> Clone for BitSet<B> {
    fn clone(&self) -> Self {
        BitSet {
            bit_vec: self.bit_vec.clone(),
        }
    }

    fn clone_from(&mut self, other: &Self) {
        self.bit_vec.clone_from(&other.bit_vec);
    }
}

impl<B: BitBlock> Default for BitSet<B> {
    #[inline]
    fn default() -> Self {
        BitSet {
            bit_vec: Default::default(),
        }
    }
}

impl<B: BitBlock> FromIterator<usize> for BitSet<B> {
    fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> Self {
        let mut ret = Self::default();
        ret.extend(iter);
        ret
    }
}

impl<B: BitBlock> Extend<usize> for BitSet<B> {
    #[inline]
    fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I) {
        for i in iter {
            self.insert(i);
        }
    }
}

impl<B: BitBlock> PartialOrd for BitSet<B> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<B: BitBlock> Ord for BitSet<B> {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.iter().cmp(other)
    }
}

impl<B: BitBlock> PartialEq for BitSet<B> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.iter().eq(other)
    }
}

impl<B: BitBlock> Eq for BitSet<B> {}

impl BitSet<u32> {
    /// Creates a new empty `BitSet`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut s = BitSet::new();
    /// ```
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new `BitSet` with initially no contents, able to
    /// hold `nbits` elements without resizing.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut s = BitSet::with_capacity(100);
    /// assert!(s.capacity() >= 100);
    /// ```
    #[inline]
    pub fn with_capacity(nbits: usize) -> Self {
        let bit_vec = BitVec::from_elem(nbits, false);
        Self::from_bit_vec(bit_vec)
    }

    /// Creates a new `BitSet` from the given bit vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_vec::BitVec;
    /// use bit_set::BitSet;
    ///
    /// let bv = BitVec::from_bytes(&[0b01100000]);
    /// let s = BitSet::from_bit_vec(bv);
    ///
    /// // Print 1, 2 in arbitrary order
    /// for x in s.iter() {
    ///     println!("{}", x);
    /// }
    /// ```
    #[inline]
    pub fn from_bit_vec(bit_vec: BitVec) -> Self {
        BitSet { bit_vec }
    }

    pub fn from_bytes(bytes: &[u8]) -> Self {
        BitSet {
            bit_vec: BitVec::from_bytes(bytes),
        }
    }
}

#[allow(clippy::multiple_inherent_impl)]
impl<B: BitBlock> BitSet<B> {
    /// Creates a new empty `BitSet`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut s = <BitSet>::new_general();
    /// ```
    #[inline]
    pub fn new_general() -> Self {
        Self::default()
    }

    /// Creates a new `BitSet` with initially no contents, able to
    /// hold `nbits` elements without resizing.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut s = <BitSet>::with_capacity_general(100);
    /// assert!(s.capacity() >= 100);
    /// ```
    #[inline]
    pub fn with_capacity_general(nbits: usize) -> Self {
        let bit_vec = BitVec::from_elem_general(nbits, false);
        Self::from_bit_vec_general(bit_vec)
    }

    /// Creates a new `BitSet` from the given bit vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_vec::BitVec;
    /// use bit_set::BitSet;
    ///
    /// let bv: BitVec<u64> = BitVec::from_bytes_general(&[0b01100000]);
    /// let s = BitSet::from_bit_vec_general(bv);
    ///
    /// // Print 1, 2 in arbitrary order
    /// for x in s.iter() {
    ///     println!("{}", x);
    /// }
    /// ```
    #[inline]
    pub fn from_bit_vec_general(bit_vec: BitVec<B>) -> Self {
        BitSet { bit_vec }
    }

    pub fn from_bytes_general(bytes: &[u8]) -> Self {
        BitSet {
            bit_vec: BitVec::from_bytes_general(bytes),
        }
    }

    /// Returns the capacity in bits for this bit vector. Inserting any
    /// element less than this amount will not trigger a resizing.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut s = BitSet::with_capacity(100);
    /// assert!(s.capacity() >= 100);
    /// ```
    #[inline]
    pub fn capacity(&self) -> usize {
        self.bit_vec.capacity()
    }

    /// Reserves capacity for the given `BitSet` to contain `len` distinct elements. In the case
    /// of `BitSet` this means reallocations will not occur as long as all inserted elements
    /// are less than `len`.
    ///
    /// The collection may reserve more space to avoid frequent reallocations.
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut s = BitSet::new();
    /// s.reserve_len(10);
    /// assert!(s.capacity() >= 10);
    /// ```
    pub fn reserve_len(&mut self, len: usize) {
        let cur_len = self.bit_vec.len();
        if len >= cur_len {
            self.bit_vec.reserve(len - cur_len);
        }
    }

    /// Reserves the minimum capacity for the given `BitSet` to contain `len` distinct elements.
    /// In the case of `BitSet` this means reallocations will not occur as long as all inserted
    /// elements are less than `len`.
    ///
    /// Note that the allocator may give the collection more space than it requests. Therefore
    /// capacity can not be relied upon to be precisely minimal. Prefer `reserve_len` if future
    /// insertions are expected.
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut s = BitSet::new();
    /// s.reserve_len_exact(10);
    /// assert!(s.capacity() >= 10);
    /// ```
    pub fn reserve_len_exact(&mut self, len: usize) {
        let cur_len = self.bit_vec.len();
        if len >= cur_len {
            self.bit_vec.reserve_exact(len - cur_len);
        }
    }

    /// Consumes this set to return the underlying bit vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut s = BitSet::new();
    /// s.insert(0);
    /// s.insert(3);
    ///
    /// let bv = s.into_bit_vec();
    /// assert!(bv[0]);
    /// assert!(bv[3]);
    /// ```
    #[inline]
    pub fn into_bit_vec(self) -> BitVec<B> {
        self.bit_vec
    }

    /// Returns a reference to the underlying bit vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut set = BitSet::new();
    /// set.insert(0);
    ///
    /// let bv = set.get_ref();
    /// assert_eq!(bv[0], true);
    /// ```
    #[inline]
    pub fn get_ref(&self) -> &BitVec<B> {
        &self.bit_vec
    }

    /// Returns a mutable reference to the underlying bit vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut set = BitSet::new();
    /// set.insert(0);
    /// set.insert(3);
    ///
    /// {
    ///     let bv = set.get_mut();
    ///     bv.set(1, true);
    /// }
    ///
    /// assert!(set.contains(0));
    /// assert!(set.contains(1));
    /// assert!(set.contains(3));
    /// ```
    #[inline]
    pub fn get_mut(&mut self) -> &mut BitVec<B> {
        &mut self.bit_vec
    }

    /// # Safety
    ///
    /// Safe and upholds invariant if function `f` does not alter most
    /// significant bits of the first argument where respective bits
    /// in the second argument are equal 0.
    ///
    /// In other words, this is safe if `f` is XOR, OR, AND, but violates
    /// invariant if it is XNOR, NAND.
    ///
    /// See the safety section below.
    #[inline]
    fn other_op<F>(&mut self, other: &Self, mut f: F)
    where
        F: FnMut(B, B) -> B,
    {
        // Unwrap BitVecs
        let self_bit_vec = &mut self.bit_vec;
        let other_bit_vec = &other.bit_vec;

        let self_len = self_bit_vec.len();
        let other_len = other_bit_vec.len();

        // Expand the vector if necessary
        if self_len < other_len {
            self_bit_vec.grow(other_len - self_len, false);
        }

        // virtually pad other with 0's for equal lengths
        let other_words = util::match_words(self_bit_vec, other_bit_vec).1;

        debug_assert!(self_bit_vec.len() >= other_bit_vec.len());

        // Apply values found in other
        for (i, w) in other_words {
            let old = self_bit_vec.storage()[i];
            let new = f(old, w);
            // Safety:
            // We do not change the underlying Vec's size, so this is always ok.
            // - What do we do to uphold the invariant for trailing bits?
            // - We have a debug assert below that guards us against polluting
            //   trailing bits.
            unsafe {
                self_bit_vec.storage_mut()[i] = new;
            }
            if i == self_bit_vec.storage().len() - 1 && self_bit_vec.len() % B::bits() > 0 {
                debug_assert!(new >> (self_bit_vec.len() % B::bits()) == B::zero());
            }
        }
    }

    /// Truncates the underlying vector to the least length required.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let mut s = BitSet::new();
    /// s.insert(3231);
    /// s.remove(3231);
    ///
    /// // Internal storage will probably be bigger than necessary
    /// println!("old capacity: {}", s.capacity());
    /// assert!(s.capacity() >= 3231);
    ///
    /// // Now should be smaller
    /// s.shrink_to_fit();
    /// println!("new capacity: {}", s.capacity());
    /// ```
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        let bit_vec = &mut self.bit_vec;
        // Obtain original length
        let old_len = bit_vec.storage().len();
        // Obtain coarse trailing zero length
        let n = bit_vec
            .storage()
            .iter()
            .rev()
            .take_while(|&&n| n == B::zero())
            .count();
        // Truncate away all empty trailing blocks, then shrink_to_fit
        let trunc_len = old_len - n;
        // Safety:
        // Those function calls may seem unsafe, but they are guaranteed
        // not to introduce any memory unsafety.
        // We set the correct length as a multiple of `B::bits()`,
        // thus maintaining the trailing bit invariant.
        unsafe {
            bit_vec.storage_mut().truncate(trunc_len);
            bit_vec.set_len(trunc_len * B::bits());
        }
        bit_vec.shrink_to_fit();
    }

    /// Unions in-place with the specified other bit vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let a   = 0b01101000;
    /// let b   = 0b10100000;
    /// let res = 0b11101000;
    ///
    /// let mut a = BitSet::from_bytes(&[a]);
    /// let b = BitSet::from_bytes(&[b]);
    /// let res = BitSet::from_bytes(&[res]);
    ///
    /// a.union_with(&b);
    /// assert_eq!(a, res);
    /// ```
    #[inline]
    pub fn union_with(&mut self, other: &Self) {
        self.other_op(other, |w1, w2| w1 | w2);
    }

    /// Intersects in-place with the specified other bit vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let a   = 0b01101000;
    /// let b   = 0b10100000;
    /// let res = 0b00100000;
    ///
    /// let mut a = BitSet::from_bytes(&[a]);
    /// let b = BitSet::from_bytes(&[b]);
    /// let res = BitSet::from_bytes(&[res]);
    ///
    /// a.intersect_with(&b);
    /// assert_eq!(a, res);
    /// ```
    #[inline]
    pub fn intersect_with(&mut self, other: &Self) {
        self.other_op(other, |w1, w2| w1 & w2);
    }

    /// Makes this bit vector the difference with the specified other bit vector
    /// in-place.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let a   = 0b01101000;
    /// let b   = 0b10100000;
    /// let a_b = 0b01001000; // a - b
    /// let b_a = 0b10000000; // b - a
    ///
    /// let mut bva = BitSet::from_bytes(&[a]);
    /// let bvb = BitSet::from_bytes(&[b]);
    /// let bva_b = BitSet::from_bytes(&[a_b]);
    /// let bvb_a = BitSet::from_bytes(&[b_a]);
    ///
    /// bva.difference_with(&bvb);
    /// assert_eq!(bva, bva_b);
    ///
    /// let bva = BitSet::from_bytes(&[a]);
    /// let mut bvb = BitSet::from_bytes(&[b]);
    ///
    /// bvb.difference_with(&bva);
    /// assert_eq!(bvb, bvb_a);
    /// ```
    #[inline]
    pub fn difference_with(&mut self, other: &Self) {
        self.other_op(other, |w1, w2| w1 & !w2);
    }

    /// Makes this bit vector the symmetric difference with the specified other
    /// bit vector in-place.
    ///
    /// # Examples
    ///
    /// ```
    /// use bit_set::BitSet;
    ///
    /// let a   = 0b01101000;
    /// let b   = 0b10100000;
    /// let res = 0b11001000;
    ///
    /// let mut a = BitSet::from_bytes(&[a]);
    /// let b = BitSet::from_bytes(&[b]);
    /// let res = BitSet::from_bytes(&[res]);
    ///
    /// a.symmetric_difference_with(&b);
    /// assert_eq!(a, res);
    /// ```
    #[inline]
    pub fn symmetric_difference_with(&mut self, other: &Self) {
        self.other_op(other, |w1, w2| w1 ^ w2);
    }

    /*
        /// Moves all elements from `other` into `Self`, leaving `other` empty.
        ///
        /// # Examples
        ///
        /// ```
        /// use bit_set::BitSet;
        ///
        /// let mut a = BitSet::new();
        /// a.insert(2);
        /// a.insert(6);
        ///
        /// let mut b = BitSet::new();
        /// b.insert(1);
        /// b.insert(3);
        /// b.insert(6);
        ///
        /// a.append(&mut b);
        ///
        /// assert_eq!(a.len(), 4);
        /// assert_eq!(b.len(), 0);
        /// assert_eq!(a, BitSet::from_bytes(&[0b01110010]));
        /// ```
        pub fn append(&mut self, other: &mut Self) {
            self.union_with(other);
            other.clear();
        }

        /// Splits the `BitSet` into two at the given key including the key.
        /// Retains the first part in-place while returning the second part.
        ///
        /// # Examples
        ///
        /// ```
        /// use bit_set::BitSet;
        ///
        /// let mut a = BitSet::new();
        /// a.insert(2);
        /// a.insert(6);
        /// a.insert(1);
        /// a.insert(3);
        ///
        /// let b = a.split_off(3);
        ///
        /// assert_eq!(a.len(), 2);
        /// assert_eq!(b.len(), 2);
        /// assert_eq!(a, BitSet::from_bytes(&[0b01100000]));
        /// assert_eq!(b, BitSet::from_bytes(&[0b00010010]));
        /// ```
        pub fn split_off(&mut self, at: usize) -> Self {
            let mut other = BitSet::new();

            if at == 0 {
                swap(self, &mut other);
                return other;
            } else if at >= self.bit_vec.len() {
                return other;
            }

            // Calculate block and bit at which to split
            let w = at / BITS;
            let b = at % BITS;

            // Pad `other` with `w` zero blocks,
            // append `self`'s blocks in the range from `w` to the end to `other`
            other.bit_vec.storage_mut().extend(repeat(0u32).take(w)
                                         .chain(self.bit_vec.storage()[w..].iter().cloned()));
            other.bit_vec.nbits = self.bit_vec.nbits;

            if b > 0 {
                other.bit_vec.storage_mut()[w] &= !0 << b;
            }

            // Sets `bit_vec.len()` and fixes the last block as well
            self.bit_vec.truncate(at);

            other
        }
    */

    /// Counts the number of set bits in this set.
    ///
    /// Note that this function scans the set to calculate the number.
    #[inline]
    pub fn count(&self) -> usize {
        self.bit_vec.blocks().fold(0, |acc, n| acc + n.count_ones())
    }

    /// Counts the number of set bits in this set.
    ///
    /// Note that this function scans the set to calculate the number.
    #[inline]
    #[deprecated = "use BitVec::count() instead"]
    pub fn len(&self) -> usize {
        self.count()
    }

    /// Returns whether there are no bits set in this set
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.bit_vec.none()
    }

    /// Removes all elements of this set.
    ///
    /// Different from [`reset`] only in that the capacity is preserved.
    ///
    /// [`reset`]: Self::reset
    #[inline]
    pub fn make_empty(&mut self) {
        self.bit_vec.fill(false);
    }

    /// Resets this set to an empty state.
    ///
    /// Different from [`make_empty`] only in that the capacity may NOT be preserved.
    ///
    /// [`make_empty`]: Self::make_empty
    #[inline]
    pub fn reset(&mut self) {
        self.bit_vec.remove_all();
    }

    /// Clears all bits in this set
    #[deprecated(since = "0.9.0", note = "please use `fn make_empty` instead")]
    #[inline]
    pub fn clear(&mut self) {
        self.make_empty();
    }

    /// Returns `true` if this set contains the specified integer.
    #[inline]
    pub fn contains(&self, value: usize) -> bool {
        let bit_vec = &self.bit_vec;
        value < bit_vec.len() && bit_vec[value]
    }

    /// Returns `true` if the set has no elements in common with `other`.
    /// This is equivalent to checking for an empty intersection.
    #[inline]
    pub fn is_disjoint(&self, other: &Self) -> bool {
        self.intersection(other).next().is_none()
    }

    /// Returns `true` if the set is a subset of another.
    #[inline]
    pub fn is_subset(&self, other: &Self) -> bool {
        let self_bit_vec = &self.bit_vec;
        let other_bit_vec = &other.bit_vec;
        let other_blocks = util::blocks_for_bits::<B>(other_bit_vec.len());

        // Check that `self` intersect `other` is self
        self_bit_vec.blocks().zip(other_bit_vec.blocks()).all(|(w1, w2)| w1 & w2 == w1) &&
        // Make sure if `self` has any more blocks than `other`, they're all 0
        self_bit_vec.blocks().skip(other_blocks).all(|w| w == B::zero())
    }

    /// Returns `true` if the set is a superset of another.
    #[inline]
    pub fn is_superset(&self, other: &Self) -> bool {
        other.is_subset(self)
    }

    /// Adds a value to the set. Returns `true` if the value was not already
    /// present in the set.
    pub fn insert(&mut self, value: usize) -> bool {
        if self.contains(value) {
            return false;
        }

        // Ensure we have enough space to hold the new element
        let len = self.bit_vec.len();
        if value >= len {
            self.bit_vec.grow(value - len + 1, false);
        }

        self.bit_vec.set(value, true);
        true
    }

    /// Removes a value from the set. Returns `true` if the value was
    /// present in the set.
    pub fn remove(&mut self, value: usize) -> bool {
        if !self.contains(value) {
            return false;
        }

        self.bit_vec.set(value, false);

        true
    }

    /// Excludes `element` and all greater elements from the `BitSet`.
    pub fn truncate(&mut self, element: usize) {
        self.bit_vec.truncate(element);
    }
}

impl<B: BitBlock> fmt::Debug for BitSet<B> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("BitSet")
            .field("bit_vec", &self.bit_vec)
            .finish()
    }
}

impl<B: BitBlock> fmt::Display for BitSet<B> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_set().entries(self).finish()
    }
}

impl<B: BitBlock> hash::Hash for BitSet<B> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        for pos in self {
            pos.hash(state);
        }
    }
}