use alloc::collections::BinaryHeap;
use alloc::vec;
use alloc::vec::Vec;
use core::cmp::Reverse;
use super::Rul;
use crate::stn::steps;
use crate::{Error, Ticks};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Status {
NotYet,
Started,
Done,
}
struct Local {
cc_loop: bool,
dist: Vec<Option<Ticks>>,
unstarted: Vec<usize>,
}
pub(super) struct Rul2021<'g, 'm> {
g: &'g mut Rul<'m>,
status: Vec<Status>,
activates: Vec<Vec<usize>>,
}
pub(super) fn run(g: &mut Rul<'_>) -> Result<bool, Error> {
let k = g.links.len();
let mut activates = vec![Vec::new(); g.n];
for (r, link) in g.links.iter().enumerate() {
activates[link.activation.0].push(r);
}
let mut s = Rul2021 {
g,
status: vec![Status::NotYet; k],
activates,
};
for r in 0..k {
if !s.process(r)? {
return Ok(false);
}
}
Ok(true)
}
impl Rul2021<'_, '_> {
fn process(&mut self, r: usize) -> Result<bool, Error> {
match self.status[r] {
Status::Started => return Ok(false),
Status::Done => return Ok(true),
Status::NotYet => {}
}
self.status[r] = Status::Started;
let n = self.g.n;
let mut loc = Local {
cc_loop: false,
dist: vec![None; n],
unstarted: Vec::new(),
};
let mut queue = self.initial_queue(r)?;
loop {
if !self.phase1(r, &mut queue, &mut loc)? {
return Ok(false);
}
if loc.unstarted.is_empty() {
break;
}
if !self.resume_after_interruptions(&mut queue, &mut loc)? {
return Ok(false);
}
}
if loc.cc_loop && self.lower_case_reducible(r, &loc.dist)? {
return Ok(false);
}
if !self.phase2(r, &loc.dist)? {
return Ok(false);
}
self.status[r] = Status::Done;
Ok(true)
}
fn initial_queue(&self, r: usize) -> Result<BinaryHeap<Reverse<(Ticks, usize)>>, Error> {
let n = self.g.n;
let mut queue = BinaryHeap::new();
for x in 0..n {
if let Some(w) = self.g.into[r * n + x] {
queue.push(Reverse((self.g.key(x, w)?, x)));
}
}
Ok(queue)
}
fn resume_after_interruptions(
&mut self,
queue: &mut BinaryHeap<Reverse<(Ticks, usize)>>,
loc: &mut Local,
) -> Result<bool, Error> {
let points = core::mem::take(&mut loc.unstarted);
for &x in &points {
for r in self.activates[x].clone() {
if self.status[r] == Status::NotYet && !self.process(r)? {
return Ok(false);
}
}
}
queue.clear();
for &x in &points {
let d = loc.dist[x].ok_or(Error::Internal)?;
queue.push(Reverse((self.g.key(x, d)?, x)));
loc.dist[x] = None;
}
Ok(true)
}
fn phase1(
&mut self,
r: usize,
queue: &mut BinaryHeap<Reverse<(Ticks, usize)>>,
loc: &mut Local,
) -> Result<bool, Error> {
let (n, c, delta) = (self.g.n, self.g.links[r].contingent.0, self.g.delta(r));
while let Some(Reverse((key, x))) = queue.pop() {
let d = key.checked_sub(self.g.h[x]).ok_or(Error::Overflow)?;
if loc.dist[x].is_some_and(|old| old <= d) {
continue;
}
loc.dist[x] = Some(d);
if d >= delta {
continue;
}
if x == c {
if d < Ticks::ZERO {
return Ok(false);
}
loc.cc_loop = true;
continue;
}
let links = &self.activates[x];
if links.iter().any(|&l| self.status[l] == Status::Started) {
return Ok(false);
}
if links.iter().any(|&l| self.status[l] == Status::NotYet) {
if !loc.unstarted.contains(&x) {
loc.unstarted.push(x);
}
continue;
}
for (w, v) in self.relax_lower(x, d)? {
if self.g.into[r * n + w].is_none_or(|old| v < old) {
queue.push(Reverse((self.g.key(w, v)?, w)));
}
}
}
Ok(true)
}
fn relax_lower(&mut self, v: usize, d: Ticks) -> Result<Vec<(usize, Ticks)>, Error> {
if let Some(j) = self.g.link_of[v] {
let link = &self.g.links[j];
let w = link.lo.checked_add(d).ok_or(Error::Overflow)?;
return Ok(vec![(link.activation.0, w)]);
}
self.g.meter.spend(steps(self.g.incoming[v].len())?)?;
self.g.incoming[v]
.iter()
.map(|&(p, w)| Ok((p, w.checked_add(d).ok_or(Error::Overflow)?)))
.collect()
}
fn lower_case_reducible(&mut self, r: usize, dist: &[Option<Ticks>]) -> Result<bool, Error> {
let (n, c, delta) = (self.g.n, self.g.links[r].contingent.0, self.g.delta(r));
let outgoing = self.outgoing()?;
let mut best: Vec<Option<Ticks>> = vec![None; n];
let mut queue = BinaryHeap::new();
let start = Ticks::ZERO
.checked_sub(self.g.h[c])
.ok_or(Error::Overflow)?;
queue.push(Reverse((start, c)));
while let Some(Reverse((key, x))) = queue.pop() {
if best[x].is_some_and(|b| b <= key) {
continue;
}
best[x] = Some(key);
if dist[x].is_none_or(|d| d >= delta) {
continue;
}
let dcx = key.checked_add(self.g.h[x]).ok_or(Error::Overflow)?;
if dcx < Ticks::ZERO {
return Ok(true);
}
self.g.meter.spend(steps(outgoing[x].len())?)?;
for &(y, w) in &outgoing[x] {
let next = dcx.checked_add(w).ok_or(Error::Overflow)?;
let next_key = next.checked_sub(self.g.h[y]).ok_or(Error::Overflow)?;
queue.push(Reverse((next_key, y)));
}
}
Ok(false)
}
fn phase2(&mut self, r: usize, dist: &[Option<Ticks>]) -> Result<bool, Error> {
let link = self.g.links[r];
let delta = self.g.delta(r);
let mut inserted = false;
for (tail, d) in dist.iter().enumerate() {
let Some(d) = *d else { continue };
if tail != link.contingent.0 && d >= delta {
let w = d.checked_sub(link.hi).ok_or(Error::Overflow)?;
self.g.incoming[link.activation.0].push((tail, w));
inserted = true;
}
}
Ok(!inserted || self.g.update_potential(link.activation.0)?)
}
fn outgoing(&mut self) -> Result<Vec<Vec<(usize, Ticks)>>, Error> {
self.g.meter.spend(steps(self.g.edge_count())?)?;
let mut out = vec![Vec::new(); self.g.n];
for (q, edges) in self.g.incoming.iter().enumerate() {
for &(p, w) in edges {
out[p].push((q, w));
}
}
Ok(out)
}
}