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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
//! # hibitset
//!
//! Provides hierarchical bit sets,
//! which allow very fast iteration
//! on sparse data structures.
//!
//! ## What it does
//!
//! A `BitSet` may be considered analogous to a `HashSet<u32>`. It
//! tracks whether or not certain indices exist within it. Its
//! implementation is very different, however.
//!
//! At its root, a `BitSet` relies on an array of bits, which express
//! whether or not indices exist. This provides the functionality to
//! `add( )` and `remove( )` indices.
//!
//! This array is referred to as Layer 0. Above it, there is another
//! layer: Layer 1. Layer 1 acts as a 'summary' of Layer 0. It contains
//! one bit for each `usize` bits of Layer 0. If any bit in that `usize`
//! of Layer 0 is set, the bit in Layer 1 will be set.
//!
//! There are, in total, four layers. Layers 1 through 3 are each a
//! summary of the layer immediately below them.
//!
//! ```no_compile
//! Example, with an imaginary 4-bit usize:
//!
//! Layer 3: 1------------------------------------------------ ...
//! Layer 2: 1------------------ 1------------------ 0-------- ...
//! Layer 1: 1--- 0--- 0--- 0--- 1--- 0--- 1--- 0--- 0--- 0--- ...
//! Layer 0: 0010 0000 0000 0000 0011 0000 1111 0000 0000 0000 ...
//! ```
//!
//! This method makes operations that operate over the whole `BitSet`,
//! such as unions, intersections, and iteration, very fast (because if
//! any bit in any summary layer is zero, an entire range of bits
//! below it can be skipped.)
//!
//! However, there is a maximum on index size. The top layer (Layer 3)
//! of the BitSet is a single `usize` long. This makes the maximum index
//! `usize**4` (`1,048,576` for a 32-bit `usize`, `16,777,216` for a
//! 64-bit `usize`). Attempting to add indices larger than that will cause
//! the `BitSet` to panic.
//!

#![deny(missing_docs)]

#[cfg(test)]
extern crate rand;
#[cfg(feature = "parallel")]
extern crate rayon;

mod atomic;
mod iter;
mod ops;
mod util;

pub use atomic::AtomicBitSet;
pub use iter::{BitIter, DrainBitIter};
#[cfg(feature = "parallel")]
pub use iter::{BitParIter, BitProducer};
pub use ops::{BitSetAll, BitSetAnd, BitSetNot, BitSetOr, BitSetXor};

use util::*;

/// A `BitSet` is a simple set designed to track which indices are placed
/// into it.
///
/// Note, a `BitSet` is limited by design to only `usize**4` indices.
/// Adding beyond this limit will cause the `BitSet` to panic.
#[derive(Clone, Debug, Default)]
pub struct BitSet {
    layer3: usize,
    layer2: Vec<usize>,
    layer1: Vec<usize>,
    layer0: Vec<usize>,
}

impl BitSet {
    /// Creates an empty `BitSet`.
    pub fn new() -> BitSet {
        Default::default()
    }

    #[inline]
    fn valid_range(max: Index) {
        if (MAX_EID as u32) < max {
            panic!("Expected index to be less then {}, found {}", MAX_EID, max);
        }
    }

    /// Creates an empty `BitSet`, preallocated for up to `max` indices.
    pub fn with_capacity(max: Index) -> BitSet {
        Self::valid_range(max);
        let mut value = BitSet::new();
        value.extend(max);
        value
    }

    #[inline(never)]
    fn extend(&mut self, id: Index) {
        Self::valid_range(id);
        let (p0, p1, p2) = offsets(id);

        Self::fill_up(&mut self.layer2, p2);
        Self::fill_up(&mut self.layer1, p1);
        Self::fill_up(&mut self.layer0, p0);
    }

    fn fill_up(vec: &mut Vec<usize>, upper_index: usize) {
        if vec.len() <= upper_index {
            vec.resize(upper_index + 1, 0);
        }
    }

    /// This is used to set the levels in the hierarchy
    /// when the lowest layer was set from 0.
    #[inline(never)]
    fn add_slow(&mut self, id: Index) {
        let (_, p1, p2) = offsets(id);
        self.layer1[p1] |= id.mask(SHIFT1);
        self.layer2[p2] |= id.mask(SHIFT2);
        self.layer3 |= id.mask(SHIFT3);
    }

