1- use std:: { fmt:: { Display , Debug } , rc:: Rc , cell:: RefCell , hash:: Hash , collections:: HashMap } ;
1+ use std:: { fmt:: Display , rc:: Rc , cell:: RefCell , hash:: Hash , collections:: HashMap } ;
22
3- use crate :: { Node , Edge , Graph , AddEdgeError , ShortestPath } ;
3+ use crate :: { Node , Edge , Graph , AddEdgeError } ;
44
55// Node implementations
66
@@ -71,7 +71,7 @@ impl<T: Display + Eq> PartialEq for Edge<T> {
7171
7272// Graph implementations
7373
74- impl < ' a , T : Display + Clone + Eq + Hash > Graph < T > {
74+ impl < T : Display + Clone + Eq + Hash > Graph < T > {
7575
7676 /// Creates a new and empty graph.
7777 pub fn new ( ) -> Self {
@@ -184,10 +184,10 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
184184 if !self . nodes . contains_key ( source_id) && !self . nodes . contains_key ( target_id) {
185185 return false ;
186186 }
187- let parent = Rc :: clone ( & self . nodes . get ( source_id) . unwrap ( ) ) ;
188- let target = Rc :: clone ( & self . nodes . get ( target_id) . unwrap ( ) ) ;
187+ let parent = Rc :: clone ( self . nodes . get ( source_id) . unwrap ( ) ) ;
188+ let target = Rc :: clone ( self . nodes . get ( target_id) . unwrap ( ) ) ;
189189 self . nodes . get ( source_id) . unwrap ( ) . borrow_mut ( ) . edges . push ( Edge :: new ( weight, parent, target) ) ;
190- return true ;
190+ true
191191 }
192192
193193 /// Tries to add a new edge to the graph that connects two nodes in a single direction from source to target.
@@ -226,8 +226,8 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
226226 } else if !self . nodes . contains_key ( target_id) {
227227 return Err ( AddEdgeError :: TargetMissing ) ;
228228 }
229- let parent = Rc :: clone ( & self . nodes . get ( source_id) . unwrap ( ) ) ;
230- let target = Rc :: clone ( & self . nodes . get ( target_id) . unwrap ( ) ) ;
229+ let parent = Rc :: clone ( self . nodes . get ( source_id) . unwrap ( ) ) ;
230+ let target = Rc :: clone ( self . nodes . get ( target_id) . unwrap ( ) ) ;
231231 self . nodes . get ( source_id) . unwrap ( ) . borrow_mut ( ) . edges . push ( Edge :: new ( weight, parent, target) ) ;
232232 Ok ( ( ) )
233233 }
@@ -257,8 +257,8 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
257257 if !self . nodes . contains_key ( source_id) && !self . nodes . contains_key ( target) {
258258 return false ;
259259 }
260- let parent = Rc :: clone ( & self . nodes . get ( source_id) . unwrap ( ) ) ;
261- let destination = Rc :: clone ( & self . nodes . get ( target) . unwrap ( ) ) ;
260+ let parent = Rc :: clone ( self . nodes . get ( source_id) . unwrap ( ) ) ;
261+ let destination = Rc :: clone ( self . nodes . get ( target) . unwrap ( ) ) ;
262262 self . nodes . get ( source_id) . unwrap ( ) . borrow_mut ( ) . edges . push ( Edge :: new ( weight, parent. clone ( ) , destination. clone ( ) ) ) ;
263263 self . nodes . get ( target) . unwrap ( ) . borrow_mut ( ) . edges . push ( Edge :: new ( weight, destination, parent) ) ;
264264 true
@@ -300,8 +300,8 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
300300 } else if !self . nodes . contains_key ( target_id) {
301301 return Err ( AddEdgeError :: TargetMissing ) ;
302302 }
303- let parent = Rc :: clone ( & self . nodes . get ( source_id) . unwrap ( ) ) ;
304- let destination = Rc :: clone ( & self . nodes . get ( target_id) . unwrap ( ) ) ;
303+ let parent = Rc :: clone ( self . nodes . get ( source_id) . unwrap ( ) ) ;
304+ let destination = Rc :: clone ( self . nodes . get ( target_id) . unwrap ( ) ) ;
305305 self . nodes . get ( source_id) . unwrap ( ) . borrow_mut ( ) . edges . push ( Edge :: new ( weight, parent. clone ( ) , destination. clone ( ) ) ) ;
306306 self . nodes . get ( target_id) . unwrap ( ) . borrow_mut ( ) . edges . push ( Edge :: new ( weight, destination, parent) ) ;
307307 Ok ( ( ) )
@@ -374,13 +374,19 @@ impl<'a, T: Display + Clone + Eq + Hash> Graph<T> {
374374
375375}
376376
377+ impl < T : Display + Clone + Eq + Hash > Default for Graph < T > {
378+ fn default ( ) -> Self {
379+ Self :: new ( )
380+ }
381+ }
382+
377383impl < T : Display > Display for Graph < T > {
378384 /// Formats the graph to show all edges between nodes
379385 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
380386 let mut graph = String :: new ( ) ;
381387 graph. push_str ( & format ! ( "{:13} | {:08} | edges\n " , "id" , "distance" ) ) ;
382388 graph. push_str ( "--------------------------------------------------------------------\n " ) ;
383- for ( _ , node) in & self . nodes {
389+ for node in self . nodes . values ( ) {
384390 let id = & node. borrow ( ) . id ;
385391 let distance = node. borrow ( ) . distance ;
386392 if distance != i32:: MAX {
@@ -389,11 +395,7 @@ impl<T: Display> Display for Graph<T> {
389395 graph. push_str ( & format ! ( "{:13} | {:8} | " , id, "" ) ) ;
390396 }
391397 for edge in & node. borrow ( ) . edges {
392- if edge. weight < 0 {
393- graph. push_str ( & format ! ( "(--({})-> {})" , edge. weight, edge. target. borrow( ) . id) ) ;
394- } else {
395- graph. push_str ( & format ! ( "(--({})-> {})" , edge. weight, edge. target. borrow( ) . id) ) ;
396- }
398+ graph. push_str ( & format ! ( "(--({})-> {})" , edge. weight, edge. target. borrow( ) . id) ) ;
397399 }
398400 graph. push ( '\n' ) ;
399401 }
@@ -429,7 +431,7 @@ impl<T: Display + Clone + Eq + Hash + From<String>> From<&Vec<Vec<i32>>> for Gra
429431 let mut graph: Graph < T > = Graph :: new ( ) ;
430432 for ( i_y, y) in value. iter ( ) . enumerate ( ) {
431433 for ( i_x, _x) in y. iter ( ) . enumerate ( ) {
432- graph. add_node ( String :: from ( format ! ( "[{}|{}]" , i_x, i_y) ) . into ( ) ) ;
434+ graph. add_node ( format ! ( "[{}|{}]" , i_x, i_y) . into ( ) ) ;
433435 }
434436 }
435437 for ( i_y, y) in value. iter ( ) . enumerate ( ) {
0 commit comments