use std::cmp;
use crate::sketch_schemes::KmerCount;
pub fn cardinality(sketch: &[KmerCount]) -> Result<u64, &'static str> {
if sketch.is_empty() {
return Ok(0u64);
}
Ok(((sketch.len() - 1) as f32
/ (sketch.last().unwrap().hash as f32 / usize::max_value() as f32)) as u64)
}
pub fn hist(sketch: &[KmerCount]) -> Vec<u64> {
let mut counts = vec![0u64; 65536];
let mut max_count: u64 = 0;
for kmer in sketch {
max_count = cmp::max(max_count, u64::from(kmer.count));
counts[kmer.count as usize - 1] += 1;
}
counts.truncate(max_count as usize);
counts
}
#[test]
fn test_hist() {
let sketch = vec![
KmerCount {
hash: 1,
kmer: vec![],
count: 1,
extra_count: 0,
label: None,
},
KmerCount {
hash: 2,
kmer: vec![],
count: 1,
extra_count: 0,
label: None,
},
KmerCount {
hash: 3,
kmer: vec![],
count: 1,
extra_count: 0,
label: None,
},
];
let hist_data = hist(&sketch);
assert_eq!(hist_data.len(), 1);
assert_eq!(hist_data[0], 3);
let sketch = vec![
KmerCount {
hash: 1,
kmer: vec![],
count: 4,
extra_count: 0,
label: None,
},
KmerCount {
hash: 2,
kmer: vec![],
count: 2,
extra_count: 0,
label: None,
},
KmerCount {
hash: 3,
kmer: vec![],
count: 4,
extra_count: 0,
label: None,
},
KmerCount {
hash: 4,
kmer: vec![],
count: 3,
extra_count: 0,
label: None,
},
];
let hist_data = hist(&sketch);
assert_eq!(hist_data.len(), 4);
assert_eq!(hist_data[0], 0);
assert_eq!(hist_data[1], 1);
assert_eq!(hist_data[2], 1);
assert_eq!(hist_data[3], 2);
}