Initial commit
This commit is contained in:
commit
7d47a10acf
18 changed files with 1545 additions and 0 deletions
48
src/graph/wheighted_graph/mod.rs
Normal file
48
src/graph/wheighted_graph/mod.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
pub mod adjacency_list;
|
||||
pub mod shortest_path;
|
||||
|
||||
use clap::builder::NonEmptyStringValueParser;
|
||||
|
||||
pub trait WheightedGraph: Sized {
|
||||
type Node;
|
||||
fn num_edges(&self, node: &Self::Node) -> usize;
|
||||
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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue