1 use {crate::*, anyhow::*}; 2 3 /// this table is garanteed to contain at least 2 sequences. 4 #[derive(Debug)] 5 pub struct Tbl { 6 seqs: Vec<Seq>, 7 } 8 9 impl Tbl { from_raw(mut raw_tbl: RawTbl) -> Result<Self>10 pub fn from_raw(mut raw_tbl: RawTbl) -> Result<Self> { 11 if raw_tbl.row_count() < 2 { 12 bail!("two rows needed for a graph"); 13 } 14 let mut seqs = Vec::new(); 15 for (col_idx, raw_col) in raw_tbl.cols.drain(..).enumerate() { 16 match Seq::new(raw_col) { 17 Ok(seq) => { 18 seqs.push(seq); 19 } 20 Err(e) => { 21 info!("column {} can't be used: {}", col_idx, e); 22 } 23 } 24 } 25 if seqs.len() < 2 { 26 bail!("not enough usable columns") 27 } 28 Ok(Self { seqs }) 29 } from_seqs(seqs: Vec<Seq>) -> Result<Self>30 pub fn from_seqs(seqs: Vec<Seq>) -> Result<Self> { 31 if seqs.len() < 2 { 32 bail!("not enough columns"); 33 } 34 if !seqs[0].is_full_and_increasing() { 35 bail!("first sequence must be full and increasing"); 36 } 37 Ok(Self { seqs }) 38 } seqs_count(&self) -> usize39 pub fn seqs_count(&self) -> usize { 40 self.seqs.len() 41 } y_seqs_count(&self) -> usize42 pub fn y_seqs_count(&self) -> usize { 43 self.seqs.len() - 1 44 } dim(&self) -> (usize, usize)45 pub fn dim(&self) -> (usize, usize) { 46 (self.seqs_count(), self.seqs[0].len()) 47 } x_seq(&self) -> &Seq48 pub fn x_seq(&self) -> &Seq { 49 &self.seqs[0] 50 } y_seqs(&self) -> std::iter::Skip<std::slice::Iter<'_, seq::Seq>>51 pub fn y_seqs(&self) -> std::iter::Skip<std::slice::Iter<'_, seq::Seq>> { 52 self.seqs.iter().skip(1) 53 } y_min_max(&self) -> (i64, i64)54 pub fn y_min_max(&self) -> (i64, i64) { 55 let mut y_seqs = self.y_seqs(); 56 let first_y = y_seqs.next().unwrap(); 57 y_seqs.fold((first_y.min, first_y.max), |(min, max), seq| { 58 (min.min(seq.min), max.max(seq.max)) 59 }) 60 } 61 } 62