ldpc_toolbox/encoder/
staircase.rs

1use crate::sparse::SparseMatrix;
2
3pub fn is_staircase(h: &SparseMatrix) -> bool {
4    let n = h.num_rows();
5    let m = h.num_cols();
6    let mut num_checked = 0; // number of ones in parity part
7    // Check that all the ones in the parity part of the matrix are staircase
8    // positions
9    for (j, k) in h.iter_all() {
10        if k >= m - n {
11            if j == 0 && k != m - n {
12                // unexpected one in first row
13                return false;
14            }
15            if j != 0 && k != m - n + j - 1 && k != m - n + j {
16                // unexpected one in row other than first
17                return false;
18            }
19            num_checked += 1;
20        }
21    }
22    // There must be exactly 2n-1 ones in the staircase
23    num_checked == 2 * n - 1
24}
25
26#[cfg(test)]
27mod test {
28    use super::*;
29
30    #[test]
31    fn staircase() {
32        let mut h = SparseMatrix::new(3, 5);
33        assert!(!is_staircase(&h));
34        h.insert(0, 2);
35        assert!(!is_staircase(&h));
36        h.insert(1, 2);
37        assert!(!is_staircase(&h));
38        h.insert(1, 3);
39        assert!(!is_staircase(&h));
40        h.insert(2, 3);
41        assert!(!is_staircase(&h));
42        h.insert(2, 4);
43        assert!(is_staircase(&h)); // now it must be a staircase
44        h.insert(0, 3);
45        assert!(!is_staircase(&h)); // not a staircase anymore
46    }
47}