modify u8 matrix to u8 option

This commit is contained in:
M Alghazwi 2025-06-26 11:28:36 +02:00
parent 9121487ffe
commit 3f6bbdb97d

View File

@ -11,7 +11,7 @@ pub struct Params{
pub n: usize, pub n: usize,
pub m: usize, pub m: usize,
} }
/// data struct contains shards matrix where each "shard" is row /// data struct od type `T` contains data matrix with dimensions `n`*`m`
/// the matrix contains n rows, k of which are source data and the rest (n-k) are parity /// the matrix contains n rows, k of which are source data and the rest (n-k) are parity
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Data<T>{ pub struct Data<T>{
@ -19,21 +19,21 @@ pub struct Data<T>{
pub matrix: Vec<Vec<T>>, pub matrix: Vec<Vec<T>>,
} }
impl DataMatrix<u8> for Data<u8> { impl DataMatrix<u8> for Data<Option<u8>> {
type Params = Params; type Params = Params;
/// new from random /// new from random
fn new_random(params: Self::Params) -> Self { fn new_random(params: Self::Params) -> Self {
let mut rng = rand::rng(); let mut rng = rand::rng();
// generate random data shards // generate random data shards
let matrix: Vec<Vec<u8>> = (0..params.n) let matrix: Vec<Vec<Option<u8>>> = (0..params.n)
.map(|i| { .map(|i| {
if i < params.k { if i < params.k {
// data shard: random u8 // data: random u8
(0..params.m).map(|_| rng.random::<u8>()).collect() (0..params.m).map(|_| Some(rng.random::<u8>())).collect()
} else { } else {
// parity shard: zero // parity: zero
vec![0u8; params.m] vec![None; params.m]
} }
}) })
.collect(); .collect();
@ -43,35 +43,42 @@ impl DataMatrix<u8> for Data<u8> {
} }
} }
/// Update col `c` in shards. /// Update col `c` in matrix.
/// given `new_col` will replace the column `c` or `shards[0..k][c]` /// given `new_col` will replace the column `c` or `matrix[0..k][c]`
fn update_col(&mut self, c: usize, new_col: &[u8]) { fn update_col(&mut self, c: usize, new_col: &[u8]) {
// sanity checks // sanity checks
assert!( assert!(
new_col.len() == self.params.k, new_col.len() == self.params.k,
"new_row length ({}) must equal k ({})", "new_col length ({}) must equal k ({})",
new_col.len(), new_col.len(),
self.params.k self.params.k
); );
assert!( assert!(
c < self.params.m, c < self.params.m,
"row index {} out of bounds; must be < {}", "col index {} out of bounds; must be < {}",
c, c,
self.params.m self.params.m
); );
// write into each of the k data shards at position c // write into each of the k data rows at position c
for i in 0..self.params.k { for i in 0..self.params.n {
self.matrix[i][c] = new_col[i]; if i < self.params.k {
self.matrix[i][c] = Some(new_col[i]);
}else{
self.matrix[i][c] = None;
}
} }
} }
/// Print all shards /// Print all matrix
fn pretty_print(&self) { fn pretty_print(&self) {
for (i, shard) in self.matrix.iter().enumerate() { for (i, shard) in self.matrix.iter().enumerate() {
print!("Row {:>2}: ", i); print!("Row {:>2}: ", i);
for &b in shard { for opt in shard {
print!("{:>3} ", b); match opt {
Some(byte) => print!("{:>3} ", byte),
None => print!(" - "),
}
} }
println!(); println!();
} }