snark_tool/graph/undirected/
edge.rs1use crate::graph::edge::{Edge, EdgeConstructor};
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
4pub struct UndirectedEdge {
5 from: usize,
6 to: usize,
7 color: u8,
8}
9
10impl Edge for UndirectedEdge {
11 fn from(&self) -> usize {
12 self.from
13 }
14
15 fn to(&self) -> usize {
16 self.to
17 }
18
19 fn color(&self) -> u8 {
20 unimplemented!()
21 }
22
23 fn set_color(&mut self, color: u8) {
24 self.color = color;
25 }
26}
27
28impl EdgeConstructor for UndirectedEdge {
29 fn new(from: usize, to: usize) -> Self {
30 if from > to {
31 return UndirectedEdge {
32 from: to,
33 to: from,
34 color: 0,
35 };
36 }
37 UndirectedEdge { from, to, color: 0 }
38 }
39
40 fn new_with_colour(from: usize, to: usize, colour: u8) -> Self {
41 let mut edge = UndirectedEdge::new(from, to);
42 edge.set_color(colour);
43 edge
44 }
45}
46
47impl<'a> Edge for &'a UndirectedEdge {
48 fn from(&self) -> usize {
49 (*self).from()
50 }
51
52 fn to(&self) -> usize {
53 (*self).to()
54 }
55
56 fn color(&self) -> u8 {
57 (*self).color()
58 }
59
60 fn set_color(&mut self, _color: u8) {}
61}
62
63impl UndirectedEdge {
64 pub(crate) fn is_adjacent(&self, other: &UndirectedEdge) -> bool {
65 if self.from() == other.from()
66 || self.from() == other.to()
67 || self.to() == other.from()
68 || self.to() == other.to()
69 {
70 return true;
71 }
72 false
73 }
74}