open_hypergraphs/lax/
open_hypergraph.rs1use super::hypergraph::*;
3use crate::array::vec::VecKind;
4
5#[derive(Debug, Clone)]
8pub struct OpenHypergraph<O, A> {
9 pub sources: Vec<NodeId>,
10 pub targets: Vec<NodeId>,
11 pub hypergraph: Hypergraph<O, A>,
12}
13
14impl<O, A> OpenHypergraph<O, A> {
16 pub fn empty() -> Self {
20 OpenHypergraph {
21 sources: vec![],
22 targets: vec![],
23 hypergraph: Hypergraph::empty(),
24 }
25 }
26
27 pub fn from_strict(f: crate::open_hypergraph::OpenHypergraph<VecKind, O, A>) -> Self {
28 let sources = f.s.table.0.into_iter().map(NodeId).collect();
29 let targets = f.t.table.0.into_iter().map(NodeId).collect();
30 let hypergraph = Hypergraph::from_strict(f.h);
31 OpenHypergraph {
32 sources,
33 targets,
34 hypergraph,
35 }
36 }
37
38 pub fn new_node(&mut self, w: O) -> NodeId {
40 self.hypergraph.new_node(w)
41 }
42
43 pub fn new_edge(&mut self, x: A, interface: Hyperedge) -> EdgeId {
44 self.hypergraph.new_edge(x, interface)
45 }
46
47 pub fn new_operation(
58 &mut self,
59 x: A,
60 source_type: Vec<O>,
61 target_type: Vec<O>,
62 ) -> (EdgeId, Interface) {
63 self.hypergraph.new_operation(x, source_type, target_type)
64 }
65
66 pub fn unify(&mut self, v: NodeId, w: NodeId) {
68 self.hypergraph.unify(v, w);
69 }
70
71 pub fn add_edge_source(&mut self, edge_id: EdgeId, w: O) -> NodeId {
72 self.hypergraph.add_edge_source(edge_id, w)
73 }
74
75 pub fn add_edge_target(&mut self, edge_id: EdgeId, w: O) -> NodeId {
76 self.hypergraph.add_edge_target(edge_id, w)
77 }
78}
79
80impl<O: Clone + PartialEq, A: Clone + PartialEq> OpenHypergraph<O, A> {
81 pub fn quotient(&mut self) {
84 let q = self.hypergraph.quotient();
86
87 self.sources
90 .iter_mut()
91 .for_each(|x| *x = NodeId(q.table[x.0]));
92 self.targets
93 .iter_mut()
94 .for_each(|x| *x = NodeId(q.table[x.0]));
95 }
96
97 pub fn to_open_hypergraph(mut self) -> crate::prelude::OpenHypergraph<O, A> {
100 use crate::array::vec::VecArray;
101 use crate::finite_function::FiniteFunction;
102 use crate::open_hypergraph::OpenHypergraph;
103
104 self.quotient();
105
106 let target = self.hypergraph.nodes.len();
107
108 let s = {
109 let table = self.sources.iter().map(|x| x.0).collect();
110 FiniteFunction::new(VecArray(table), target).expect("Valid by construction")
111 };
112
113 let t = {
114 let table = self.targets.iter().map(|x| x.0).collect();
115 FiniteFunction::new(VecArray(table), target).expect("Valid by construction")
116 };
117
118 let h = self.hypergraph.to_hypergraph();
119
120 OpenHypergraph::new(s, t, h).expect("any valid lax::Hypergraph must be quotientable!")
121 }
122}