1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
use std::collections::HashMap; use graph_description::{Edge, EdgeList, GeneratedSubgraphs, Node}; use graph_description::Graph; use node::NodeT; impl Graph { pub fn new(timestamp: u64) -> Self { Graph { nodes: HashMap::new(), edges: HashMap::new(), timestamp } } pub fn is_empty(&self) -> bool { self.nodes.is_empty() && self.edges.is_empty() } pub fn merge(&mut self, other: &Graph) { self.edges.extend(other.edges.clone()); for (node_key, other_node) in other.nodes.iter() { self.nodes .entry(node_key.clone()) .and_modify(|node| { node.merge(other_node); }) .or_insert_with(|| other_node.clone()); } } pub fn add_node<N>(&mut self, node: N) where N: Into<Node> { let node = node.into(); let key = node.clone_node_key(); self.nodes.insert(key.to_string(), node); self.edges .entry(key) .or_insert_with(|| { EdgeList { edges: vec![] } }); } pub fn with_node<N>(mut self, node: N) -> Graph where N: Into<Node> { self.add_node(node); self } pub fn add_edge(&mut self, edge_name: impl Into<String>, from: impl Into<String>, to: impl Into<String>) { let from = from.into(); let to = to.into(); let edge_name = edge_name.into(); let edge = Edge { from: from.clone(), to, edge_name }; self.edges .entry(from) .or_insert_with(|| { EdgeList { edges: Vec::with_capacity(1) } }) .edges.push(edge); } } impl GeneratedSubgraphs { pub fn new(subgraphs: Vec<Graph>) -> GeneratedSubgraphs { GeneratedSubgraphs { subgraphs } } }