Skip to content

Commit

Permalink
Various refactoring (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamnemecek committed Apr 30, 2024
1 parent b8ff743 commit 832d3ca
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 75 deletions.
16 changes: 8 additions & 8 deletions ext/crates/fp/src/prime/primes_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
}
}

Expand All @@ -61,7 +61,7 @@ impl Prime for ValidPrime {
self.p as i32
}

fn to_dyn(self) -> ValidPrime {
fn to_dyn(self) -> Self {
self
}
}
Expand All @@ -73,7 +73,7 @@ impl TryFrom<u32> for ValidPrime {

fn try_from(p: u32) -> Result<Self, PrimeError> {
if is_prime(p) {
Ok(ValidPrime { p })
Ok(Self { p })
} else {
Err(PrimeError::InvalidPrime(p))
}
Expand All @@ -85,7 +85,7 @@ impl FromStr for ValidPrime {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let p: u32 = s.parse().map_err(PrimeError::NotAnInteger)?;
ValidPrime::try_from(p)
Self::try_from(p)
}
}

Expand All @@ -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)
}
}
8 changes: 4 additions & 4 deletions ext/crates/fp/src/vector/impl_fpvectorp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<P: Prime> FpVectorP<P> {

/// Add `other` to `self` on the assumption that the first `offset` entries of `other` are
/// empty.
pub fn add_offset(&mut self, other: &FpVectorP<P>, 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 {
Expand All @@ -148,7 +148,7 @@ impl<P: Prime> FpVectorP<P> {

/// 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<P>, 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 {
Expand All @@ -167,11 +167,11 @@ impl<P: Prime> FpVectorP<P> {
}
}

pub fn add(&mut self, other: &FpVectorP<P>, 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<P>, c: u32) {
pub fn add_nosimd(&mut self, other: &Self, c: u32) {
self.add_offset_nosimd(other, c, 0);
}

Expand Down
4 changes: 2 additions & 2 deletions ext/crates/fp/src/vector/impl_slicep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions ext/crates/fp/src/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mod test {
result
}

pub fn diff_vec(&self, other: &FpVector) -> Vec<VectorDiffEntry> {
pub fn diff_vec(&self, other: &Self) -> Vec<VectorDiffEntry> {
assert!(self.len() == other.len());
let mut result = Vec::new();
for index in 0..self.len() {
Expand Down Expand Up @@ -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;
Expand All @@ -94,7 +94,7 @@ mod test {
"assert {} == {:?}\n{}",
self,
other,
FpVector::format_diff(diff)
Self::format_diff(diff)
);
}
}
Expand Down Expand Up @@ -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]
Expand Down
44 changes: 22 additions & 22 deletions ext/crates/fp/src/vector/vector_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,41 +230,41 @@ 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;

pub(crate) fn limbs(&self) -> (&[Limb]);
pub(crate) fn limbs_mut(&mut self) -> (&mut [Limb]);
}

pub fn new<P: Prime>(p: P, len: usize) -> FpVector {
pub fn new<P: Prime>(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: Prime>(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: Prime>(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))),
}
}

Expand Down Expand Up @@ -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");
}
Expand Down
12 changes: 2 additions & 10 deletions ext/crates/once/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,7 @@ where
T: PartialEq,
{
fn eq(&self, other: &OnceVec<T>) -> 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())
}
}

Expand Down Expand Up @@ -672,7 +664,7 @@ impl<T: fmt::Debug> fmt::Debug for OnceBiVec<T> {

impl<T> OnceBiVec<T> {
pub fn new(min_degree: i32) -> Self {
OnceBiVec {
Self {
data: OnceVec::new(),
min_degree,
}
Expand Down
4 changes: 2 additions & 2 deletions ext/crates/sseq/src/coordinates/bidegree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions ext/crates/sseq/src/coordinates/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions ext/crates/sseq/src/coordinates/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ pub struct BidegreeGenerator {
}

impl BidegreeGenerator {
pub fn new<T: Into<Bidegree>>(degree: T, idx: usize) -> BidegreeGenerator {
BidegreeGenerator {
pub fn new<T: Into<Bidegree>>(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)
}

Expand Down
2 changes: 1 addition & 1 deletion ext/crates/sseq/src/differential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions ext/examples/steenrod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ mod sum_module {

impl<M: Module> SumModule<M> {
pub fn new(algebra: Arc<M::Algebra>, modules: Vec<Arc<M>>, min_degree: i32) -> Self {
SumModule {
Self {
algebra,
modules,
min_degree,
Expand Down Expand Up @@ -325,7 +325,7 @@ mod sum_module {

impl<M: Module> ZeroModule for SumModule<M> {
fn zero_module(algebra: Arc<M::Algebra>, min_degree: i32) -> Self {
SumModule::new(algebra, vec![], min_degree)
Self::new(algebra, vec![], min_degree)
}
}

Expand Down
2 changes: 1 addition & 1 deletion python_ext/pyo3/python_fp/src/basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Basis {
impl Basis {
#[new]
pub fn new(p: u32, dimension : usize) -> PyResult<Self> {
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]
Expand Down
4 changes: 2 additions & 2 deletions python_ext/pyo3/python_fp/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl FpVector {
#[staticmethod]
pub fn from_list(p : u32, l : PyObject) -> PyResult<Self> {
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)))
}


Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion web_ext/sseq_gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Handler for Server {

impl Server {
pub fn new(out: WsSender) -> Self {
Server {
Self {
manager: None,
out: Some(out),
}
Expand Down
4 changes: 2 additions & 2 deletions web_ext/sseq_gui/src/managers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 832d3ca

Please sign in to comment.