    /// Adds `id` to the `BitSet`. Returns `true` if the value was
    /// already in the set.
    #[inline]
    pub fn add(&mut self, id: Index) -> bool {
        let (p0, mask) = (id.offset(SHIFT1), id.mask(SHIFT0));

        if p0 >= self.layer0.len() {
            self.extend(id);
        }

        if self.layer0[p0] & mask != 0 {
            return true;
        }

        // we need to set the bit on every layer to indicate
        // that the value can be found here.
        let old = self.layer0[p0];
        self.layer0[p0] |= mask;
        if old == 0 {
            self.add_slow(id);
        }
        false
    }

    fn layer_mut(&mut self, level: usize, idx: usize) -> &mut usize {
        match level {
            0 => {
                Self::fill_up(&mut self.layer0, idx);
                &mut self.layer0[idx]
            }
            1 => {
                Self::fill_up(&mut self.layer1, idx);
                &mut self.layer1[idx]
            }
            2 => {
                Self::fill_up(&mut self.layer2, idx);
                &mut self.layer2[idx]
            }
            3 => &mut self.layer3,
            _ => panic!("Invalid layer: {}", level),
        }
    }

    /// Removes `id` from the set, returns `true` if the value
    /// was removed, and `false` if the value was not set
    /// to begin with.
    #[inline]
    pub fn remove(&mut self, id: Index) -> bool {
        let (p0, p1, p2) = offsets(id);

        if p0 >= self.layer0.len() {
            return false;
        }

        if self.layer0[p0] & id.mask(SHIFT0) == 0 {
            return false;
        }

        // if the bitmask was set we need to clear
        // its bit from layer0 to 3. the layers abover only
        // should be cleared if the bit cleared was the last bit
        // in its set
        self.layer0[p0] &= !id.mask(SHIFT0);
        if self.layer0[p0] != 0 {
            return true;
        }

        self.layer1[p1] &= !id.mask(SHIFT1);
        if self.layer1[p1] != 0 {
            return true;
        }

        self.layer2[p2] &= !id.mask(SHIFT2);
        if self.layer2[p2] != 0 {
            return true;
        }

        self.layer3 &= !id.mask(SHIFT3);
        return true;
    }

    /// Returns `true` if `id` is in the set.
    #[inline]
    pub fn contains(&self, id: Index) -> bool {
        let p0 = id.offset(SHIFT1);
        p0 < self.layer0.len() && (self.layer0[p0] & id.mask(SHIFT0)) != 0
    }

    /// Returns `true` if all ids in `other` are contained in this set
    #[inline]
    pub fn contains_set(&self, other: &BitSet) -> bool {
        for id in other.iter() {
            if !self.contains(id) {
                return false;
            }
        }
        true
    }

    /// Completely wipes out the bit set.
    pub fn clear(&mut self) {
        self.layer0.clear();
        self.layer1.clear();
        self.layer2.clear();
        self.layer3 = 0;
    }

    /// How many bits are in a `usize`.
    ///
    /// This value can be trivially determined. It is provided here as a constant for clarity.
    ///
    /// # Example
    ///
    /// ```
    /// use hibitset::BitSet;
    /// assert_eq!(BitSet::BITS_PER_USIZE, std::mem::size_of::<usize>()*8);
    /// ```
    #[cfg(target_pointer_width = "32")]
    pub const BITS_PER_USIZE: usize = 32;

    /// How many bits are in a `usize`.
    ///
    /// This value can be trivially determined. It is provided here as a constant for clarity.
    ///
    /// # Example
    ///
    /// ```
    /// use hibitset::BitSet;
    /// assert_eq!(BitSet::BITS_PER_USIZE, std::mem::size_of::<usize>()*8);
    /// ```
    #[cfg(target_pointer_width = "64")]
    pub const BITS_PER_USIZE: usize = 64;

    /// Returns the bottom layer of the bitset as a slice. Each bit in this slice refers to a single
    /// `Index`.
    ///
    /// The slice's length will be at least the length needed to reflect all the `1`s in the bitset,
    /// but is not otherwise guaranteed. Consider it to be an implementation detail.
    ///
    /// # Example
    ///
    /// ```
    /// use hibitset::BitSet;
    ///
    /// let index: u32 = 12345;
    ///
    /// let mut bitset = BitSet::new();
    /// bitset.add(index);
    ///
    /// // layer 0 is 1:1 with Indexes, so we expect that bit in the slice to be set
    /// let slice = bitset.layer0_as_slice();
    /// let bit_index = index as usize;
    ///
    /// // map that bit index to a usize in the slice and a bit within that usize
    /// let slice_index = bit_index / BitSet::BITS_PER_USIZE;
    /// let bit_at_index = bit_index % BitSet::BITS_PER_USIZE;
    ///
    /// assert_eq!(slice[slice_index], 1 << bit_at_index);
    /// ```
    pub fn layer0_as_slice(&self) -> &[usize] {
        self.layer0.as_slice()
    }

