|
| 1 | +use std::ops::{Add, AddAssign, BitAnd, BitXor, BitXorAssign, Not, Sub, SubAssign}; |
| 2 | + |
| 3 | +#[derive(Copy, Clone, Eq, PartialEq)] |
| 4 | +pub struct Bitset<const SIZE: usize>(pub [u32; SIZE]); |
| 5 | + |
| 6 | +impl<const SIZE: usize> Bitset<SIZE> { |
| 7 | + pub const fn new() -> Self { |
| 8 | + Bitset([0; SIZE]) |
| 9 | + } |
| 10 | + |
| 11 | + fn _add(&mut self, bit: impl Into<usize>) { |
| 12 | + let bit = bit.into(); |
| 13 | + let array_index = bit >> 5; |
| 14 | + let pos_index = bit & 31; |
| 15 | + self.0[array_index] |= 1 << pos_index; |
| 16 | + } |
| 17 | + |
| 18 | + fn _sub(&mut self, bit: impl Into<usize>) { |
| 19 | + let bit = bit.into(); |
| 20 | + let array_index = bit >> 5; |
| 21 | + let pos_index = bit & 31; |
| 22 | + self.0[array_index] &= !(1 << pos_index); |
| 23 | + } |
| 24 | + |
| 25 | + pub fn contains(&self, bit: impl Into<usize>) -> bool { |
| 26 | + let bit = bit.into(); |
| 27 | + let array_index = bit >> 5; |
| 28 | + let pos_index = bit & 31; |
| 29 | + self.0[array_index] & (1 << pos_index) != 0 |
| 30 | + } |
| 31 | + |
| 32 | + pub const fn len(&self) -> usize { |
| 33 | + let mut sum = 0; |
| 34 | + let mut i = 0; |
| 35 | + while i < self.0.len() { |
| 36 | + sum += self.0[i].count_ones(); |
| 37 | + i += 1; |
| 38 | + } |
| 39 | + sum as usize |
| 40 | + } |
| 41 | + |
| 42 | + pub const fn is_empty(&self) -> bool { |
| 43 | + self.len() == 0 |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl<const SIZE: usize> Default for Bitset<SIZE> { |
| 48 | + fn default() -> Self { |
| 49 | + Bitset([0; SIZE]) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl<const SIZE: usize, T: Into<usize>> Add<T> for Bitset<SIZE> { |
| 54 | + type Output = Bitset<SIZE>; |
| 55 | + |
| 56 | + fn add(mut self, rhs: T) -> Self::Output { |
| 57 | + self._add(rhs); |
| 58 | + self |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<const SIZE: usize, T: Into<usize>> AddAssign<T> for Bitset<SIZE> { |
| 63 | + fn add_assign(&mut self, rhs: T) { |
| 64 | + self._add(rhs) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<const SIZE: usize, T: Into<usize>> Sub<T> for Bitset<SIZE> { |
| 69 | + type Output = Bitset<SIZE>; |
| 70 | + |
| 71 | + fn sub(mut self, rhs: T) -> Self::Output { |
| 72 | + self._sub(rhs); |
| 73 | + self |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<const SIZE: usize, T: Into<usize>> SubAssign<T> for Bitset<SIZE> { |
| 78 | + fn sub_assign(&mut self, rhs: T) { |
| 79 | + self._sub(rhs); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl<const SIZE: usize> Add<Bitset<SIZE>> for Bitset<SIZE> { |
| 84 | + type Output = Bitset<SIZE>; |
| 85 | + |
| 86 | + fn add(mut self, rhs: Bitset<SIZE>) -> Self::Output { |
| 87 | + for i in 0..self.0.len() { |
| 88 | + self.0[i] |= rhs.0[i]; |
| 89 | + } |
| 90 | + self |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<const SIZE: usize> AddAssign<Bitset<SIZE>> for Bitset<SIZE> { |
| 95 | + fn add_assign(&mut self, rhs: Bitset<SIZE>) { |
| 96 | + for i in 0..self.0.len() { |
| 97 | + self.0[i] |= rhs.0[i]; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<const SIZE: usize> Sub<Bitset<SIZE>> for Bitset<SIZE> { |
| 103 | + type Output = Bitset<SIZE>; |
| 104 | + |
| 105 | + fn sub(mut self, rhs: Bitset<SIZE>) -> Self::Output { |
| 106 | + for i in 0..self.0.len() { |
| 107 | + self.0[i] &= !rhs.0[i]; |
| 108 | + } |
| 109 | + self |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl<const SIZE: usize> SubAssign<Bitset<SIZE>> for Bitset<SIZE> { |
| 114 | + fn sub_assign(&mut self, rhs: Bitset<SIZE>) { |
| 115 | + for i in 0..self.0.len() { |
| 116 | + self.0[i] &= !rhs.0[i]; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl<const SIZE: usize> BitAnd<Bitset<SIZE>> for Bitset<SIZE> { |
| 122 | + type Output = Bitset<SIZE>; |
| 123 | + |
| 124 | + fn bitand(mut self, rhs: Bitset<SIZE>) -> Self::Output { |
| 125 | + for i in 0..self.0.len() { |
| 126 | + self.0[i] &= rhs.0[i]; |
| 127 | + } |
| 128 | + self |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl<const SIZE: usize> BitXor<Bitset<SIZE>> for Bitset<SIZE> { |
| 133 | + type Output = Bitset<SIZE>; |
| 134 | + |
| 135 | + fn bitxor(mut self, rhs: Bitset<SIZE>) -> Self::Output { |
| 136 | + for i in 0..self.0.len() { |
| 137 | + self.0[i] ^= rhs.0[i]; |
| 138 | + } |
| 139 | + self |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +impl<const SIZE: usize> BitXorAssign for Bitset<SIZE> { |
| 144 | + fn bitxor_assign(&mut self, rhs: Self) { |
| 145 | + for i in 0..self.0.len() { |
| 146 | + self.0[i] ^= rhs.0[i]; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +impl<const SIZE: usize> Not for Bitset<SIZE> { |
| 152 | + type Output = Bitset<SIZE>; |
| 153 | + |
| 154 | + fn not(mut self) -> Self::Output { |
| 155 | + for i in 0..self.0.len() { |
| 156 | + self.0[i] = !self.0[i]; |
| 157 | + } |
| 158 | + self |
| 159 | + } |
| 160 | +} |
0 commit comments