From 84da9b21b5812c89da04e232ef09d63cfc0717f5 Mon Sep 17 00:00:00 2001 From: specCon18 Date: Tue, 28 May 2024 22:18:32 -0400 Subject: [PATCH] Revert "implemented weighted adjacency matrix data structure" This reverts commit 81fd2e42c68ad2ce7fb82554e4cf5de2a9f8901a. --- src/data_structures/mod.rs | 1 - src/data_structures/weighted_adj_matrix.rs | 84 ---------------------- src/main.rs | 22 ------ 3 files changed, 107 deletions(-) delete mode 100644 src/data_structures/weighted_adj_matrix.rs diff --git a/src/data_structures/mod.rs b/src/data_structures/mod.rs index 9826a9e..06f904a 100644 --- a/src/data_structures/mod.rs +++ b/src/data_structures/mod.rs @@ -2,5 +2,4 @@ pub mod queue; pub mod stack; pub mod min_heap; pub mod weighted_adj_list; -pub mod weighted_adj_matrix; pub mod ring_buffer; diff --git a/src/data_structures/weighted_adj_matrix.rs b/src/data_structures/weighted_adj_matrix.rs deleted file mode 100644 index 5edbeb0..0000000 --- a/src/data_structures/weighted_adj_matrix.rs +++ /dev/null @@ -1,84 +0,0 @@ -pub struct WeightedAdjacencyMatrix { - matrix: Vec>, -} - -impl WeightedAdjacencyMatrix { - pub fn new(matrix: Vec>) -> Self { - WeightedAdjacencyMatrix { matrix } - } - - pub fn get_value(&self, row: usize, col: usize) -> Option { - self.matrix.get(row).and_then(|r| r.get(col).cloned()) - } - - pub fn set_value(&mut self, row: usize, col: usize, value: u64) -> Result<(), &'static str> { - if let Some(row_vec) = self.matrix.get_mut(row) { - if let Some(cell) = row_vec.get_mut(col) { - *cell = value; - return Ok(()); - } - } - Err("Coordinates out of bounds") - } - - pub fn transpose(&self) -> WeightedAdjacencyMatrix { - let transposed = (0..self.matrix[0].len()) - .map(|i| (0..self.matrix.len()).map(|j| self.matrix[j][i]).collect()) - .collect(); - WeightedAdjacencyMatrix::new(transposed) - } - - pub fn get_row(&self, row_index: usize) -> Option<&Vec> { - self.matrix.get(row_index) - } - - pub fn get_column(&self, col_index: usize) -> Option> { - if col_index >= self.matrix[0].len() { - return None; - } - Some(self.matrix.iter().map(|row| row[col_index]).collect()) - } - - pub fn multiply_scalar(&mut self, scalar: u64) { - for row in self.matrix.iter_mut() { - for cell in row.iter_mut() { - *cell *= scalar; - } - } - } - - pub fn add_matrix(&mut self, other: &WeightedAdjacencyMatrix) -> Option<()> { - if self.matrix.len() != other.matrix.len() || self.matrix[0].len() != other.matrix[0].len() { - return None; // Matrices must have the same dimensions to add - } - for i in 0..self.matrix.len() { - for j in 0..self.matrix[i].len() { - self.matrix[i][j] += other.matrix[i][j]; - } - } - Some(()) - } - - pub fn subtract_matrix(&mut self, other: &WeightedAdjacencyMatrix) -> Option<()> { - if self.matrix.len() != other.matrix.len() || self.matrix[0].len() != other.matrix[0].len() { - return None; // Matrices must have the same dimensions to subtract - } - for i in 0..self.matrix.len() { - for j in 0..self.matrix[i].len() { - self.matrix[i][j] -= other.matrix[i][j]; - } - } - Some(()) - } - - pub fn display_matrix(&self) { - // Printing the array with row and column indices - for row in self.matrix.iter() { - for col in row.iter() { - print!("{} ", col); - } - println!(); - } - } -} - diff --git a/src/main.rs b/src/main.rs index 404eb58..d2b17ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -88,27 +88,6 @@ fn heap_sort_demo(){ heap_sort::heap_sort(&mut arr); println!("Array after heap sort: {:?}",arr); } -fn weighted_adj_matrix_demo(){ - println!("-------------------"); - println!("WEIGHTED ADJACENCY MATRIX DEMO:"); - println!("-------------------"); - let mut matrix = data_structures::weighted_adj_matrix::WeightedAdjacencyMatrix::new(vec![ - vec![1, 2, 3], - vec![4, 5, 6], - vec![7, 8, 9], - ]); - // Display the matrix before modification - println!("Matrix before modification:"); - matrix.display_matrix(); - // Demo of set_value function - match matrix.set_value(1, 1, 10) { - Ok(_) => println!("Successfully set value at row 1, column 1"), - Err(err) => println!("Error: {}", err), - } - // Display the matrix after modification - println!("Matrix after modification:"); - matrix.display_matrix(); -} fn main() { println!(""); println!("|~~~~~~~~~~~~~~~|"); @@ -127,5 +106,4 @@ fn main() { merge_sort_demo(); radix_sort_demo(); heap_sort_demo(); - weighted_adj_matrix_demo(); }