    /// How many `Index`es are described by as single layer 1 bit, intended for use with
    /// `BitSet::layer1_as_slice()`.
    ///
    /// `BitSet`s are defined in terms of `usize`s summarizing `usize`s, so this value can be
    /// trivially determined. It is provided here as a constant for clarity.
    ///
    /// # Example
    ///
    /// ```
    /// use hibitset::BitSet;
    /// assert_eq!(BitSet::LAYER1_GRANULARITY, BitSet::BITS_PER_USIZE);
    /// ```
    pub const LAYER1_GRANULARITY: usize = Self::BITS_PER_USIZE;

    /// Returns the second layer of the bitset as a slice. Each bit in this slice summarizes a
    /// corresponding `usize` from `layer0`. (If `usize` is 64 bits, bit 0 will be set if any
    /// `Index`es 0-63 are set, bit 1 will be set if any `Index`es 64-127 are set, etc.)
    /// `BitSet::LAYER1_GRANULARITY` reflects how many indexes are summarized per layer 1 bit.
    ///
    /// The slice's length is not guaranteed, except that it will be at least the length needed to
    /// reflect all the `1`s in the bitset.
    ///
    /// # Example
    ///
    /// ```
    /// use hibitset::BitSet;
    ///
    /// let index: u32 = 12345;
    ///
    /// let mut bitset = BitSet::new();
    /// bitset.add(index);
    ///
    /// // layer 1 summarizes multiple indexes per bit, so divide appropriately
    /// let slice = bitset.layer1_as_slice();
    /// let bit_index = index as usize / BitSet::LAYER1_GRANULARITY;
    ///
    /// // map that bit index to a usize in the slice and a bit within that usize
    /// let slice_index = bit_index / BitSet::BITS_PER_USIZE;
    /// let bit_at_index = bit_index % BitSet::BITS_PER_USIZE;
    ///
    /// assert_eq!(slice[slice_index], 1 << bit_at_index);
    /// ```
    pub fn layer1_as_slice(&self) -> &[usize] {
        self.layer1.as_slice()
    }

    /// How many `Index`es are described by as single layer 2 bit, intended for use with
    /// `BitSet::layer2_as_slice()`.
    ///
    /// `BitSet`s are defined in terms of `usize`s summarizing `usize`s, so this value can be
    /// trivially determined. It is provided here as a constant for clarity.
    ///
    /// # Example
    ///
    /// ```
    /// use hibitset::BitSet;
    /// assert_eq!(BitSet::LAYER2_GRANULARITY, BitSet::LAYER1_GRANULARITY * BitSet::BITS_PER_USIZE);
    /// ```
    pub const LAYER2_GRANULARITY: usize = Self::LAYER1_GRANULARITY * Self::BITS_PER_USIZE;

    /// Returns the third layer of the bitset as a slice. Each bit in this slice summarizes a
    /// corresponding `usize` from `layer1`. If `usize` is 64 bits, bit 0 will be set if any
    /// `Index`es 0-4095 are set, bit 1 will be set if any `Index`es 4096-8191 are set, etc.
    ///
    /// The slice's length is not guaranteed, except that it will be at least the length needed to
    /// reflect all the `1`s in the bitset.
    ///
    /// # Example
    ///
    /// ```
    /// use hibitset::BitSet;
    ///
    /// let index: u32 = 12345;
    ///
    /// let mut bitset = BitSet::new();
    /// bitset.add(index);
    ///
    /// // layer 2 summarizes multiple indexes per bit, so divide appropriately
    /// let slice = bitset.layer2_as_slice();
    /// let bit_index = index as usize / BitSet::LAYER2_GRANULARITY;
    ///
    /// // map that bit index to a usize in the slice and a bit within that usize
    /// let slice_index = bit_index / BitSet::BITS_PER_USIZE;
    /// let bit_at_index = bit_index % BitSet::BITS_PER_USIZE;
    ///
    /// assert_eq!(slice[slice_index], 1 << bit_at_index);
    /// ```
    pub fn layer2_as_slice(&self) -> &[usize] {
        self.layer2.as_slice()
    }
}

