indexical/bitset/
bitvec.rs

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
//! A bit-set from the [`bitvec`] crate.

use bitvec::{prelude::Lsb0, slice::IterOnes};

use crate::{
    bitset::BitSet,
    pointer::{ArcFamily, RcFamily, RefFamily},
};

pub use ::bitvec::{self, vec::BitVec};

impl BitSet for BitVec {
    type Iter<'a> = IterOnes<'a, usize, Lsb0>;

    fn empty(size: usize) -> Self {
        bitvec::bitvec![usize, Lsb0; 0; size]
    }

    fn contains(&self, index: usize) -> bool {
        self[index]
    }

    fn insert(&mut self, index: usize) -> bool {
        let contained = self[index];
        self.set(index, true);
        !contained
    }

    fn iter(&self) -> Self::Iter<'_> {
        self.iter_ones()
    }

    fn len(&self) -> usize {
        self.count_ones()
    }

    fn union(&mut self, other: &Self) {
        *self |= other;
    }

    fn intersect(&mut self, other: &Self) {
        *self &= other;
    }

    fn invert(&mut self) {
        take_mut::take(self, |this| !this)
    }

    fn clear(&mut self) {
        self.clear();
    }

    fn subtract(&mut self, other: &Self) {
        let mut other_copy = other.clone();
        other_copy.invert();
        self.intersect(&other_copy);
    }

    fn insert_all(&mut self) {
        self.fill(true);
    }

    fn copy_from(&mut self, other: &Self) {
        self.copy_from_bitslice(other);
    }
}

/// [`IndexSet`](crate::IndexSet) specialized to the [`BitVec`] implementation.
pub type IndexSet<T> = crate::IndexSet<'static, T, BitVec, RcFamily>;

/// [`IndexSet`](crate::IndexSet) specialized to the [`BitVec`] implementation with the [`ArcFamily`].
pub type ArcIndexSet<'a, T> = crate::IndexSet<'a, T, BitVec, ArcFamily>;

/// [`IndexSet`](crate::IndexSet) specialized to the [`BitVec`] implementation with the [`RefFamily`].
pub type RefIndexSet<'a, T> = crate::IndexSet<'a, T, BitVec, RefFamily<'a>>;

/// [`IndexMatrix`](crate::IndexMatrix) specialized to the [`BitVec`] implementation.
pub type IndexMatrix<R, C> = crate::IndexMatrix<'static, R, C, BitVec, RcFamily>;

/// [`IndexMatrix`](crate::IndexMatrix) specialized to the [`BitVec`] implementation with the [`ArcFamily`].
pub type ArcIndexMatrix<R, C> = crate::IndexMatrix<'static, R, C, BitVec, ArcFamily>;

/// [`IndexMatrix`](crate::IndexMatrix) specialized to the [`BitVec`] implementation with the [`RefFamily`].
pub type RefIndexMatrix<'a, R, C> = crate::IndexMatrix<'a, R, C, BitVec, RefFamily<'a>>;

#[test]
fn test_bitvec() {
    crate::test_utils::impl_test::<BitVec>();
}