use std::sync::Arc;
use rustc_hash::FxHashSet;
use crate::algebra::Algebra;
use crate::quiver::ArrowId;
use super::aag::AagFunction;
use super::canonical::{GentleKey, GentleLabeling};
use super::errors::GentleError;
use super::invariant::GentleDerivedInvariant;
use super::shape::{Shape, Sign};
use super::surface::RibbonGraph;
use super::threads::{GentleThread, Threads};
use super::validate::{check_gentle_quiver, reduced_quadratic_relations};
#[derive(Clone, Debug)]
pub struct GentlePresentation {
algebra: Arc<Algebra>,
relations: Vec<(ArrowId, ArrowId)>,
shape: Shape,
threads: Threads,
}
impl GentlePresentation {
pub fn new(algebra: &Arc<Algebra>) -> Result<GentlePresentation, GentleError> {
let forbidden = reduced_quadratic_relations(algebra)?;
GentlePresentation::from_relations(algebra, &forbidden)
}
pub(crate) fn from_relations(
algebra: &Arc<Algebra>,
forbidden: &FxHashSet<(ArrowId, ArrowId)>,
) -> Result<GentlePresentation, GentleError> {
check_gentle_quiver(algebra.quiver(), forbidden)?;
let shape = Shape::solve(algebra.quiver(), forbidden);
let relations = shape.relations();
assert!(
relations.len() == forbidden.len()
&& relations.iter().all(|pair| forbidden.contains(pair)),
"the signs do not reproduce the relations; library bug"
);
let threads = Threads::new(&shape);
Ok(GentlePresentation {
algebra: algebra.clone(),
relations,
shape,
threads,
})
}
accessor_methods! {
pub algebra() -> &Arc<Algebra> = |this| &this.algebra;
pub relations() -> &[(ArrowId, ArrowId)] = |this| &this.relations;
pub sigma(arrow: ArrowId) -> Sign = |this| this.shape.sigma(arrow);
pub epsilon(arrow: ArrowId) -> Sign = |this| this.shape.epsilon(arrow);
pub permitted_threads() -> &[GentleThread] = |this| &this.threads.permitted;
pub forbidden_threads() -> &[GentleThread] = |this| &this.threads.forbidden;
pub full_relation_cycles() -> &[Vec<ArrowId>] = |this| &this.threads.cycles;
}
pub fn aag_function(&self) -> AagFunction {
AagFunction::new(&self.threads, self.shape.vertices())
}
pub fn genus(&self) -> usize {
let doubled = self.shape.vertices() as i64 + 2
- self.threads.permitted.len() as i64
- self.aag_function().len() as i64;
assert!(
doubled >= 0 && doubled % 2 == 0,
"the doubled genus {doubled} is negative or odd; library bug"
);
(doubled / 2) as usize
}
pub fn complete_invariant(&self) -> GentleDerivedInvariant {
GentleDerivedInvariant::new(self.aag_function(), self.genus(), &self.ribbon_graph())
}
pub(super) fn ribbon_graph(&self) -> RibbonGraph {
RibbonGraph::new(&self.shape, &self.threads.permitted)
}
pub fn canonical_labeling(&self) -> GentleLabeling {
GentleLabeling::new(&self.shape)
}
pub fn key(&self) -> GentleKey {
self.canonical_labeling().into_key()
}
}