/// A generic interface for [`BitSetLike`]-like types.
///
/// Every `BitSetLike` is hierarchical, meaning that there
/// are multiple levels that branch out in a tree like structure.
///
/// Layer0 each bit represents one Index of the set
/// Layer1 each bit represents one `usize` of Layer0, and will be
/// set only if the word below it is not zero.
/// Layer2 has the same arrangement but with Layer1, and Layer3 with Layer2.
///
/// This arrangement allows for rapid jumps across the key-space.
///
/// [`BitSetLike`]: ../trait.BitSetLike.html
pub trait BitSetLike {
    /// Gets the `usize` corresponding to layer and index.
    ///
    /// The `layer` should be in the range [0, 3]
    fn get_from_layer(&self, layer: usize, idx: usize) -> usize {
        match layer {
            0 => self.layer0(idx),
            1 => self.layer1(idx),
            2 => self.layer2(idx),
            3 => self.layer3(),
            _ => panic!("Invalid layer: {}", layer),
        }
    }

    /// Returns true if this `BitSetLike` contains nothing, and false otherwise.
    fn is_empty(&self) -> bool {
        self.layer3() == 0
    }

    /// Return a `usize` where each bit represents if any word in layer2
    /// has been set.
    fn layer3(&self) -> usize;

    /// Return the `usize` from the array of usizes that indicates if any
    /// bit has been set in layer1
    fn layer2(&self, i: usize) -> usize;

    /// Return the `usize` from the array of usizes that indicates if any
    /// bit has been set in layer0
    fn layer1(&self, i: usize) -> usize;

    /// Return a `usize` that maps to the direct 1:1 association with
    /// each index of the set
    fn layer0(&self, i: usize) -> usize;

    /// Allows checking if set bit is contained in the bit set.
    fn contains(&self, i: Index) -> bool;

    /// Create an iterator that will scan over the keyspace
    fn iter(self) -> BitIter<Self>
    where
        Self: Sized,
    {
        let layer3 = self.layer3();

        BitIter::new(self, [0, 0, 0, layer3], [0; LAYERS - 1])
    }

    /// Create a parallel iterator that will scan over the keyspace
    #[cfg(feature = "parallel")]
    fn par_iter(self) -> BitParIter<Self>
    where
        Self: Sized,
    {
        BitParIter::new(self)
    }
}

/// A extension to the [`BitSetLike`] trait which allows draining it.
pub trait DrainableBitSet: BitSetLike {
    /// Removes bit from the bit set.
    ///
    /// Returns `true` if removal happened and `false` otherwise.
    fn remove(&mut self, i: Index) -> bool;

    /// Create a draining iterator that will scan over the keyspace and clears it while doing so.
    fn drain<'a>(&'a mut self) -> DrainBitIter<'a, Self>
    where
        Self: Sized,
    {
        let layer3 = self.layer3();

        DrainBitIter::new(self, [0, 0, 0, layer3], [0; LAYERS - 1])
    }
}

impl<'a, T> BitSetLike for &'a T
where
    T: BitSetLike + ?Sized,
{
    #[inline]
    fn layer3(&self) -> usize {
        (*self).layer3()
    }

    #[inline]
    fn layer2(&self, i: usize) -> usize {
        (*self).layer2(i)
    }

    #[inline]
    fn layer1(&self, i: usize) -> usize {
        (*self).layer1(i)
    }

    #[inline]
    fn layer0(&self, i: usize) -> usize {
        (*self).layer0(i)
    }

    #[inline]
    fn contains(&self, i: Index) -> bool {
        (*self).contains(i)
    }
}

impl<'a, T> BitSetLike for &'a mut T
where
    T: BitSetLike + ?Sized,
{
    #[inline]
    fn layer3(&self) -> usize {
        (**self).layer3()
    }

    #[inline]
    fn layer2(&self, i: usize) -> usize {
        (**self).layer2(i)
    }

    #[inline]
    fn layer1(&self, i: usize) -> usize {
        (**self).layer1(i)
    }

    #[inline]
    fn layer0(&self, i: usize) -> usize {
        (**self).layer0(i)
    }

    #[inline]
    fn contains(&self, i: Index) -> bool {
        (**self).contains(i)
    }
}

impl<'a, T> DrainableBitSet for &'a mut T
where
    T: DrainableBitSet,
{
    #[inline]
    fn remove(&mut self, i: Index) -> bool {
        (**self).remove(i)
    }
}

