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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
/*
* Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/piot/bit-array-rs
* Licensed under the MIT License. See LICENSE in the project root for license information.
*/
use std::ops::Index;
use std::vec::Vec;
type BitArrayAtom = u32;
const BIT_ARRAY_BITS_IN_ATOM: usize = 32;
#[derive(Clone)]
pub struct BitArray {
array: Vec<BitArrayAtom>,
bit_count: usize,
number_of_bits_set: usize,
}
impl BitArray {
/// Initializes a new `BitArray`.
///
/// # Arguments
///
/// * `bit_count` - The maximum number of bits in the array.
/// # Panics
///
/// This function will panic if `bit_count` is zero.
#[must_use]
pub fn new(bit_count: usize) -> Self {
assert_ne!(bit_count, 0, "bit_count must be greater than zero");
let atom_count = bit_count.div_ceil(BIT_ARRAY_BITS_IN_ATOM);
let array = vec![0; atom_count];
Self {
array,
bit_count,
number_of_bits_set: 0,
}
}
/// Resets all bits in the array.
pub fn reset(&mut self) {
self.array.fill(0);
self.number_of_bits_set = 0;
}
/// Checks if all bits are set.
///
/// # Returns
///
/// * `true` if all bits in the array are set, otherwise `false`.
#[inline]
#[must_use]
pub const fn all_set(&self) -> bool {
self.bit_count == self.number_of_bits_set
}
/// Finds the first bit that is not set in the array.
///
/// # Returns
///
/// * The index of the first unset bit, or `None` if all bits are set.
#[must_use]
pub fn first_unset_bit(&self) -> Option<usize> {
for (i, &atom) in self.array.iter().enumerate() {
if atom != u32::MAX {
return (0..BIT_ARRAY_BITS_IN_ATOM).find_map(|bit| {
if atom & (1 << bit) == 0 {
Some(i * BIT_ARRAY_BITS_IN_ATOM + bit)
} else {
None
}
});
}
}
None
}
/// Finds the first bit that is set in the array.
///
/// # Returns
///
/// * The index of the first set bit, or `None` if no bits are set.
#[must_use]
pub fn first_set_bit(&self) -> Option<usize> {
for (i, &atom) in self.array.iter().enumerate() {
if atom != 0 {
return (0..BIT_ARRAY_BITS_IN_ATOM).find_map(|bit| {
if atom & (1 << bit) != 0 {
Some(i * BIT_ARRAY_BITS_IN_ATOM + bit)
} else {
None
}
});
}
}
None
}
/// Returns the number of bits that are currently set to `1`.
///
/// # Returns
///
/// The number of bits that are set in the `BitArray`.
#[inline]
#[must_use]
pub const fn count_set_bits(&self) -> usize {
self.number_of_bits_set
}
/// Returns the total number of bits in the `BitArray`.
///
/// # Returns
///
/// The total number of bits in the `BitArray`.
#[inline]
#[must_use]
pub const fn bit_count(&self) -> usize {
self.bit_count
}
/// Sets the bit at the given index.
///
/// # Arguments
///
/// * `index` - The zero-based index of the bit to set.
///
/// # Panics
///
/// This function will panic if the index is out of bounds.
#[inline]
pub fn set(&mut self, index: usize) {
assert!(index < self.bit_count, "Index out of bounds");
let array_index = index / BIT_ARRAY_BITS_IN_ATOM;
let bit_index = index % BIT_ARRAY_BITS_IN_ATOM;
let mask = 1 << bit_index;
if self.array[array_index] & mask == 0 {
self.number_of_bits_set += 1;
}
self.array[array_index] |= mask;
}
/// Unsets (clears) the bit at the given index.
///
/// # Arguments
///
/// * `index` - The zero-based index of the bit to clear.
///
/// # Panics
///
/// This function will panic if the index is out of bounds.
#[inline]
pub fn unset(&mut self, index: usize) {
assert!(index < self.bit_count, "Index out of bounds");
let array_index = index / BIT_ARRAY_BITS_IN_ATOM;
let bit_index = index % BIT_ARRAY_BITS_IN_ATOM;
let mask = 1 << bit_index;
if self.array[array_index] & mask != 0 {
self.number_of_bits_set -= 1;
}
self.array[array_index] &= !mask;
}
/// Sets or unsets the bit at the given index based on the value of `set`.
///
/// # Arguments
///
/// * `index` - The zero-based index of the bit to modify.
/// * `set` - If `true`, the bit will be set (1). If `false`, the bit will be unset (0).
///
/// # Panics
///
/// This function will panic if the index is out of bounds.
pub fn set_bit(&mut self, index: usize, set: bool) {
assert!(index < self.bit_count, "Index out of bounds");
let array_index = index / BIT_ARRAY_BITS_IN_ATOM;
let bit_index = index % BIT_ARRAY_BITS_IN_ATOM;
let mask = 1 << bit_index;
if set {
if self.array[array_index] & mask == 0 {
self.number_of_bits_set += 1;
}
self.array[array_index] |= mask;
} else {
if self.array[array_index] & mask != 0 {
self.number_of_bits_set -= 1;
}
self.array[array_index] &= !mask;
}
}
/// Returns the atom value that is located at the specified index.
///
/// # Arguments
///
/// * `from_index` - The index from which to start reading.
///
/// # Returns
///
/// The atom value at the specified index.
#[must_use]
pub fn atom_from_index(&self, from_index: usize) -> BitArrayAtom {
let mut result = 0;
for i in 0..BIT_ARRAY_BITS_IN_ATOM {
let index = from_index + (BIT_ARRAY_BITS_IN_ATOM - 1) - i;
result <<= 1;
if index < self.bit_count {
result |= u32::from(self.get(index));
}
}
result
}
/// Returns the bit value at the specified index.
///
/// # Arguments
///
/// * `index` - The bit index to read from.
///
/// # Returns
///
/// The read bit value (0 or 1).
///
/// # Panics
///
/// This function will panic if the index is out of bounds.
#[must_use]
pub fn get(&self, index: usize) -> bool {
assert!(index < self.bit_count, "Index out of bounds");
let array_index = index / BIT_ARRAY_BITS_IN_ATOM;
let bit_index = index % BIT_ARRAY_BITS_IN_ATOM;
((self.array[array_index] >> bit_index) & 0x1) != 0
}
}
impl Index<usize> for BitArray {
type Output = bool;
/// Provides indexed access to individual bits in the `BitArray`.
///
/// # Arguments
///
/// * `index` - The zero-based index of the bit to access.
///
/// # Returns
///
/// A reference to a boolean value (`true` or `false`) representing the state of the bit
/// at the specified index.
///
/// # Panics
///
/// This function will panic if the `index` is out of bounds.
///
/// # Example
///
/// ```
/// use bit_array_rs::BitArray;
/// let mut bit_array = BitArray::new(16);
/// bit_array.set(3);
/// assert_eq!(bit_array[3], true);
/// assert_eq!(bit_array[0], false);
/// ```
fn index(&self, index: usize) -> &Self::Output {
if self.get(index) {
&true
} else {
&false
}
}
}
impl std::fmt::Debug for BitArray {
/// Formats the `BitArray` as a binary string with groups of 8 bits separated by a space.
///
/// # Arguments
///
/// * `f` - The formatter used to output the debug string.
///
/// # Returns
///
/// A `Result` indicating whether the formatting was successful.
///
/// # Example
///
/// ```
/// use bit_array_rs::BitArray;
/// let mut bit_array = BitArray::new(16);
/// bit_array.set(3);
/// bit_array.set(7);
/// bit_array.set(9);
/// bit_array.set(15);
///
/// assert_eq!(format!("{:?}", bit_array), "00010001 01000001");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for i in 0..self.bit_count {
if i > 0 && i % 8 == 0 {
write!(f, " ")?;
}
write!(f, "{}", u8::from(self.get(i)))?;
}
Ok(())
}
}
impl std::fmt::Display for BitArray {
/// Formats the `BitArray` as a continuous binary string without any spaces.
///
/// # Arguments
///
/// * `f` - The formatter used to output the display string.
///
/// # Returns
///
/// A `Result` indicating whether the formatting was successful.
///
/// # Example
///
/// ```
/// use bit_array_rs::BitArray;
/// let mut bit_array = BitArray::new(16);
/// bit_array.set(3);
/// bit_array.set(7);
/// bit_array.set(9);
/// bit_array.set(15);
///
/// assert_eq!(format!("{}", bit_array), "0001000101000001");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for i in 0..self.bit_count {
write!(f, "{}", u8::from(self.get(i)))?;
}
Ok(())
}
}