| 1 | use crate::AtomicU64; |
| 2 | use alloc::{boxed::Box, vec::Vec}; |
| 3 | use portable_atomic::Ordering::Relaxed; |
| 4 | |
| 5 | /// A bit vector partitioned in to `u64` blocks. |
| 6 | #[derive(Debug, Clone)] |
| 7 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 8 | pub(crate) struct BitVec { |
| 9 | bits: Box<[u64]>, |
| 10 | } |
| 11 | |
| 12 | /// A bit vector partitioned in to `u64` blocks. |
| 13 | #[derive(Debug)] |
| 14 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 15 | pub(crate) struct AtomicBitVec { |
| 16 | bits: Box<[AtomicU64]>, |
| 17 | } |
| 18 | |
| 19 | macro_rules! impl_bitvec { |
| 20 | ($name:ident, $bits:ty) => { |
| 21 | impl $name { |
| 22 | #[inline(always)] |
| 23 | pub(crate) const fn len(&self) -> usize { |
| 24 | self.bits.len() |
| 25 | } |
| 26 | |
| 27 | #[inline(always)] |
| 28 | pub(crate) const fn num_bits(&self) -> usize { |
| 29 | self.len() * u64::BITS as usize |
| 30 | } |
| 31 | |
| 32 | #[inline(always)] |
| 33 | pub(crate) fn as_slice(&self) -> &[$bits] { |
| 34 | &self.bits |
| 35 | } |
| 36 | |
| 37 | #[inline(always)] |
| 38 | pub(crate) fn iter(&self) -> impl Iterator<Item = u64> + '_ { |
| 39 | self.bits.iter().map(Self::fetch) |
| 40 | } |
| 41 | |
| 42 | #[inline(always)] |
| 43 | pub(crate) fn check(&self, index: usize) -> bool { |
| 44 | let (index, bit) = coord(index); |
| 45 | Self::fetch(&self.bits[index]) & bit > 0 |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl FromIterator<u64> for $name { |
| 50 | fn from_iter<I: IntoIterator<Item = u64>>(iter: I) -> Self { |
| 51 | let mut bits = iter.into_iter().map(Self::new).collect::<Vec<_>>(); |
| 52 | bits.shrink_to_fit(); |
| 53 | Self { bits: bits.into() } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl PartialEq for $name { |
| 58 | fn eq(&self, other: &Self) -> bool { |
| 59 | if self.len() != other.len() { |
| 60 | return false; |
| 61 | } |
| 62 | core::iter::zip(self.iter(), other.iter()).all(|(l, r)| l == r) |
| 63 | } |
| 64 | } |
| 65 | impl Eq for $name {} |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | impl_bitvec!(BitVec, u64); |
| 70 | impl_bitvec!(AtomicBitVec, AtomicU64); |
| 71 | |
| 72 | impl BitVec { |
| 73 | #[inline(always)] |
| 74 | fn new(x: u64) -> u64 { |
| 75 | x |
| 76 | } |
| 77 | |
| 78 | #[inline(always)] |
| 79 | fn fetch(x: &u64) -> u64 { |
| 80 | *x |
| 81 | } |
| 82 | |
| 83 | #[inline(always)] |
| 84 | pub(crate) fn set(&mut self, index: usize) -> bool { |
| 85 | let (index, bit) = coord(index); |
| 86 | let previously_contained = self.bits[index] & bit > 0; |
| 87 | self.bits[index] |= bit; |
| 88 | previously_contained |
| 89 | } |
| 90 | |
| 91 | #[inline(always)] |
| 92 | pub(crate) fn clear(&mut self) { |
| 93 | for i in 0..self.len() { |
| 94 | self.bits[i] = 0; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[inline(always)] |
| 99 | pub(crate) fn union(&mut self, other: &BitVec) { |
| 100 | assert_eq!(self.len(), other.len(), "expected same length"); |
| 101 | for i in 0..self.len() { |
| 102 | self.bits[i] |= other.bits[i]; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #[inline(always)] |
| 107 | pub(crate) fn intersect(&mut self, other: &BitVec) { |
| 108 | assert_eq!(self.len(), other.len(), "expected same length"); |
| 109 | for i in 0..self.len() { |
| 110 | self.bits[i] &= other.bits[i]; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl AtomicBitVec { |
| 116 | #[inline] |
| 117 | fn new(x: u64) -> AtomicU64 { |
| 118 | AtomicU64::new(x) |
| 119 | } |
| 120 | |
| 121 | #[inline] |
| 122 | fn fetch(x: &AtomicU64) -> u64 { |
| 123 | x.load(Relaxed) |
| 124 | } |
| 125 | |
| 126 | #[inline] |
| 127 | pub(crate) fn set(&self, index: usize) -> bool { |
| 128 | let (index, bit) = coord(index); |
| 129 | self.bits[index].fetch_or(bit, Relaxed) & bit > 0 |
| 130 | } |
| 131 | |
| 132 | #[inline] |
| 133 | pub(crate) fn clear(&self) { |
| 134 | for i in 0..self.len() { |
| 135 | self.bits[i].store(0, Relaxed); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[inline] |
| 140 | pub(crate) fn union(&self, other: &AtomicBitVec) { |
| 141 | assert_eq!(self.len(), other.len(), "expected same length"); |
| 142 | for i in 0..self.len() { |
| 143 | let x = other.bits[i].load(Relaxed); |
| 144 | self.bits[i].fetch_or(x, Relaxed); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | #[inline] |
| 149 | pub(crate) fn intersect(&self, other: &AtomicBitVec) { |
| 150 | assert_eq!(self.len(), other.len(), "expected same length"); |
| 151 | for i in 0..self.len() { |
| 152 | let x = other.bits[i].load(Relaxed); |
| 153 | self.bits[i].fetch_and(x, Relaxed); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | impl Clone for AtomicBitVec { |
| 159 | fn clone(&self) -> Self { |
| 160 | self.iter().collect() |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #[inline] |
| 165 | fn coord(index: usize) -> (usize, u64) { |
| 166 | (index >> 6, 1u64 << (index & 0b111111)) |
| 167 | } |
| 168 | |
| 169 | macro_rules! impl_tests { |
| 170 | ($modname:ident, $name:ident) => { |
| 171 | #[allow(unused_mut)] |
| 172 | #[cfg(not(feature = "loom"))] |
| 173 | #[cfg(test)] |
| 174 | mod $modname { |
| 175 | use super::*; |
| 176 | use core::iter::repeat; |
| 177 | |
| 178 | #[test] |
| 179 | fn test_to_from_vec() { |
| 180 | let size = 42; |
| 181 | let b: BitVec = repeat(0).take(size).collect(); |
| 182 | assert_eq!(b.num_bits(), b.len() * 64); |
| 183 | assert!(size <= b.len()); |
| 184 | assert!((size + 64) > b.len()); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn test_only_random_inserts_are_contained() { |
| 189 | let mut vec: BitVec = repeat(0).take(80).collect(); |
| 190 | let mut control = Vec::with_capacity(1000); |
| 191 | let mut rng = fastrand::Rng::with_seed(4364); |
| 192 | |
| 193 | for _ in 0..1000 { |
| 194 | let index = rng.usize(0..vec.num_bits()); |
| 195 | |
| 196 | if !control.contains(&index) { |
| 197 | assert!(!vec.check(index)); |
| 198 | } |
| 199 | control.push(index); |
| 200 | vec.set(index); |
| 201 | assert!(vec.check(index)); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | }; |
| 206 | } |
| 207 | |
| 208 | impl_tests!(non_atomic, BitVec); |
| 209 | impl_tests!(atomic, AtomicBitVec); |