impl BitSetLike for BitSet {
    #[inline]
    fn layer3(&self) -> usize {
        self.layer3
    }

    #[inline]
    fn layer2(&self, i: usize) -> usize {
        self.layer2.get(i).map(|&x| x).unwrap_or(0)
    }

    #[inline]
    fn layer1(&self, i: usize) -> usize {
        self.layer1.get(i).map(|&x| x).unwrap_or(0)
    }

    #[inline]
    fn layer0(&self, i: usize) -> usize {
        self.layer0.get(i).map(|&x| x).unwrap_or(0)
    }

    #[inline]
    fn contains(&self, i: Index) -> bool {
        self.contains(i)
    }
}

impl DrainableBitSet for BitSet {
    #[inline]
    fn remove(&mut self, i: Index) -> bool {
        self.remove(i)
    }
}

impl PartialEq for BitSet {
    #[inline]
    fn eq(&self, rhv: &BitSet) -> bool {
        if self.layer3 != rhv.layer3 {
            return false;
        }
        if self.layer2.len() != rhv.layer2.len()
            || self.layer1.len() != rhv.layer1.len()
            || self.layer0.len() != rhv.layer0.len()
        {
            return false;
        }

        for i in 0..self.layer2.len() {
            if self.layer2(i) != rhv.layer2(i) {
                return false;
            }
        }
        for i in 0..self.layer1.len() {
            if self.layer1(i) != rhv.layer1(i) {
                return false;
            }
        }
        for i in 0..self.layer0.len() {
            if self.layer0(i) != rhv.layer0(i) {
                return false;
            }
        }

        true
    }
}
impl Eq for BitSet {}

#[cfg(test)]
mod tests {
    use super::{BitSet, BitSetAnd, BitSetLike, BitSetNot};

    #[test]
    fn insert() {
        let mut c = BitSet::new();
        for i in 0..1_000 {
            assert!(!c.add(i));
            assert!(c.add(i));
        }

        for i in 0..1_000 {
            assert!(c.contains(i));
        }
    }

    #[test]
    fn insert_100k() {
        let mut c = BitSet::new();
        for i in 0..100_000 {
            assert!(!c.add(i));
            assert!(c.add(i));
        }

        for i in 0..100_000 {
            assert!(c.contains(i));
        }
    }
    #[test]
    fn remove() {
        let mut c = BitSet::new();
        for i in 0..1_000 {
            assert!(!c.add(i));
        }

        for i in 0..1_000 {
            assert!(c.contains(i));
            assert!(c.remove(i));
            assert!(!c.contains(i));
            assert!(!c.remove(i));
        }
    }

    #[test]
    fn iter() {
        let mut c = BitSet::new();
        for i in 0..100_000 {
            c.add(i);
        }

        let mut count = 0;
        for (idx, i) in c.iter().enumerate() {
            count += 1;
            assert_eq!(idx, i as usize);
        }
        assert_eq!(count, 100_000);
    }

    #[test]
    fn iter_odd_even() {
        let mut odd = BitSet::new();
        let mut even = BitSet::new();
        for i in 0..100_000 {
            if i % 2 == 1 {
                odd.add(i);
            } else {
                even.add(i);
            }
        }

        assert_eq!((&odd).iter().count(), 50_000);
        assert_eq!((&even).iter().count(), 50_000);
        assert_eq!(BitSetAnd(&odd, &even).iter().count(), 0);
    }

    #[test]
    fn iter_random_add() {
        use rand::prelude::*;

        let mut set = BitSet::new();
        let mut rng = thread_rng();
        let limit = 1_048_576;
        let mut added = 0;
        for _ in 0..(limit / 10) {
            let index = rng.gen_range(0, limit);
            if !set.add(index) {
                added += 1;
            }
        }
        assert_eq!(set.iter().count(), added as usize);
    }

    #[test]
    fn iter_clusters() {
        let mut set = BitSet::new();
        for x in 0..8 {
            let x = (x * 3) << (::BITS * 2); // scale to the last slot
            for y in 0..8 {
                let y = (y * 3) << (::BITS);
                for z in 0..8 {
                    let z = z * 2;
                    set.add(x + y + z);
                }
            }
        }
        assert_eq!(set.iter().count(), 8usize.pow(3));
    }

