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