snark_tool/graph/undirected/
vertex.rs

1use crate::graph::edge::{Edge, EdgeConstructor};
2use crate::graph::undirected::edge::UndirectedEdge;
3use crate::graph::vertex::{Vertex, VertexConstructor};
4
5#[derive(Debug, Hash, Eq, PartialEq, Clone)]
6pub struct VertexWithEdges {
7    index: usize,
8    active: bool,
9    pub edges: Vec<UndirectedEdge>,
10}
11
12impl Vertex for VertexWithEdges {
13    fn index(&self) -> usize {
14        self.index
15    }
16}
17
18impl VertexConstructor for VertexWithEdges {
19    fn new(index: usize) -> Self {
20        VertexWithEdges {
21            index,
22            active: true,
23            edges: vec![],
24        }
25    }
26}
27
28impl VertexWithEdges {
29    pub fn new_non_active(index: usize) -> Self {
30        VertexWithEdges {
31            index,
32            active: false,
33            edges: vec![],
34        }
35    }
36
37    pub fn add_edge(&mut self, to: usize, colour: u8) {
38        self.edges
39            .push(UndirectedEdge::new_with_colour(self.index, to, colour));
40        self.edges.sort();
41    }
42
43    pub fn neighbors(&self) -> Vec<usize> {
44        let mut neighbors = vec![];
45        for edge in self.edges.iter() {
46            if edge.from() == self.index {
47                neighbors.push(edge.to())
48            } else {
49                neighbors.push(edge.from());
50            }
51        }
52        neighbors
53    }
54
55    pub fn active(&self) -> bool {
56        self.active
57    }
58
59    pub fn set_active(&mut self, active: bool) {
60        self.active = active;
61    }
62}