    #[test]
    fn not() {
        let mut c = BitSet::new();
        for i in 0..10_000 {
            if i % 2 == 1 {
                c.add(i);
            }
        }
        let d = BitSetNot(c);
        for (idx, i) in d.iter().take(5_000).enumerate() {
            assert_eq!(idx * 2, i as usize);
        }
    }
}

#[cfg(all(test, feature = "parallel"))]
mod test_parallel {
    use super::{BitSet, BitSetAnd, BitSetLike};
    use rayon::iter::ParallelIterator;

    #[test]
    fn par_iter_one() {
        let step = 5000;
        let tests = 1_048_576 / step;
        for n in 0..tests {
            let n = n * step;
            let mut set = BitSet::new();
            set.add(n);
            assert_eq!(set.par_iter().count(), 1);
        }
        let mut set = BitSet::new();
        set.add(1_048_576 - 1);
        assert_eq!(set.par_iter().count(), 1);
    }

    #[test]
    fn par_iter_random_add() {
        use rand::prelude::*;
        use std::collections::HashSet;
        use std::sync::{Arc, Mutex};

        let mut set = BitSet::new();
        let mut check_set = HashSet::new();
        let mut rng = thread_rng();
        let limit = 1_048_576;
        for _ in 0..(limit / 10) {
            let index = rng.gen_range(0, limit);
            set.add(index);
            check_set.insert(index);
        }
        let check_set = Arc::new(Mutex::new(check_set));
        let missing_set = Arc::new(Mutex::new(HashSet::new()));
        set.par_iter().for_each(|n| {
            let check_set = check_set.clone();
            let missing_set = missing_set.clone();
            let mut check = check_set.lock().unwrap();
            if !check.remove(&n) {
                let mut missing = missing_set.lock().unwrap();
                missing.insert(n);
            }
        });
        let check_set = check_set.lock().unwrap();
        let missing_set = missing_set.lock().unwrap();
        if !check_set.is_empty() && !missing_set.is_empty() {
            panic!(
                "There were values that didn't get iterated: {:?}
            There were values that got iterated, but that shouldn't be: {:?}",
                *check_set, *missing_set
            );
        }
        if !check_set.is_empty() {
            panic!(
                "There were values that didn't get iterated: {:?}",
                *check_set
            );
        }
        if !missing_set.is_empty() {
            panic!(
                "There were values that got iterated, but that shouldn't be: {:?}",
                *missing_set
            );
        }
    }

    #[test]
    fn par_iter_odd_even() {
        let mut odd = BitSet::new();
        let mut even = BitSet::new();
        for i in 0..100_000 {
            if i % 2 == 1 {
                odd.add(i);
            } else {
                even.add(i);
            }
        }

        assert_eq!((&odd).par_iter().count(), 50_000);
        assert_eq!((&even).par_iter().count(), 50_000);
        assert_eq!(BitSetAnd(&odd, &even).par_iter().count(), 0);
    }

    #[test]
    fn par_iter_clusters() {
        use std::collections::HashSet;
        use std::sync::{Arc, Mutex};
        let mut set = BitSet::new();
        let mut check_set = HashSet::new();
        for x in 0..8 {
            let x = (x * 3) << (::BITS * 2); // scale to the last slot
            for y in 0..8 {
                let y = (y * 3) << (::BITS);
                for z in 0..8 {
                    let z = z * 2;
                    let index = x + y + z;
                    set.add(index);
                    check_set.insert(index);
                }
            }
        }
        let check_set = Arc::new(Mutex::new(check_set));
        let missing_set = Arc::new(Mutex::new(HashSet::new()));
        set.par_iter().for_each(|n| {
            let check_set = check_set.clone();
            let missing_set = missing_set.clone();
            let mut check = check_set.lock().unwrap();
            if !check.remove(&n) {
                let mut missing = missing_set.lock().unwrap();
                missing.insert(n);
            }
        });
        let check_set = check_set.lock().unwrap();
        let missing_set = missing_set.lock().unwrap();
        if !check_set.is_empty() && !missing_set.is_empty() {
            panic!(
                "There were values that didn't get iterated: {:?}
            There were values that got iterated, but that shouldn't be: {:?}",
                *check_set, *missing_set
            );
        }
        if !check_set.is_empty() {
            panic!(
                "There were values that didn't get iterated: {:?}",
                *check_set
            );
        }
        if !missing_set.is_empty() {
            panic!(
                "There were values that got iterated, but that shouldn't be: {:?}",
                *missing_set
            );
        }
    }
}