-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(23/2024): extract graph struct
- Loading branch information
Showing
3 changed files
with
90 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
use std::hash::Hash; | ||
|
||
enum GraphType { | ||
#[allow(dead_code)] | ||
Directed, | ||
Undirected, | ||
} | ||
|
||
pub struct Graph<T> { | ||
nodes: HashSet<T>, | ||
edges: HashSet<(T, T)>, | ||
neighbours: HashMap<T, Vec<T>>, | ||
graph_type: GraphType, | ||
} | ||
|
||
impl<T> Graph<T> { | ||
pub fn undirected() -> Self { | ||
Self { | ||
nodes: HashSet::new(), | ||
edges: HashSet::new(), | ||
neighbours: HashMap::new(), | ||
graph_type: GraphType::Undirected, | ||
} | ||
} | ||
|
||
pub fn add_edge(&mut self, a: T, b: T) | ||
where | ||
T: Eq + Hash + Copy, | ||
{ | ||
match self.graph_type { | ||
GraphType::Directed => self.add_directed_edge(a, b), | ||
GraphType::Undirected => self.add_undirected_edge(a, b), | ||
} | ||
} | ||
|
||
fn add_directed_edge(&mut self, a: T, b: T) | ||
where | ||
T: Eq + Hash + Copy, | ||
{ | ||
self.nodes.insert(a); | ||
self.nodes.insert(b); | ||
self.edges.insert((a, b)); | ||
self.neighbours.entry(a).or_default().push(b); | ||
} | ||
|
||
fn add_undirected_edge(&mut self, a: T, b: T) | ||
where | ||
T: Eq + Hash + Copy, | ||
{ | ||
self.nodes.insert(a); | ||
self.nodes.insert(b); | ||
self.edges.insert((a, b)); | ||
self.neighbours.entry(a).or_default().push(b); | ||
self.neighbours.entry(b).or_default().push(a); | ||
} | ||
|
||
pub fn edges(&self) -> &HashSet<(T, T)> { | ||
&self.edges | ||
} | ||
|
||
pub fn neighbours(&self, node: &T) -> Vec<T> | ||
where | ||
T: Eq + Hash + Clone, | ||
{ | ||
self.neighbours.get(node).unwrap_or(&Vec::new()).to_vec() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod a_star; | ||
pub mod all_paths; | ||
pub mod dijkstra; | ||
pub mod graph; | ||
pub mod longest_path; | ||
mod state_utils; |