temporal_control/stn/minimal.rs
1//! Minimal (all-pairs tightest) networks.
2
3use alloc::vec::Vec;
4
5use super::TimePoint;
6use crate::{Error, Ticks};
7
8/// The minimal network of a consistent STN.
9///
10/// For every ordered pair `(i, j)`, [`Self::upper`] is the tightest upper bound on `t_j - t_i`
11/// implied by the constraints. Every bound is tight: some schedule attains it.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct MinimalNetwork {
14 /// Number of time points.
15 pub(super) n: usize,
16 /// Row-major `n * n` shortest-path distances; `None` means unbounded. No entry is
17 /// `i64::MIN`, so every entry can be negated.
18 pub(super) dist: Vec<Option<Ticks>>,
19}
20
21impl MinimalNetwork {
22 /// Wraps a distance matrix, ensuring every entry can be negated.
23 ///
24 /// # Errors
25 ///
26 /// [`Error::Overflow`] if an entry is `i64::MIN`.
27 pub(crate) fn new(n: usize, dist: Vec<Option<Ticks>>) -> Result<Self, Error> {
28 if dist.iter().flatten().any(|d| d.0 == i64::MIN) {
29 return Err(Error::Overflow);
30 }
31
32 Ok(Self { n, dist })
33 }
34
35 /// Number of time points.
36 #[must_use]
37 pub fn time_point_count(&self) -> usize {
38 self.n
39 }
40
41 /// Tightest upper bound on `t_to - t_from`; `None` if unbounded or a handle is unknown.
42 #[must_use]
43 pub fn upper(&self, from: TimePoint, to: TimePoint) -> Option<Ticks> {
44 if from.0 >= self.n || to.0 >= self.n {
45 return None;
46 }
47
48 self.dist[from.0 * self.n + to.0]
49 }
50
51 /// Tightest lower bound on `t_to - t_from`; `None` if unbounded or a handle is unknown.
52 #[must_use]
53 pub fn lower(&self, from: TimePoint, to: TimePoint) -> Option<Ticks> {
54 self.upper(to, from).and_then(Ticks::checked_neg)
55 }
56}