@@ -57,8 +57,8 @@ pub mod algorithms;
5757pub mod instruction;
5858
5959/// A node inside the graph
60- #[ derive( Debug , Clone , Eq ) ]
61- struct Node < T : Display > {
60+ #[ derive( Clone , Eq , Debug ) ]
61+ struct Node < T : Display + Eq > {
6262 /// Identifier of this node
6363 id : T ,
6464 /// Edges of this node
@@ -70,8 +70,8 @@ struct Node<T: Display> {
7070}
7171
7272// An edge between two nodes inside a graph
73- #[ derive( Debug , Clone , Eq ) ]
74- struct Edge < T : Display > {
73+ #[ derive( Clone , Eq , Ord , PartialOrd , Debug ) ]
74+ struct Edge < T : Display + Eq > {
7575 /// The "cost" of moving along this edge
7676 weight : i32 ,
7777 /// The parent of this edge
@@ -81,16 +81,30 @@ struct Edge<T: Display> {
8181}
8282
8383/// Graph data structure to organize nodes that are connected to each other with edges.
84- #[ derive( Debug ) ]
85- pub struct Graph < T : Display > {
84+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
85+ pub struct Graph < T : Display + Eq + Hash > {
8686 /// All nodes contained in this graph
8787 //nodes: Vec<Rc<RefCell<Node<T>>>>,
8888
8989 /// Stores all nodes contained in this graph mapped to the node's id.
9090 nodes : HashMap < T , Rc < RefCell < Node < T > > > > ,
9191}
9292
93- impl < T : Display > Graph < T > {
93+ impl < T : Display + Eq + Hash + Clone > PartialOrd for Graph < T > {
94+ /// Compares the size of this graph with the size of another graph.
95+ fn partial_cmp ( & self , other : & Self ) -> Option < std:: cmp:: Ordering > {
96+ self . size ( ) . partial_cmp ( & other. size ( ) )
97+ }
98+ }
99+
100+ impl < T : Display + Eq + Hash + Clone > Ord for Graph < T > {
101+ /// Compares the size of this graph with the size of another graph.
102+ fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
103+ self . size ( ) . cmp ( & other. size ( ) )
104+ }
105+ }
106+
107+ impl < T : Display + Eq + Hash > Graph < T > {
94108
95109 /// Resets the distance of each node in the graph back to `i32::MAX` and resets the shortest path string.
96110 ///
@@ -335,7 +349,7 @@ impl<T: Display + Clone + Debug> Display for ShortestPath<T> {
335349}
336350
337351#[ doc( hidden) ]
338- impl < T : Display + Clone > TryFrom < & Rc < RefCell < Node < T > > > > for ShortestPath < T > {
352+ impl < T : Display + Clone + Eq > TryFrom < & Rc < RefCell < Node < T > > > > for ShortestPath < T > {
339353 type Error = & ' static str ;
340354
341355 /// Tries to read the shortest path from the node.
0 commit comments