| 1 | #![no_std] |
| 2 | #![cfg_attr( |
| 3 | feature = "unstable-512-bit-simd", |
| 4 | feature(link_llvm_intrinsics, repr_simd, simd_ffi, platform_intrinsics) |
| 5 | )] |
| 6 | |
| 7 | #[cfg(feature = "serde")] |
| 8 | mod serde_implrust-analyzer cargo bitarray 0.10.0 serde_impl/; |
| 9 | |
| 10 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{ |
| 11 | fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/, |
| 12 | hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/::{Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#, Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#}, |
| 13 | opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{BitAndrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#, BitOrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#, BitXorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#, Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#, DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut#}, |
| 14 | slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/, |
| 15 | }; |
| 16 | |
| 17 | #[cfg(feature = "space")] |
| 18 | use spacerust-analyzer cargo space 0.18.0 crate/::Metricrust-analyzer cargo space 0.18.0 Metric#; |
| 19 | |
| 20 | cfg_ifrust-analyzer cargo cfg-if 1.0.4 crate/::cfg_ifrust-analyzer cargo cfg-if 1.0.4 cfg_if!! { |
| 21 | if #[cfg(feature = "unstable-512-bit-simd")] { |
| 22 | #[repr(simd)] |
| 23 | #[derive(Copy, Clone)] |
| 24 | struct Tup(u128, u128, u128, u128); |
| 25 | |
| 26 | #[allow(improper_ctypes, dead_code)] |
| 27 | extern "C" { |
| 28 | #[link_name = "llvm.ctpop.v4i128"] |
| 29 | fn ctpop_512(x: Tup) -> Tup; |
| 30 | #[link_name = "llvm.experimental.vector.reduce.add.v4i128"] |
| 31 | fn reduce_add_512(x: Tup) -> u128; |
| 32 | } |
| 33 | |
| 34 | extern "platform-intrinsic" { |
| 35 | fn simd_xor<T>(x: T, y: T) -> T; |
| 36 | } |
| 37 | |
| 38 | /// Split the bytes up into number of operations of size (512, 64, 8) |
| 39 | const fn split_up_simd(n: usize) -> (usize, usize, usize) { |
| 40 | let n_512 = n >> 6; |
| 41 | let bytes_512 = n_512 << 6; |
| 42 | let n_64 = (n - bytes_512) >> 3; |
| 43 | let bytes_64 = n_64 << 3; |
| 44 | let n_8 = n - bytes_512 - bytes_64; |
| 45 | (n_512, n_64, n_8) |
| 46 | } |
| 47 | } else { |
| 48 | /// Split the bytes up into number of operations of size (512, 64, 8) |
| 49 | const fn split_up_simdrust-analyzer cargo bitarray 0.10.0 split_up_simd().(n: usize) -> (usize, usize) { |
| 50 | let n_64 = n >> 3; |
| 51 | let bytes_64 = n_64 << 3; |
| 52 | let n_8 = n -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). bytes_64; |
| 53 | (n_64, n_8) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// A constant sized array of bits. `B` defines the number of bytes. |
| 59 | /// This has an alignment of 64 to maximize the efficiency of SIMD operations. |
| 60 | /// It will automatically utilize SIMD at runtime where possible. |
| 61 | #[repr(align(64))] |
| 62 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 63 | pub struct BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<const B: usize> { |
| 64 | pub bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes.: [u8; B], |
| 65 | } |
| 66 | |
| 67 | impl<const B: usize> BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> { |
| 68 | /// Create a new `BitArray`. |
| 69 | /// |
| 70 | /// ``` |
| 71 | /// use bitarray::BitArray; |
| 72 | /// let array = BitArray::new([0]); |
| 73 | /// assert_eq!(*array.bytes(), [0]); |
| 74 | /// ``` |
| 75 | pub fn newrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]new().(bytes: [u8; B]) -> Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`] { |
| 76 | Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`] { bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes. } |
| 77 | } |
| 78 | |
| 79 | /// Create a new `BitArray` with all zeros. |
| 80 | /// |
| 81 | /// ``` |
| 82 | /// use bitarray::BitArray; |
| 83 | /// let array = BitArray::zeros(); |
| 84 | /// assert_eq!(array, BitArray::new([0])); |
| 85 | /// assert_eq!(*array, [0]); |
| 86 | /// ``` |
| 87 | pub fn zerosrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]zeros().() -> Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`] { |
| 88 | Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`] { bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes.: [0; B] } |
| 89 | } |
| 90 | |
| 91 | /// Retrieve the byte array of a `BitArray`. |
| 92 | /// |
| 93 | /// ``` |
| 94 | /// use bitarray::BitArray; |
| 95 | /// let array = BitArray::new([1, 2]); |
| 96 | /// assert_eq!(*array, [1, 2]); |
| 97 | /// ``` |
| 98 | pub fn bytesrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]bytes().(&self) -> &[u8; B] { |
| 99 | &self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes. |
| 100 | } |
| 101 | |
| 102 | /// Retrieve the mutable byte array of a `BitArray`. |
| 103 | /// |
| 104 | /// ``` |
| 105 | /// use bitarray::BitArray; |
| 106 | /// let mut array = BitArray::new([1, 2]); |
| 107 | /// array.bytes_mut()[0] = 3; |
| 108 | /// assert_eq!(*array, [3, 2]); |
| 109 | /// ``` |
| 110 | pub fn bytes_mutrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]bytes_mut().(&mut self) -> &mut [u8; B] { |
| 111 | &mut self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes. |
| 112 | } |
| 113 | |
| 114 | /// Compute the hamming weight (number of ones) of the `BitArray`. |
| 115 | /// |
| 116 | /// This is also called `count_ones` in the standard library. |
| 117 | /// |
| 118 | /// ``` |
| 119 | /// use bitarray::BitArray; |
| 120 | /// let array = BitArray::new([0xAA; 83]); |
| 121 | /// assert_eq!(array.weight(), 4 * 83); |
| 122 | /// ``` |
| 123 | #[allow(clippy::cast_ptr_alignment)] |
| 124 | pub fn weightrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]weight().(&self) -> u32 { |
| 125 | cfg_ifrust-analyzer cargo cfg-if 1.0.4 crate/::cfg_ifrust-analyzer cargo cfg-if 1.0.4 cfg_if!! { |
| 126 | if #[cfg(feature = "unstable-512-bit-simd")] { |
| 127 | let (n_512, n_64, n_8) = split_up_simdrust-analyzer cargo bitarray 0.10.0 split_up_simd().(self.bytes.len()); |
| 128 | let sum_512 = unsafe { |
| 129 | slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(self.bytes.as_ptr() as *const Tup, n_512) |
| 130 | .iter() |
| 131 | .copied() |
| 132 | .map(|chunk| reduce_add_512(ctpop_512(chunk)) as u32) |
| 133 | .sum::<u32>() |
| 134 | }; |
| 135 | let sum_64 = unsafe { |
| 136 | slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(self.bytes.as_ptr() as *const u64, n_64) |
| 137 | .iter() |
| 138 | .copied() |
| 139 | .map(|chunk| chunk.count_ones()) |
| 140 | .sum::<u32>() |
| 141 | }; |
| 142 | |
| 143 | let sum_8 = self.bytes[self.bytes.len() - n_8..] |
| 144 | .iter() |
| 145 | .copied() |
| 146 | .map(|b| b.count_ones()) |
| 147 | .sum::<u32>(); |
| 148 | |
| 149 | sum_512 + sum_64 + sum_8 |
| 150 | } else { |
| 151 | let (n_64, n_8) = split_up_simdrust-analyzer cargo bitarray 0.10.0 split_up_simd().(self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 152 | let sum_64 = unsafe { |
| 153 | slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes..as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().() as *const u64, n_64) |
| 154 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 155 | .copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().() |
| 156 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|chunk| chunk.count_onesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]count_ones().()) |
| 157 | .sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<u32>() |
| 158 | }; |
| 159 | |
| 160 | let sum_8 = self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). n_8..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). |
| 161 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 162 | .copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().() |
| 163 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|b| b.count_onesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]count_ones().()) |
| 164 | .sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<u32>(); |
| 165 | |
| 166 | sum_64 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). sum_8 |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /// Compute the hamming distance to another `BitArray`. |
| 172 | /// |
| 173 | /// ``` |
| 174 | /// use bitarray::BitArray; |
| 175 | /// |
| 176 | /// // All the bits are different. |
| 177 | /// let a = BitArray::new([0xAA; 65]); |
| 178 | /// let b = BitArray::new([0x55; 65]); |
| 179 | /// assert_eq!(a.distance(&b), 8 * 65); |
| 180 | /// |
| 181 | /// // None of the bits are different. |
| 182 | /// let a = BitArray::new([0xAA; 65]); |
| 183 | /// let b = BitArray::new([0xAA; 65]); |
| 184 | /// assert_eq!(a.distance(&b), 0); |
| 185 | /// ``` |
| 186 | #[allow(clippy::cast_ptr_alignment)] |
| 187 | pub fn distancerust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]distance().(&self, other: &Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]) -> u32 { |
| 188 | cfg_ifrust-analyzer cargo cfg-if 1.0.4 crate/::cfg_ifrust-analyzer cargo cfg-if 1.0.4 cfg_if!! { |
| 189 | if #[cfg(feature = "unstable-512-bit-simd")] { |
| 190 | let simd_len = B >> 6; |
| 191 | let simd_bytes = simd_len << 6; |
| 192 | let simd_sum = unsafe { |
| 193 | slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(self.bytes.as_ptr() as *const Tup, simd_len) |
| 194 | .iter() |
| 195 | .copied() |
| 196 | .zip( |
| 197 | slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(other.bytes.as_ptr() as *const Tup, simd_len) |
| 198 | .iter() |
| 199 | .copied(), |
| 200 | ) |
| 201 | .map(|(a, b)| reduce_add_512(ctpop_512(simd_xor(a, b))) as u32) |
| 202 | .sum::<u32>() |
| 203 | }; |
| 204 | let remaining_sum = self.bytes[simd_bytes..] |
| 205 | .iter() |
| 206 | .copied() |
| 207 | .zip(other.bytes[simd_bytes..].iter().copied()) |
| 208 | .map(|(a, b)| (a ^ b).count_ones()) |
| 209 | .sum::<u32>(); |
| 210 | simd_sum + remaining_sum |
| 211 | } else { |
| 212 | self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes. |
| 213 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 214 | .copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().() |
| 215 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(other.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().()) |
| 216 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(a, b)| (a ^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXor<Self>`]bitxor(). b).count_onesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]count_ones().()) |
| 217 | .sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<u32>() |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | impl<const B: usize> BitAndrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd# for BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> { |
| 224 | type Outputrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitAnd<Self>`]Output# = Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitAnd<Self>`]; |
| 225 | |
| 226 | fn bitandrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitAnd<Self>`]bitand().(mut self, rhs: Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitAnd<Self>`]) -> Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitAnd<Self>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitAnd#Output# { |
| 227 | for (d, s) in self.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(rhs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().()) { |
| 228 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().d rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAndAssign<Self>`]bitand_assign().&=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAndAssign<Self>`]bitand_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAndAssign<Self>`]bitand_assign().s; |
| 229 | } |
| 230 | self |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | impl<const B: usize> BitOrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr# for BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> { |
| 235 | type Outputrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitOr<Self>`]Output# = Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitOr<Self>`]; |
| 236 | |
| 237 | fn bitorrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitOr<Self>`]bitor().(mut self, rhs: Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitOr<Self>`]) -> Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitOr<Self>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#Output# { |
| 238 | for (d, s) in self.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(rhs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().()) { |
| 239 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().d rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitOrAssign<Self>`]bitor_assign().s; |
| 240 | } |
| 241 | self |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | impl<const B: usize> BitXorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor# for BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> { |
| 246 | type Outputrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitXor<Self>`]Output# = Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitXor<Self>`]; |
| 247 | |
| 248 | fn bitxorrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitXor<Self>`]bitxor().(mut self, rhs: Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitXor<Self>`]) -> Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitXor<Self>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#Output# { |
| 249 | for (d, s) in self.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(rhs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().()) { |
| 250 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().d rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXorAssign<Self>`]bitxor_assign().^=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXorAssign<Self>`]bitxor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXorAssign<Self>`]bitxor_assign().s; |
| 251 | } |
| 252 | self |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | impl<const B: usize> PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# for BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> { |
| 257 | fn eqrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`PartialEq<Self>`]eq().(&self, other: &Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`PartialEq<Self>`]) -> bool { |
| 258 | self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes. |
| 259 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 260 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(other.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()) |
| 261 | .allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#all().(|(&a, &b)| a rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | impl<const B: usize> Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# for BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> {} |
| 266 | |
| 267 | impl<const B: usize> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> { |
| 268 | fn fmtrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 269 | self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()..fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`[T]`][Debug]fmt().(f) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | impl<const B: usize> Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# for BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> { |
| 274 | fn hashrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][Hash]hash().<H: Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#>(&self, state: &mut H) { |
| 275 | self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()..hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/impls/impl#[`[T]`][Hash]hash().(state) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /// ``` |
| 280 | /// use bitarray::BitArray; |
| 281 | /// let mut array = BitArray::new([1, 2]); |
| 282 | /// assert_eq!(*array, [1, 2]); |
| 283 | /// ``` |
| 284 | impl<const B: usize> Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> { |
| 285 | type Targetrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][Deref]Target# = [u8; B]; |
| 286 | |
| 287 | fn derefrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][Deref]deref().(&self) -> &Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][Deref]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# { |
| 288 | &self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes. |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /// ``` |
| 293 | /// use bitarray::BitArray; |
| 294 | /// let mut array = BitArray::zeros(); |
| 295 | /// array[0] = 1; |
| 296 | /// array[1] = 2; |
| 297 | /// assert_eq!(*array, [1, 2]); |
| 298 | /// ``` |
| 299 | impl<const B: usize> DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut# for BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B> { |
| 300 | fn deref_mutrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][DerefMut]deref_mut().(&mut self) -> &mut Selfrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][DerefMut]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# { |
| 301 | &mut self.bytesrust-analyzer cargo bitarray 0.10.0 BitArray#bytes. |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /// Provides [hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) as a metric. |
| 306 | #[cfg(feature = "space")] |
| 307 | #[cfg_attr(feature = "serde", derive(serderust-analyzer cargo serde 1.0.229 crate/::Serializerust-analyzer cargo serde_derive 1.0.229 Serialize:, serderust-analyzer cargo serde 1.0.229 crate/::Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:))] |
| 308 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 309 | pub struct Hammingrust-analyzer cargo bitarray 0.10.0 Hamming#; |
| 310 | |
| 311 | #[cfg(feature = "space")] |
| 312 | impl<const B: usize> Metricrust-analyzer cargo space 0.18.0 Metric#<BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B>> for Hammingrust-analyzer cargo bitarray 0.10.0 Hamming# { |
| 313 | type Unitrust-analyzer cargo bitarray 0.10.0 impl#[Hamming][`Metric<BitArray<B>>`]Unit# = u32; |
| 314 | |
| 315 | fn distancerust-analyzer cargo bitarray 0.10.0 impl#[Hamming][`Metric<BitArray<B>>`]distance().(&self, a: &BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B>, b: &BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B>) -> u32 { |
| 316 | a.distancerust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]distance().(b) as u32 |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /// Provides [Jaccard distance](https://en.wikipedia.org/wiki/Jaccard_index) as a metric. |
| 321 | /// |
| 322 | /// The Jaccard similarity is computed and then subtracted from `1.0` |
| 323 | /// so that items are ordered by Jaccard distance/dissimilarity. |
| 324 | #[cfg(feature = "space")] |
| 325 | #[cfg_attr(feature = "serde", derive(serderust-analyzer cargo serde 1.0.229 crate/::Serializerust-analyzer cargo serde_derive 1.0.229 Serialize:, serderust-analyzer cargo serde 1.0.229 crate/::Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:))] |
| 326 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 327 | pub struct Jaccardrust-analyzer cargo bitarray 0.10.0 Jaccard#; |
| 328 | |
| 329 | #[cfg(feature = "space")] |
| 330 | impl<const B: usize> Metricrust-analyzer cargo space 0.18.0 Metric#<BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B>> for Jaccardrust-analyzer cargo bitarray 0.10.0 Jaccard# { |
| 331 | type Unitrust-analyzer cargo bitarray 0.10.0 impl#[Jaccard][`Metric<BitArray<B>>`]Unit# = u32; |
| 332 | |
| 333 | fn distancerust-analyzer cargo bitarray 0.10.0 impl#[Jaccard][`Metric<BitArray<B>>`]distance().(&self, &a: &BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B>, &b: &BitArrayrust-analyzer cargo bitarray 0.10.0 BitArray#<B>) -> u32 { |
| 334 | let intersection = (a rust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitAnd<Self>`]bitand().&rust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitAnd<Self>`]bitand(). rust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitAnd<Self>`]bitand().b).weightrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]weight().(); |
| 335 | let union = (a rust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitOr<Self>`]bitor().|rust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitOr<Self>`]bitor(). rust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`][`BitOr<Self>`]bitor().b).weightrust-analyzer cargo bitarray 0.10.0 impl#[`BitArray<B>`]weight().(); |
| 336 | if union rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().0 { |
| 337 | 0 |
| 338 | } else { |
| 339 | (1.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().intersection as f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().union as f32).to_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]to_bits().() |
| 340 | } |
| 341 | } |
| 342 | } |