use super::hypergraph::*;
use crate::array::vec::VecKind;
#[derive(Debug, Clone)]
pub struct OpenHypergraph<O, A> {
pub sources: Vec<NodeId>,
pub targets: Vec<NodeId>,
pub hypergraph: Hypergraph<O, A>,
}
impl<O, A> OpenHypergraph<O, A> {
pub fn empty() -> Self {
OpenHypergraph {
sources: vec![],
targets: vec![],
hypergraph: Hypergraph::empty(),
}
}
pub fn from_strict(f: crate::open_hypergraph::OpenHypergraph<VecKind, O, A>) -> Self {
let sources = f.s.table.0.into_iter().map(NodeId).collect();
let targets = f.t.table.0.into_iter().map(NodeId).collect();
let hypergraph = Hypergraph::from_strict(f.h);
OpenHypergraph {
sources,
targets,
hypergraph,
}
}
pub fn new_node(&mut self, w: O) -> NodeId {
self.hypergraph.new_node(w)
}
pub fn new_edge(&mut self, x: A, interface: Hyperedge) -> EdgeId {
self.hypergraph.new_edge(x, interface)
}
pub fn new_operation(
&mut self,
x: A,
source_type: Vec<O>,
target_type: Vec<O>,
) -> (EdgeId, Interface) {
self.hypergraph.new_operation(x, source_type, target_type)
}
pub fn singleton(x: A, source_type: Vec<O>, target_type: Vec<O>) -> Self {
let mut f = Self::empty();
let (_, (s, t)) = f.new_operation(x, source_type, target_type);
f.sources = s;
f.targets = t;
f
}
pub fn unify(&mut self, v: NodeId, w: NodeId) {
self.hypergraph.unify(v, w);
}
pub fn add_edge_source(&mut self, edge_id: EdgeId, w: O) -> NodeId {
self.hypergraph.add_edge_source(edge_id, w)
}
pub fn add_edge_target(&mut self, edge_id: EdgeId, w: O) -> NodeId {
self.hypergraph.add_edge_target(edge_id, w)
}
}
impl<O: Clone + PartialEq, A: Clone + PartialEq> OpenHypergraph<O, A> {
pub fn quotient(&mut self) {
let q = self.hypergraph.quotient();
self.sources
.iter_mut()
.for_each(|x| *x = NodeId(q.table[x.0]));
self.targets
.iter_mut()
.for_each(|x| *x = NodeId(q.table[x.0]));
}
pub fn to_open_hypergraph(mut self) -> crate::prelude::OpenHypergraph<O, A> {
use crate::array::vec::VecArray;
use crate::finite_function::FiniteFunction;
use crate::open_hypergraph::OpenHypergraph;
self.quotient();
let target = self.hypergraph.nodes.len();
let s = {
let table = self.sources.iter().map(|x| x.0).collect();
FiniteFunction::new(VecArray(table), target).expect("Valid by construction")
};
let t = {
let table = self.targets.iter().map(|x| x.0).collect();
FiniteFunction::new(VecArray(table), target).expect("Valid by construction")
};
let h = self.hypergraph.to_hypergraph();
OpenHypergraph::new(s, t, h).expect("any valid lax::Hypergraph must be quotientable!")
}
}