use crate::basic::{BasicDecomposition, ProjectiveSupport};
use crate::context::VerificationContext;
use crate::profile::{Site, hit};
use crate::taurigid::{TauCache, TauRigidModule};
use super::conditions::{
as_support, check_conditions, checked_omissions, positional, recheck_shared,
support_complement, vertex_count,
};
use super::errors::{Checked, PairRejection, SupportTauError};
pub struct AlmostCompletePair {
pub(super) module: BasicDecomposition,
pub(super) rigid: TauRigidModule,
pub(super) omitted: Option<u32>,
}
debug_fields!(AlmostCompletePair |this| {
"module_dim_vectors" => this.module.dim_vectors();
"projective_support" => this.support();
"omitted_vertex" => this.omitted;
});
#[derive(Debug)]
pub enum AlmostCompleteClassification {
Pair(AlmostCompletePair),
Rejected(PairRejection),
}
impl AlmostCompleteClassification {
binary_outcome_accessors!(
Pair,
Rejected,
pair -> AlmostCompletePair = |value| value,
rejection -> PairRejection = |value| value,
is_pair,
into_pair -> AlmostCompletePair = |value| value;
flag = "Whether the candidate is an almost complete pair.";
positive = "The pair, or `None` for a rejection.";
negative = "The rejection, or `None` for a pair.";
into = "Takes the pair out, or `None` for a rejection.";
);
}
impl AlmostCompletePair {
pub fn classify_with_cache(
module: BasicDecomposition,
projective: ProjectiveSupport,
summand_indices: &[usize],
cache: Option<&mut TauCache>,
) -> Result<AlmostCompleteClassification, SupportTauError> {
hit(Site::AlmostCompleteClassify);
let expected = vertex_count(&module).saturating_sub(1);
match check_conditions(&module, &projective, expected, summand_indices, cache)? {
Checked::Accepted(rigid) => {
let omitted =
checked_omissions(&module, &projective, 1, "an almost complete pair")?;
Ok(AlmostCompleteClassification::Pair(AlmostCompletePair {
module,
rigid,
omitted: omitted.first().copied(),
}))
}
Checked::Rejected(rejection) => Ok(AlmostCompleteClassification::Rejected(rejection)),
}
}
pub fn classify(
module: BasicDecomposition,
projective: ProjectiveSupport,
) -> Result<AlmostCompleteClassification, SupportTauError> {
let indices = positional(&module);
Self::classify_with_cache(module, projective, &indices, None)
}
pub fn new(
module: BasicDecomposition,
projective: ProjectiveSupport,
) -> Result<Option<AlmostCompletePair>, SupportTauError> {
Ok(Self::classify(module, projective)?.into_pair())
}
accessor_methods! {
pub module() -> &BasicDecomposition = |this| &this.module;
pub omitted_vertex() -> Option<u32> = |this| this.omitted;
pub rigid() -> &TauRigidModule = |this| &this.rigid;
}
fn support(&self) -> Vec<u32> {
support_complement(&self.module)
.into_iter()
.filter(|vertex| Some(*vertex) != self.omitted)
.collect()
}
accessor_methods! {
pub projective() -> ProjectiveSupport = |this|
as_support(&this.module, &this.support());
pub summand_count() -> usize = |this| this.module.len() + this.support().len();
}
verify_methods!(pub(crate), { hit(Site::AlmostCompleteVerify); },
|self, context| {
let complement = support_complement(&self.module);
verify_guard!(self.omitted.is_none_or(|omitted| complement.contains(&omitted)));
recheck_shared(
&self.module,
self.support().len(),
&self.rigid,
vertex_count(&self.module).saturating_sub(1),
) && self.rigid.verify_with_context(context)
});
}