Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed Nov 29, 2024
1 parent 594c25c commit df54197
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/aminoacidsequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ impl AminoAcidSequence {

fn __add__(&self, other: AminoAcidSequenceInput) -> PyResult<Self> {
Ok(Self {
amino_acids: self.add(AminoAcidSequence::try_from(other)?.members().clone(), false)?,
amino_acids: self.add(AminoAcidSequence::try_from(other)?.members(), false),
})
}

fn __radd__(&self, other: AminoAcidSequenceInput) -> PyResult<Self> {
Ok(Self {
amino_acids: self.add(AminoAcidSequence::try_from(other)?.members().clone(), true)?,
amino_acids: self.add(AminoAcidSequence::try_from(other)?.members(), true),
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/dnasequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ impl DNASequence {

fn __add__(&self, other: DNASequenceInput) -> PyResult<Self> {
Ok(Self {
bases: self.add(DNASequence::try_from(other)?.members().clone(), false)?,
bases: self.add(DNASequence::try_from(other)?.members(), false),
})
}

fn __radd__(&self, other: DNASequenceInput) -> PyResult<Self> {
Ok(Self {
bases: self.add(DNASequence::try_from(other)?.members().clone(), true)?,
bases: self.add(DNASequence::try_from(other)?.members(), true),
})
}

Expand Down
10 changes: 2 additions & 8 deletions src/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@ where
{
fn add(&self, other: &[T], swap: bool) -> Vec<T> {
match swap {
true => other
.par_iter()
.cloned()
.chain(once(self.clone()))
.collect(),
false => once(self.clone())
.chain(other.par_iter().cloned())
.collect(),
true => other.par_iter().chain(once(self)).cloned().collect(),
false => once(self).chain(other.par_iter()).cloned().collect(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/rnasequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ impl RNASequence {

fn __add__(&self, other: RNASequenceInput) -> PyResult<Self> {
Ok(Self {
bases: self.add(RNASequence::try_from(other)?.members().clone(), false)?,
bases: self.add(RNASequence::try_from(other)?.members(), false),
})
}

fn __radd__(&self, other: RNASequenceInput) -> PyResult<Self> {
Ok(Self {
bases: self.add(RNASequence::try_from(other)?.members().clone(), true)?,
bases: self.add(RNASequence::try_from(other)?.members(), true),
})
}

Expand Down
26 changes: 13 additions & 13 deletions src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ where
})
}

fn add(&self, other: Vec<T>, swap: bool) -> PyResult<Vec<T>>
fn add(&self, other: &[T], swap: bool) -> Vec<T>
where
T: Send,
for<'a> &'a [T]: IntoParallelIterator<Item = &'a T>,
{
let mut members = Vec::with_capacity(self.len() + other.len());

match swap {
true => {
members.par_extend(other);
members.par_extend(self.members().to_vec());
}
false => {
members.par_extend(self.members().to_vec());
members.par_extend(other);
}
true => other
.par_iter()
.chain(self.members().par_iter())
.cloned()
.collect(),
false => self
.members()
.par_iter()
.chain(other.par_iter())
.cloned()
.collect(),
}

Ok(members)
}

fn getitem(&self, index_or_slice: IntOrSlice) -> PyResult<MemberOrMembers<T>> {
Expand Down

0 comments on commit df54197

Please sign in to comment.