Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: [1.67.0, 1.68.0]
rust: [1.76.0, 1.77.0]
timeout-minutes: 45
steps:
- uses: actions/checkout@v5
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ documentation = "https://docs.rs/bit-vec/"
keywords = ["data-structures", "bitvec", "bitmask", "bitmap", "bit"]
readme = "README.md"
edition = "2021"
rust-version = "1.67"
rust-version = "1.71"

[dependencies]
borsh = { version = "1.5.5", default-features = false, features = ["derive"], optional = true }
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![crates.io][crates.io shield]][crates.io link]
[![Documentation][docs.rs badge]][docs.rs link]
![Rust CI][github ci badge]
![MSRV][rustc 1.67+]
![MSRV][rustc 1.71+]
<br />
<br />
[![Dependency Status][deps.rs status]][deps.rs link]
Expand All @@ -21,8 +21,8 @@
[crates.io link]: https://crates.io/crates/bit-vec
[docs.rs badge]: https://docs.rs/bit-vec/badge.svg?version=0.8.0
[docs.rs link]: https://docs.rs/bit-vec/0.8.0/bit_vec/
[github ci badge]: https://github.com/contain-rs/bit-vec/workflows/Rust/badge.svg?branch=master
[rustc 1.67+]: https://img.shields.io/badge/rustc-1.67%2B-blue.svg
[github ci badge]: https://github.com/contain-rs/bit-vec/actions/workflows/rust.yml/badge.svg
[rustc 1.71+]: https://img.shields.io/badge/rustc-1.71%2B-blue.svg
[deps.rs status]: https://deps.rs/crate/bit-vec/0.8.0/status.svg
[deps.rs link]: https://deps.rs/crate/bit-vec/0.8.0
[shields.io download count]: https://img.shields.io/crates/d/bit-vec.svg
Expand Down
12 changes: 12 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ fn bench_usize_small(b: &mut Bencher) {
});
}

#[bench]
fn bench_to_bytes(b: &mut Bencher) {
let mut bit_vec = BitVec::from_elem(BENCH_BITS, false);
let mut r = small_rng();
for _ in 0..BENCH_BITS / 10 {
bit_vec.set((r.next_u32() as usize) % BENCH_BITS, true);
}
b.iter(|| {
black_box(bit_vec.to_bytes());
});
}

#[bench]
fn bench_bit_set_big_fixed(b: &mut Bencher) {
let mut r = small_rng();
Expand Down
53 changes: 29 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ impl<B: BitBlock> BitVec<B> {
pub fn or(&mut self, other: &Self) -> bool {
self.ensure_invariant();
debug_assert!(other.is_last_block_fixed());
self.process(other, |w1, w2| (w1 | w2))
self.process(other, |w1, w2| w1 | w2)
}

/// Calculates the bitwise `and` of two bitvectors.
Expand Down Expand Up @@ -857,7 +857,7 @@ impl<B: BitBlock> BitVec<B> {
pub fn and(&mut self, other: &Self) -> bool {
self.ensure_invariant();
debug_assert!(other.is_last_block_fixed());
self.process(other, |w1, w2| (w1 & w2))
self.process(other, |w1, w2| w1 & w2)
}

/// Calculates the difference between two bitvectors.
Expand Down Expand Up @@ -896,7 +896,7 @@ impl<B: BitBlock> BitVec<B> {
pub fn difference(&mut self, other: &Self) -> bool {
self.ensure_invariant();
debug_assert!(other.is_last_block_fixed());
self.process(other, |w1, w2| (w1 & !w2))
self.process(other, |w1, w2| w1 & !w2)
}

/// Calculates the xor of two bitvectors.
Expand Down Expand Up @@ -927,7 +927,7 @@ impl<B: BitBlock> BitVec<B> {
pub fn xor(&mut self, other: &Self) -> bool {
self.ensure_invariant();
debug_assert!(other.is_last_block_fixed());
self.process(other, |w1, w2| (w1 ^ w2))
self.process(other, |w1, w2| w1 ^ w2)
}

/// Calculates the nand of two bitvectors.
Expand Down Expand Up @@ -1322,30 +1322,35 @@ impl<B: BitBlock> BitVec<B> {
/// assert_eq!(bv.to_bytes(), [0b00100000, 0b10000000]);
/// ```
pub fn to_bytes(&self) -> Vec<u8> {
static REVERSE_TABLE: [u8; 256] = {
let mut tbl = [0u8; 256];
let mut i: u8 = 0;
loop {
tbl[i as usize] = i.reverse_bits();
if i == 255 {
break;
}
i += 1;
}
tbl
};
self.ensure_invariant();
// Oh lord, we're mapping this to bytes bit-by-bit!
fn bit<B: BitBlock>(bit_vec: &BitVec<B>, byte: usize, bit: usize) -> u8 {
let offset = byte * 8 + bit;
if offset >= bit_vec.nbits {
0
} else {
(bit_vec[offset] as u8) << (7 - bit)

let len = self.nbits / 8 + if self.nbits % 8 == 0 { 0 } else { 1 };
let mut result = Vec::with_capacity(len);

for byte_idx in 0..len {
let mut byte = 0u8;
for bit_idx in 0..8 {
let offset = byte_idx * 8 + bit_idx;
if offset < self.nbits && self[offset] {
byte |= 1 << bit_idx;
}
}
result.push(REVERSE_TABLE[byte as usize]);
}

let len = self.nbits / 8 + if self.nbits % 8 == 0 { 0 } else { 1 };
(0..len)
.map(|i| {
bit(self, i, 0)
| bit(self, i, 1)
| bit(self, i, 2)
| bit(self, i, 3)
| bit(self, i, 4)
| bit(self, i, 5)
| bit(self, i, 6)
| bit(self, i, 7)
})
.collect()
result
}

/// Compares a `BitVec` to a slice of `bool`s.
Expand Down
Loading