use std::{ borrow::Cow, iter::once, ops::{Index, IndexMut}, }; use tabled::Tabled; #[derive(Debug)] pub struct MatrixList { data: Vec, } impl MatrixList { #[allow(dead_code)] pub fn new(data: Vec) -> Self { Self { data } } } impl MatrixList { pub fn with_index(index: usize, mut data: Vec) -> Self { assert_eq!(data.len(), N); data.insert(0, index.to_string()); Self { data } } } impl Index for MatrixList { type Output = String; fn index(&self, index: usize) -> &Self::Output { &self.data[index] } } impl IndexMut for MatrixList { fn index_mut(&mut self, index: usize) -> &mut Self::Output { &mut self.data[index] } } impl Tabled for MatrixList { const LENGTH: usize = N + 1; fn fields(&self) -> Vec> { self.data.iter().cloned().map(Cow::Owned).collect() } fn headers() -> Vec> { let header = (0..N).map(|n| format!("column {n}")); match INDEX { true => once("N".to_owned()).chain(header).map(Cow::Owned).collect(), false => header.map(Cow::Owned).collect(), } } }