|
| 1 | +//! Memory layout of matrices |
| 2 | +
|
| 3 | +pub type LDA = i32; |
| 4 | +pub type LEN = i32; |
| 5 | +pub type Col = i32; |
| 6 | +pub type Row = i32; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, Copy, PartialEq)] |
| 9 | +pub enum MatrixLayout { |
| 10 | + C((Row, LDA)), |
| 11 | + F((Col, LDA)), |
| 12 | +} |
| 13 | + |
| 14 | +impl MatrixLayout { |
| 15 | + pub fn size(&self) -> (Row, Col) { |
| 16 | + match *self { |
| 17 | + MatrixLayout::C((row, lda)) => (row, lda), |
| 18 | + MatrixLayout::F((col, lda)) => (lda, col), |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + pub fn resized(&self, row: Row, col: Col) -> MatrixLayout { |
| 23 | + match *self { |
| 24 | + MatrixLayout::C(_) => MatrixLayout::C((row, col)), |
| 25 | + MatrixLayout::F(_) => MatrixLayout::F((col, row)), |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn lda(&self) -> LDA { |
| 30 | + std::cmp::max( |
| 31 | + 1, |
| 32 | + match *self { |
| 33 | + MatrixLayout::C((_, lda)) | MatrixLayout::F((_, lda)) => lda, |
| 34 | + }, |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + pub fn len(&self) -> LEN { |
| 39 | + match *self { |
| 40 | + MatrixLayout::C((row, _)) => row, |
| 41 | + MatrixLayout::F((col, _)) => col, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + pub fn is_empty(&self) -> bool { |
| 46 | + self.len() == 0 |
| 47 | + } |
| 48 | + |
| 49 | + pub fn lapacke_layout(&self) -> lapacke::Layout { |
| 50 | + match *self { |
| 51 | + MatrixLayout::C(_) => lapacke::Layout::RowMajor, |
| 52 | + MatrixLayout::F(_) => lapacke::Layout::ColumnMajor, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn same_order(&self, other: &MatrixLayout) -> bool { |
| 57 | + self.lapacke_layout() == other.lapacke_layout() |
| 58 | + } |
| 59 | + |
| 60 | + pub fn toggle_order(&self) -> Self { |
| 61 | + match *self { |
| 62 | + MatrixLayout::C((row, col)) => MatrixLayout::F((col, row)), |
| 63 | + MatrixLayout::F((col, row)) => MatrixLayout::C((row, col)), |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments