From 0c1ccc9cff58a9d6da7c35d2226e83fe32972662 Mon Sep 17 00:00:00 2001 From: Armin Becher Date: Fri, 24 Apr 2020 08:52:22 +0200 Subject: [PATCH] Add insert col --- src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ba332bb..09b38b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -565,13 +565,44 @@ impl Grid { /// assert_eq!(grid.size(), (3,3)) /// ``` pub fn insert_row(&mut self, index: usize, row: Vec) { - if row.len() != self.cols() { - panic!("Inserted row must be of length {}, but was {}.", self.cols(), row.len()); + if row.len() != self.cols { + panic!("Inserted row must be of length {}, but was {}.", self.cols, row.len()); + } + if index > self.rows { + panic!("Out of range. Index was {}, but must be less or equal to {}.", index, self.cols); } self.rows += 1; let data_idx = index * self.cols; self.data.splice(data_idx..data_idx, row.iter().cloned()); } + + /// Insert a new column at the index. + /// + /// Important! Insertion of columns is a lot slower than the lines insertion. + /// This is because of the memory layout of the grid data structure. + /// + /// # Examples + /// ``` + /// use grid::*; + /// let mut grid = grid![[1,2,3][4,5,6]]; + /// grid.insert_col(1, vec![9,9]); + /// assert_eq!(grid[0], [1,9,2,3]); + /// assert_eq!(grid[1], [4,9,5,6]); + /// assert_eq!(grid.size(), (2,4)) + /// ``` + pub fn insert_col(&mut self, index: usize, col: Vec) { + if col.len() != self.rows { + panic!("Inserted col must be of length {}, but was {}.", self.rows, col.len()); + } + if index > self.cols { + panic!("Out of range. Index was {}, but must be less or equal to {}.", index, self.rows); + } + for (row_iter, col_val) in col.iter().enumerate() { + let data_idx = row_iter * self.cols + index + row_iter; + self.data.insert(data_idx, col_val.clone()); + } + self.cols += 1; + } } impl Clone for Grid { @@ -631,6 +662,22 @@ impl Eq for Grid {} mod test { use super::*; + #[test] + fn insert_col_at_end() { + let mut grid: Grid = Grid::from_vec(vec![1, 2, 3, 4], 2); + grid.insert_col(2, vec![5, 6]); + assert_eq!(grid[0], [1 ,2, 5]); + assert_eq!(grid[1], [3 ,4, 6]); + } + + #[test] + #[should_panic] + fn insert_col_out_of_idx() { + let mut grid: Grid = Grid::from_vec(vec![1, 2, 3, 4], 2); + grid.insert_col(3, vec![4, 5]); + } + + #[test] fn insert_row_at_end() { let mut grid: Grid = Grid::from_vec(vec![1, 2, 3, 4], 2);