indexical/bitset/
bitvec.rs

1//! A bit-set from the [`bitvec`] crate.
2
3use bitvec::{prelude::Lsb0, store::BitStore};
4
5use crate::{
6    bitset::BitSet,
7    pointer::{ArcFamily, RcFamily, RefFamily},
8};
9
10pub use ::bitvec::{self, vec::BitVec};
11
12impl BitSet for BitVec {
13    fn empty(size: usize) -> Self {
14        bitvec::bitvec![usize, Lsb0; 0; size]
15    }
16
17    fn contains(&self, index: usize) -> bool {
18        self[index]
19    }
20
21    fn insert(&mut self, index: usize) -> bool {
22        let contained = self[index];
23        self.set(index, true);
24        !contained
25    }
26
27    fn remove(&mut self, index: usize) -> bool {
28        let contained = self[index];
29        self.set(index, false);
30        contained
31    }
32
33    fn iter(&self) -> impl Iterator<Item = usize> {
34        self.iter_ones()
35    }
36
37    fn len(&self) -> usize {
38        self.count_ones()
39    }
40
41    fn union(&mut self, other: &Self) {
42        *self |= other;
43    }
44
45    fn intersect(&mut self, other: &Self) {
46        *self &= other;
47    }
48
49    fn invert(&mut self) {
50        // Inline defn of Not::not bc it assumes ownership of the BitVec
51        for elem in self.as_raw_mut_slice() {
52            elem.store_value(!elem.load_value());
53        }
54    }
55
56    fn clear(&mut self) {
57        for elem in self.as_raw_mut_slice() {
58            elem.store_value(0);
59        }
60    }
61
62    fn subtract(&mut self, other: &Self) {
63        let mut other_copy = other.clone();
64        other_copy.invert();
65        self.intersect(&other_copy);
66    }
67
68    fn insert_all(&mut self) {
69        self.fill(true);
70    }
71
72    fn copy_from(&mut self, other: &Self) {
73        self.copy_from_bitslice(other);
74    }
75}
76
77/// [`IndexSet`](crate::set::IndexSet) specialized to the [`BitVec`] implementation with the [`RcFamily`].
78pub type RcIndexSet<T> = crate::set::IndexSet<'static, T, BitVec, RcFamily>;
79
80/// [`IndexSet`](crate::set::IndexSet) specialized to the [`BitVec`] implementation with the [`ArcFamily`].
81pub type ArcIndexSet<T> = crate::set::IndexSet<'static, T, BitVec, ArcFamily>;
82
83/// [`IndexSet`](crate::set::IndexSet) specialized to the [`BitVec`] implementation with the [`RefFamily`].
84pub type RefIndexSet<'a, T> = crate::set::IndexSet<'a, T, BitVec, RefFamily<'a>>;
85
86/// [`IndexMatrix`](crate::matrix::IndexMatrix) specialized to the [`BitVec`] implementation with the [`RcFamily`].
87pub type RcIndexMatrix<R, C> = crate::matrix::IndexMatrix<'static, R, C, BitVec, RcFamily>;
88
89/// [`IndexMatrix`](crate::matrix::IndexMatrix) specialized to the [`BitVec`] implementation with the [`ArcFamily`].
90pub type ArcIndexMatrix<R, C> = crate::matrix::IndexMatrix<'static, R, C, BitVec, ArcFamily>;
91
92/// [`IndexMatrix`](crate::matrix::IndexMatrix) specialized to the [`BitVec`] implementation with the [`RefFamily`].
93pub type RefIndexMatrix<'a, R, C> = crate::matrix::IndexMatrix<'a, R, C, BitVec, RefFamily<'a>>;
94
95#[test]
96fn test_bitvec() {
97    crate::test_utils::impl_test::<BitVec>();
98}