From df54197bd1e06d8b65f1a793afe152c939dbcd45 Mon Sep 17 00:00:00 2001 From: Tom Godkin Date: Fri, 29 Nov 2024 11:14:42 +0000 Subject: [PATCH] Minor refactor --- src/aminoacidsequence.rs | 4 ++-- src/dnasequence.rs | 4 ++-- src/member.rs | 10 ++-------- src/rnasequence.rs | 4 ++-- src/sequence.rs | 26 +++++++++++++------------- 5 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/aminoacidsequence.rs b/src/aminoacidsequence.rs index c022332..785a57d 100644 --- a/src/aminoacidsequence.rs +++ b/src/aminoacidsequence.rs @@ -46,13 +46,13 @@ impl AminoAcidSequence { fn __add__(&self, other: AminoAcidSequenceInput) -> PyResult { 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 { Ok(Self { - amino_acids: self.add(AminoAcidSequence::try_from(other)?.members().clone(), true)?, + amino_acids: self.add(AminoAcidSequence::try_from(other)?.members(), true), }) } diff --git a/src/dnasequence.rs b/src/dnasequence.rs index f9fc4e4..21a980e 100644 --- a/src/dnasequence.rs +++ b/src/dnasequence.rs @@ -70,13 +70,13 @@ impl DNASequence { fn __add__(&self, other: DNASequenceInput) -> PyResult { 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 { Ok(Self { - bases: self.add(DNASequence::try_from(other)?.members().clone(), true)?, + bases: self.add(DNASequence::try_from(other)?.members(), true), }) } diff --git a/src/member.rs b/src/member.rs index 9106ed2..fdb36b2 100644 --- a/src/member.rs +++ b/src/member.rs @@ -16,14 +16,8 @@ where { fn add(&self, other: &[T], swap: bool) -> Vec { 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(), } } } diff --git a/src/rnasequence.rs b/src/rnasequence.rs index f045671..c7779c5 100644 --- a/src/rnasequence.rs +++ b/src/rnasequence.rs @@ -103,13 +103,13 @@ impl RNASequence { fn __add__(&self, other: RNASequenceInput) -> PyResult { 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 { Ok(Self { - bases: self.add(RNASequence::try_from(other)?.members().clone(), true)?, + bases: self.add(RNASequence::try_from(other)?.members(), true), }) } diff --git a/src/sequence.rs b/src/sequence.rs index c2c3999..5d3f4d4 100644 --- a/src/sequence.rs +++ b/src/sequence.rs @@ -63,24 +63,24 @@ where }) } - fn add(&self, other: Vec, swap: bool) -> PyResult> + fn add(&self, other: &[T], swap: bool) -> Vec where T: Send, + for<'a> &'a [T]: IntoParallelIterator, { - 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> {