Refactor into different crates

This commit is contained in:
hal8174 2025-01-18 17:30:55 +01:00
parent 94473c64e0
commit dfdeae5638
82 changed files with 624 additions and 647 deletions

View file

@ -1,49 +0,0 @@
pub mod adjacency_list;
pub mod shortest_path;
pub trait WheightedGraph: Sized {
type Node;
fn num_edges(&self, node: &Self::Node) -> usize {
self.edge_iter(node).count()
}
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)>;
fn edge_iter<'a, 'b>(&'a self, node: &'b Self::Node) -> WheightedGraphEdgeIter<'a, 'b, Self> {
WheightedGraphEdgeIter::new(self, node)
}
}
pub struct WheightedGraphEdgeIter<'a, 'b, G>
where
G: WheightedGraph,
{
graph: &'a G,
node: &'b G::Node,
count: usize,
}
impl<'a, 'b, G> WheightedGraphEdgeIter<'a, 'b, G>
where
G: WheightedGraph,
{
pub fn new(graph: &'a G, node: &'b G::Node) -> Self {
Self {
graph,
node,
count: 0,
}
}
}
impl<'a, 'b, G> Iterator for WheightedGraphEdgeIter<'a, 'b, G>
where
G: WheightedGraph,
{
type Item = (G::Node, f64);
fn next(&mut self) -> Option<Self::Item> {
self.count += 1;
self.graph.edge(self.node, self.count - 1)
}
}