use crate::structure::matrix::matrix_trait::MatrixDataTrait;
use crate::structure::matrix::Matrix;
use core::array::from_fn as array_fn;
#[derive(Debug, Clone)]
pub struct MatrixVis<const ROWS: usize, const COLS: usize = ROWS> {
pub(crate) data: [[String; COLS]; ROWS],
}
impl<const ROWS: usize, const COLS: usize> MatrixVis<ROWS, COLS> {
pub fn from_path(path: Vec<(usize, usize)>, w: char, b: char) -> Self {
let mut data: [[String; COLS]; ROWS] = array_fn(|_| array_fn(|_| String::from(b)));
path.iter().for_each(|n| data[n.0][n.1] = String::from(w));
Self { data }
}
pub fn from_mat<T: MatrixDataTrait>(matrix: &Matrix<T, ROWS, COLS>, w: char, b: char) -> Self {
let mut data: [[String; COLS]; ROWS] = array_fn(|_| array_fn(|_| String::from(b)));
for i in 0..ROWS {
for j in 0..COLS {
data[i][j] = if matrix[(i, j)] > T::zero() {
String::from(w)
} else {
String::from(b)
};
}
}
Self { data }
}
pub fn highlight_path<F>(&self, path: Vec<(usize, usize)>, w: char, fmt: F) -> Self
where
F: Fn(String) -> String,
{
let mut cloned = self.clone();
path.iter()
.for_each(|n| cloned.data[n.0][n.1] = fmt(String::from(w)));
cloned
}
}
impl<const ROWS: usize, const COLS: usize> std::fmt::Display for MatrixVis<ROWS, COLS> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> core::fmt::Result {
writeln!(f).expect("Failed to write to formatter");
for i in 0..ROWS {
for j in 0..COLS {
write!(f, "{}", self.data[i][j]).expect("Failed to write to formatter");
}
writeln!(f).expect("Failed to write to formatter");
}
Ok(())
}
}