diff --git a/src/lib.rs b/src/lib.rs index c2fc7f7..e078ac4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -624,6 +624,21 @@ impl Grid { pub fn into_vec(self) -> Vec { self.data } + + /// Transpose the grid so that columns become rows in new grid. + pub fn transpose(&self) -> Grid { + let mut data = Vec::with_capacity(self.data.len()); + for c in 0..self.cols { + for r in 0..self.rows { + data.push(self[r][c].clone()); + } + } + Grid { + data, + cols: self.rows, + rows: self.cols, + } + } } impl Clone for Grid {