use alloc::vec;
use alloc::vec::Vec;
use super::{ContingentId, Stnu};
use crate::stn::{Bound, CycleEdge};
use crate::{Error, Ticks, TimePoint};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LabelledEdge {
Requirement(CycleEdge),
LowerCase(ContingentId),
UpperCase(ContingentId),
}
impl LabelledEdge {
pub fn resolve(self, stnu: &Stnu) -> Result<(TimePoint, TimePoint, Ticks), Error> {
match self {
Self::Requirement(edge) => {
let c = stnu
.requirements()
.constraint(edge.constraint)
.ok_or(Error::UnknownTimePoint)?;
let missing = Error::UnknownTimePoint;
match edge.bound {
Bound::Upper => Ok((c.from, c.to, c.hi.ok_or(missing)?)),
Bound::Lower => {
let lo = c.lo.ok_or(missing)?;
Ok((c.to, c.from, lo.checked_neg().ok_or(Error::Overflow)?))
}
}
}
Self::LowerCase(id) => {
let link = stnu.links().get(id.0).ok_or(Error::UnknownTimePoint)?;
Ok((link.activation, link.contingent, link.lo))
}
Self::UpperCase(id) => {
let link = stnu.links().get(id.0).ok_or(Error::UnknownTimePoint)?;
let weight = link.hi.checked_neg().ok_or(Error::Overflow)?;
Ok((link.contingent, link.activation, weight))
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CycleStep {
Edge(LabelledEdge),
Derived(usize),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DerivedEdge {
pub from: TimePoint,
pub to: TimePoint,
pub weight: Ticks,
pub steps: Vec<CycleStep>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SemiReducibleCycle {
steps: Vec<CycleStep>,
derived: Vec<DerivedEdge>,
weight: Ticks,
}
impl SemiReducibleCycle {
pub(crate) fn new(steps: Vec<CycleStep>, derived: Vec<DerivedEdge>, weight: Ticks) -> Self {
Self {
steps,
derived,
weight,
}
}
#[must_use]
pub fn steps(&self) -> &[CycleStep] {
&self.steps
}
#[must_use]
pub fn derived(&self) -> &[DerivedEdge] {
&self.derived
}
#[must_use]
pub fn weight(&self) -> Ticks {
self.weight
}
pub fn edges(&self) -> impl Iterator<Item = LabelledEdge> + '_ {
let mut stack = vec![self.steps.iter()];
core::iter::from_fn(move || {
loop {
let top = stack.last_mut()?;
match top.next() {
None => {
stack.pop();
}
Some(CycleStep::Edge(e)) => return Some(*e),
Some(CycleStep::Derived(i)) => stack.push(self.derived[*i].steps.iter()),
}
}
})
}
#[must_use]
pub fn expanded_len(&self) -> Option<usize> {
let mut lens: Vec<usize> = Vec::with_capacity(self.derived.len());
for d in &self.derived {
let len = steps_len(&d.steps, &lens)?;
lens.push(len);
}
steps_len(&self.steps, &lens)
}
#[must_use]
pub fn contingent_links(&self) -> Vec<ContingentId> {
let all = self.derived.iter().flat_map(|d| d.steps.iter());
let mut links: Vec<ContingentId> = self
.steps
.iter()
.chain(all)
.filter_map(|s| match *s {
CycleStep::Edge(LabelledEdge::LowerCase(id) | LabelledEdge::UpperCase(id)) => {
Some(id)
}
_ => None,
})
.collect();
links.sort_unstable();
links.dedup();
links
}
}
fn steps_len(steps: &[CycleStep], lens: &[usize]) -> Option<usize> {
steps.iter().try_fold(0usize, |acc, s| match *s {
CycleStep::Edge(_) => acc.checked_add(1),
CycleStep::Derived(i) => acc.checked_add(*lens.get(i)?),
})
}