From 832d3ca2d2d4ba9f9bf6da0fd4f887db2e367b6a Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Tue, 30 Apr 2024 04:41:31 -0700 Subject: [PATCH] Various refactoring (#155) --- ext/crates/fp/src/prime/primes_generic.rs | 16 +++---- ext/crates/fp/src/vector/impl_fpvectorp.rs | 8 ++-- ext/crates/fp/src/vector/impl_slicep.rs | 4 +- ext/crates/fp/src/vector/mod.rs | 24 +++++------ ext/crates/fp/src/vector/vector_generic.rs | 44 ++++++++++---------- ext/crates/once/src/lib.rs | 12 +----- ext/crates/sseq/src/coordinates/bidegree.rs | 4 +- ext/crates/sseq/src/coordinates/element.rs | 4 +- ext/crates/sseq/src/coordinates/generator.rs | 8 ++-- ext/crates/sseq/src/differential.rs | 2 +- ext/examples/steenrod.rs | 4 +- python_ext/pyo3/python_fp/src/basis.rs | 2 +- python_ext/pyo3/python_fp/src/vector.rs | 4 +- web_ext/sseq_gui/src/main.rs | 2 +- web_ext/sseq_gui/src/managers.rs | 4 +- 15 files changed, 67 insertions(+), 75 deletions(-) diff --git a/ext/crates/fp/src/prime/primes_generic.rs b/ext/crates/fp/src/prime/primes_generic.rs index 0d8cb4dc8..0280a66f2 100644 --- a/ext/crates/fp/src/prime/primes_generic.rs +++ b/ext/crates/fp/src/prime/primes_generic.rs @@ -35,7 +35,7 @@ pub const fn is_prime(p: u32) -> bool { } impl ValidPrime { - pub const fn new(p: u32) -> ValidPrime { + pub const fn new(p: u32) -> Self { // We need the size restriction for a few reasons. // // First, we need `bit_length(p)` to be smaller than 64. Otherwise, shifting a u64 by 64 @@ -48,11 +48,11 @@ impl ValidPrime { // but it doesn't seem worth it for now. assert!(p < (1 << 31), "Tried to construct a prime larger than 2^31"); assert!(is_prime(p), "Tried to construct a composite dynamic prime"); - ValidPrime { p } + Self { p } } - pub const fn new_unchecked(p: u32) -> ValidPrime { - ValidPrime { p } + pub const fn new_unchecked(p: u32) -> Self { + Self { p } } } @@ -61,7 +61,7 @@ impl Prime for ValidPrime { self.p as i32 } - fn to_dyn(self) -> ValidPrime { + fn to_dyn(self) -> Self { self } } @@ -73,7 +73,7 @@ impl TryFrom for ValidPrime { fn try_from(p: u32) -> Result { if is_prime(p) { - Ok(ValidPrime { p }) + Ok(Self { p }) } else { Err(PrimeError::InvalidPrime(p)) } @@ -85,7 +85,7 @@ impl FromStr for ValidPrime { fn from_str(s: &str) -> Result { let p: u32 = s.parse().map_err(PrimeError::NotAnInteger)?; - ValidPrime::try_from(p) + Self::try_from(p) } } @@ -104,6 +104,6 @@ impl<'de> Deserialize<'de> for ValidPrime { D: Deserializer<'de>, { let p: u32 = u32::deserialize(deserializer)?; - ValidPrime::try_from(p).map_err(D::Error::custom) + Self::try_from(p).map_err(D::Error::custom) } } diff --git a/ext/crates/fp/src/vector/impl_fpvectorp.rs b/ext/crates/fp/src/vector/impl_fpvectorp.rs index 6b2a08335..01881695e 100644 --- a/ext/crates/fp/src/vector/impl_fpvectorp.rs +++ b/ext/crates/fp/src/vector/impl_fpvectorp.rs @@ -129,7 +129,7 @@ impl FpVectorP

{ /// Add `other` to `self` on the assumption that the first `offset` entries of `other` are /// empty. - pub fn add_offset(&mut self, other: &FpVectorP

, c: u32, offset: usize) { + pub fn add_offset(&mut self, other: &Self, c: u32, offset: usize) { assert_eq!(self.len(), other.len()); let min_limb = offset / limb::entries_per_limb(self.p); if self.p == 2 { @@ -148,7 +148,7 @@ impl FpVectorP

{ /// Add `other` to `self` on the assumption that the first `offset` entries of `other` are /// empty. - pub fn add_offset_nosimd(&mut self, other: &FpVectorP

, c: u32, offset: usize) { + pub fn add_offset_nosimd(&mut self, other: &Self, c: u32, offset: usize) { assert_eq!(self.len(), other.len()); let min_limb = offset / limb::entries_per_limb(self.p); if self.p == 2 { @@ -167,11 +167,11 @@ impl FpVectorP

{ } } - pub fn add(&mut self, other: &FpVectorP

, c: u32) { + pub fn add(&mut self, other: &Self, c: u32) { self.add_offset(other, c, 0); } - pub fn add_nosimd(&mut self, other: &FpVectorP

, c: u32) { + pub fn add_nosimd(&mut self, other: &Self, c: u32) { self.add_offset_nosimd(other, c, 0); } diff --git a/ext/crates/fp/src/vector/impl_slicep.rs b/ext/crates/fp/src/vector/impl_slicep.rs index d861f2094..72d5f9e80 100644 --- a/ext/crates/fp/src/vector/impl_slicep.rs +++ b/ext/crates/fp/src/vector/impl_slicep.rs @@ -68,10 +68,10 @@ impl<'a, P: Prime> SliceP<'a, P> { } #[must_use] - pub fn slice(self, start: usize, end: usize) -> SliceP<'a, P> { + pub fn slice(self, start: usize, end: usize) -> Self { assert!(start <= end && end <= self.len()); - SliceP { + Self { p: self.p, limbs: self.limbs, start: self.start + start, diff --git a/ext/crates/fp/src/vector/mod.rs b/ext/crates/fp/src/vector/mod.rs index aa448524c..75828f0bd 100644 --- a/ext/crates/fp/src/vector/mod.rs +++ b/ext/crates/fp/src/vector/mod.rs @@ -50,7 +50,7 @@ mod test { result } - pub fn diff_vec(&self, other: &FpVector) -> Vec { + pub fn diff_vec(&self, other: &Self) -> Vec { assert!(self.len() == other.len()); let mut result = Vec::new(); for index in 0..self.len() { @@ -81,11 +81,11 @@ mod test { "assert {} == {:?}\n{}", self, other, - FpVector::format_diff(diff) + Self::format_diff(diff) ); } - pub fn assert_vec_eq(&self, other: &FpVector) { + pub fn assert_vec_eq(&self, other: &Self) { let diff = self.diff_vec(other); if diff.is_empty() { return; @@ -94,7 +94,7 @@ mod test { "assert {} == {:?}\n{}", self, other, - FpVector::format_diff(diff) + Self::format_diff(diff) ); } } @@ -502,15 +502,15 @@ mod test { #[test] fn test_iterator((p, v_arr) in arb_vec()) { - let v = FpVector::from_slice(p, &v_arr); + let v = FpVector::from_slice(p, &v_arr); - let w = v.iter(); - let mut counter = 0; - for (i, x) in w.enumerate() { - prop_assert_eq!(v.entry(i), x); - counter += 1; - } - prop_assert_eq!(counter, v.len()); + let w = v.iter(); + let mut counter = 0; + for (i, x) in w.enumerate() { + prop_assert_eq!(v.entry(i), x); + counter += 1; + } + prop_assert_eq!(counter, v.len()); } #[test] diff --git a/ext/crates/fp/src/vector/vector_generic.rs b/ext/crates/fp/src/vector/vector_generic.rs index f682213cf..42d255b9a 100644 --- a/ext/crates/fp/src/vector/vector_generic.rs +++ b/ext/crates/fp/src/vector/vector_generic.rs @@ -230,7 +230,7 @@ impl FpVector { pub(crate) fn trim_start(&mut self, n: usize); pub fn add_truncate(&mut self, other: &Self, c: u32) -> (Option<()>); pub fn sign_rule(&self, other: &Self) -> bool; - pub fn add_carry(&mut self, other: &Self, c: u32, rest: &mut [FpVector]) -> bool; + pub fn add_carry(&mut self, other: &Self, c: u32, rest: &mut [Self]) -> bool; pub fn first_nonzero(&self) -> (Option<(usize, u32)>); pub fn density(&self) -> f32; @@ -238,33 +238,33 @@ impl FpVector { pub(crate) fn limbs_mut(&mut self) -> (&mut [Limb]); } - pub fn new(p: P, len: usize) -> FpVector { + pub fn new(p: P, len: usize) -> Self { match p.as_u32() { - 2 => FpVector::_2(FpVectorP::new(P2, len)), - 3 => FpVector::_3(FpVectorP::new(P3, len)), - 5 => FpVector::_5(FpVectorP::new(P5, len)), - 7 => FpVector::_7(FpVectorP::new(P7, len)), - _ => FpVector::Big(FpVectorP::new(p.to_dyn(), len)), + 2 => Self::_2(FpVectorP::new(P2, len)), + 3 => Self::_3(FpVectorP::new(P3, len)), + 5 => Self::_5(FpVectorP::new(P5, len)), + 7 => Self::_7(FpVectorP::new(P7, len)), + _ => Self::Big(FpVectorP::new(p.to_dyn(), len)), } } pub fn new_with_capacity(p: P, len: usize, capacity: usize) -> FpVector { match p.as_u32() { - 2 => FpVector::_2(FpVectorP::new_with_capacity(P2, len, capacity)), - 3 => FpVector::_3(FpVectorP::new_with_capacity(P3, len, capacity)), - 5 => FpVector::_5(FpVectorP::new_with_capacity(P5, len, capacity)), - 7 => FpVector::_7(FpVectorP::new_with_capacity(P7, len, capacity)), - _ => FpVector::Big(FpVectorP::new_with_capacity(p.to_dyn(), len, capacity)), + 2 => Self::_2(FpVectorP::new_with_capacity(P2, len, capacity)), + 3 => Self::_3(FpVectorP::new_with_capacity(P3, len, capacity)), + 5 => Self::_5(FpVectorP::new_with_capacity(P5, len, capacity)), + 7 => Self::_7(FpVectorP::new_with_capacity(P7, len, capacity)), + _ => Self::Big(FpVectorP::new_with_capacity(p.to_dyn(), len, capacity)), } } pub fn from_slice(p: P, slice: &[u32]) -> Self { match p.as_u32() { - 2 => FpVector::_2(FpVectorP::from((P2, &slice))), - 3 => FpVector::_3(FpVectorP::from((P3, &slice))), - 5 => FpVector::_5(FpVectorP::from((P5, &slice))), - 7 => FpVector::_7(FpVectorP::from((P7, &slice))), - _ => FpVector::Big(FpVectorP::from((p.to_dyn(), &slice))), + 2 => Self::_2(FpVectorP::from((P2, &slice))), + 3 => Self::_3(FpVectorP::from((P3, &slice))), + 5 => Self::_5(FpVectorP::from((P5, &slice))), + 7 => Self::_7(FpVectorP::from((P7, &slice))), + _ => Self::Big(FpVectorP::from((p.to_dyn(), &slice))), } } @@ -357,11 +357,11 @@ impl<'a> SliceMut<'a> { pub fn add_tensor(&mut self, offset: usize, coeff: u32, left: Slice, right: Slice) { match (self, left, right) { - (SliceMut::_2(x), Slice::_2(y), Slice::_2(z)) => x.add_tensor(offset, coeff, y, z), - (SliceMut::_3(x), Slice::_3(y), Slice::_3(z)) => x.add_tensor(offset, coeff, y, z), - (SliceMut::_5(x), Slice::_5(y), Slice::_5(z)) => x.add_tensor(offset, coeff, y, z), - (SliceMut::_7(x), Slice::_7(y), Slice::_7(z)) => x.add_tensor(offset, coeff, y, z), - (SliceMut::Big(x), Slice::Big(y), Slice::Big(z)) => x.add_tensor(offset, coeff, y, z), + (Self::_2(x), Slice::_2(y), Slice::_2(z)) => x.add_tensor(offset, coeff, y, z), + (Self::_3(x), Slice::_3(y), Slice::_3(z)) => x.add_tensor(offset, coeff, y, z), + (Self::_5(x), Slice::_5(y), Slice::_5(z)) => x.add_tensor(offset, coeff, y, z), + (Self::_7(x), Slice::_7(y), Slice::_7(z)) => x.add_tensor(offset, coeff, y, z), + (Self::Big(x), Slice::Big(y), Slice::Big(z)) => x.add_tensor(offset, coeff, y, z), _ => { panic!("Applying add_tensor to vectors over different primes"); } diff --git a/ext/crates/once/src/lib.rs b/ext/crates/once/src/lib.rs index f2fd75c4f..84877cb2a 100644 --- a/ext/crates/once/src/lib.rs +++ b/ext/crates/once/src/lib.rs @@ -225,15 +225,7 @@ where T: PartialEq, { fn eq(&self, other: &OnceVec) -> bool { - if self.len() != other.len() { - return false; - } - for i in 0..self.len() { - if self[i] != other[i] { - return false; - } - } - true + self.len() == other.len() && self.iter().eq(other.iter()) } } @@ -672,7 +664,7 @@ impl fmt::Debug for OnceBiVec { impl OnceBiVec { pub fn new(min_degree: i32) -> Self { - OnceBiVec { + Self { data: OnceVec::new(), min_degree, } diff --git a/ext/crates/sseq/src/coordinates/bidegree.rs b/ext/crates/sseq/src/coordinates/bidegree.rs index 059d11491..65608f2a6 100644 --- a/ext/crates/sseq/src/coordinates/bidegree.rs +++ b/ext/crates/sseq/src/coordinates/bidegree.rs @@ -74,7 +74,7 @@ impl Display for Bidegree { impl Add for Bidegree { type Output = Self; - fn add(self, other: Bidegree) -> Self { + fn add(self, other: Self) -> Self { Self { s: self.s + other.s, t: self.t + other.t, @@ -85,7 +85,7 @@ impl Add for Bidegree { impl Sub for Bidegree { type Output = Self; - fn sub(self, other: Bidegree) -> Self { + fn sub(self, other: Self) -> Self { Self { s: self.s - other.s, t: self.t - other.t, diff --git a/ext/crates/sseq/src/coordinates/element.rs b/ext/crates/sseq/src/coordinates/element.rs index 0819facf8..63448a51d 100644 --- a/ext/crates/sseq/src/coordinates/element.rs +++ b/ext/crates/sseq/src/coordinates/element.rs @@ -19,8 +19,8 @@ pub struct BidegreeElement { } impl BidegreeElement { - pub fn new(degree: Bidegree, vec: FpVector) -> BidegreeElement { - BidegreeElement { degree, vec } + pub fn new(degree: Bidegree, vec: FpVector) -> Self { + Self { degree, vec } } pub fn s(&self) -> u32 { diff --git a/ext/crates/sseq/src/coordinates/generator.rs b/ext/crates/sseq/src/coordinates/generator.rs index 46d91002a..bc1969ccb 100644 --- a/ext/crates/sseq/src/coordinates/generator.rs +++ b/ext/crates/sseq/src/coordinates/generator.rs @@ -15,18 +15,18 @@ pub struct BidegreeGenerator { } impl BidegreeGenerator { - pub fn new>(degree: T, idx: usize) -> BidegreeGenerator { - BidegreeGenerator { + pub fn new>(degree: T, idx: usize) -> Self { + Self { degree: degree.into(), idx, } } - pub fn s_t(s: u32, t: i32, idx: usize) -> BidegreeGenerator { + pub fn s_t(s: u32, t: i32, idx: usize) -> Self { Self::new(Bidegree::s_t(s, t), idx) } - pub fn n_s(n: i32, s: u32, idx: usize) -> BidegreeGenerator { + pub fn n_s(n: i32, s: u32, idx: usize) -> Self { Self::new(Bidegree::n_s(n, s), idx) } diff --git a/ext/crates/sseq/src/differential.rs b/ext/crates/sseq/src/differential.rs index 05620dbe9..fd0132c3c 100644 --- a/ext/crates/sseq/src/differential.rs +++ b/ext/crates/sseq/src/differential.rs @@ -15,7 +15,7 @@ pub struct Differential { impl Differential { pub fn new(p: ValidPrime, source_dim: usize, target_dim: usize) -> Self { // Leave more rows to make room for inconsistent differentials - Differential { + Self { matrix: Matrix::new(p, source_dim + target_dim + 1, source_dim + target_dim), source_dim, target_dim, diff --git a/ext/examples/steenrod.rs b/ext/examples/steenrod.rs index 999ce1169..0878bf59a 100644 --- a/ext/examples/steenrod.rs +++ b/ext/examples/steenrod.rs @@ -221,7 +221,7 @@ mod sum_module { impl SumModule { pub fn new(algebra: Arc, modules: Vec>, min_degree: i32) -> Self { - SumModule { + Self { algebra, modules, min_degree, @@ -325,7 +325,7 @@ mod sum_module { impl ZeroModule for SumModule { fn zero_module(algebra: Arc, min_degree: i32) -> Self { - SumModule::new(algebra, vec![], min_degree) + Self::new(algebra, vec![], min_degree) } } diff --git a/python_ext/pyo3/python_fp/src/basis.rs b/python_ext/pyo3/python_fp/src/basis.rs index 57fd1dd1f..fb5bd47b3 100644 --- a/python_ext/pyo3/python_fp/src/basis.rs +++ b/python_ext/pyo3/python_fp/src/basis.rs @@ -48,7 +48,7 @@ impl Basis { impl Basis { #[new] pub fn new(p: u32, dimension : usize) -> PyResult { - Ok(Basis::box_and_wrap(BasisRust::new(new_valid_prime(p)?, dimension))) + Ok(Self::box_and_wrap(BasisRust::new(new_valid_prime(p)?, dimension))) } #[getter] diff --git a/python_ext/pyo3/python_fp/src/vector.rs b/python_ext/pyo3/python_fp/src/vector.rs index 55b682620..5b72962b0 100644 --- a/python_ext/pyo3/python_fp/src/vector.rs +++ b/python_ext/pyo3/python_fp/src/vector.rs @@ -238,7 +238,7 @@ impl FpVector { #[staticmethod] pub fn from_list(p : u32, l : PyObject) -> PyResult { let vec = vec_from_pyobj(p, l)?; - Ok(FpVector::box_and_wrap(FpVectorRust::from_vec(new_valid_prime(p)?, &vec))) + Ok(Self::box_and_wrap(FpVectorRust::from_vec(new_valid_prime(p)?, &vec))) } @@ -255,7 +255,7 @@ impl FpVector { } #[args(c=1)] - pub fn add(&mut self, other : &FpVector, c : i32) -> PyResult<()> { + pub fn add(&mut self, other : &Self, c : i32) -> PyResult<()> { self.check_not_null()?; other.check_not_null()?; if self.equal(other) { diff --git a/web_ext/sseq_gui/src/main.rs b/web_ext/sseq_gui/src/main.rs index a23d1a82d..4735b63ee 100644 --- a/web_ext/sseq_gui/src/main.rs +++ b/web_ext/sseq_gui/src/main.rs @@ -215,7 +215,7 @@ impl Handler for Server { impl Server { pub fn new(out: WsSender) -> Self { - Server { + Self { manager: None, out: Some(out), } diff --git a/web_ext/sseq_gui/src/managers.rs b/web_ext/sseq_gui/src/managers.rs index 5ad511d97..d883176d0 100644 --- a/web_ext/sseq_gui/src/managers.rs +++ b/web_ext/sseq_gui/src/managers.rs @@ -31,7 +31,7 @@ impl ResolutionManager { /// # Arguments /// * `sender` - The `sender` object to send messages to. pub fn new(sender: Sender) -> Self { - ResolutionManager { + Self { sender, resolution: None, is_unit: false, @@ -212,7 +212,7 @@ impl SseqManager { /// # Arguments /// * `sender` - The `Sender` object to send messages to. pub fn new(sender: Sender) -> Self { - SseqManager { + Self { sender, sseq: None, unit_sseq: None,