Skip to content
Snippets Groups Projects
Commit 3141f9f7 authored by Martin Larralde's avatar Martin Larralde
Browse files

Make `lightmotif::scores::Scores` generic over the element type

parent eb622230
No related branches found
No related tags found
No related merge requests found
...@@ -19,17 +19,19 @@ use crate::pli::Threshold; ...@@ -19,17 +19,19 @@ use crate::pli::Threshold;
/// Simple vector storing scores for a score sequence. /// Simple vector storing scores for a score sequence.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Scores { pub struct Scores<T> {
/// The raw vector storing the scores. /// The raw vector storing the scores.
data: Vec<f32>, data: Vec<T>,
} }
impl Scores { impl<T> Scores<T> {
/// Create a new collection from an array of scores. /// Create a new collection from an array of scores.
pub fn new(data: Vec<f32>) -> Self { pub fn new(data: Vec<T>) -> Self {
Self { data } Self { data }
} }
}
impl<T: PartialOrd> Scores<T> {
/// Find the position with the highest score. /// Find the position with the highest score.
pub fn argmax(&self) -> Option<usize> { pub fn argmax(&self) -> Option<usize> {
self.data self.data
...@@ -39,40 +41,42 @@ impl Scores { ...@@ -39,40 +41,42 @@ impl Scores {
.map(|(i, _)| i) .map(|(i, _)| i)
} }
/// Find the highest score.
pub fn max(&self) -> Option<f32> {
self.data
.iter()
.max_by(|x, y| x.partial_cmp(y).unwrap())
.cloned()
}
/// Return the positions with score equal to or greater than the threshold. /// Return the positions with score equal to or greater than the threshold.
pub fn threshold(&self, threshold: f32) -> Vec<usize> { pub fn threshold(&self, threshold: &T) -> Vec<usize> {
self.data self.data
.iter() .iter()
.enumerate() .enumerate()
.filter(|(_, &x)| x >= threshold) .filter(|(_, x)| x >= &threshold)
.map(|(i, _)| i) .map(|(i, _)| i)
.collect() .collect()
} }
} }
impl Deref for Scores { impl<T: PartialOrd + Clone> Scores<T> {
type Target = Vec<f32>; /// Find the highest score.
pub fn max(&self) -> Option<T> {
self.data
.iter()
.max_by(|x, y| x.partial_cmp(y).unwrap())
.cloned()
}
}
impl<T> Deref for Scores<T> {
type Target = Vec<T>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.data &self.data
} }
} }
impl From<Vec<f32>> for Scores { impl<T> From<Vec<T>> for Scores<T> {
fn from(data: Vec<f32>) -> Self { fn from(data: Vec<T>) -> Self {
Self::new(data) Self::new(data)
} }
} }
impl From<Scores> for Vec<f32> { impl<T> From<Scores<T>> for Vec<T> {
fn from(scores: Scores) -> Self { fn from(scores: Scores<T>) -> Self {
scores.data scores.data
} }
} }
...@@ -139,7 +143,7 @@ impl<C: StrictlyPositive> StripedScores<C> { ...@@ -139,7 +143,7 @@ impl<C: StrictlyPositive> StripedScores<C> {
/// Convert the striped scores into an array. /// Convert the striped scores into an array.
#[inline] #[inline]
pub fn unstripe(&self) -> Scores { pub fn unstripe(&self) -> Scores<f32> {
self.iter().cloned().collect::<Vec<f32>>().into() self.iter().cloned().collect::<Vec<f32>>().into()
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment