use crate::homotopy::BoundedComplex;
use super::cover::ComplexProjectiveCover;
use super::replacement::PerfectReplacement;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ReplacementLimits {
pub max_resolution_steps: usize,
pub max_complex_terms: usize,
pub max_total_dimension: usize,
pub max_matrix_entries: usize,
pub max_work_units: usize,
}
impl Default for ReplacementLimits {
fn default() -> Self {
ReplacementLimits {
max_resolution_steps: 16,
max_complex_terms: 4_096,
max_total_dimension: 1_000_000,
max_matrix_entries: 16_000_000,
max_work_units: 20_000_000,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ReplacementResource {
ResolutionSteps,
ComplexTerms,
TotalDimension,
MatrixEntries,
WorkUnits,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ReplacementReservation {
pub(super) resource: ReplacementResource,
pub(super) requested: usize,
pub(super) limit: usize,
}
impl ReplacementReservation {
accessor_methods! {
pub resource() -> ReplacementResource = |this| this.resource;
pub requested() -> usize = |this| this.requested;
pub limit() -> usize = |this| this.limit;
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ReplacementWork {
pub(super) resolution_steps: usize,
pub(super) complex_terms: usize,
pub(super) total_dimension: usize,
pub(super) matrix_entries: usize,
pub(super) work_units: usize,
}
impl ReplacementWork {
accessor_methods! {
pub resolution_steps() -> usize = |this| this.resolution_steps;
pub complex_terms() -> usize = |this| this.complex_terms;
pub total_dimension() -> usize = |this| this.total_dimension;
pub matrix_entries() -> usize = |this| this.matrix_entries;
pub work_units() -> usize = |this| this.work_units;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ReplacementCutStage {
Resolution,
Totalization,
}
#[derive(Clone, Debug)]
pub struct ReplacementCut {
pub(super) prefix: Vec<ComplexProjectiveCover>,
pub(super) next_kernel: Option<BoundedComplex>,
pub(super) stage: ReplacementCutStage,
pub(super) limits: ReplacementLimits,
pub(super) work: ReplacementWork,
pub(super) rejected: ReplacementReservation,
}
fn cut_stage_matches(cut: &ReplacementCut) -> bool {
cut.rejected.requested > cut.rejected.limit
&& (cut.next_kernel.is_some() == matches!(cut.stage, ReplacementCutStage::Resolution))
}
impl ReplacementCut {
accessor_methods! {
pub prefix() -> &[ComplexProjectiveCover] = |this| &this.prefix;
pub next_kernel() -> Option<&BoundedComplex> = |this| this.next_kernel.as_ref();
pub stage() -> ReplacementCutStage = |this| this.stage;
pub limits() -> ReplacementLimits = |this| this.limits;
pub work() -> ReplacementWork = |this| this.work;
pub rejected() -> ReplacementReservation = |this| this.rejected;
}
pub fn verify(&self) -> bool {
self.prefix.iter().all(ComplexProjectiveCover::verify)
&& self.next_kernel.as_ref().is_none_or(|kernel| {
!kernel.is_zero()
&& kernel.verify()
&& self
.prefix
.last()
.is_none_or(|cover| cover.kernel().agrees_with(kernel))
})
&& cut_stage_matches(self)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ReplacementCompletedStage {
Input,
Cover { resolution_step: usize },
Resolution,
}
#[derive(Clone, Debug)]
pub struct ReplacementCancellation {
pub(super) prefix: Vec<ComplexProjectiveCover>,
pub(super) next_kernel: Option<BoundedComplex>,
pub(super) last_completed: ReplacementCompletedStage,
pub(super) limits: ReplacementLimits,
pub(super) work: ReplacementWork,
}
fn verified_nonzero_kernel(
prefix: &[ComplexProjectiveCover],
next_kernel: Option<&BoundedComplex>,
) -> bool {
next_kernel.is_some_and(|kernel| {
kernel.verify()
&& !kernel.is_zero()
&& prefix
.last()
.is_none_or(|cover| cover.kernel().agrees_with(kernel))
})
}
fn cancellation_check(condition: bool, check: impl FnOnce() -> bool) -> bool {
if condition { check() } else { false }
}
fn cancellation_boundary_matches(cancellation: &ReplacementCancellation) -> bool {
let prefix = &cancellation.prefix;
let next_kernel = cancellation.next_kernel.as_ref();
let check = || verified_nonzero_kernel(prefix, next_kernel);
match cancellation.last_completed {
ReplacementCompletedStage::Input => cancellation_check(prefix.is_empty(), check),
ReplacementCompletedStage::Cover { resolution_step } => {
cancellation_check(resolution_step.checked_add(1) == Some(prefix.len()), check)
}
ReplacementCompletedStage::Resolution => cancellation_check(next_kernel.is_none(), || {
prefix.last().is_some_and(|cover| cover.kernel().is_zero())
}),
}
}
impl ReplacementCancellation {
accessor_methods! {
pub prefix() -> &[ComplexProjectiveCover] = |this| &this.prefix;
pub next_kernel() -> Option<&BoundedComplex> = |this| this.next_kernel.as_ref();
pub last_completed() -> ReplacementCompletedStage = |this| this.last_completed;
pub limits() -> ReplacementLimits = |this| this.limits;
pub work() -> ReplacementWork = |this| this.work;
}
pub fn verify(&self) -> bool {
let prefix = &self.prefix;
prefix.iter().all(ComplexProjectiveCover::verify) && cancellation_boundary_matches(self)
}
}
#[derive(Clone, Debug)]
pub enum ReplacementOutcome {
Replaced(PerfectReplacement),
Cut(ReplacementCut),
Cancelled(ReplacementCancellation),
}
impl ReplacementOutcome {
optional_accessors! {
pub replacement() -> &PerfectReplacement = Self::Replaced(value) => value;
pub cut() -> &ReplacementCut = Self::Cut(value) => value;
pub cancellation() -> &ReplacementCancellation = Self::Cancelled(value) => value;
}
pub fn is_replaced(&self) -> bool {
matches!(self, ReplacementOutcome::Replaced(_))
}
}