Skip to content

Commit f98da85

Browse files
authored
Merge pull request #132 from contain-rs/fereidani
Fereidani's first contribution
2 parents 0536b24 + 3decce2 commit f98da85

File tree

5 files changed

+46
-29
lines changed

5 files changed

+46
-29
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
strategy:
5555
fail-fast: false
5656
matrix:
57-
rust: [1.67.0, 1.68.0]
57+
rust: [1.82.0, 1.83.0]
5858
timeout-minutes: 45
5959
steps:
6060
- uses: actions/checkout@v5

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ documentation = "https://docs.rs/bit-vec/"
1010
keywords = ["data-structures", "bitvec", "bitmask", "bitmap", "bit"]
1111
readme = "README.md"
1212
edition = "2021"
13-
rust-version = "1.67"
13+
rust-version = "1.82"
1414

1515
[dependencies]
1616
borsh = { version = "1.5.5", default-features = false, features = ["derive"], optional = true }

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![crates.io][crates.io shield]][crates.io link]
99
[![Documentation][docs.rs badge]][docs.rs link]
1010
![Rust CI][github ci badge]
11-
![MSRV][rustc 1.67+]
11+
![MSRV][rustc 1.82+]
1212
<br />
1313
<br />
1414
[![Dependency Status][deps.rs status]][deps.rs link]
@@ -21,8 +21,8 @@
2121
[crates.io link]: https://crates.io/crates/bit-vec
2222
[docs.rs badge]: https://docs.rs/bit-vec/badge.svg?version=0.8.0
2323
[docs.rs link]: https://docs.rs/bit-vec/0.8.0/bit_vec/
24-
[github ci badge]: https://github.com/contain-rs/bit-vec/workflows/Rust/badge.svg?branch=master
25-
[rustc 1.67+]: https://img.shields.io/badge/rustc-1.67%2B-blue.svg
24+
[github ci badge]: https://github.com/contain-rs/bit-vec/actions/workflows/rust.yml/badge.svg
25+
[rustc 1.82+]: https://img.shields.io/badge/rustc-1.82%2B-blue.svg
2626
[deps.rs status]: https://deps.rs/crate/bit-vec/0.8.0/status.svg
2727
[deps.rs link]: https://deps.rs/crate/bit-vec/0.8.0
2828
[shields.io download count]: https://img.shields.io/crates/d/bit-vec.svg

benches/bench.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ fn bench_usize_small(b: &mut Bencher) {
4242
});
4343
}
4444

45+
#[bench]
46+
fn bench_to_bytes(b: &mut Bencher) {
47+
let mut bit_vec = BitVec::from_elem(BENCH_BITS, false);
48+
let mut r = small_rng();
49+
for _ in 0..BENCH_BITS / 10 {
50+
bit_vec.set((r.next_u32() as usize) % BENCH_BITS, true);
51+
}
52+
b.iter(|| {
53+
black_box(bit_vec.to_bytes());
54+
});
55+
}
56+
4557
#[bench]
4658
fn bench_bit_set_big_fixed(b: &mut Bencher) {
4759
let mut r = small_rng();

src/lib.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ impl<B: BitBlock> BitVec<B> {
826826
pub fn or(&mut self, other: &Self) -> bool {
827827
self.ensure_invariant();
828828
debug_assert!(other.is_last_block_fixed());
829-
self.process(other, |w1, w2| (w1 | w2))
829+
self.process(other, |w1, w2| w1 | w2)
830830
}
831831

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

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

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

933933
/// Calculates the nand of two bitvectors.
@@ -1322,30 +1322,35 @@ impl<B: BitBlock> BitVec<B> {
13221322
/// assert_eq!(bv.to_bytes(), [0b00100000, 0b10000000]);
13231323
/// ```
13241324
pub fn to_bytes(&self) -> Vec<u8> {
1325+
static REVERSE_TABLE: [u8; 256] = {
1326+
let mut tbl = [0u8; 256];
1327+
let mut i: u8 = 0;
1328+
loop {
1329+
tbl[i as usize] = i.reverse_bits();
1330+
if i == 255 {
1331+
break;
1332+
}
1333+
i += 1;
1334+
}
1335+
tbl
1336+
};
13251337
self.ensure_invariant();
1326-
// Oh lord, we're mapping this to bytes bit-by-bit!
1327-
fn bit<B: BitBlock>(bit_vec: &BitVec<B>, byte: usize, bit: usize) -> u8 {
1328-
let offset = byte * 8 + bit;
1329-
if offset >= bit_vec.nbits {
1330-
0
1331-
} else {
1332-
(bit_vec[offset] as u8) << (7 - bit)
1338+
1339+
let len = self.nbits / 8 + if self.nbits % 8 == 0 { 0 } else { 1 };
1340+
let mut result = Vec::with_capacity(len);
1341+
1342+
for byte_idx in 0..len {
1343+
let mut byte = 0u8;
1344+
for bit_idx in 0..8 {
1345+
let offset = byte_idx * 8 + bit_idx;
1346+
if offset < self.nbits && self[offset] {
1347+
byte |= 1 << bit_idx;
1348+
}
13331349
}
1350+
result.push(REVERSE_TABLE[byte as usize]);
13341351
}
13351352

1336-
let len = self.nbits / 8 + if self.nbits % 8 == 0 { 0 } else { 1 };
1337-
(0..len)
1338-
.map(|i| {
1339-
bit(self, i, 0)
1340-
| bit(self, i, 1)
1341-
| bit(self, i, 2)
1342-
| bit(self, i, 3)
1343-
| bit(self, i, 4)
1344-
| bit(self, i, 5)
1345-
| bit(self, i, 6)
1346-
| bit(self, i, 7)
1347-
})
1348-
.collect()
1353+
result
13491354
}
13501355

13511356
/// Compares a `BitVec` to a slice of `bool`s.

0 commit comments

Comments
 (0)