Skip to content

Commit

Permalink
iterators are always created, but now they might be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
luizirber committed Mar 12, 2019
1 parent 85eb1a6 commit b699c4d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "ukhs"
version = "0.2.0"
version = "0.3.0"
authors = ["Luiz Irber <luiz.irber@gmail.com>"]
description = "Universal K-mer Hitting Sets"
repository = "https://github.com/luizirber/ukhs"
Expand Down
2 changes: 1 addition & 1 deletion benches/ukhs.rs
Expand Up @@ -22,7 +22,7 @@ fn ukhs_bench(c: &mut Criterion) {
let ukhs = UKHS::new(7, 20).unwrap();
b.iter(|| {
// iter.for_each(drop);
let iter = ukhs.iter_sequence(i.as_bytes()).unwrap();
let iter = ukhs.iter_sequence(i.as_bytes());
let _res: Vec<(String, String)> = iter.collect();
})
});
Expand Down
29 changes: 10 additions & 19 deletions src/lib.rs
Expand Up @@ -68,32 +68,23 @@ impl<'a> UKHS {
}

/// Creates a new UKHSIterator with internal state properly initialized.
pub fn iter_sequence(&'a self, seq: &'a [u8]) -> Result<UKHSIterator<'a>, Error> {
if self.k > seq.len() {
return Err(UKHSError::KSizeOutOfRange {
ksize: self.k,
sequence: String::from_utf8(seq.to_vec()).unwrap(),
}
.into());
}
pub fn iter_sequence(&'a self, seq: &'a [u8]) -> UKHSIterator<'a> {
let mut max_idx = seq.len() - self.k + 1;

if self.w > seq.len() {
return Err(UKHSError::WSizeOutOfRange {
wsize: self.w,
sequence: String::from_utf8(seq.to_vec()).unwrap(),
}
.into());
if self.k > seq.len() || self.w > seq.len() {
// In these cases the sequence is too small for having any k-mers or
// w-mers, so the iterator will return None right away.
max_idx = 0;
}

let current_idx = 0;
let max_idx = seq.len() - self.k + 1;

Ok(UKHSIterator {
UKHSIterator {
seq,
ukhs: self,
current_idx,
max_idx,
})
}
}

/// Creates a new UKHSHashIterator with internal state properly initialized.
Expand Down Expand Up @@ -165,7 +156,7 @@ impl<'a> UKHS {
/// let seq = b"ACACCGTAGCCTCCAGATGC";
/// let ukhs = UKHS::new(7, 20)?;
///
/// let it = ukhs.iter_sequence(seq)?;
/// let it = ukhs.iter_sequence(seq);
/// let ukhs: Vec<(String, String)> = it.collect();
/// assert_eq!(ukhs,
/// [
Expand Down Expand Up @@ -297,7 +288,7 @@ mod test {
let seq = b"ACACCGTAGCCTCCAGATGC";
let ukhs = UKHS::new(7, 20).unwrap();

let it = ukhs.iter_sequence(seq).unwrap();
let it = ukhs.iter_sequence(seq);
let mut unikmers: Vec<String> = it.map(|(_, x)| x).collect();
unikmers.sort_unstable();

Expand Down

0 comments on commit b699c4d

Please sign in to comment.