diff --git a/src/adjacency_list.rs b/src/adjacency_list.rs index 5c62b19..d2f8b0d 100644 --- a/src/adjacency_list.rs +++ b/src/adjacency_list.rs @@ -49,14 +49,16 @@ impl GraphStorage for AdjacencyList { } } - fn neighbors(&self, vertex: &Vertex) -> Vec<(Vertex, Edge)> { + fn neighbors(&self, vertex: &Vertex) -> Option> { if let Some(neighbors) = self.adj_list.get(vertex) { - neighbors - .iter() - .map(|(v, e)| (v.clone(), e.clone())) - .collect() + Some( + neighbors + .iter() + .map(|(v, e)| (v.clone(), e.clone())) + .collect(), + ) } else { - Vec::new() + None } } @@ -68,7 +70,7 @@ impl GraphStorage for AdjacencyList { todo!() } - fn set_vertex(&mut self, old_vertex: &Vertex, new_vertex: &Vertex) -> bool { + fn set_vertex(&mut self, old_vertex: &Vertex, new_vertex: &Vertex) -> Option { todo!() } @@ -78,7 +80,7 @@ impl GraphStorage for AdjacencyList { to: &Vertex, old_edge: &crate::storage::Edge, new_edge: &crate::storage::Edge, - ) -> bool { + ) -> Option { todo!() } } diff --git a/src/adjacency_matrix.rs b/src/adjacency_matrix.rs index 46ec06d..e0b9231 100644 --- a/src/adjacency_matrix.rs +++ b/src/adjacency_matrix.rs @@ -89,21 +89,27 @@ impl GraphStorage for BinaryAdjMatrix { return self.hash.contains_key(from) && self.hash.contains_key(to); } - fn neighbors(&self, vertex: &Vertex) -> Vec<(Vertex, crate::storage::Edge)> { + fn neighbors(&self, vertex: &Vertex) -> Option> { todo!() } - fn set_vertex(&mut self, old_vertex: &Vertex, new_vertex: &Vertex) -> bool { - todo!() + fn set_vertex(&mut self, old_vertex: &Vertex, new_vertex: &Vertex) -> Option { + let index = self.hash.remove(old_vertex); + if index.is_none() { + return Some(false); + } else { + self.hash.insert(new_vertex.clone(), index.unwrap()); + return Some(true); + } } fn set_edge( &mut self, - from: &Vertex, - to: &Vertex, + _from: &Vertex, + _to: &Vertex, _old_edge: &crate::storage::Edge, _new_edge: &crate::storage::Edge, - ) -> bool { - todo!() + ) -> Option { + None } } diff --git a/src/storage.rs b/src/storage.rs index 919af4c..3fb54aa 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -52,7 +52,7 @@ pub trait GraphStorage { fn edge_size(&self) -> u64; fn vertex_size(&self) -> u64; fn add_vertex(&mut self, vertex: &Vertex) -> bool; - fn set_vertex(&mut self, old_vertex: &Vertex, new_vertex: &Vertex) -> bool; + fn set_vertex(&mut self, old_vertex: &Vertex, new_vertex: &Vertex) -> Option; fn remove_vertex(&mut self, vertex: &Vertex) -> bool; fn add_edge(&mut self, from: &Vertex, to: &Vertex, edge: &Edge) -> bool; fn remove_edge(&mut self, from: &Vertex, to: &Vertex, edge: &Edge) -> bool; @@ -63,7 +63,13 @@ pub trait GraphStorage { to: &Vertex, old_edge: &crate::storage::Edge, new_edge: &crate::storage::Edge, - ) -> bool; + ) -> Option; fn has_edge(&self, from: &Vertex, to: &Vertex, edge: &Edge) -> bool; - fn neighbors(&self, vertex: &Vertex) -> Vec<(Vertex, Edge)>; + fn neighbors(&self, vertex: &Vertex) -> Option>; } + +#[cfg(test)] +mod fixed_storage {} + +#[cfg(test)] +mod dynamic_storage {} diff --git a/src/u_graph.rs b/src/u_graph.rs index fc23001..f36868d 100644 --- a/src/u_graph.rs +++ b/src/u_graph.rs @@ -27,15 +27,15 @@ where fn remove_edge(&mut self, from: &Vertex, to: &Vertex, edge: &Edge) -> bool; fn has_vertex(&self, vertex: &Vertex) -> bool; fn has_edge(&self, from: &Vertex, to: &Vertex, edge: &Edge) -> bool; - fn neighbors(&self, vertex: &Vertex) -> Vec<(Vertex, Edge)>; + fn neighbors(&self, vertex: &Vertex) -> Option>; fn set_edge( &mut self, from: &Vertex, to: &Vertex, old_edge: &Edge, new_edge: &Edge - ) -> bool ; - fn set_vertex(&mut self, old_vertex: &Vertex, new_vertex: &Vertex) -> bool; + ) -> Option; + fn set_vertex(&mut self, old_vertex: &Vertex, new_vertex: &Vertex) -> Option; } } } @@ -59,3 +59,14 @@ where UGraph { storage: S::new() } } } +#[cfg(test)] +mod u_graph { + use crate::adjacency_matrix::BinaryAdjMatrix; + + use super::*; + #[test] + fn test() {} +} + +#[test] +fn a() {}