haar_lib/mul_graph/
mod.rs1pub mod dijkstra;
10
11use std::collections::HashMap;
12use std::hash::Hash;
13
14#[derive(Clone, Debug)]
16pub struct Edge<V, W> {
17 pub from: V,
19 pub to: V,
21 pub weight: W,
23}
24
25impl<V, W> Edge<V, W> {
26 pub fn new(from: V, to: V, weight: W) -> Self {
28 Self { from, to, weight }
29 }
30}
31
32#[derive(Clone, Default, Debug)]
34pub struct MulGraph<V, W> {
35 nodes: HashMap<V, Vec<Edge<V, W>>>,
36}
37
38impl<V, W> MulGraph<V, W>
39where
40 V: Hash + Eq + Copy,
41 W: Copy,
42{
43 pub fn new() -> Self {
45 Self {
46 nodes: HashMap::new(),
47 }
48 }
49
50 pub fn add_undirected(&mut self, u: V, v: V, weight: W) {
52 self.add_directed(u, v, weight);
53 self.add_directed(v, u, weight);
54 }
55
56 pub fn add_directed(&mut self, from: V, to: V, weight: W) {
58 self.nodes
59 .entry(from)
60 .or_default()
61 .push(Edge::new(from, to, weight));
62 }
63
64 pub fn neighbours_of(&self, cur: V) -> impl Iterator<Item = &Edge<V, W>> {
66 self.nodes.get(&cur).into_iter().flatten()
67 }
68}