From c548938190c10f3e6aa8709f0f5cc0a8933c135b Mon Sep 17 00:00:00 2001 From: Arhan Chaudhary Date: Sun, 5 Apr 2026 07:42:44 -0400 Subject: [PATCH 1/3] chore: apply clippy lints --- bitgauss/src/bitmatrix.rs | 60 +++++++++++++++++++-------------------- bitgauss/src/bitvector.rs | 18 ++++++------ bitgauss/src/data.rs | 22 +++++++------- 3 files changed, 50 insertions(+), 50 deletions(-) diff --git a/bitgauss/src/bitmatrix.rs b/bitgauss/src/bitmatrix.rs index b18df93..584cd91 100644 --- a/bitgauss/src/bitmatrix.rs +++ b/bitgauss/src/bitmatrix.rs @@ -82,7 +82,7 @@ impl BitMatrix { } /// Creates a new `BitMatrix` from a vector of bool vectors - pub fn from_bool_vec(data: &Vec>) -> Self { + pub fn from_bool_vec(data: &[Vec]) -> Self { Self::build( data.len(), if data.is_empty() { 0 } else { data[0].len() }, @@ -91,7 +91,7 @@ impl BitMatrix { } /// Creates a new `BitMatrix` from a vector of integer vectors - pub fn from_int_vec(data: &Vec>) -> Self { + pub fn from_int_vec(data: &[Vec]) -> Self { Self::build( data.len(), if data.is_empty() { 0 } else { data[0].len() }, @@ -466,7 +466,7 @@ impl BitMatrix { /// # Arguments /// - `full`: if this is true, compute reduced echelon form /// - `blocksize`: a Patel-Markov-Hayes blocksize. This can be set to reduce the total number of - /// row operations + /// row operations /// - `proxy`: a struct that implements [`RowOps`] and receives the same row operations as the /// matrix being reduced. This can be used e.g. for reversible logic circuit synthesis #[inline] @@ -829,12 +829,12 @@ mod test { let m = BitMatrix::from_bool_vec(&data); assert_eq!(m.rows(), 2); assert_eq!(m.cols(), 3); - assert_eq!(m.bit(0, 0), true); - assert_eq!(m.bit(0, 1), false); - assert_eq!(m.bit(0, 2), true); - assert_eq!(m.bit(1, 0), false); - assert_eq!(m.bit(1, 1), true); - assert_eq!(m.bit(1, 2), false); + assert!(m.bit(0, 0)); + assert!(!m.bit(0, 1)); + assert!(m.bit(0, 2)); + assert!(!m.bit(1, 0)); + assert!(m.bit(1, 1)); + assert!(!m.bit(1, 2)); } // test from_int_vec @@ -844,12 +844,12 @@ mod test { let m = BitMatrix::from_int_vec(&data); assert_eq!(m.rows(), 2); assert_eq!(m.cols(), 3); - assert_eq!(m.bit(0, 0), true); - assert_eq!(m.bit(0, 1), false); - assert_eq!(m.bit(0, 2), true); - assert_eq!(m.bit(1, 0), false); - assert_eq!(m.bit(1, 1), true); - assert_eq!(m.bit(1, 2), false); + assert!(m.bit(0, 0)); + assert!(!m.bit(0, 1)); + assert!(m.bit(0, 2)); + assert!(!m.bit(1, 0)); + assert!(m.bit(1, 1)); + assert!(!m.bit(1, 2)); } // test construction from empty vectors @@ -1188,10 +1188,10 @@ mod test { assert_eq!(result.cols(), 2); // Check that the stacking worked correctly - assert_eq!(result[(0, 0)], true); // From identity - assert_eq!(result[(1, 1)], true); // From identity - assert_eq!(result[(2, 0)], false); // From zeros - assert_eq!(result[(5, 1)], true); // From all-ones row + assert!(result[(0, 0)]); // From identity + assert!(result[(1, 1)]); // From identity + assert!(!result[(2, 0)]); // From zeros + assert!(result[(5, 1)]); // From all-ones row } #[test] @@ -1205,10 +1205,10 @@ mod test { assert_eq!(result.cols(), 6); // Check that the stacking worked correctly - assert_eq!(result[(0, 0)], true); // From identity - assert_eq!(result[(1, 1)], true); // From identity - assert_eq!(result[(0, 2)], false); // From zeros - assert_eq!(result[(0, 5)], true); // From all-ones column + assert!(result[(0, 0)]); // From identity + assert!(result[(1, 1)]); // From identity + assert!(!result[(0, 2)]); // From zeros + assert!(result[(0, 5)]); // From all-ones column } #[test] @@ -1264,10 +1264,10 @@ mod test { // Test row swap m.swap_rows(0, 2); - assert_eq!(m[(0, 0)], false); - assert_eq!(m[(2, 2)], false); - assert_eq!(m[(0, 2)], true); - assert_eq!(m[(2, 0)], true); + assert!(!m[(0, 0)]); + assert!(!m[(2, 2)]); + assert!(m[(0, 2)]); + assert!(m[(2, 0)]); // Swap back m.swap_rows(0, 2); @@ -1275,8 +1275,8 @@ mod test { // Test row addition (XOR) m.add_row(0, 1); // Add row 0 to row 1 - assert_eq!(m[(1, 0)], true); // XOR of 0 and 1 - assert_eq!(m[(1, 1)], true); // XOR of 1 and 0 + assert!(m[(1, 0)]); // XOR of 0 and 1 + assert!(m[(1, 1)]); // XOR of 1 and 0 } #[test] @@ -1305,7 +1305,7 @@ mod test { { let row1 = m_mut.row_mut(1); // Modify the row (this is at the BitVec level) - if row1.len() > 0 { + if !row1.is_empty() { row1[0] ^= MSB_ON; // Flip the first bit } } diff --git a/bitgauss/src/bitvector.rs b/bitgauss/src/bitvector.rs index 6cbb1bb..18a6394 100644 --- a/bitgauss/src/bitvector.rs +++ b/bitgauss/src/bitvector.rs @@ -33,12 +33,12 @@ impl BitVector { } /// Creates a new `BitVector` from a vector of bool vectors - pub fn from_bool_vec(data: &Vec) -> Self { + pub fn from_bool_vec(data: &[bool]) -> Self { Self::build(data.len(), |i| data[i]) } /// Creates a new `BitVector` from a vector of integer vectors - pub fn from_int_vec(data: &Vec) -> Self { + pub fn from_int_vec(data: &[usize]) -> Self { Self::build(data.len(), |i| data[i] != 0) } @@ -239,12 +239,12 @@ mod tests { // Row 0: 1*1 + 0*1 + 1*0 = 1 + 0 + 0 = 1 (in GF(2)) // Row 1: 0*1 + 1*1 + 1*0 = 0 + 1 + 0 = 1 (in GF(2)) // Row 2: 1*1 + 1*1 + 0*0 = 1 + 1 + 0 = 0 (in GF(2), since 1⊕1=0) - let matrix = BitMatrix::from_bool_vec(&vec![ + let matrix = BitMatrix::from_bool_vec(&[ vec![true, false, true], vec![false, true, true], vec![true, true, false], ]); - let vector = BitVector::from_bool_vec(&vec![true, true, false]); + let vector = BitVector::from_bool_vec(&[true, true, false]); let result = &matrix * &vector; assert_eq!(result.len(), 3); @@ -257,20 +257,20 @@ mod tests { fn test_matrix_vector_multiplication_identity() { // Test multiplication with identity matrix let identity = BitMatrix::identity(3); - let vector = BitVector::from_bool_vec(&vec![true, false, true]); + let vector = BitVector::from_bool_vec(&[true, false, true]); let result = &identity * &vector; assert_eq!(result.len(), 3); - assert_eq!(result[0], true); - assert_eq!(result[1], false); - assert_eq!(result[2], true); + assert!(result[0]); + assert!(!result[1]); + assert!(result[2]); } #[test] fn test_matrix_vector_multiplication_zeros() { // Test multiplication with zero matrix let zero_matrix = BitMatrix::zeros(2, 3); - let vector = BitVector::from_bool_vec(&vec![true, true, true]); + let vector = BitVector::from_bool_vec(&[true, true, true]); let result = &zero_matrix * &vector; assert_eq!(result.len(), 2); diff --git a/bitgauss/src/data.rs b/bitgauss/src/data.rs index 4217cf2..423a808 100644 --- a/bitgauss/src/data.rs +++ b/bitgauss/src/data.rs @@ -52,7 +52,7 @@ pub fn min_blocks(bits: usize) -> usize { /// /// Many methods are implemented via dereferencing to [`BitSlice`], which provides /// additional bitwise and range operations. -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Default)] pub struct BitData(Vec); /// A range of bits, represented as a slice of [`BitBlock`]s. @@ -107,7 +107,7 @@ impl BitSlice { /// Returns an iterator over all bits in this range as `bool`s. #[inline] - pub fn iter(&self) -> BitIter { + pub fn iter(&self) -> BitIter<'_> { BitIter { inner: self.0.iter(), c: BLOCKSIZE, @@ -309,7 +309,7 @@ impl BitData { /// Returns an iterator over all bits in this vector as `bool`s. #[inline] - pub fn iter(&self) -> BitIter { + pub fn iter(&self) -> BitIter<'_> { self.deref().iter() } @@ -373,11 +373,6 @@ impl BitData { BitData(vec![BitBlock::MAX; num_blocks]) } - /// Constructs a new empty [`BitData`]. - pub fn new() -> Self { - BitData(Vec::new()) - } - /// Constructs a new [`BitData`] with the specified capacity in blocks pub fn with_capacity(num_blocks: usize) -> Self { BitData(Vec::with_capacity(num_blocks)) @@ -588,9 +583,14 @@ mod test { // ...so the remaining bits should be 0 assert_eq!(vec.num_bits(), bool_vec1.len()); - for i in bool_vec.len()..vec.len() { + for (i, &x) in bool_vec1 + .iter() + .enumerate() + .take(vec.len()) + .skip(bool_vec.len()) + { assert_eq!((i, vec.bit(i)), (i, false)); - assert_eq!((i, bool_vec1[i]), (i, false)); + assert_eq!((i, x), (i, false)); } } @@ -642,7 +642,7 @@ mod test { } else if i < 20 * BLOCKSIZE - shift { assert_eq!(v3.bit(i), v2.bit(i - (10 * BLOCKSIZE - shift))); } else { - assert_eq!(v3.bit(i), false); + assert!(!v3.bit(i)); } } } From bbc66558ace74b133faa1e9d311238212c441740 Mon Sep 17 00:00:00 2001 From: Arhan Chaudhary Date: Sun, 5 Apr 2026 07:44:32 -0400 Subject: [PATCH 2/3] feat: return pivot_columns from gauss method --- bitgauss/src/bitmatrix.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/bitgauss/src/bitmatrix.rs b/bitgauss/src/bitmatrix.rs index 584cd91..8bcb838 100644 --- a/bitgauss/src/bitmatrix.rs +++ b/bitgauss/src/bitmatrix.rs @@ -457,8 +457,8 @@ impl BitMatrix { /// If `full` is true, then perform full Gauss-Jordan to produce reduced echelon form, otherwise /// just returns echelon form. #[inline] - pub fn gauss(&mut self, full: bool) { - self.gauss_helper(full, 1, &mut ()); + pub fn gauss(&mut self, full: bool) -> Vec { + self.gauss_helper(full, 1, &mut ()) } /// Performs gaussian elimination with a `chunksize` and a `proxy` @@ -470,14 +470,19 @@ impl BitMatrix { /// - `proxy`: a struct that implements [`RowOps`] and receives the same row operations as the /// matrix being reduced. This can be used e.g. for reversible logic circuit synthesis #[inline] - pub fn gauss_with_proxy(&mut self, full: bool, chunksize: usize, proxy: &mut impl RowOps) { - self.gauss_helper(full, chunksize, proxy); + pub fn gauss_with_proxy( + &mut self, + full: bool, + chunksize: usize, + proxy: &mut impl RowOps, + ) -> Vec { + self.gauss_helper(full, chunksize, proxy) } /// Performs gaussian elimination using the Patel-Markov-Hayes algorithm with the given `chunksize` #[inline] - pub fn gauss_with_chunksize(&mut self, full: bool, chunksize: usize) { - self.gauss_helper(full, chunksize, &mut ()); + pub fn gauss_with_chunksize(&mut self, full: bool, chunksize: usize) -> Vec { + self.gauss_helper(full, chunksize, &mut ()) } /// Computes the rank of the matrix using gaussian elimination From 90e35b58d6807d3d9b6a1e37ed40db1998643b48 Mon Sep 17 00:00:00 2001 From: Arhan Chaudhary Date: Wed, 8 Apr 2026 20:26:56 -0400 Subject: [PATCH 3/3] fix: add back BitData::new --- bitgauss/src/data.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bitgauss/src/data.rs b/bitgauss/src/data.rs index 423a808..97b832f 100644 --- a/bitgauss/src/data.rs +++ b/bitgauss/src/data.rs @@ -373,6 +373,11 @@ impl BitData { BitData(vec![BitBlock::MAX; num_blocks]) } + /// Constructs a new empty [`BitData`]. + pub fn new() -> Self { + Self::default() + } + /// Constructs a new [`BitData`] with the specified capacity in blocks pub fn with_capacity(num_blocks: usize) -> Self { BitData(Vec::with_capacity(num_blocks))