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

Make `StripedSequence::new` return an error when given an invalid length

parent e26e11ef
No related branches found
No related tags found
No related merge requests found
......@@ -142,7 +142,7 @@ pub trait Stripe<A: Alphabet, C: StrictlyPositive> {
let s = seq.as_ref();
let length = s.len();
let rows = (length / C::USIZE) + ((length % C::USIZE > 0) as usize);
let mut striped = StripedSequence::new(DenseMatrix::new(rows), length);
let mut striped = StripedSequence::new(DenseMatrix::new(rows), length).unwrap();
self.stripe_into(s, &mut striped);
striped
}
......@@ -159,7 +159,7 @@ pub trait Stripe<A: Alphabet, C: StrictlyPositive> {
for (i, &x) in s.iter().enumerate() {
data[i % rows][i / rows] = x;
}
for i in s.len()..matrix.rows() * matrix.columns() {
for i in s.len()..data.rows() * data.columns() {
data[i % rows][i / rows] = A::default_symbol();
}
}
......
......@@ -11,6 +11,7 @@ use typenum::marker_traits::Unsigned;
use super::abc::Alphabet;
use super::abc::Symbol;
use super::dense::DenseMatrix;
use super::err::InvalidData;
use super::err::InvalidSymbol;
use super::num::StrictlyPositive;
use super::pwm::ScoringMatrix;
......@@ -145,12 +146,16 @@ pub struct StripedSequence<A: Alphabet, C: StrictlyPositive> {
impl<A: Alphabet, C: StrictlyPositive> StripedSequence<A, C> {
/// Create a new striped sequence from the given dense matrix.
pub fn new(data: DenseMatrix<A::Symbol, C>, length: usize) -> Self {
Self {
data,
length,
wrap: 0,
alphabet: std::marker::PhantomData,
pub fn new(data: DenseMatrix<A::Symbol, C>, length: usize) -> Result<Self, InvalidData> {
if data.rows() * data.columns() < length {
Err(InvalidData)
} else {
Ok(Self {
data,
length,
wrap: 0,
alphabet: std::marker::PhantomData,
})
}
}
......
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